removing imports
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / taxon / TaxonBase.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.taxon;
11
12 import java.lang.reflect.Method;
13
14 import javax.persistence.Entity;
15 import javax.persistence.FetchType;
16 import javax.persistence.JoinColumn;
17 import javax.persistence.ManyToOne;
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.XmlAttribute;
23 import javax.xml.bind.annotation.XmlElement;
24 import javax.xml.bind.annotation.XmlIDREF;
25 import javax.xml.bind.annotation.XmlSchemaType;
26 import javax.xml.bind.annotation.XmlType;
27
28 import org.apache.log4j.Logger;
29 import org.hibernate.annotations.Cascade;
30 import org.hibernate.annotations.CascadeType;
31 import org.hibernate.annotations.Index;
32 import org.hibernate.annotations.Table;
33 import org.hibernate.envers.Audited;
34 import org.hibernate.search.annotations.IndexedEmbedded;
35
36 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
37 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
38 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
39 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
40 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
41 import eu.etaxonomy.cdm.validation.Level2;
42 import eu.etaxonomy.cdm.validation.Level3;
43 import eu.etaxonomy.cdm.validation.annotation.TaxonNameCannotBeAcceptedAndSynonym;
44
45 /**
46 * The upmost (abstract) class for the use of a {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon name} in a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase reference}
47 * or within a taxonomic view/treatment either as a {@link Taxon taxon}
48 * ("accepted" respectively "correct" name) or as a (junior) {@link Synonym synonym}.
49 * Within a taxonomic view/treatment or a reference a taxon name can be used
50 * only in one of both described meanings. The reference using the taxon name
51 * is generally cited with "sec." (secundum, sensu). For instance:
52 * "<i>Juncus longirostris</i> Kuvaev sec. Kirschner, J. et al. 2002".
53 * <P>
54 * This class corresponds to: <ul>
55 * <li> TaxonConcept according to the TDWG ontology
56 * <li> TaxonConcept according to the TCS
57 * </ul>
58 *
59 * @author m.doering
60 * @version 1.0
61 * @created 08-Nov-2007 13:06:56
62 */
63 @XmlAccessorType(XmlAccessType.FIELD)
64 @XmlType(name = "TaxonBase", propOrder = {
65 "name",
66 "sec",
67 "doubtful",
68 "appendedPhrase",
69 "useNameCache"
70 })
71 @Entity
72 @Audited
73 @Table(appliesTo="TaxonBase", indexes = { @Index(name = "taxonBaseTitleCacheIndex", columnNames = { "titleCache" }) })
74 @TaxonNameCannotBeAcceptedAndSynonym(groups = Level3.class)
75 public abstract class TaxonBase<S extends IIdentifiableEntityCacheStrategy> extends IdentifiableEntity<S> {
76 private static final long serialVersionUID = -3589185949928938529L;
77 private static final Logger logger = Logger.getLogger(TaxonBase.class);
78
79 private static Method methodTaxonNameAddTaxonBase;
80
81 static {
82 try {
83 methodTaxonNameAddTaxonBase = TaxonNameBase.class.getDeclaredMethod("addTaxonBase", TaxonBase.class);
84 methodTaxonNameAddTaxonBase.setAccessible(true);
85 } catch (Exception e) {
86 logger.error(e);
87 for(StackTraceElement ste : e.getStackTrace()) {
88 logger.error(ste);
89 }
90 }
91 }
92
93 //The assignment to the Taxon or to the Synonym class is not definitive
94 @XmlAttribute(name = "isDoubtful")
95 private boolean doubtful;
96
97 @XmlElement(name = "Name", required = true)
98 @XmlIDREF
99 @XmlSchemaType(name = "IDREF")
100 @ManyToOne(fetch = FetchType.LAZY)
101 @JoinColumn(name="taxonName_fk")
102 @IndexedEmbedded
103 @Cascade(CascadeType.SAVE_UPDATE)
104 @NotNull(groups = Level2.class)
105 private TaxonNameBase name;
106
107 // The concept reference
108 @XmlElement(name = "Sec")
109 @XmlIDREF
110 @XmlSchemaType(name = "IDREF")
111 @ManyToOne(fetch = FetchType.LAZY)
112 @IndexedEmbedded
113 @Cascade(CascadeType.SAVE_UPDATE)
114 @NotNull(groups = Level2.class)
115 private ReferenceBase sec;
116
117
118 @XmlElement(name = "AppendedPhrase")
119 private String appendedPhrase;
120
121 @XmlAttribute(name= "UseNameCache")
122 private boolean useNameCache = false;
123
124
125 // ************* CONSTRUCTORS *************/
126 /**
127 * Class constructor: creates a new empty (abstract) taxon.
128 *
129 * @see #TaxonBase(TaxonNameBase, ReferenceBase)
130 */
131 protected TaxonBase(){
132 super();
133 }
134
135 /**
136 * Class constructor: creates a new (abstract) taxon with the
137 * {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon name} used and the {@link eu.etaxonomy.cdm.model.reference.ReferenceBase reference}
138 * using it.
139 *
140 * @param taxonNameBase the taxon name used
141 * @param sec the reference using the taxon name
142 * @see #TaxonBase()
143 */
144 protected TaxonBase(TaxonNameBase taxonNameBase, ReferenceBase sec){
145 super();
146 if (taxonNameBase != null){
147 this.invokeSetMethod(methodTaxonNameAddTaxonBase, taxonNameBase);
148 }
149 this.setSec(sec);
150 }
151
152 //********* METHODS **************************************/
153
154 /**
155 * Generates and returns the string with the full scientific name (including
156 * authorship) of the {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon name} used in <i>this</i>
157 * (abstract) taxon as well as the title of the {@link eu.etaxonomy.cdm.model.reference.ReferenceBase reference} using
158 * this taxon name. This string may be stored in the inherited
159 * {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#getTitleCache() titleCache} attribute.
160 * This method overrides the generic and inherited generateTitle() method
161 * from {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity IdentifiableEntity}.
162 *
163 * @return the string with the full scientific name of the taxon name
164 * and with the title of the reference involved in <i>this</i> (abstract) taxon
165 * @see eu.etaxonomy.cdm.model.common.IdentifiableEntity#generateTitle()
166 * @see eu.etaxonomy.cdm.model.common.IdentifiableEntity#getTitleCache()
167 */
168 // @Override
169 // public String generateTitle() {
170 // String title;
171 // if (name != null && name.getTitleCache() != null){
172 // title = name.getTitleCache() + " sec. ";
173 // if (sec != null){
174 // title += sec.getTitleCache();
175 // }else{
176 // title += "???";
177 // }
178 // }else{
179 // title = this.toString();
180 // }
181 // return title;
182 // }
183
184 /**
185 * Returns the {@link TaxonNameBase taxon name} used in <i>this</i> (abstract) taxon.
186 */
187 public TaxonNameBase getName(){
188 return this.name;
189 }
190
191 /*
192 * @see #getName
193 */
194 public void setName(TaxonNameBase name) {
195 if(name != null) {
196 name.getTaxonBases().add(this);
197 }
198 this.name = name;
199 }
200
201 /**
202 * Returns the {@link eu.etaxonomy.cdm.model.name.HomotypicalGroup homotypical group} of the
203 * {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon name} used in <i>this</i> (abstract) taxon.
204 */
205 @Transient
206 public HomotypicalGroup getHomotypicGroup(){
207 if (this.getName() == null){
208 return null;
209 }else{
210 return this.getName().getHomotypicalGroup();
211 }
212 }
213
214 /**
215 * Returns the boolean value indicating whether the assignment of <i>this</i>
216 * (abstract) taxon to the {@link Taxon Taxon} or to the {@link Synonym Synonym} class is definitive
217 * (false) or not (true). If this flag is set the use of <i>this</i> (abstract)
218 * taxon as an "accepted/correct" name or as a (junior) "synonym" might
219 * still change in the course of taxonomical working process.
220 */
221 public boolean isDoubtful(){
222 return this.doubtful;
223 }
224 /**
225 * @see #isDoubtful()
226 */
227 public void setDoubtful(boolean doubtful){
228 this.doubtful = doubtful;
229 }
230
231 /**
232 * Returns the {@link eu.etaxonomy.cdm.model.reference.ReferenceBase reference} of <i>this</i> (abstract) taxon.
233 * This is the reference or the treatment using the {@link TaxonNameBase taxon name}
234 * in <i>this</i> (abstract) taxon.
235 */
236 public ReferenceBase getSec() {
237 return sec;
238 }
239
240 /**
241 * @see #getSec()
242 */
243 public void setSec(ReferenceBase sec) {
244 this.sec = sec;
245 }
246
247
248
249 /**
250 * An appended phrase is a phrase that is added to the {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon name}
251 * 's title cache to be used just in this taxon. E.g. the phrase "sensu latu" may be added
252 * to the name to describe this taxon more precisely.
253 * If {@link #isUseNameCache()}
254 * @return the appendedPhrase
255 */
256 public String getAppendedPhrase() {
257 return appendedPhrase;
258 }
259
260 /**
261 * @param appendedPhrase the appendedPhrase to set
262 */
263 public void setAppendedPhrase(String appendedPhrase) {
264 this.appendedPhrase = appendedPhrase;
265 }
266
267 /**
268 * @return the useNameCache
269 */
270 public boolean isUseNameCache() {
271 return useNameCache;
272 }
273
274 /**
275 * @param useNameCache the useNameCache to set
276 */
277 public void setUseNameCache(boolean useNameCache) {
278 this.useNameCache = useNameCache;
279 }
280
281 /**
282 * Returns the boolean value indicating whether <i>this</i> (abstract) taxon
283 * might be saved (true) or not (false). An (abstract) taxon is meaningful
284 * as long as both its {@link #getName() taxon name} and its {@link #getSec() reference}
285 * exist (are not "null").
286 * FIXME This should be part of a more generic validation architecture
287 */
288 @Deprecated
289 @Transient
290 public boolean isSaveable(){
291 if ( (this.getName() == null) || (this.getSec() == null) ){
292 return false;
293 }else{
294 this.toString();
295 return true;
296 }
297 }
298
299
300 }