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.Iterator;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import javax.persistence.Entity;
|
18
|
import javax.persistence.FetchType;
|
19
|
import javax.persistence.JoinTable;
|
20
|
import javax.persistence.ManyToMany;
|
21
|
import javax.persistence.ManyToOne;
|
22
|
import javax.persistence.OneToMany;
|
23
|
import javax.persistence.Transient;
|
24
|
import javax.validation.constraints.NotNull;
|
25
|
import javax.xml.bind.annotation.XmlAccessType;
|
26
|
import javax.xml.bind.annotation.XmlAccessorType;
|
27
|
import javax.xml.bind.annotation.XmlElement;
|
28
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
29
|
import javax.xml.bind.annotation.XmlIDREF;
|
30
|
import javax.xml.bind.annotation.XmlRootElement;
|
31
|
import javax.xml.bind.annotation.XmlSchemaType;
|
32
|
import javax.xml.bind.annotation.XmlType;
|
33
|
|
34
|
import org.apache.log4j.Logger;
|
35
|
import org.hibernate.annotations.Cascade;
|
36
|
import org.hibernate.annotations.CascadeType;
|
37
|
import org.hibernate.envers.Audited;
|
38
|
|
39
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
40
|
import eu.etaxonomy.cdm.model.common.Language;
|
41
|
import eu.etaxonomy.cdm.model.common.Representation;
|
42
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
43
|
import eu.etaxonomy.cdm.model.name.Rank;
|
44
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
45
|
import eu.etaxonomy.cdm.strategy.cache.description.DescriptiveDataSetDefaultCacheStrategy;
|
46
|
|
47
|
/**
|
48
|
*
|
49
|
* The working set class allows the demarcation of a set of descriptions
|
50
|
* associated with representations and a set of features and their
|
51
|
* dependencies.
|
52
|
*
|
53
|
* @author h.fradin
|
54
|
* @since 12.08.2009
|
55
|
*/
|
56
|
|
57
|
@XmlAccessorType(XmlAccessType.FIELD)
|
58
|
@XmlType(name = "DescriptiveDataSet", propOrder = {
|
59
|
"representations",
|
60
|
"descriptiveSystem",
|
61
|
"descriptions",
|
62
|
"taxonSubtreeFilter",
|
63
|
"geoFilter",
|
64
|
"minRank",
|
65
|
"maxRank"
|
66
|
})
|
67
|
@XmlRootElement(name = "DescriptiveDataSet")
|
68
|
@Entity
|
69
|
@Audited
|
70
|
public class DescriptiveDataSet extends IdentifiableEntity<DescriptiveDataSetDefaultCacheStrategy> {
|
71
|
private static final long serialVersionUID = 3256448866757415686L;
|
72
|
private static final Logger logger = Logger.getLogger(DescriptiveDataSet.class);
|
73
|
|
74
|
@XmlElementWrapper(name = "Representations")
|
75
|
@XmlElement(name = "Representation")
|
76
|
@OneToMany(fetch=FetchType.EAGER)
|
77
|
@Cascade( { CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE })
|
78
|
private Set<Representation> representations = new HashSet<>();
|
79
|
|
80
|
@XmlElement(name = "DescriptiveSystem")
|
81
|
@XmlIDREF
|
82
|
@XmlSchemaType(name = "IDREF")
|
83
|
@ManyToOne(fetch = FetchType.LAZY)
|
84
|
@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
|
85
|
private FeatureTree descriptiveSystem;
|
86
|
|
87
|
@XmlElementWrapper(name = "Descriptions")
|
88
|
@XmlElement(name = "Description")
|
89
|
@XmlIDREF
|
90
|
@XmlSchemaType(name = "IDREF")
|
91
|
@ManyToMany(fetch = FetchType.LAZY)
|
92
|
@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
|
93
|
@NotNull
|
94
|
private Set<DescriptionBase> descriptions = new HashSet<>();
|
95
|
|
96
|
@XmlElementWrapper(name = "SubtreeTaxonFilter")
|
97
|
@XmlElement(name = "Subtree")
|
98
|
@XmlIDREF
|
99
|
@XmlSchemaType(name = "IDREF")
|
100
|
@ManyToMany(fetch = FetchType.LAZY)
|
101
|
@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
|
102
|
@NotNull
|
103
|
//a positive filter that defines that all taxa in the subtree belong to
|
104
|
//the dataset. If the filter is NOT set, taxa need to be explicitly defined
|
105
|
//via the descriptions set. If the filter is set all taxa not having
|
106
|
//a description in descriptions yet are considered to have an empty description
|
107
|
//TODO what, if a taxon is removed from the subtree but a description exists in
|
108
|
//descriptions
|
109
|
private Set<TaxonNode> taxonSubtreeFilter = new HashSet<>();
|
110
|
|
111
|
@XmlElementWrapper(name = "GeoFilter")
|
112
|
@XmlElement(name = "FilteredArea")
|
113
|
@XmlIDREF
|
114
|
@XmlSchemaType(name = "IDREF")
|
115
|
@ManyToMany(fetch = FetchType.LAZY)
|
116
|
@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
|
117
|
@JoinTable(name="DescriptiveDataSet_NamedArea")
|
118
|
@NotNull
|
119
|
private Set<NamedArea> geoFilter = new HashSet<>();
|
120
|
|
121
|
@XmlElement(name = "minRank")
|
122
|
@XmlIDREF
|
123
|
@XmlSchemaType(name = "IDREF")
|
124
|
@ManyToOne(fetch = FetchType.LAZY)
|
125
|
private Rank minRank;
|
126
|
|
127
|
@XmlElement(name = "maxRank")
|
128
|
@XmlIDREF
|
129
|
@XmlSchemaType(name = "IDREF")
|
130
|
@ManyToOne(fetch = FetchType.LAZY)
|
131
|
private Rank maxRank;
|
132
|
|
133
|
// ******************* FACTORY *********************************************/
|
134
|
|
135
|
public static DescriptiveDataSet NewInstance(){
|
136
|
return new DescriptiveDataSet();
|
137
|
}
|
138
|
|
139
|
|
140
|
// *******************CONSTRUCTOR **********************************/
|
141
|
/**
|
142
|
* Class constructor: creates a new empty working set instance.
|
143
|
*/
|
144
|
protected DescriptiveDataSet() {
|
145
|
super();
|
146
|
this.cacheStrategy = new DescriptiveDataSetDefaultCacheStrategy();
|
147
|
}
|
148
|
|
149
|
// ******************** GETTER / SETTER ************************/
|
150
|
|
151
|
|
152
|
public Set<TaxonNode> getTaxonSubtreeFilter() {
|
153
|
return taxonSubtreeFilter;
|
154
|
}
|
155
|
|
156
|
public void setTaxonSubtreeFilter(Set<TaxonNode> taxonSubtreeFilter) {
|
157
|
this.taxonSubtreeFilter = taxonSubtreeFilter;
|
158
|
}
|
159
|
|
160
|
public void addTaxonSubtree(TaxonNode subtree) {
|
161
|
this.taxonSubtreeFilter.add(subtree);
|
162
|
}
|
163
|
|
164
|
public void removeTaxonSubtree(TaxonNode subtree) {
|
165
|
this.taxonSubtreeFilter.remove(subtree);
|
166
|
}
|
167
|
|
168
|
//geo filter
|
169
|
public Set<NamedArea> getGeoFilter() {
|
170
|
return geoFilter;
|
171
|
}
|
172
|
public void setGeoFilter(Set<NamedArea> geoFilter) {
|
173
|
this.geoFilter = geoFilter;
|
174
|
}
|
175
|
public void addGeoFilterArea(NamedArea area){
|
176
|
this.geoFilter.add(area);
|
177
|
}
|
178
|
public boolean removeGeoFilterArea(NamedArea area) {
|
179
|
return this.geoFilter.remove(area);
|
180
|
}
|
181
|
|
182
|
//min rank
|
183
|
public Rank getMinRank() {
|
184
|
return minRank;
|
185
|
}
|
186
|
public void setMinRank(Rank minRank) {
|
187
|
this.minRank = minRank;
|
188
|
}
|
189
|
|
190
|
//max rank
|
191
|
public Rank getMaxRank() {
|
192
|
return maxRank;
|
193
|
}
|
194
|
public void setMaxRank(Rank maxRank) {
|
195
|
this.maxRank = maxRank;
|
196
|
}
|
197
|
|
198
|
//representations
|
199
|
public Set<Representation> getRepresentations() {
|
200
|
return this.representations;
|
201
|
}
|
202
|
public void addRepresentation(Representation representation) {
|
203
|
this.representations.add(representation);
|
204
|
}
|
205
|
public void removeRepresentation(Representation representation) {
|
206
|
this.representations.remove(representation);
|
207
|
}
|
208
|
|
209
|
public Representation getRepresentation(Language lang) {
|
210
|
for (Representation repr : representations){
|
211
|
Language reprLanguage = repr.getLanguage();
|
212
|
if (reprLanguage != null && reprLanguage.equals(lang)){
|
213
|
return repr;
|
214
|
}
|
215
|
}
|
216
|
return null;
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* @see #getPreferredRepresentation(Language)
|
221
|
* @param language
|
222
|
* @return
|
223
|
*/
|
224
|
public Representation getPreferredRepresentation(Language language) {
|
225
|
Representation repr = getRepresentation(language);
|
226
|
if(repr == null){
|
227
|
repr = getRepresentation(Language.DEFAULT());
|
228
|
}
|
229
|
if(repr == null){
|
230
|
repr = getRepresentations().iterator().next();
|
231
|
}
|
232
|
return repr;
|
233
|
}
|
234
|
|
235
|
/**
|
236
|
* Returns the Representation in the preferred language. Preferred languages
|
237
|
* are specified by the parameter languages, which receives a list of
|
238
|
* Language instances in the order of preference. If no representation in
|
239
|
* any preferred languages is found the method falls back to return the
|
240
|
* Representation in Language.DEFAULT() and if necessary further falls back
|
241
|
* to return the first element found if any.
|
242
|
*
|
243
|
* TODO think about this fall-back strategy &
|
244
|
* see also {@link TextData#getPreferredLanguageString(List)}
|
245
|
*
|
246
|
* @param languages
|
247
|
* @return
|
248
|
*/
|
249
|
public Representation getPreferredRepresentation(List<Language> languages) {
|
250
|
Representation repr = null;
|
251
|
if(languages != null){
|
252
|
for(Language language : languages) {
|
253
|
repr = getRepresentation(language);
|
254
|
if(repr != null){
|
255
|
return repr;
|
256
|
}
|
257
|
}
|
258
|
}
|
259
|
if(repr == null){
|
260
|
repr = getRepresentation(Language.DEFAULT());
|
261
|
}
|
262
|
if(repr == null){
|
263
|
Iterator<Representation> it = getRepresentations().iterator();
|
264
|
if(it.hasNext()){
|
265
|
repr = getRepresentations().iterator().next();
|
266
|
}
|
267
|
}
|
268
|
return repr;
|
269
|
}
|
270
|
|
271
|
@Transient
|
272
|
public String getLabel() {
|
273
|
if(getLabel(Language.DEFAULT())!=null){
|
274
|
Representation repr = getRepresentation(Language.DEFAULT());
|
275
|
return (repr == null)? null :repr.getLabel();
|
276
|
}else{
|
277
|
for (Representation r : representations){
|
278
|
return r.getLabel();
|
279
|
}
|
280
|
}
|
281
|
return super.getUuid().toString();
|
282
|
}
|
283
|
|
284
|
public String getLabel(Language lang) {
|
285
|
Representation repr = this.getRepresentation(lang);
|
286
|
return (repr == null) ? null : repr.getLabel();
|
287
|
}
|
288
|
|
289
|
public void setLabel(String label){
|
290
|
Language lang = Language.DEFAULT();
|
291
|
setLabel(label, lang);
|
292
|
}
|
293
|
|
294
|
public void setLabel(String label, Language language){
|
295
|
if (language != null){
|
296
|
Representation repr = getRepresentation(language);
|
297
|
if (repr != null){
|
298
|
repr.setLabel(label);
|
299
|
}else{
|
300
|
repr = Representation.NewInstance(null, label, null, language);
|
301
|
}
|
302
|
this.addRepresentation(repr);
|
303
|
}
|
304
|
}
|
305
|
|
306
|
public FeatureTree getDescriptiveSystem() {
|
307
|
return descriptiveSystem;
|
308
|
}
|
309
|
public void setDescriptiveSystem(FeatureTree descriptiveSystem) {
|
310
|
this.descriptiveSystem = descriptiveSystem;
|
311
|
}
|
312
|
|
313
|
/**
|
314
|
* Returns the {@link DescriptionBase descriptions} of
|
315
|
* <i>this</i> working set.
|
316
|
*
|
317
|
* @see #addDescription(DescriptionBase)
|
318
|
* @see #removeDescription(DescriptionBase)
|
319
|
*/
|
320
|
public Set<DescriptionBase> getDescriptions() {
|
321
|
return descriptions;
|
322
|
}
|
323
|
|
324
|
/**
|
325
|
* Adds an existing {@link DescriptionBase description} to the set of
|
326
|
* {@link #getDescriptions() descriptions} of <i>this</i>
|
327
|
* working set.
|
328
|
*
|
329
|
* @param description the description to be added to <i>this</i> working set
|
330
|
* @see #getDescriptions()
|
331
|
* @see DescriptiveDataSet#addDescription(DescriptionBase)
|
332
|
*/
|
333
|
public boolean addDescription(DescriptionBase description) {
|
334
|
boolean result = this.descriptions.add(description);
|
335
|
if (! description.getDescriptiveDataSets().contains(this)){
|
336
|
description.addDescriptiveDataSet(this);
|
337
|
}
|
338
|
return result;
|
339
|
}
|
340
|
|
341
|
/**
|
342
|
* Removes one element from the set of {@link #getDescriptions() descriptions} involved
|
343
|
* in <i>this</i> working set.<BR>
|
344
|
*
|
345
|
* @param description the description which should be removed
|
346
|
* @see #getDescriptions()
|
347
|
* @see #addDescription(DescriptionBase)
|
348
|
* @see DescriptiveDataSet#removeDescription(DescriptionBase)
|
349
|
*/
|
350
|
public boolean removeDescription(DescriptionBase description) {
|
351
|
boolean result = this.descriptions.remove(description);
|
352
|
if (description.getDescriptiveDataSets().contains(this)){
|
353
|
description.removeDescriptiveDataSet(this);
|
354
|
}
|
355
|
return result;
|
356
|
}
|
357
|
|
358
|
//*********************** CLONE ********************************************************/
|
359
|
|
360
|
/**
|
361
|
* Clones <i>this</i> DescriptiveDataSet. This is a shortcut that enables to create
|
362
|
* a new instance that differs only slightly from <i>this</i> DescriptiveDataSet by
|
363
|
* modifying only some of the attributes.
|
364
|
* The descriptions and the descriptive system are the same, the representations
|
365
|
* are cloned.
|
366
|
*
|
367
|
* @see eu.etaxonomy.cdm.model.common.AnnotatableEntity#clone()
|
368
|
* @see java.lang.Object#clone()
|
369
|
*/
|
370
|
@Override
|
371
|
public Object clone() {
|
372
|
DescriptiveDataSet result;
|
373
|
try {
|
374
|
result = (DescriptiveDataSet)super.clone();
|
375
|
|
376
|
//descriptions
|
377
|
result.descriptions = new HashSet<>();
|
378
|
for (DescriptionBase<?> desc: this.descriptions){
|
379
|
result.addDescription(desc);
|
380
|
}
|
381
|
|
382
|
//representations
|
383
|
result.representations = new HashSet<>();
|
384
|
for (Representation rep : this.representations){
|
385
|
result.addRepresentation((Representation)rep.clone());
|
386
|
}
|
387
|
|
388
|
//subtree filter
|
389
|
result.taxonSubtreeFilter = new HashSet<>();
|
390
|
for (TaxonNode subtree : this.taxonSubtreeFilter){
|
391
|
result.addTaxonSubtree(subtree);
|
392
|
}
|
393
|
|
394
|
//geo filter
|
395
|
result.geoFilter = new HashSet<>();
|
396
|
for (NamedArea area : this.geoFilter){
|
397
|
result.addGeoFilterArea(area);
|
398
|
}
|
399
|
|
400
|
return result;
|
401
|
}catch (CloneNotSupportedException e) {
|
402
|
logger.warn("Object does not implement cloneable");
|
403
|
e.printStackTrace();
|
404
|
return null;
|
405
|
}
|
406
|
}
|
407
|
|
408
|
}
|