Project

General

Profile

Download (11 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
import java.util.HashSet;
13
import java.util.Set;
14

    
15
import javax.persistence.Entity;
16
import javax.persistence.FetchType;
17
import javax.persistence.JoinTable;
18
import javax.persistence.ManyToMany;
19
import javax.persistence.OneToMany;
20
import javax.validation.constraints.NotNull;
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;
29

    
30
import org.apache.log4j.Logger;
31
import org.hibernate.annotations.Cascade;
32
import org.hibernate.annotations.CascadeType;
33
import org.hibernate.envers.Audited;
34

    
35
import eu.etaxonomy.cdm.model.common.DefinedTerm;
36
import eu.etaxonomy.cdm.model.common.Representation;
37
import eu.etaxonomy.cdm.model.location.NamedArea;
38
import eu.etaxonomy.cdm.model.media.Media;
39
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41

    
42
/**
43
 * The class representing single-access fixed dichotomous or polytomous authored
44
 * decision keys (as opposed to {@link FeatureTree multiple-access keys}) used to identify
45
 * {@link SpecimenOrObservationBase specimens or observations} (this means to assign {@link Taxon taxa} to).
46
 * The determination process is based on the tree structure of the key and on
47
 * the statements of its leads.
48
 *
49
 * @author m.doering
50
 * @created 08-Nov-2007 13:06:28
51
 */
52

    
53
@XmlAccessorType(XmlAccessType.FIELD)
54
@XmlType(name = "MediaKey", propOrder = {
55
    "coveredTaxa",
56
    "taxonomicScope",
57
    "geographicalScope",
58
    "scopeRestrictions",
59
    "keyRepresentations"
60
})
61
@XmlRootElement(name = "MediaKey")
62
@Entity
63
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
64
//@Indexed(index = "eu.etaxonomy.cdm.model.media.Media")
65
@Audited
66
public class MediaKey extends Media implements IIdentificationKey{
67
	private static final long serialVersionUID = -29095811051894471L;
68
	private static final Logger logger = Logger.getLogger(MediaKey.class);
69

    
70
	@XmlElementWrapper(name = "CoveredTaxa")
71
	@XmlElement(name = "CoveredTaxon")
72
	@XmlIDREF
73
	@XmlSchemaType(name = "IDREF")
74
	@ManyToMany(fetch = FetchType.LAZY)
75
    @JoinTable(name="MediaKey_CoveredTaxon")
76
    @NotNull
77
	private Set<Taxon> coveredTaxa = new HashSet<Taxon>();
78

    
79
	@XmlElementWrapper( name = "GeographicalScope")
80
	@XmlElement( name = "Area")
81
	@XmlIDREF
82
	@XmlSchemaType(name = "IDREF")
83
	@ManyToMany(fetch = FetchType.LAZY)
84
	@JoinTable(name="MediaKey_NamedArea")
85
	@NotNull
86
	private Set<NamedArea> geographicalScope = new HashSet<NamedArea>();
87

    
88
	@XmlElementWrapper(name = "TaxonomicScope")
89
	@XmlElement(name = "Taxon")
90
	@XmlIDREF
91
	@XmlSchemaType(name = "IDREF")
92
	@ManyToMany(fetch = FetchType.LAZY)
93
	@JoinTable(name="MediaKey_TaxonScope")
94
	@NotNull
95
	private Set<Taxon> taxonomicScope = new HashSet<Taxon>();
96

    
97
	@XmlElementWrapper( name = "ScopeRestrictions")
98
	@XmlElement( name = "Restriction")
99
	@XmlIDREF
100
	@XmlSchemaType(name = "IDREF")
101
	@ManyToMany(fetch = FetchType.LAZY)
102
    @JoinTable(name="MediaKey_Scope")
103
	@NotNull
104
	private Set<DefinedTerm> scopeRestrictions = new HashSet<DefinedTerm>();
105

    
106
	@XmlElementWrapper( name = "KeyRepresentations")
107
	@XmlElement( name = "KeyRepresentation")
108
	@XmlIDREF
109
	@XmlSchemaType(name = "IDREF")
110
	@OneToMany(fetch=FetchType.LAZY, orphanRemoval=true)
111
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE,CascadeType.DELETE})
112
	@NotNull
113
	private Set<Representation> keyRepresentations = new HashSet<Representation>();
114

    
115
	/**
116
	 * Class constructor: creates a new empty identification key instance.
117
	 */
118
	protected MediaKey() {
119
		super();
120
	}
121

    
122
	/**
123
	 * Creates a new empty identification key instance.
124
	 */
125
	public static MediaKey NewInstance(){
126
		return new MediaKey();
127
	}
