Project

General

Profile

Download (13.5 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.model.agent;
11

    
12
import java.beans.PropertyChangeEvent;
13
import java.beans.PropertyChangeListener;
14
import java.util.ArrayList;
15
import java.util.List;
16

    
17
import javax.persistence.Entity;
18
import javax.persistence.FetchType;
19
import javax.persistence.ManyToMany;
20
import javax.persistence.OrderColumn;
21
import javax.persistence.Transient;
22
import javax.xml.bind.annotation.XmlAccessType;
23
import javax.xml.bind.annotation.XmlAccessorType;
24
import javax.xml.bind.annotation.XmlElement;
25
import javax.xml.bind.annotation.XmlElementWrapper;
26
import javax.xml.bind.annotation.XmlIDREF;
27
import javax.xml.bind.annotation.XmlRootElement;
28
import javax.xml.bind.annotation.XmlSchemaType;
29
import javax.xml.bind.annotation.XmlType;
30

    
31
import org.apache.log4j.Logger;
32
import org.hibernate.annotations.Cascade;
33
import org.hibernate.annotations.CascadeType;
34
import org.hibernate.annotations.ListIndexBase;
35
import org.hibernate.envers.Audited;
36
import org.springframework.beans.factory.annotation.Configurable;
37

    
38
import eu.etaxonomy.cdm.model.EntityCollectionSetterAdapter;
39
import eu.etaxonomy.cdm.model.EntityCollectionSetterAdapter.SetterAdapterException;
40
import eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy;
41
import eu.etaxonomy.cdm.strategy.match.Match;
42
import eu.etaxonomy.cdm.strategy.match.MatchMode;
43

    
44
/**
45
 * This class represents teams of {@link Person persons}. A team exists either for itself
46
 * or is built with the list of (distinct) persons who belong to it.
47
 * In the first case the inherited attribute {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#getTitleCache() titleCache} is to be used.
48
 * In the second case at least all abbreviated names
49
 * (the inherited attributes {@link TeamOrPersonBase#getNomenclaturalTitle() nomenclaturalTitle})
50
 * or all full names (the strings returned by Person.generateTitle)
51
 * of the persons must exist. A team is a {@link java.util.List list} of persons.
52
 * <P>
53
 * This class corresponds to: <ul>
54
 * <li> Team according to the TDWG ontology
55
 * <li> AgentNames (partially) according to the TCS
56
 * <li> MicroAgent (partially) according to the ABCD schema
57
 * </ul>
58
 *
59
 * @author m.doering
60
 * @since 08-Nov-2007 13:06:58
61
 */
62
@XmlAccessorType(XmlAccessType.FIELD)
63
@XmlType(name = "Team", propOrder = {
64
	"protectedNomenclaturalTitleCache",
65
	"protectedCollectorTitleCache",
66
    "teamMembers",
67
    "hasMoreMembers"
68
})
69
@XmlRootElement(name = "Team")
70
@Entity
71
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
72
//@Indexed(index = "eu.etaxonomy.cdm.model.agent.AgentBase")
73
@Audited
74
@Configurable
75
public class Team extends TeamOrPersonBase<Team> {
76
	private static final long serialVersionUID = 97640416905934622L;
77
	public static final Logger logger = Logger.getLogger(Team.class);
78

    
79
    @XmlElement(name = "ProtectedNomenclaturalTitleCache")
80
	private boolean protectedNomenclaturalTitleCache = false;
81

    
82
    //under construction #4311
83
    @XmlElement(name = "ProtectedCollectorTitleCache")
84
	private final boolean protectedCollectorTitleCache = false;
85

    
86
	//An abbreviated name for the team (e. g. in case of nomenclatural authorteams).
87
    //A non abbreviated name for the team (e. g.
88
	//in case of some bibliographical references)
89
    @XmlElementWrapper(name = "TeamMembers", nillable = true)
90
    @XmlElement(name = "TeamMember")
91
    @XmlIDREF
92
    @XmlSchemaType(name = "IDREF")
93
    @OrderColumn(name="sortIndex")
94
    @ListIndexBase(value=0)  //not really needed as this is the default
95
	@ManyToMany(fetch = FetchType.LAZY)
96
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
97
	@Match(MatchMode.MATCH)
98
	private List<Person> teamMembers;
99

    
100
    @XmlElement(name = "hasMoreMembers")
101
	private boolean hasMoreMembers;
102

    
103
// ********************************** FACTORY ***************************/
104

    
105
	/**
106
	 * Creates a new team instance without any concrete {@link Person members}.
107
	 */
108
	static public Team NewInstance(){
109
		return new Team();
110
	}
111

    
112
	/**
113
	 * Creates a new team instance with a bibliographic and nomenclatural title
114
	 * but without any {@link Person members}. The caches are set to protected.
115
	 */
116
	static public Team NewTitledInstance(String title, String nomTitle){
117
		Team result = new Team();
118
		result.setTitleCache(title, true);
119
		result.setNomenclaturalTitle(nomTitle, true);
120
		return result;
121
	}
122

    
123
	/**
124
	 * Class constructor (including the cache strategy defined in
125
	 * {@link eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy TeamDefaultCacheStrategy}).
126
	 */
127
	public Team() {
128
		super();
129
		this.cacheStrategy = TeamDefaultCacheStrategy.NewInstance();
130
		addListenersToMembers();
131
	}
132

    
133
	/**
134
	 * Adds a property change listener to all team members.
135
	 */
136
	private void addListenersToMembers() {
137
		List<Person> members = getTeamMembers();
138
		for (Person member : members){
139
			addListenerForTeamMember(member);
140
		}
141
	}
142

    
143
	private void addListenerForTeamMember(Person member) {
144
		PropertyChangeListener listener = new PropertyChangeListener() {
145
			@Override
146
            public void propertyChange(PropertyChangeEvent e) {
147

    
148
// 			   ---- code with no effect below -----
149
//				if (! isProtectedTitleCache()){
150
//					titleCache = titleCache;
151
//				}
152
//				if (! isProtectedNomenclaturalTitleCache()){
153
//					nomenclaturalTitle = nomenclaturalTitle;
154
//				}
155
			}
156
		};
157
		member.addPropertyChangeListener(listener);
158
	}
159

    
160
	/**
161
	 * Returns the list of {@link Person members} belonging to <i>this</i> team.
162
	 * A person may be a member of several distinct teams.
163
	 */
164
	public List<Person> getTeamMembers(){
165
		if(teamMembers == null) {
166
			this.teamMembers = new ArrayList<>();
167
		}
168
		return this.teamMembers;
169
	}
170

    
171
	public void setTeamMembers(List<Person> teamMembers) throws SetterAdapterException {
172
	    new EntityCollectionSetterAdapter<Team, Person>(Team.class, Person.class, "teamMembers").setCollection(this, teamMembers);
173
    }
174

    
175
	/**
176
	 * Adds a new {@link Person person} to <i>this</i> team at the end of the members' list.
177
	 *
178
	 * @param  person  the person who should be added to the other team members
179
	 * @see     	   #getTeamMembers()
180
	 * @see 		   Person
181
	 */
182
	public void addTeamMember(Person person){
183
		if (person != null){
184
			getTeamMembers().add(person);
185
			firePropertyChange("teamMember", null, person);
186
			addListenerForTeamMember(person);
187
		}
188
	}
189

    
190
	/**
191
	 * Adds a new {@link Person person} to <i>this</i> team
192
	 * at the given index place of the members' list. If the person is already
193
	 * a member of the list he will be moved to the given index place.
194
	 * The index must be an integer (>=0). If the index is larger than
195
	 * the present number of members the person will be added at the end of the list.
196
	 *
197
	 * @param  person  the person who should be added to the other team members
198
	 * @param  index   the position at which the person should be placed within the members' list (starting with 0)
199
	 * @see     	   #getTeamMembers()
200
	 * @see 		   Person
201
	 */
202
	public void addTeamMember(Person person, int index){
203
		if (person != null){
204
			int oldIndex = getTeamMembers().indexOf(person);
205
			if (oldIndex != -1 ){
206
				getTeamMembers().remove(person);
207
			}
208
			if (index >= getTeamMembers().size()){
209
				index = getTeamMembers().size();
210
			}
211
			getTeamMembers().add(index, person);
212
			addListenerForTeamMember(person);
213
			firePropertyChange("teamMember", null, person);
214
		}
215
	}
216

    
217
	/**
218
	 * Removes one person from the list of members of <i>this</i> team.
219
	 *
220
	 * @param  person  the person who should be deleted from <i>this</i> team
221
	 * @see            #getTeamMembers()
222
	 */
223
	public void removeTeamMember(Person person){
224
		boolean wasMember = getTeamMembers().remove(person);
225
		if (wasMember){
226
			firePropertyChange("teamMember", person, null);
227
		}
228
	}
229

    
230
    public boolean replaceTeamMember(Person newObject, Person oldObject){
231
        return replaceInList(this.teamMembers, newObject, oldObject);
232
    }
233

    
234

    
235
	/**
236
	 * Generates or returns the {@link TeamOrPersonBase#getnomenclaturalTitle() nomenclatural identification} string for <i>this</i> team.
237
	 * This method overrides {@link TeamOrPersonBase#getNomenclaturalTitle() getNomenclaturalTitle}.
238
	 * This string is built with the {@link TeamOrPersonBase#getNomenclaturalTitle() abbreviated names}
239
	 * of all persons belonging to its (ordered) members' list if the flag
240
	 * {@link #protectedNomenclaturalTitleCache protectedNomenclaturalTitleCache} is not set.
241
	 * Otherwise this method returns the present nomenclatural abbreviation.
242
	 * In case the string is generated the cache strategy used is defined in
243
	 * {@link eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy TeamDefaultCacheStrategy}.
244
	 * The result might be kept as nomenclatural abbreviation
245
	 * by using the {@link #setNomenclaturalTitle(String) setNomenclaturalTitle} method.
246
	 *
247
	 * @return  a string which identifies <i>this</i> team for nomenclature
248
	 */
249
	@Override
250
	@Transient
251
	public String getNomenclaturalTitle() {
252
		if (protectedNomenclaturalTitleCache == PROTECTED){
253
			return this.nomenclaturalTitle;
254
		}
255
		if (nomenclaturalTitle == null){
256
			this.nomenclaturalTitle = cacheStrategy.getNomenclaturalTitle(this);
257
		}else{
258
			//as long as team members to not inform the team about changes the cache must be created new each time
259
			nomenclaturalTitle = cacheStrategy.getNomenclaturalTitle(this);
260
		}
261
		return nomenclaturalTitle;
262
	}
263

    
264
	/**
265
	 * Assigns a {@link TeamOrPersonBase#nomenclaturalTitle nomenclatural identification} string to <i>this</i> team
266
	 * and protects it from overwriting.
267
	 * This method overrides {@link TeamOrPersonBase#setNomenclaturalTitle(String) setNomenclaturalTitle}.
268
	 *
269
	 * @see  #getNomenclaturalTitle()
270
	 * @see  #setNomenclaturalTitle(String, boolean)
271
	 */
272
	@Override
273
	public void setNomenclaturalTitle(String nomenclaturalTitle) {
274
		this.setNomenclaturalTitle(nomenclaturalTitle, PROTECTED);
275
	}
276

    
277
	/**
278
	 * Assigns a {@link TeamOrPersonBase#nomenclaturalTitle nomenclatural identification} string to <i>this</i> team
279
	 * and a protection flag status to this string.
280
	 *
281
	 * @see  #getNomenclaturalTitle()
282
	 */
283
	public void setNomenclaturalTitle(String nomenclaturalTitle, boolean protectedNomenclaturalTitleCache) {
284
		firePropertyChange("nomenclaturalTitle", this.nomenclaturalTitle, nomenclaturalTitle);
285
		this.nomenclaturalTitle = nomenclaturalTitle == "" ? null: nomenclaturalTitle;
286
		this.protectedNomenclaturalTitleCache = protectedNomenclaturalTitleCache;
287
	}
288

    
289

    
290
	@Override
291
	//@Transient //TODO a.kohlbecker remove??
292
	public String getTitleCache() {
293
		isGeneratingTitleCache = true;
294
		String result = "";
295
		if (isProtectedTitleCache()){
296
			result = this.titleCache;
297
		}else{
298
			result = generateTitle();
299
			result = replaceEmptyTitleByNomTitle(result);
300
			result = getTruncatedCache(result);
301
			this.titleCache = result;
302
		}
303
		isGeneratingTitleCache = false;
304
		return result;
305
	}
306

    
307
	/**
308
	 * Protected nomenclatural title cache flag should be set to true, if
309
	 * the title cache is to be preferred against the atomized data.
310
	 * This may be the case if no atomized data exists or if atomization
311
	 * was incomplete for whatever reason.
312
	 * @return
313
	 */
314
	public boolean isProtectedNomenclaturalTitleCache() {
315
		return protectedNomenclaturalTitleCache;
316
	}
317

    
318
	public void setProtectedNomenclaturalTitleCache(
319
			boolean protectedNomenclaturalTitleCache) {
320
		this.protectedNomenclaturalTitleCache = protectedNomenclaturalTitleCache;
321
	}
322

    
323

    
324
	/**
325
	 * The hasMoreMembers flag is true if this team has more members than
326
	 * mentioned in the {@link #teamMembers} list. This is usually the case
327
	 * when "et al." is used in the team representation. Formatters should add
328
	 * "et al." or an according post fix to the team string representation
329
	 *  if this flag is set.
330
	 * @return
331
	 */
332
	public boolean isHasMoreMembers() {
333
		return hasMoreMembers;
334
	}
335

    
336
	public void setHasMoreMembers(boolean hasMoreMembers) {
337
		this.hasMoreMembers = hasMoreMembers;
338
	}
339

    
340

    
341
    @Override
342
    public boolean updateCaches(){
343
        boolean result = super.updateCaches();
344
        if (this.protectedNomenclaturalTitleCache == false){
345
            String oldNomTitleCache = this.nomenclaturalTitle;
346
            this.protectedNomenclaturalTitleCache = false;
347

    
348
            String newNomTitleCache = cacheStrategy.getNomenclaturalTitle(this);
349

    
350
            if ( oldNomTitleCache == null   || ! oldNomTitleCache.equals(newNomTitleCache) ){
351
                 this.setNomenclaturalTitle(null, false);
352
                 String newCache = this.getNomenclaturalTitle();
353

    
354
                 if (newCache == null){
355
                     logger.warn("New nomTitleCache should never be null");
356
                 }
357
                 if (oldNomTitleCache == null){
358
                     logger.info("Old nomTitleCache should never be null");
359
                 }
360
                 result = true;
361
             }
362
         }
363
        return result;
364
     }
365

    
366
//*********************** CLONE ********************************************************/
367

    
368
	/**
369
	 * Clones <i>this</i> Team. This is a shortcut that enables to create
370
	 * a new instance that differs only slightly from <i>this</i> Team.
371
	 * The corresponding person is cloned.
372
	 *
373
	 * @see eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity#clone()
374
	 * @see java.lang.Object#clone()
375
	 */
376
	@Override
377
	public Object clone() {
378
		try{
379
			Team result = (Team)super.clone();
380
			result.teamMembers = new ArrayList<>();
381
			for (Person teamMember: this.teamMembers){
382
				result.addTeamMember(teamMember);
383
			}
384
			//no changes to protectedNomenclaturalTitleCache
385
			return result;
386
		} catch (CloneNotSupportedException e){
387
			logger.warn("Object does not implement cloneable");
388
			e.printStackTrace();
389
			return null;
390
		}
391
	}
392

    
393
}
(9-9/12)