Project

General

Profile

Download (8.19 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.description;
11

    
12

    
13
import java.util.HashSet;
14
import java.util.Set;
15

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

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

    
39
import eu.etaxonomy.cdm.model.common.CdmBase;
40
import eu.etaxonomy.cdm.model.location.NamedArea;
41
import eu.etaxonomy.cdm.model.taxon.Taxon;
42
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
43
import eu.etaxonomy.cdm.strategy.cache.description.TaxonDescriptionDefaultCacheStrategy;
44

    
45
/**
46
 * This class represents descriptions that delimit or circumscribe a real taxon.
47
 * <P>
48
 * This class corresponds to: <ul>
49
 * <li> DescriptionsBaseType with a "Class" element according to the the SDD schema
50
 * <li> SpeciesProfileModel according to the TDWG ontology
51
 * <li> CharacterCircumscription according to the TCS
52
 * </ul>
53
 * 
54
 * @author m.doering
55
 * @version 1.0
56
 * @created 08-Nov-2007 13:06:20
57
 */
58
@XmlAccessorType(XmlAccessType.FIELD)
59
@XmlType(name = "TaxonDescription", propOrder = {
60
    "scopes",
61
    "geoScopes",
62
    "taxon"
63
})
64
@XmlRootElement(name = "TaxonDescription")
65
@Entity
66
@Indexed(index = "eu.etaxonomy.cdm.model.description.DescriptionBase")
67
@Audited
68
@Configurable
69
public class TaxonDescription extends DescriptionBase<IIdentifiableEntityCacheStrategy<TaxonDescription>> implements Cloneable{
70
	private static final long serialVersionUID = 8065879180505546803L;
71
	@SuppressWarnings("unused")
72
	private static final Logger logger = Logger.getLogger(TaxonDescription.class);
73

    
74
	@XmlElementWrapper(name = "Scopes")
75
	@XmlElement(name = "Scope")
76
	@XmlIDREF
77
	@XmlSchemaType(name="IDREF")
78
	@ManyToMany(fetch = FetchType.LAZY)
79
	@JoinTable(name="DescriptionBase_Scope")
80
	private Set<Scope> scopes = new HashSet<Scope>();
81
	
82
	@XmlElementWrapper( name = "GeoScopes")
83
	@XmlElement( name = "GeoScope")
84
	@XmlIDREF
85
	@XmlSchemaType(name="IDREF")
86
	@ManyToMany(fetch = FetchType.LAZY)
87
	@JoinTable(name="DescriptionBase_GeoScope")
88
	@Cascade({CascadeType.SAVE_UPDATE})
89
	private Set<NamedArea> geoScopes = new HashSet<NamedArea>();
90
	
91
	@XmlElement( name = "Taxon")
92
	@ManyToOne(fetch = FetchType.LAZY)
93
	@XmlIDREF
94
	@XmlSchemaType(name="IDREF")
95
//	@JoinColumn(name="taxon_fk")
96
	//@Cascade(CascadeType.SAVE_UPDATE)
97
	private Taxon taxon;
98

    
99
	
100
	public void setTaxon(Taxon taxon) {
101
		this.taxon = taxon;
102
	}
103

    
104
	/**
105
	 * Creates a new empty taxon description instance.
106
	 * 
107
	 * @see	#NewInstance(Taxon)
108
	 */
109
	public static TaxonDescription NewInstance(){
110
		return new TaxonDescription();
111
	}
112
	
113
	/**
114
	 * Creates a new taxon description instance for the given {@link Taxon taxon}.
115
	 * The new taxon description will be also added to the {@link Taxon#getDescriptions() set of descriptions}
116
	 * assigned to the given taxon.
117
	 * 
118
	 * @see	#NewInstance()
119
	 */
120
	public static TaxonDescription NewInstance(Taxon taxon){
121
		TaxonDescription description = new TaxonDescription();
122
		if (taxon != null){
123
			taxon.addDescription(description);
124
		}
125
		return description;
126
	}
127
	
128
	/**
129
	 * Creates a new taxon description instance for the given {@link Taxon taxon}.
130
	 * The new taxon description will be also added to the {@link Taxon#getDescriptions() set of descriptions}
131
	 * assigned to the given taxon.
132
	 * 
133
	 * @see	#NewInstance()
134
	 */
135
	public static TaxonDescription NewInstance(Taxon taxon, boolean isImageGallery){
136
		TaxonDescription description = new TaxonDescription();
137
		taxon.addDescription(description);
138
		description.setImageGallery(isImageGallery);
139
		return description;
140
	}
141
	
142
//******************** CONSTRUCTOR *************************************************/
143
	
144
	/**
145
	 * Class constructor: creates a new empty taxon description instance.
146
	 */
147
	public TaxonDescription(){
148
		super();
149
		this.cacheStrategy = new TaxonDescriptionDefaultCacheStrategy();
150
		}
151

    
152
//************************** METHODS **********************************************/
153
	
154
	public Taxon getTaxon() {
155
		return taxon;
156
	}
157
	
158
	
159
	/** 
160
	 * Returns the set of {@link NamedArea named areas} indicating the geospatial
161
	 * data where <i>this</i> taxon description is valid.
162
	 */
163
	public Set<NamedArea> getGeoScopes(){
164
		return this.geoScopes;
165
	}
166

    
167
	/**
168
	 * Adds a {@link NamedArea named area} to the set of {@link #getGeoScopes() named areas}
169
	 * delimiting the geospatial area where <i>this</i> taxon description is valid.
170
	 * 
171
	 * @param geoScope	the named area to be additionally assigned to <i>this</i> taxon description
172
	 * @see    	   		#getGeoScopes()
173
	 */
174
	public void addGeoScope(NamedArea geoScope){
175
		this.geoScopes.add(geoScope);
176
	}
177
	
178
	/** 
179
	 * Removes one element from the set of {@link #getGeoScopes() named areas} delimiting
180
	 * the geospatial area where <i>this</i> taxon description is valid.
181
	 *
182
	 * @param  geoScope   the named area which should be removed
183
	 * @see     		  #getGeoScopes()
184
	 * @see     		  #addGeoScope(NamedArea)
185
	 */
186
	public void removeGeoScope(NamedArea geoScope){
187
		this.geoScopes.remove(geoScope);
188
	}
189

    
190
	
191
	/** 
192
	 * Returns the set of {@link Scope scopes} (this covers mostly {@link Stage life stage} or {@link Sex sex} or both)
193
	 * restricting the validity of <i>this</i> taxon description. This set
194
	 * of scopes should contain no more than one "sex" and one "life stage".
195
	 */
196
	public Set<Scope> getScopes(){
197
		return this.scopes;
198
	}
199

    
200
	/**
201
	 * Adds a {@link Scope scope} (mostly a {@link Stage life stage} or a {@link Sex sex})
202
	 * to the set of {@link #getScopes() scopes} restricting the validity of
203
	 * <i>this</i> taxon description.
204
	 * 
205
	 * @param scope	the scope to be added to <i>this</i> taxon description
206
	 * @see    	   	#getScopes()
207
	 */
208
	public void addScope(Scope scope){
209
		this.scopes.add(scope);
210
	}
211
	
212
	/** 
213
	 * Removes one element from the set of {@link #getScopes() scopes}
214
	 * restricting the validity of <i>this</i> taxon description.
215
	 *
216
	 * @param  scope	the scope which should be removed
217
	 * @see     		#getScopes()
218
	 * @see     		#addScope(Scope)
219
	 */
220
	public void removeScope(Scope scope){
221
		this.scopes.remove(scope);
222
	}
223
	
224
	/**
225
	 * Returns the first TextData element of feature type image. If no such element exists,
226
	 * a new one is created.
227
	 * @return
228
	 */
229
	@Transient
230
	public TextData getOrCreateImageTextData(){
231
		for (DescriptionElementBase element : this.getElements()){
232
			if (element.getFeature().equals(Feature.IMAGE())){
233
				if (element.isInstanceOf(TextData.class)){
234
					return CdmBase.deproxy(element, TextData.class);
235
				}
236
			}
237
		}
238
		TextData textData = TextData.NewInstance(Feature.IMAGE());
239
		addElement(textData);
240
		return textData;
241
	}
242
	
243
	
244
//*********************** CLONE ********************************************************/
245
	
246
	/** 
247
	 * Clones <i>this</i> taxon description. This is a shortcut that enables to create
248
	 * a new instance that differs only slightly from <i>this</i> taxon description by
249
	 * modifying only some of the attributes.
250
	 * 
251
	 * @see eu.etaxonomy.cdm.model.description.DescriptionBase#clone()
252
	 * @see java.lang.Object#clone()
253
	 */
254
	@Override
255
	public Object clone() {
256
		TaxonDescription result;
257
		result = (TaxonDescription)super.clone();
258
		
259
		//scopes
260
		result.scopes = new HashSet<Scope>();
261
		for (Scope scope : getScopes()){
262
			result.scopes.add(scope);
263
		}
264
		
265
		//geo-scopes
266
		result.geoScopes = new HashSet<NamedArea>();
267
		for (NamedArea namedArea : getGeoScopes()){
268
			result.geoScopes.add(namedArea);
269
		}
270
		
271
		//no changes to: taxon
272
		return result;
273
	}	
274

    
275
	
276
}
(33-33/40)