128

    
129

    
130
	/**
131
	 * Returns the set of possible {@link Taxon taxa} corresponding to
132
	 * <i>this</i> identification key.
133
	 */
134
	@Override
135
    public Set<Taxon> getCoveredTaxa() {
136
		if(coveredTaxa == null) {
137
			this.coveredTaxa = new HashSet<Taxon>();
138
		}
139
		return coveredTaxa;
140
	}
141
	/**
142
	 * @see	#getCoveredTaxa()
143
	 */
144
	protected void setCoveredTaxa(Set<Taxon> coveredTaxa) {
145
		this.coveredTaxa = coveredTaxa;
146
	}
147

    
148
	/**
149
	 * Adds a {@link Taxon taxa} to the set of {@link #getCoveredTaxa() covered taxa}
150
	 * corresponding to <i>this</i> identification key.
151
	 *
152
	 * @param	taxon	the taxon to be added to <i>this</i> identification key
153
	 * @see    	   		#getCoveredTaxa()
154
	 */
155
	@Override
156
    public void addCoveredTaxon(Taxon taxon) {
157
		this.coveredTaxa.add(taxon);
158
	}
159

    
160
	/**
161
	 * Removes one element from the set of {@link #getCoveredTaxa() covered taxa}
162
	 * corresponding to <i>this</i> identification key.
163
	 *
164
	 * @param	taxon	the taxon which should be removed
165
	 * @see     		#getCoveredTaxa()
166
	 * @see     		#addCoveredTaxon(Taxon)
167
	 */
168
	@Override
169
    public void removeCoveredTaxon(Taxon taxon) {
170
		this.coveredTaxa.remove(taxon);
171
	}
172

    
173
	/**
174
	 * Returns the set of {@link NamedArea named areas} indicating the geospatial
175
	 * data where <i>this</i> identification key is valid.
176
	 */
177
	@Override
178
    public Set<NamedArea> getGeographicalScope() {
179
		if(geographicalScope == null) {
180
			this.geographicalScope = new HashSet<NamedArea>();
181
		}
182
		return geographicalScope;
183
	}
184

    
185
	/**
186
	 * Adds a {@link NamedArea geoScope} to the set of {@link #getGeoScopes() geogspatial scopes}
187
	 * corresponding to <i>this</i> identification key.
188
	 *
189
	 * @param	geoScope	the named area to be added to <i>this</i> identification key
190
	 * @see    	   		 	#getGeoScopes()
191
	 */
192
	@Override
193
    public void addGeographicalScope(NamedArea geoScope) {
194
		this.geographicalScope.add(geoScope);
195
	}
196
	/**
197
	 * Removes one element from the set of {@link #getGeoScopes() geogspatial scopes}
198
	 * corresponding to <i>this</i> identification key.
199
	 *
200
	 * @param	geoScope	the named area which should be removed
201
	 * @see     			#getGeoScopes()
202
	 * @see     			#addGeoScope(NamedArea)
203
	 */
204
	@Override
205
    public void removeGeographicalScope(NamedArea geoScope) {
206
		this.geographicalScope.remove(geoScope);
207
	}
208

    
209
	/**
210
	 * Returns the set of {@link Taxon taxa} that define the taxonomic
211
	 * scope of <i>this</i> identification key
212
	 */
213
	@Override
214
    public Set<Taxon> getTaxonomicScope() {
215
		if(taxonomicScope == null) {
216
			this.taxonomicScope = new HashSet<Taxon>();
217
		}
218
		return taxonomicScope;
219
	}
220

    
221
	/**
222
	 * Adds a {@link Taxon taxa} to the set of {@link #getTaxonomicScope() taxonomic scopes}
223
	 * corresponding to <i>this</i> identification key.
224
	 *
225
	 * @param	taxon	the taxon to be added to <i>this</i> identification key
226
	 * @see    	   		#getTaxonomicScope()
227
	 */
228
	@Override
229
    public void addTaxonomicScope(Taxon taxon) {
230
		this.taxonomicScope.add(taxon);
231
	}
232

    
233
	/**
234
	 * Removes one element from the set of {@link #getTaxonomicScope() taxonomic scopes}
235
	 * corresponding to <i>this</i> identification key.
236
	 *
237
	 * @param	taxon	the taxon which should be removed
238
	 * @see     		#getTaxonomicScope()
239
	 * @see     		#addTaxonomicScope(Taxon)
240
	 */
241
	@Override
242
    public void removeTaxonomicScope(Taxon taxon) {
243
		this.taxonomicScope.remove(taxon);
244
	}
245

    
246
	/**
247
	 * Returns the set of {@link Representation key representations} corresponding to
248
	 * <i>this</i> identification key
249
	 */
