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
 * @version 1.0
51
 * @created 08-Nov-2007 13:06:28
52
 */
53

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

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

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

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

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

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

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

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

    
130

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
318
//*********************** CLONE ********************************************************/
319

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

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

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

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

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

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

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

    
360
			return result;
361

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

    
368

    
369
	}
370
}
(17-17/36)