Major changes to the cdmlib default term loading and initialization, plus indexing...
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / agent / Team.java
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.ManyToMany;
17 import javax.persistence.Transient;
18 import javax.xml.bind.annotation.XmlAccessType;
19 import javax.xml.bind.annotation.XmlAccessorType;
20 import javax.xml.bind.annotation.XmlElement;
21 import javax.xml.bind.annotation.XmlElementWrapper;
22 import javax.xml.bind.annotation.XmlIDREF;
23 import javax.xml.bind.annotation.XmlRootElement;
24 import javax.xml.bind.annotation.XmlSchemaType;
25 import javax.xml.bind.annotation.XmlType;
26
27 import org.apache.log4j.Logger;
28 import org.hibernate.annotations.Cascade;
29 import org.hibernate.annotations.CascadeType;
30
31 import eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy;
32
33 /**
34 * This class represents teams of {@link Person persons}. A team exists either for itself
35 * or is built with the list of (distinct) persons who belong to it.
36 * In the first case the inherited attribute {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#getTitleCache() titleCache} is to be used.
37 * In the second case at least all abbreviated names
38 * (the inherited attributes {@link TeamOrPersonBase#getNomenclaturalTitle() nomenclaturalTitle})
39 * or all full names (the strings returned by Person.generateTitle)
40 * of the persons must exist. A team is a {@link java.util.List list} of persons.
41 * <P>
42 * This class corresponds to: <ul>
43 * <li> Team according to the TDWG ontology
44 * <li> AgentNames (partially) according to the TCS
45 * <li> MicroAgent (partially) according to the ABCD schema
46 * </ul>
47 *
48 * @author m.doering
49 * @version 1.0
50 * @created 08-Nov-2007 13:06:58
51 */
52 @XmlAccessorType(XmlAccessType.FIELD)
53 @XmlType(name = "Team", propOrder = {
54 "protectedNomenclaturalTitleCache",
55 "teamMembers"
56 })
57 @XmlRootElement
58 @Entity
59 //@Audited
60 public class Team extends TeamOrPersonBase<Team> {
61 private static final long serialVersionUID = 97640416905934622L;
62 public static final Logger logger = Logger.getLogger(Team.class);
63
64 @XmlElement(name = "ProtectedNomenclaturalTitleCache")
65 private boolean protectedNomenclaturalTitleCache;
66
67 //An abreviated name for the team (e. g. in case of nomenclatural authorteams). A non abreviated name for the team (e. g.
68 //in case of some bibliographical references)
69 @XmlElementWrapper(name = "TeamMembers")
70 @XmlElement(name = "TeamMember")
71 @XmlIDREF
72 @XmlSchemaType(name = "IDREF")
73 private List<Person> teamMembers = new ArrayList<Person>();
74
75
76 /**
77 * Creates a new team instance without any concrete {@link Person members}.
78 */
79 static public Team NewInstance(){
80 return new Team();
81 }
82
83 /**
84 * Class constructor (including the cache strategy defined in
85 * {@link eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy TeamDefaultCacheStrategy}).
86 */
87 public Team() {
88 super();
89 this.cacheStrategy = TeamDefaultCacheStrategy.NewInstance();
90 }
91
92 /**
93 * Returns the list of {@link Person members} belonging to <i>this</i> team.
94 * A person may be a member of several distinct teams.
95 */
96 @ManyToMany
97 //@IndexColumn(name="sortIndex", base = 0)
98 //@JoinColumn (name = "representation_id", nullable=false)
99 @Cascade({CascadeType.SAVE_UPDATE})
100 public List<Person> getTeamMembers(){
101 return this.teamMembers;
102 }
103 /**
104 * @see #getTeamMembers()
105 */
106 protected void setTeamMembers(List<Person> teamMembers){
107 this.teamMembers = teamMembers;
108 }
109
110 /**
111 * Adds a new {@link Person person} to <i>this</i> team at the end of the members' list.
112 *
113 * @param person the person who should be added to the other team members
114 * @see #getTeamMembers()
115 * @see Person
116 */
117 public void addTeamMember(Person person){
118 this.teamMembers.add(person);
119 }
120
121 /**
122 * Adds a new {@link Person person} to <i>this</i> team
123 * at the given index place of the members' list. If the person is already
124 * a member of the list he will be moved to the given index place.
125 * The index must be a positive integer. If the index is bigger than
126 * the present number of members the person will be added at the end of the list.
127 *
128 * @param person the person who should be added to the other team members
129 * @param index the position at which the new person should be placed within the members' list
130 * @see #getTeamMembers()
131 * @see Person
132 */
133 public void addTeamMember(Person person, int index){
134 // TODO is still not fully implemented (range for index!)
135 logger.warn("not yet fully implemented (range for index!)");
136 int oldIndex = teamMembers.indexOf(person);
137 if (oldIndex != -1 ){
138 teamMembers.remove(person);
139 }
140 this.teamMembers.add(index, person);
141 }
142
143 /**
144 * Removes one person from the list of members of <i>this</i> team.
145 *
146 * @param person the person who should be deleted from <i>this</i> team
147 * @see #getTeamMembers()
148 */
149 public void removeTeamMember(Person person){
150 this.teamMembers.remove(person);
151 }
152
153 /**
154 * Generates an identification string for <i>this</i> team according to the strategy
155 * defined in {@link eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy TeamDefaultCacheStrategy}. This string is built
156 * with the full names of all persons belonging to its (ordered) members' list.
157 * This method overrides {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#generateTitle() generateTitle}.
158 * The result might be kept as {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#setTitleCache(String) titleCache} if the
159 * flag {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#protectedTitleCache protectedTitleCache} is not set.
160 *
161 * @return a string which identifies <i>this</i> team
162 */
163 @Override
164 public String generateTitle() {
165 return cacheStrategy.getTitleCache(this);
166 }
167
168
169 /**
170 * Generates or returns the {@link TeamOrPersonBase#getnomenclaturalTitle() nomenclatural identification} string for <i>this</i> team.
171 * This method overrides {@link TeamOrPersonBase#getNomenclaturalTitle() getNomenclaturalTitle}.
172 * This string is built with the {@link TeamOrPersonBase#getNomenclaturalTitle() abbreviated names}
173 * of all persons belonging to its (ordered) members' list if the flag
174 * {@link #protectedNomenclaturalTitleCache protectedNomenclaturalTitleCache} is not set.
175 * Otherwise this method returns the present nomenclatural abbreviation.
176 * In case the string is generated the cache strategy used is defined in
177 * {@link eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy TeamDefaultCacheStrategy}.
178 * The result might be kept as nomenclatural abbreviation
179 * by using the {@link #setNomenclaturalTitle(String) setNomenclaturalTitle} method.
180 *
181 * @return a string which identifies <i>this</i> team for nomenclature
182 */
183 @Override
184 @Transient
185 public String getNomenclaturalTitle() {
186 if (protectedNomenclaturalTitleCache == PROTECTED){
187 return this.nomenclaturalTitle;
188 }
189 if (nomenclaturalTitle == null){
190 this.nomenclaturalTitle = cacheStrategy.getNomenclaturalTitle(this);
191 }
192 return nomenclaturalTitle;
193 }
194
195 /**
196 * Assigns a {@link TeamOrPersonBase#nomenclaturalTitle nomenclatural identification} string to <i>this</i> team
197 * and protects it from overwriting.
198 * This method overrides {@link TeamOrPersonBase#setNomenclaturalTitle(String) setNomenclaturalTitle}.
199 *
200 * @see #getNomenclaturalTitle()
201 * @see #setNomenclaturalTitle(String, boolean)
202 */
203 @Override
204 public void setNomenclaturalTitle(String nomenclaturalTitle) {
205 setNomenclaturalTitle(nomenclaturalTitle, PROTECTED);
206 this.nomenclaturalTitle = nomenclaturalTitle;
207 }
208
209 /**
210 * Assigns a {@link TeamOrPersonBase#nomenclaturalTitle nomenclatural identification} string to <i>this</i> team
211 * and a protection flag status to this string.
212 *
213 * @see #getNomenclaturalTitle()
214 */
215 public void setNomenclaturalTitle(String nomenclaturalTitle, boolean protectedNomenclaturalTitleCache) {
216 this.nomenclaturalTitle = nomenclaturalTitle;
217 this.protectedNomenclaturalTitleCache = protectedNomenclaturalTitleCache;
218 }
219
220
221
222
223
224 }