2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.model
.agent
;
12 import java
.beans
.PropertyChangeEvent
;
13 import java
.beans
.PropertyChangeListener
;
14 import java
.util
.ArrayList
;
15 import java
.util
.List
;
17 import javax
.persistence
.Entity
;
18 import javax
.persistence
.FetchType
;
19 import javax
.persistence
.ManyToMany
;
20 import javax
.persistence
.Transient
;
21 import javax
.xml
.bind
.annotation
.XmlAccessType
;
22 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
23 import javax
.xml
.bind
.annotation
.XmlElement
;
24 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
25 import javax
.xml
.bind
.annotation
.XmlIDREF
;
26 import javax
.xml
.bind
.annotation
.XmlRootElement
;
27 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
28 import javax
.xml
.bind
.annotation
.XmlType
;
30 import org
.apache
.log4j
.Logger
;
31 import org
.hibernate
.annotations
.Cascade
;
32 import org
.hibernate
.annotations
.CascadeType
;
33 import org
.hibernate
.annotations
.IndexColumn
;
34 import org
.hibernate
.envers
.Audited
;
35 import org
.hibernate
.search
.annotations
.Indexed
;
36 import org
.springframework
.beans
.factory
.annotation
.Configurable
;
38 import eu
.etaxonomy
.cdm
.strategy
.cache
.agent
.TeamDefaultCacheStrategy
;
39 import eu
.etaxonomy
.cdm
.strategy
.match
.Match
;
40 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchMode
;
43 * This class represents teams of {@link Person persons}. A team exists either for itself
44 * or is built with the list of (distinct) persons who belong to it.
45 * In the first case the inherited attribute {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#getTitleCache() titleCache} is to be used.
46 * In the second case at least all abbreviated names
47 * (the inherited attributes {@link TeamOrPersonBase#getNomenclaturalTitle() nomenclaturalTitle})
48 * or all full names (the strings returned by Person.generateTitle)
49 * of the persons must exist. A team is a {@link java.util.List list} of persons.
51 * This class corresponds to: <ul>
52 * <li> Team according to the TDWG ontology
53 * <li> AgentNames (partially) according to the TCS
54 * <li> MicroAgent (partially) according to the ABCD schema
59 * @created 08-Nov-2007 13:06:58
61 @XmlAccessorType(XmlAccessType
.FIELD
)
62 @XmlType(name
= "Team", propOrder
= {
63 "protectedNomenclaturalTitleCache",
66 @XmlRootElement(name
= "Team")
68 @Indexed(index
= "eu.etaxonomy.cdm.model.agent.AgentBase")
71 public class Team
extends TeamOrPersonBase
<Team
> {
72 private static final long serialVersionUID
= 97640416905934622L;
73 public static final Logger logger
= Logger
.getLogger(Team
.class);
75 @XmlElement(name
= "ProtectedNomenclaturalTitleCache")
76 private boolean protectedNomenclaturalTitleCache
= false;
78 //An abbreviated name for the team (e. g. in case of nomenclatural authorteams). A non abbreviated name for the team (e. g.
79 //in case of some bibliographical references)
80 @XmlElementWrapper(name
= "TeamMembers", nillable
= true)
81 @XmlElement(name
= "TeamMember")
83 @XmlSchemaType(name
= "IDREF")
84 @IndexColumn(name
="sortIndex", base
= 0)
85 @ManyToMany(fetch
= FetchType
.LAZY
)
86 @Cascade(CascadeType
.SAVE_UPDATE
)
87 @Match(MatchMode
.MATCH
)
88 private List
<Person
> teamMembers
;
92 * Creates a new team instance without any concrete {@link Person members}.
94 static public Team
NewInstance(){
99 * Creates a new team instance with a bibliographic and nomenclatural title
100 * but without any {@link Person members}. The caches are set to protected.
102 static public Team
NewTitledInstance(String title
, String nomTitle
){
103 Team result
= new Team();
104 result
.setTitleCache(title
, true);
105 result
.setNomenclaturalTitle(nomTitle
, true);
110 * Class constructor (including the cache strategy defined in
111 * {@link eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy TeamDefaultCacheStrategy}).
115 this.cacheStrategy
= TeamDefaultCacheStrategy
.NewInstance();
116 addListenersToMembers();
120 * Adds a property change listener to all team members.
122 private void addListenersToMembers() {
123 List
<Person
> members
= getTeamMembers();
124 for (Person member
: members
){
125 addListenerForTeamMember(member
);
132 private void addListenerForTeamMember(Person member
) {
133 PropertyChangeListener listener
= new PropertyChangeListener() {
135 public void propertyChange(PropertyChangeEvent e
) {
137 // ---- code with no effect below -----
138 // if (! isProtectedTitleCache()){
139 // titleCache = titleCache;
141 // if (! isProtectedNomenclaturalTitleCache()){
142 // nomenclaturalTitle = nomenclaturalTitle;
146 member
.addPropertyChangeListener(listener
);
150 * Returns the list of {@link Person members} belonging to <i>this</i> team.
151 * A person may be a member of several distinct teams.
153 public List
<Person
> getTeamMembers(){
154 if(teamMembers
== null) {
155 this.teamMembers
= new ArrayList
<Person
>();
157 return this.teamMembers
;
160 protected void setTeamMembers(List
<Person
> teamMembers
) {
161 this.teamMembers
= teamMembers
;
162 addListenersToMembers();
166 * Adds a new {@link Person person} to <i>this</i> team at the end of the members' list.
168 * @param person the person who should be added to the other team members
169 * @see #getTeamMembers()
172 public void addTeamMember(Person person
){
174 getTeamMembers().add(person
);
175 firePropertyChange("teamMember", null, person
);
176 addListenerForTeamMember(person
);
181 * Adds a new {@link Person person} to <i>this</i> team
182 * at the given index place of the members' list. If the person is already
183 * a member of the list he will be moved to the given index place.
184 * The index must be an integer (>=0). If the index is larger than
185 * the present number of members the person will be added at the end of the list.
187 * @param person the person who should be added to the other team members
188 * @param index the position at which the person should be placed within the members' list (starting with 0)
189 * @see #getTeamMembers()
192 public void addTeamMember(Person person
, int index
){
194 int oldIndex
= getTeamMembers().indexOf(person
);
195 if (oldIndex
!= -1 ){
196 getTeamMembers().remove(person
);
198 if (index
>= getTeamMembers().size()){
199 index
= getTeamMembers().size();
201 getTeamMembers().add(index
, person
);
202 addListenerForTeamMember(person
);
203 firePropertyChange("teamMember", null, person
);
208 * Removes one person from the list of members of <i>this</i> team.
210 * @param person the person who should be deleted from <i>this</i> team
211 * @see #getTeamMembers()
213 public void removeTeamMember(Person person
){
214 boolean wasMember
= getTeamMembers().remove(person
);
216 firePropertyChange("teamMember", person
, null);
223 * Generates or returns the {@link TeamOrPersonBase#getnomenclaturalTitle() nomenclatural identification} string for <i>this</i> team.
224 * This method overrides {@link TeamOrPersonBase#getNomenclaturalTitle() getNomenclaturalTitle}.
225 * This string is built with the {@link TeamOrPersonBase#getNomenclaturalTitle() abbreviated names}
226 * of all persons belonging to its (ordered) members' list if the flag
227 * {@link #protectedNomenclaturalTitleCache protectedNomenclaturalTitleCache} is not set.
228 * Otherwise this method returns the present nomenclatural abbreviation.
229 * In case the string is generated the cache strategy used is defined in
230 * {@link eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy TeamDefaultCacheStrategy}.
231 * The result might be kept as nomenclatural abbreviation
232 * by using the {@link #setNomenclaturalTitle(String) setNomenclaturalTitle} method.
234 * @return a string which identifies <i>this</i> team for nomenclature
238 public String
getNomenclaturalTitle() {
239 if (protectedNomenclaturalTitleCache
== PROTECTED
){
240 return this.nomenclaturalTitle
;
242 if (nomenclaturalTitle
== null){
243 this.nomenclaturalTitle
= cacheStrategy
.getNomenclaturalTitle(this);
245 //as long as team members to not inform the team about changes the cache must be created new each time
246 nomenclaturalTitle
= cacheStrategy
.getNomenclaturalTitle(this);
248 return nomenclaturalTitle
;
252 * Assigns a {@link TeamOrPersonBase#nomenclaturalTitle nomenclatural identification} string to <i>this</i> team
253 * and protects it from overwriting.
254 * This method overrides {@link TeamOrPersonBase#setNomenclaturalTitle(String) setNomenclaturalTitle}.
256 * @see #getNomenclaturalTitle()
257 * @see #setNomenclaturalTitle(String, boolean)
260 public void setNomenclaturalTitle(String nomenclaturalTitle
) {
261 this.setNomenclaturalTitle(nomenclaturalTitle
, PROTECTED
);
265 * Assigns a {@link TeamOrPersonBase#nomenclaturalTitle nomenclatural identification} string to <i>this</i> team
266 * and a protection flag status to this string.
268 * @see #getNomenclaturalTitle()
270 public void setNomenclaturalTitle(String nomenclaturalTitle
, boolean protectedNomenclaturalTitleCache
) {
271 firePropertyChange("nomenclaturalTitle", this.nomenclaturalTitle
, nomenclaturalTitle
);
272 this.nomenclaturalTitle
= nomenclaturalTitle
;
273 this.protectedNomenclaturalTitleCache
= protectedNomenclaturalTitleCache
;
277 * @see eu.etaxonomy.cdm.model.agent.TeamOrPersonBase#getTitleCache()
280 //@Transient //TODO a.kohlbecker remove??
281 public String
getTitleCache() {
282 isGeneratingTitleCache
= true;
284 if (isProtectedTitleCache()){
285 result
= this.titleCache
;
287 result
= generateTitle();
288 result
= replaceEmptyTitleByNomTitle(result
);
289 result
= getTruncatedCache(result
);
290 this.titleCache
= result
;
292 isGeneratingTitleCache
= false;
296 public boolean isProtectedNomenclaturalTitleCache() {
297 return protectedNomenclaturalTitleCache
;
300 public void setProtectedNomenclaturalTitleCache(
301 boolean protectedNomenclaturalTitleCache
) {
302 this.protectedNomenclaturalTitleCache
= protectedNomenclaturalTitleCache
;
305 //*********************** CLONE ********************************************************/
308 * Clones <i>this</i> Team. This is a shortcut that enables to create
309 * a new instance that differs only slightly from <i>this</i> Team.
310 * The corresponding person is cloned.
312 * @see eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity#clone()
313 * @see java.lang.Object#clone()
316 public Object
clone() {
318 Team result
= (Team
)super.clone();
319 result
.teamMembers
= new ArrayList
<Person
>();
320 for (Person teamMember
: this.teamMembers
){
321 result
.addTeamMember(teamMember
);
323 //no changes to protectedNomenclaturalTitleCache
325 } catch (CloneNotSupportedException e
){
326 logger
.warn("Object does not implement cloneable");