250
	public Set<Representation> getKeyRepresentations() {
251
		if(keyRepresentations == null) {
252
			this.keyRepresentations = new HashSet<Representation>();
253
		}
254
		return keyRepresentations;
255
	}
256

    
257
	/**
258
	 * Adds a {@link Representation key representation} to the set of {@link #getKeyRepresentations() key representations}
259
	 * corresponding to <i>this</i> identification key.
260
	 *
261
	 * @param	keyRepresentation	the key representation to be added to <i>this</i> identification key
262
	 * @see    	   		#getKeyRepresentations()
263
	 */
264
	public void addKeyRepresentation(Representation keyRepresentation) {
265
		this.keyRepresentations.add(keyRepresentation);
266
	}
267

    
268
	/**
269
	 * Removes one element from the set of {@link #getKeyRepresentations() key representations}
270
	 * corresponding to <i>this</i> identification key.
271
	 *
272
	 * @param	keyRepresentation	the key representation which should be removed
273
	 * @see     		#getKeyRepresentations()
274
	 * @see     		#addKeyRepresentation(Representation)
275
	 */
276
	public void removeKeyRepresentation(Representation keyRepresentation) {
277
		this.keyRepresentations.remove(keyRepresentation);
278
	}
279

    
280
	/**
281
	 * Returns the set of {@link Scope scope restrictions} corresponding to
282
	 * <i>this</i> identification key
283
	 */
284
	@Override
285
    public Set<DefinedTerm> getScopeRestrictions() {
286
		if(scopeRestrictions == null) {
287
			this.scopeRestrictions = new HashSet<DefinedTerm>();
288
		}
289
		return scopeRestrictions;
290
	}
291

    
292
	/**
293
	 * Adds a {@link Scope scope restriction} to the set of {@link #getScopeRestrictions() scope restrictions}
294
	 * corresponding to <i>this</i> identification key.
295
	 *
296
	 * @param	scopeRestriction	the scope restriction to be added to <i>this</i> identification key
297
	 * @see    	   		#getScopeRestrictions()
298
	 */
299
	@Override
300
    public void addScopeRestriction(DefinedTerm scopeRestriction) {
301
		this.scopeRestrictions.add(scopeRestriction);
302
	}
303

    
304
	/**
305
	 * Removes one element from the set of {@link #getScopeRestrictions() scope restrictions}
306
	 * corresponding to <i>this</i> identification key.
307
	 *
308
	 * @param	scopeRestriction	the scope restriction which should be removed
309
	 * @see     		#getScopeRestrictions()
310
	 * @see     		#addScopeRestriction(Scope)
311
	 */
312
	@Override
313
    public void removeScopeRestriction(DefinedTerm scopeRestriction) {
314
		this.scopeRestrictions.remove(scopeRestriction);
315
	}
316

    
317
//*********************** CLONE ********************************************************/
318

    
319
	/**
320
	 * Clones <i>this</i> MediaKey. This is a shortcut that enables to create
321
	 * a new instance that differs only slightly from <i>this</i> MediaKey by
322
	 * modifying only some of the attributes.
323
	 *
324
	 * @see eu.etaxonomy.cdm.model.media.Media#clone()
325
	 * @see java.lang.Object#clone()
326
	 */
327
	@Override
328
	public Object clone() {
329
		MediaKey result;
330

    
331
		try{
332
			result = (MediaKey)super.clone();
333

    
334
			result.coveredTaxa = new HashSet<Taxon>();
335
			for (Taxon taxon: this.coveredTaxa){
336
				result.addCoveredTaxon(taxon);
337
			}
338

    
339
			result.geographicalScope = new HashSet<NamedArea>();
340
			for (NamedArea area: this.geographicalScope){
341
				result.addGeographicalScope(area);
342
			}
343

    
344
			result.keyRepresentations = new HashSet<Representation>();
345
			for (Representation rep: this.keyRepresentations) {
346
				result.addKeyRepresentation(rep);
347
			}
348

    
349
			result.scopeRestrictions = new HashSet<DefinedTerm>();
350
			for (DefinedTerm scope: this.scopeRestrictions){
351
				result.addScopeRestriction(scope);
352
			}
353

    
354
			result.taxonomicScope = new HashSet<Taxon>();
355
			for (Taxon taxon: this.taxonomicScope){
356
				result.addTaxonomicScope(taxon);
357
			}
358

    
359
			return result;
360

    
361
		}catch (CloneNotSupportedException e) {
362
			logger.warn("Object does not implement cloneable");
363
			e.printStackTrace();
364
			return null;
365
		}
366

    
367

    
368
	}
369
}
(18-18/37)