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.EnumSet;
|
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.Inheritance;
|
19
|
import javax.persistence.InheritanceType;
|
20
|
import javax.persistence.JoinColumn;
|
21
|
import javax.persistence.ManyToMany;
|
22
|
import javax.persistence.ManyToOne;
|
23
|
import javax.persistence.OneToMany;
|
24
|
import javax.persistence.Transient;
|
25
|
import javax.validation.constraints.NotNull;
|
26
|
import javax.xml.bind.annotation.XmlAccessType;
|
27
|
import javax.xml.bind.annotation.XmlAccessorType;
|
28
|
import javax.xml.bind.annotation.XmlAttribute;
|
29
|
import javax.xml.bind.annotation.XmlElement;
|
30
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
31
|
import javax.xml.bind.annotation.XmlElements;
|
32
|
import javax.xml.bind.annotation.XmlIDREF;
|
33
|
import javax.xml.bind.annotation.XmlSchemaType;
|
34
|
import javax.xml.bind.annotation.XmlType;
|
35
|
|
36
|
import org.apache.logging.log4j.LogManager;
|
37
|
import org.apache.logging.log4j.Logger;
|
38
|
import org.hibernate.annotations.Cascade;
|
39
|
import org.hibernate.annotations.CascadeType;
|
40
|
import org.hibernate.annotations.Parameter;
|
41
|
import org.hibernate.annotations.Type;
|
42
|
import org.hibernate.envers.Audited;
|
43
|
import org.hibernate.search.annotations.ClassBridge;
|
44
|
import org.hibernate.search.annotations.ClassBridges;
|
45
|
import org.hibernate.search.annotations.ContainedIn;
|
46
|
import org.hibernate.search.annotations.FieldBridge;
|
47
|
|
48
|
import eu.etaxonomy.cdm.hibernate.HHH_9751_Util;
|
49
|
import eu.etaxonomy.cdm.hibernate.search.DescriptionBaseClassBridge;
|
50
|
import eu.etaxonomy.cdm.hibernate.search.GroupByTaxonClassBridge;
|
51
|
import eu.etaxonomy.cdm.hibernate.search.NotNullAwareIdBridge;
|
52
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
53
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
54
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
55
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
56
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
57
|
import eu.etaxonomy.cdm.model.reference.ICdmTarget;
|
58
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
59
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
60
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
61
|
|
62
|
/**
|
63
|
* The upmost (abstract) class for a description as a whole (with possibly
|
64
|
* several {@link DescriptionElementBase elementary information data})
|
65
|
* for a {@link SpecimenOrObservationBase specimen}, a {@link Taxon taxon}
|
66
|
* or even a {@link TaxonName taxon name}.
|
67
|
* <P>
|
68
|
* This class corresponds to: <ul>
|
69
|
* <li> DescriptionsSectionType according to the the SDD schema
|
70
|
* <li> MeasurementOrFact according to the ABCD schema
|
71
|
* </ul>
|
72
|
*
|
73
|
* @author m.doering
|
74
|
* @since 08-Nov-2007 13:06:24
|
75
|
*/
|
76
|
@XmlAccessorType(XmlAccessType.FIELD)
|
77
|
@XmlType(name = "DescriptionBase", propOrder = {
|
78
|
"describedSpecimenOrObservation",
|
79
|
"descriptionSources",
|
80
|
"descriptiveDataSets",
|
81
|
"descriptionElements",
|
82
|
"imageGallery",
|
83
|
"isDefault",
|
84
|
"types"
|
85
|
})
|
86
|
@Entity
|
87
|
@Audited
|
88
|
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
|
89
|
@ClassBridges({
|
90
|
@ClassBridge(impl=DescriptionBaseClassBridge.class),
|
91
|
@ClassBridge(impl=GroupByTaxonClassBridge.class)
|
92
|
})
|
93
|
public abstract class DescriptionBase<S extends IIdentifiableEntityCacheStrategy>
|
94
|
extends IdentifiableEntity<S>
|
95
|
implements ICdmTarget{
|
96
|
|
97
|
private static final long serialVersionUID = 5504218413819040193L;
|
98
|
private static final Logger logger = LogManager.getLogger(DescriptionBase.class);
|
99
|
|
100
|
@XmlElement( name = "DescribedSpecimenOrObservation")
|
101
|
@ManyToOne(fetch = FetchType.LAZY)
|
102
|
@XmlIDREF
|
103
|
@XmlSchemaType(name="IDREF")
|
104
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
|
105
|
@JoinColumn(name="specimen_id")
|
106
|
@FieldBridge(impl=NotNullAwareIdBridge.class)
|
107
|
//TODO maybe move down to specific classes SpecimenDescription (with Cascade.Delete) and TaxonDescription (without Cascade)
|
108
|
private SpecimenOrObservationBase<?> describedSpecimenOrObservation;
|
109
|
|
110
|
|
111
|
@XmlElementWrapper(name = "DescriptionSources")
|
112
|
@XmlElement(name = "DescriptionSource")
|
113
|
@XmlIDREF
|
114
|
@XmlSchemaType(name="IDREF")
|
115
|
@ManyToMany(fetch = FetchType.LAZY) //FIXME what is the difference between this and IdentifiableEntity.sources
|
116
|
@Deprecated
|
117
|
private Set<Reference> descriptionSources = new HashSet<>();
|
118
|
|
119
|
@XmlElementWrapper(name = "DescriptiveDataSets")
|
120
|
@XmlElement(name = "DescriptiveDataSet")
|
121
|
@XmlIDREF
|
122
|
@XmlSchemaType(name = "IDREF")
|
123
|
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "descriptions")
|
124
|
@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
|
125
|
private Set<DescriptiveDataSet> descriptiveDataSets = new HashSet<>();
|
126
|
|
127
|
@XmlElementWrapper(name = "DescriptionElements")
|
128
|
@XmlElements({
|
129
|
@XmlElement(name = "CategorialData", namespace = "http://etaxonomy.eu/cdm/model/description/1.0", type = CategoricalData.class),
|
130
|
@XmlElement(name = "CommonTaxonName", namespace = "http://etaxonomy.eu/cdm/model/description/1.0", type = CommonTaxonName.class),
|
131
|
@XmlElement(name = "Distribution", namespace = "http://etaxonomy.eu/cdm/model/description/1.0", type = Distribution.class),
|
132
|
@XmlElement(name = "IndividualsAssociation", namespace = "http://etaxonomy.eu/cdm/model/description/1.0", type = IndividualsAssociation.class),
|
133
|
@XmlElement(name = "QuantitativeData", namespace = "http://etaxonomy.eu/cdm/model/description/1.0", type = QuantitativeData.class),
|
134
|
@XmlElement(name = "TaxonInteraction", namespace = "http://etaxonomy.eu/cdm/model/description/1.0", type = TaxonInteraction.class),
|
135
|
@XmlElement(name = "TextData", namespace = "http://etaxonomy.eu/cdm/model/description/1.0", type = TextData.class),
|
136
|
@XmlElement(name = "TemporalData", namespace = "http://etaxonomy.eu/cdm/model/description/1.0", type = TemporalData.class)
|
137
|
})
|
138
|
@OneToMany(fetch=FetchType.LAZY, mappedBy = "inDescription", orphanRemoval=true)
|
139
|
@Cascade( { CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
|
140
|
@ContainedIn
|
141
|
private Set<DescriptionElementBase> descriptionElements = new HashSet<>();
|
142
|
|
143
|
@XmlElement(name = "ImageGallery")
|
144
|
private boolean imageGallery;
|
145
|
|
146
|
//TODO make it a DescriptionState
|
147
|
@XmlElement(name = "isDefault")
|
148
|
private boolean isDefault;
|
149
|
|
150
|
@XmlAttribute(name ="types")
|
151
|
@NotNull
|
152
|
@Type(type = "eu.etaxonomy.cdm.hibernate.EnumSetUserType",
|
153
|
parameters = {@Parameter(name = "enumClass", value = "eu.etaxonomy.cdm.model.description.DescriptionType")}
|
154
|
)
|
155
|
private EnumSet<DescriptionType> types = EnumSet.noneOf(DescriptionType.class);
|
156
|
|
157
|
//******************************** GETTER / SETTER ***************************/
|
158
|
|
159
|
/**
|
160
|
* Returns a {@link SpecimenOrObservationBase specimen or observation} involved in
|
161
|
* <i>this</i> description as a whole. {@link TaxonDescription Taxon descriptions} are also often based
|
162
|
* on concrete specimens or observations. For {@link TaxonNameDescription taxon name descriptions}
|
163
|
* this attribute should be empty.
|
164
|
* To handle sets of specimen or observations one may first group them by a derivation event of type
|
165
|
* "Grouping" and then use the grouped unit here.
|
166
|
*/
|
167
|
public SpecimenOrObservationBase getDescribedSpecimenOrObservation() {
|
168
|
return describedSpecimenOrObservation;
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* @see #getDescribedSpecimenOrObservation()
|
173
|
* @param describedSpecimenOrObservation
|
174
|
*/
|
175
|
//TODO bidirectional method should maybe removed as a description should belong to its specimen or taxon
|
176
|
public void setDescribedSpecimenOrObservation(SpecimenOrObservationBase describedSpecimenOrObservation) {
|
177
|
if (describedSpecimenOrObservation == null ){
|
178
|
if (this.describedSpecimenOrObservation != null){
|
179
|
this.describedSpecimenOrObservation.removeDescription(this);
|
180
|
}
|
181
|
}else if (! describedSpecimenOrObservation.getDescriptions().contains(this)){
|
182
|
describedSpecimenOrObservation.addDescription(this);
|
183
|
}
|
184
|
this.describedSpecimenOrObservation = describedSpecimenOrObservation;
|
185
|
}
|
186
|
|
187
|
|
188
|
|
189
|
|
190
|
/**
|
191
|
* Returns the set of {@link DescriptionElementBase elementary description data} which constitute
|
192
|
* <i>this</i> description as a whole.
|
193
|
*
|
194
|
* @see #addElement(DescriptionElementBase)
|
195
|
* @see #removeElement(DescriptionElementBase)
|
196
|
*/
|
197
|
public Set<DescriptionElementBase> getElements() {
|
198
|
return this.descriptionElements;
|
199
|
}
|
200
|
|
201
|
/**
|
202
|
* Adds an existing {@link DescriptionElementBase elementary description} to the set of
|
203
|
* {@link #getElements() elementary description data} which constitute <i>this</i>
|
204
|
* description as a whole.
|
205
|
* If the elementary descriptions already belongs to a description it is first removed from
|
206
|
* the old description.
|
207
|
*
|
208
|
* @param element the elementary description to be added to <i>this</i> description
|
209
|
* @see #getDescriptionSources()
|
210
|
*/
|
211
|
public void addElement(DescriptionElementBase element) {
|
212
|
HHH_9751_Util.removeAllNull(this.descriptionElements);
|
213
|
if (element.getInDescription() != null){
|
214
|
element.getInDescription().removeElement(element);
|
215
|
}
|
216
|
element.setInDescription(this);
|
217
|
this.descriptionElements.add(element);
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* Convenience method to add multiple elements.
|
222
|
* @param elements
|
223
|
*/
|
224
|
public void addElements(DescriptionElementBase ... elements) {
|
225
|
HHH_9751_Util.removeAllNull(this.descriptionElements);
|
226
|
for (DescriptionElementBase element : elements){
|
227
|
addElement(element);
|
228
|
}
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* Removes one element from the set of {@link #getElements() elementary description data} which
|
233
|
* constitute <i>this</i> description as a whole.
|
234
|
*
|
235
|
* @param element the reference source which should be deleted
|
236
|
* @see #getElements()
|
237
|
* @see #addElement(DescriptionElementBase)
|
238
|
*/
|
239
|
public void removeElement(DescriptionElementBase element) {
|
240
|
HHH_9751_Util.removeAllNull(this.descriptionElements);
|
241
|
this.descriptionElements.remove(element);
|
242
|
element.setInDescription(null);
|
243
|
}
|
244
|
|
245
|
|
246
|
|
247
|
/**
|
248
|
* Returns the number of {@link DescriptionElementBase elementary description data} which constitute
|
249
|
* <i>this</i> description as a whole. This is the cardinality of the set of
|
250
|
* elementary description data.
|
251
|
*
|
252
|
* @see #getElements()
|
253
|
* @return the number of elements of the elementary description data set
|
254
|
*/
|
255
|
public int size(){
|
256
|
return this.descriptionElements.size();
|
257
|
}
|
258
|
|
259
|
public boolean isImageGallery() {
|
260
|
return imageGallery;
|
261
|
}
|
262
|
public void setImageGallery(boolean imageGallery) {
|
263
|
this.imageGallery = imageGallery;
|
264
|
}
|
265
|
|
266
|
public boolean isDefault() {
|
267
|
return isDefault;
|
268
|
}
|
269
|
public void setDefault(boolean isDefault) {
|
270
|
this.isDefault = isDefault;
|
271
|
}
|
272
|
|
273
|
public EnumSet<DescriptionType> getTypes() {
|
274
|
return types;
|
275
|
}
|
276
|
public void setTypes(EnumSet<DescriptionType> types) {
|
277
|
this.types = types;
|
278
|
}
|
279
|
public void addType(DescriptionType type) {
|
280
|
this.types.add(type);
|
281
|
}
|
282
|
public void addTypes(Set<DescriptionType> types) {
|
283
|
this.types.addAll(types);
|
284
|
}
|
285
|
|
286
|
|
287
|
public Set<DescriptiveDataSet> getDescriptiveDataSets() {
|
288
|
return descriptiveDataSets;
|
289
|
}
|
290
|
public boolean addDescriptiveDataSet(DescriptiveDataSet descriptiveDataSet){
|
291
|
boolean result = this.descriptiveDataSets.add(descriptiveDataSet);
|
292
|
if (! descriptiveDataSet.getDescriptions().contains(this)){
|
293
|
descriptiveDataSet.addDescription(this);
|
294
|
}
|
295
|
return result;
|
296
|
}
|
297
|
public boolean removeDescriptiveDataSet(DescriptiveDataSet descriptiveDataSet){
|
298
|
boolean result = this.descriptiveDataSets.remove(descriptiveDataSet);
|
299
|
if (descriptiveDataSet.getDescriptions().contains(this)){
|
300
|
descriptiveDataSet.removeDescription(this);
|
301
|
}
|
302
|
return result;
|
303
|
}
|
304
|
protected void setDescriptiveDataSet(Set<DescriptiveDataSet> descriptiveDataSets) {
|
305
|
this.descriptiveDataSets = descriptiveDataSets;
|
306
|
}
|
307
|
|
308
|
/**
|
309
|
* Returns the set of {@link Reference references} used as sources for <i>this</i> description as a
|
310
|
* whole. More than one source can be used for a general description without
|
311
|
* assigning for each data element of the description one of those sources.
|
312
|
*
|
313
|
* @see #addDescriptionSource(Reference)
|
314
|
* @see #removeDescriptionSource(Reference)
|
315
|
*/
|
316
|
@Deprecated //will probably be removed in future versions due to #2240
|
317
|
public Set<Reference> getDescriptionSources() {
|
318
|
return this.descriptionSources;
|
319
|
}
|
320
|
|
321
|
/**
|
322
|
* Adds an existing {@link Reference reference} to the set of
|
323
|
* {@link #getDescriptionSources() references} used as sources for <i>this</i>
|
324
|
* description.
|
325
|
*
|
326
|
* @param descriptionSource the reference source to be added to <i>this</i> description
|
327
|
* @see #getDescriptionSources()
|
328
|
*/
|
329
|
@Deprecated //will probably be removed in future versions due to #2240
|
330
|
public void addDescriptionSource(Reference descriptionSource) {
|
331
|
this.descriptionSources.add(descriptionSource);
|
332
|
}
|
333
|
|
334
|
/**
|
335
|
* Removes one element from the set of {@link #getDescriptionSources() references} used as
|
336
|
* sources for <i>this</i> description.
|
337
|
*
|
338
|
* @param descriptionSource the reference source which should be deleted
|
339
|
* @see #getDescriptionSources()
|
340
|
* @see #addDescriptionSource(Reference)
|
341
|
*/
|
342
|
@Deprecated //will probably be removed in future versions due to #2240
|
343
|
public void removeDescriptionSource(Reference descriptionSource) {
|
344
|
this.descriptionSources.remove(descriptionSource);
|
345
|
}
|
346
|
|
347
|
// *********************** METHODS ******************************/
|
348
|
|
349
|
@Transient
|
350
|
//TODO this is not correct
|
351
|
public boolean hasStructuredData(){
|
352
|
for (DescriptionElementBase element : this.getElements()){
|
353
|
if (element.isInstanceOf(QuantitativeData.class) ||
|
354
|
element.isInstanceOf(CategoricalData.class)){
|
355
|
return true;
|
356
|
}
|
357
|
}
|
358
|
return false;
|
359
|
}
|
360
|
|
361
|
/**
|
362
|
* if this is of type {@link DescriptionType#COMPUTED} computed.<BR><BR>
|
363
|
* Note: Computed is a base type. It has children like {@link DescriptionType#AGGREGATED}.
|
364
|
* Also for them this method returns <code>true</code>.
|
365
|
*/
|
366
|
public boolean isComputed() {
|
367
|
return DescriptionType.isComputed(types);
|
368
|
}
|
369
|
public boolean isAggregated() {
|
370
|
return DescriptionType.includesType(types, DescriptionType.AGGREGATED);
|
371
|
}
|
372
|
public boolean isAggregatedDistribution() {
|
373
|
return DescriptionType.includesType(types, DescriptionType.AGGREGATED_DISTRIBUTION);
|
374
|
}
|
375
|
public boolean isAggregatedStructuredDescription() {
|
376
|
return DescriptionType.includesType(types, DescriptionType.AGGREGATED_STRUC_DESC);
|
377
|
}
|
378
|
public boolean isCloneForSource() {
|
379
|
return DescriptionType.includesType(types, DescriptionType.CLONE_FOR_SOURCE);
|
380
|
}
|
381
|
public static boolean isDefaultForAggregation(EnumSet<DescriptionType> set) {
|
382
|
return DescriptionType.includesType(set, DescriptionType.DEFAULT_VALUES_FOR_AGGREGATION);
|
383
|
}
|
384
|
public static boolean isSecondaryData(EnumSet<DescriptionType> set) {
|
385
|
return DescriptionType.includesType(set, DescriptionType.SECONDARY_DATA);
|
386
|
}
|
387
|
|
388
|
public abstract IDescribable<?> describedEntity();
|
389
|
|
390
|
|
391
|
//*********************** CLONE ********************************************************/
|
392
|
|
393
|
/**
|
394
|
* Clones <i>this</i> description. This is a shortcut that enables to create
|
395
|
* a new instance that differs only slightly from <i>this</i> description by
|
396
|
* modifying only some of the attributes.<BR>
|
397
|
*
|
398
|
* Usages of this name in a taxon concept are NOT cloned.<BR>
|
399
|
* The name is added to the same homotypical group as the original name
|
400
|
* (CAUTION: this behavior needs to be discussed and may change in future).<BR>
|
401
|
* {@link TaxonNameDescription Name descriptions} are cloned as XXX.<BR>
|
402
|
* {@link TypeDesignationBase Type designations} are cloned as XXX.<BR>
|
403
|
* {@link NameRelationship Name relation} are cloned as XXX.<BR>
|
404
|
*
|
405
|
* @see java.lang.Object#clone()
|
406
|
*/
|
407
|
@Override
|
408
|
public DescriptionBase<S> clone() {
|
409
|
DescriptionBase<S> result;
|
410
|
try{
|
411
|
result = (DescriptionBase<S>)super.clone();
|
412
|
|
413
|
//descriptive dataset
|
414
|
//TODO do we really want to add the cloned description automatically to the dataset?
|
415
|
result.descriptiveDataSets = new HashSet<>();
|
416
|
for (DescriptiveDataSet descriptiveDataSet : getDescriptiveDataSets()){
|
417
|
descriptiveDataSet.addDescription(result);
|
418
|
}
|
419
|
|
420
|
//reference based descriptions
|
421
|
//TODO remove
|
422
|
result.descriptionSources = new HashSet<>();
|
423
|
for (Reference reference : getDescriptionSources()){
|
424
|
result.descriptionSources.add(reference);
|
425
|
}
|
426
|
|
427
|
//elements
|
428
|
result.descriptionElements = new HashSet<>();
|
429
|
for (DescriptionElementBase element : getElements()){
|
430
|
DescriptionElementBase newElement = element.clone();
|
431
|
result.addElement(newElement);
|
432
|
}
|
433
|
|
434
|
result.types = this.types.clone();
|
435
|
|
436
|
//no changes to: imageGallery
|
437
|
return result;
|
438
|
} catch (CloneNotSupportedException e) {
|
439
|
logger.warn("Object does not implement cloneable");
|
440
|
e.printStackTrace();
|
441
|
return null;
|
442
|
}
|
443
|
|
444
|
}
|
445
|
}
|