Project

General

Profile

Download (9.47 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.util.ArrayList;
13
import java.util.List;
14

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

    
29
import org.apache.log4j.Logger;
30
import org.hibernate.annotations.Cascade;
31
import org.hibernate.annotations.CascadeType;
32
import org.hibernate.annotations.IndexColumn;
33
import org.hibernate.envers.Audited;
34
import org.hibernate.search.annotations.Indexed;
35
import org.springframework.beans.factory.annotation.Configurable;
36

    
37
import eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy;
38
import eu.etaxonomy.cdm.strategy.match.Match;
39
import eu.etaxonomy.cdm.strategy.match.MatchMode;
40

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

    
77
	//An abreviated name for the team (e. g. in case of nomenclatural authorteams). A non abreviated name for the team (e. g.
78
	//in case of some bibliographical references)
79
    @XmlElementWrapper(name = "TeamMembers", nillable = true)
80
    @XmlElement(name = "TeamMember")
81
    @XmlIDREF
82
    @XmlSchemaType(name = "IDREF")
83
    @IndexColumn(name="sortIndex", base = 0)
84
	@ManyToMany(fetch = FetchType.LAZY)
85
	@Cascade(CascadeType.SAVE_UPDATE)
86
	@Match(MatchMode.MATCH)
87
	private List<Person> teamMembers;
88
	
89
	
90
	/** 
91
	 * Creates a new team instance without any concrete {@link Person members}.
92
	 */
93
	static public Team NewInstance(){
94
		return new Team();
95
	}
96
	
97
	/** 
98
	 * Class constructor (including the cache strategy defined in
99
	 * {@link eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy TeamDefaultCacheStrategy}).
100
	 */
101
	public Team() {
102
		super();
103
		this.cacheStrategy = TeamDefaultCacheStrategy.NewInstance();
104
	}
105

    
106
	/** 
107
	 * Returns the list of {@link Person members} belonging to <i>this</i> team. 
108
	 * A person may be a member of several distinct teams. 
109
	 */
110
	public List<Person> getTeamMembers(){
111
		if(teamMembers == null) {
112
			this.teamMembers = new ArrayList<Person>();
113
		}
114
		return this.teamMembers;
115
	}
116
	
117
	protected void setTeamMembers(List<Person> teamMembers) {
118
		this.teamMembers = teamMembers;
119
	}
120
	
121
	/** 
122
	 * Adds a new {@link Person person} to <i>this</i> team at the end of the members' list. 
123
	 *
124
	 * @param  person  the person who should be added to the other team members
125
	 * @see     	   #getTeamMembers()
126
	 * @see 		   Person
127
	 */
128
	public void addTeamMember(Person person){
129
		getTeamMembers().add(person);
130
	}
131
	
132
	/** 
133
	 * Adds a new {@link Person person} to <i>this</i> team
134
	 * at the given index place of the members' list. If the person is already
135
	 * a member of the list he will be moved to the given index place. 
136
	 * The index must be a positive integer. If the index is bigger than
137
	 * the present number of members the person will be added at the end of the list.
138
	 *
139
	 * @param  person  the person who should be added to the other team members
140
	 * @param  index   the position at which the new person should be placed within the members' list
141
	 * @see     	   #getTeamMembers()
142
	 * @see 		   Person
143
	 */
144
	public void addTeamMember(Person person, int index){
145
		// TODO is still not fully implemented (range for index!)
146
		logger.warn("not yet fully implemented (range for index!)");
147
		int oldIndex = getTeamMembers().indexOf(person);
148
		if (oldIndex != -1 ){
149
			getTeamMembers().remove(person);
150
		}
151
		getTeamMembers().add(index, person);
152
	}
153
	
154
	/** 
155
	 * Removes one person from the list of members of <i>this</i> team.
156
	 *
157
	 * @param  person  the person who should be deleted from <i>this</i> team
158
	 * @see            #getTeamMembers()
159
	 */
160
	public void removeTeamMember(Person person){
161
		getTeamMembers().remove(person);
162
	}
163

    
164
	/**
165
	 * Generates an identification string for <i>this</i> team according to the strategy
166
	 * defined in {@link eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy TeamDefaultCacheStrategy}. This string is built
167
	 * with the full names of all persons belonging to its (ordered) members' list.
168
	 * This method overrides {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#generateTitle() generateTitle}.
169
	 * The result might be kept as {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#setTitleCache(String) titleCache} if the
170
	 * flag {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#protectedTitleCache protectedTitleCache} is not set.
171
	 * 
172
	 * @return  a string which identifies <i>this</i> team
173
	 */
174
//	@Override
175
//	public String generateTitle() {
176
//		return cacheStrategy.getTitleCache(this);
177
//	}
178
	
179
	
180
	/**
181
	 * Generates or returns the {@link TeamOrPersonBase#getnomenclaturalTitle() nomenclatural identification} string for <i>this</i> team.
182
	 * This method overrides {@link TeamOrPersonBase#getNomenclaturalTitle() getNomenclaturalTitle}.
183
	 * This string is built with the {@link TeamOrPersonBase#getNomenclaturalTitle() abbreviated names}
184
	 * of all persons belonging to its (ordered) members' list if the flag
185
	 * {@link #protectedNomenclaturalTitleCache protectedNomenclaturalTitleCache} is not set.
186
	 * Otherwise this method returns the present nomenclatural abbreviation.
187
	 * In case the string is generated the cache strategy used is defined in
188
	 * {@link eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy TeamDefaultCacheStrategy}.
189
	 * The result might be kept as nomenclatural abbreviation
190
	 * by using the {@link #setNomenclaturalTitle(String) setNomenclaturalTitle} method.
191
	 * 
192
	 * @return  a string which identifies <i>this</i> team for nomenclature
193
	 */
194
	@Override
195
	@Transient
196
	public String getNomenclaturalTitle() {
197
		if (protectedNomenclaturalTitleCache == PROTECTED){
198
			return this.nomenclaturalTitle;
199
		}
200
		if (nomenclaturalTitle == null){
201
			this.nomenclaturalTitle = cacheStrategy.getNomenclaturalTitle(this);
202
		}else{
203
			//as long as team members to not inform the team about changes the cache must be created new each time
204
			nomenclaturalTitle = cacheStrategy.getNomenclaturalTitle(this);
205
		}
206
		return nomenclaturalTitle;	
207
	}
208
	
209
	/**
210
	 * Assigns a {@link TeamOrPersonBase#nomenclaturalTitle nomenclatural identification} string to <i>this</i> team
211
	 * and protects it from overwriting.
212
	 * This method overrides {@link TeamOrPersonBase#setNomenclaturalTitle(String) setNomenclaturalTitle}.
213
	 * 
214
	 * @see  #getNomenclaturalTitle()
215
	 * @see  #setNomenclaturalTitle(String, boolean)
216
	 */
217
	@Override
218
	public void setNomenclaturalTitle(String nomenclaturalTitle) {
219
		this.setNomenclaturalTitle(nomenclaturalTitle, PROTECTED);
220
	}
221

    
222
	/**
223
	 * Assigns a {@link TeamOrPersonBase#nomenclaturalTitle nomenclatural identification} string to <i>this</i> team
224
	 * and a protection flag status to this string.
225
	 * 
226
	 * @see  #getNomenclaturalTitle()
227
	 */
228
	public void setNomenclaturalTitle(String nomenclaturalTitle, boolean protectedNomenclaturalTitleCache) {
229
		this.nomenclaturalTitle = nomenclaturalTitle;
230
		this.protectedNomenclaturalTitleCache = protectedNomenclaturalTitleCache;
231
	}
232
	
233
	/* (non-Javadoc)
234
	 * @see eu.etaxonomy.cdm.model.agent.TeamOrPersonBase#getTitleCache()
235
	 */
236
	@Override
237
	@Transient
238
	public String getTitleCache() {
239
		isGeneratingTitleCache = true;
240
		String result = "";
241
		if (isProtectedTitleCache()){
242
			result =  this.titleCache;			
243
		}else{
244
			result = generateTitle();
245
			result = replaceEmptyTitleByNomTitle(result);
246
			result = getTruncatedCache(result);
247
		}
248
		isGeneratingTitleCache = false;
249
		return result;
250
	}
251

    
252
	public boolean isProtectedNomenclaturalTitleCache() {
253
		return protectedNomenclaturalTitleCache;
254
	}
255

    
256
	public void setProtectedNomenclaturalTitleCache(
257
			boolean protectedNomenclaturalTitleCache) {
258
		this.protectedNomenclaturalTitleCache = protectedNomenclaturalTitleCache;
259
	}
260
	
261
	
262
}
(9-9/12)