Opportunity to omit term loading during DB initialization.
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / occurrence / DerivedUnitBase.java
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.occurrence;
11
12 import java.util.Set;
13
14 import javax.persistence.Entity;
15 import javax.persistence.ManyToOne;
16 import javax.persistence.Transient;
17 import javax.xml.bind.annotation.XmlAccessType;
18 import javax.xml.bind.annotation.XmlAccessorType;
19 import javax.xml.bind.annotation.XmlElement;
20 import javax.xml.bind.annotation.XmlIDREF;
21 import javax.xml.bind.annotation.XmlRootElement;
22 import javax.xml.bind.annotation.XmlSchemaType;
23 import javax.xml.bind.annotation.XmlType;
24
25 import org.hibernate.annotations.Cascade;
26 import org.hibernate.annotations.CascadeType;
27
28 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
29
30 /**
31 * http://www.bgbm.org/biodivinf/docs/CollectionModel/ReprintTNR.pdf
32 * http://www.bgbm.org/biodivinf/docs/CollectionModel/
33 * <BR>
34 * Type figures are derived units with at least a figure object in media
35 *
36 * @author markus
37 *
38 */
39 @XmlAccessorType(XmlAccessType.FIELD)
40 @XmlType(name = "DerivedUnitBase", propOrder = {
41 "collection",
42 "catalogNumber",
43 "storedUnder",
44 "derivedFrom"
45 })
46 @XmlRootElement(name = "DerivedUnitBase")
47 @Entity
48 public abstract class DerivedUnitBase extends SpecimenOrObservationBase {
49
50 @XmlElement(name = "Collection")
51 @XmlIDREF
52 @XmlSchemaType(name = "IDREF")
53 private Collection collection;
54
55 @XmlElement(name = "CatalogNumber")
56 private String catalogNumber;
57
58 @XmlElement(name = "StoredUnder")
59 @XmlIDREF
60 @XmlSchemaType(name = "IDREF")
61 private TaxonNameBase storedUnder;
62
63 @XmlElement(name = "DerivedFrom")
64 @XmlIDREF
65 @XmlSchemaType(name = "IDREF")
66 private DerivationEvent derivedFrom;
67
68 /**
69 * Constructor
70 */
71 protected DerivedUnitBase() {
72 super();
73 }
74 /**
75 * create new unit derived from an existing field observation
76 * @param fieldObservation existing field observation from where this unit is derived
77 */
78 protected DerivedUnitBase(FieldObservation fieldObservation) {
79 super();
80 DerivationEvent derivedFrom = new DerivationEvent();
81 // TODO: should be done in a more controlled way. Probably by making derivation event implement a general relationship interface (for bidirectional add/remove etc)
82 fieldObservation.addDerivationEvent(derivedFrom);
83 derivedFrom.getOriginals().add(fieldObservation);
84 derivedFrom.getDerivatives().add(this);
85 this.setDerivedFrom(derivedFrom);
86 }
87 /**
88 * create new unit derived from an existing gathering event,
89 * thereby creating a new empty field observation
90 * @param gatheringEvent the gathering event this unit was collected at
91 */
92 protected DerivedUnitBase(GatheringEvent gatheringEvent) {
93 this(new FieldObservation());
94 FieldObservation field = (FieldObservation) this.getOriginalUnit();
95 field.setGatheringEvent(gatheringEvent);
96 }
97
98
99
100 @ManyToOne
101 @Deprecated //only for bidirectional and persistence use
102 private DerivationEvent getDerivationEvent() {
103 return getDerivedFrom();
104 }
105 @Deprecated //only for bidirectional and persistence use
106 private void setDerivationEvent(DerivationEvent derivationEvent) {
107 this.derivedFrom = derivationEvent;
108 }
109 @Transient
110 public DerivationEvent getDerivedFrom() {
111 return derivedFrom;
112 }
113 public void setDerivedFrom(DerivationEvent derivedFrom){
114 if (getDerivedFrom() != null){
115 getDerivedFrom().getDerivatives().remove(derivedFrom);
116 }
117 this.derivedFrom = derivedFrom;
118 if (derivedFrom != null){
119 derivedFrom.getDerivatives().add(this);
120 }
121 }
122
123 @Transient
124 public Set<SpecimenOrObservationBase> getOriginals(){
125 return this.getDerivedFrom().getOriginals();
126 }
127
128
129 @Override
130 @Transient
131 public GatheringEvent getGatheringEvent() {
132 // FIXME: implement efficient way of getting original gathering event
133 // keep link to original gathering event for performance mainly.
134 return null;
135 }
136
137
138 @ManyToOne
139 @Cascade({CascadeType.SAVE_UPDATE})
140 public Collection getCollection(){
141 return this.collection;
142 }
143 public void setCollection(Collection collection){
144 this.collection = collection;
145 }
146
147
148 public String getCatalogNumber() {
149 return catalogNumber;
150 }
151
152 public void setCatalogNumber(String catalogNumber) {
153 this.catalogNumber = catalogNumber;
154 }
155
156 @ManyToOne
157 @Cascade({CascadeType.SAVE_UPDATE})
158 public TaxonNameBase getStoredUnder() {
159 return storedUnder;
160 }
161 public void setStoredUnder(TaxonNameBase storedUnder) {
162 this.storedUnder = storedUnder;
163 }
164
165 }