cdmlib 2.5
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / occurrence / DerivationEvent.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.HashSet;
13 import java.util.Set;
14
15 import javax.persistence.Entity;
16 import javax.persistence.FetchType;
17 import javax.persistence.ManyToMany;
18 import javax.persistence.ManyToOne;
19 import javax.persistence.OneToMany;
20 import javax.xml.bind.annotation.XmlAccessType;
21 import javax.xml.bind.annotation.XmlAccessorType;
22 import javax.xml.bind.annotation.XmlElement;
23 import javax.xml.bind.annotation.XmlElementWrapper;
24 import javax.xml.bind.annotation.XmlIDREF;
25 import javax.xml.bind.annotation.XmlRootElement;
26 import javax.xml.bind.annotation.XmlSchemaType;
27 import javax.xml.bind.annotation.XmlType;
28
29 import org.apache.log4j.Logger;
30 import org.hibernate.annotations.Cascade;
31 import org.hibernate.annotations.CascadeType;
32 import org.hibernate.envers.Audited;
33 import org.hibernate.search.annotations.Indexed;
34 import org.hibernate.search.annotations.IndexedEmbedded;
35
36 import eu.etaxonomy.cdm.model.common.EventBase;
37
38 /**
39 * @author a.mueller
40 * @date 17.05.2010
41 *
42 */
43 @XmlAccessorType(XmlAccessType.FIELD)
44 @XmlType(name = "DerivationEvent", propOrder = {
45 "originals",
46 "derivatives",
47 "type"
48 })
49 @XmlRootElement(name = "DerivationEvent")
50 @Entity
51 @Indexed
52 @Audited
53 public class DerivationEvent extends EventBase implements Cloneable{
54
55 static Logger logger = Logger.getLogger(DerivationEvent.class);
56
57 @XmlElementWrapper(name = "Originals")
58 @XmlElement(name = "Original")
59 @XmlIDREF
60 @XmlSchemaType(name = "IDREF")
61 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
62 @ManyToMany(fetch = FetchType.LAZY,mappedBy="derivationEvents")
63 @IndexedEmbedded(depth = 3)
64 protected Set<SpecimenOrObservationBase> originals = new HashSet<SpecimenOrObservationBase>();
65
66 @XmlElementWrapper(name = "Derivatives")
67 @XmlElement(name = "Derivative")
68 @XmlIDREF
69 @XmlSchemaType(name = "IDREF")
70 @OneToMany(fetch=FetchType.LAZY, mappedBy="derivationEvent")
71 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
72 protected Set<DerivedUnitBase> derivatives = new HashSet<DerivedUnitBase>();
73
74 @XmlElement(name = "DerivationEventType")
75 @XmlIDREF
76 @XmlSchemaType(name = "IDREF")
77 @ManyToOne(fetch = FetchType.LAZY)
78 private DerivationEventType type;
79
80 /**
81 * Factory method
82 * @return
83 */
84 public static DerivationEvent NewInstance(){
85 return new DerivationEvent();
86 }
87
88 /**
89 * Constructor
90 */
91 protected DerivationEvent() {
92 super();
93 }
94
95
96 /**
97 * The specimen or observations that are the input for this derviation event.
98 * @return
99 */
100 public Set<SpecimenOrObservationBase> getOriginals() {
101 return originals;
102 }
103
104
105 /**
106 * Adds a new input specimen or observation for this derviation event.
107 * @see #getOriginals()
108 * @return
109 */
110 public void addOriginal(SpecimenOrObservationBase original) {
111 if (! this.originals.contains(original)){
112 this.originals.add(original);
113 original.addDerivationEvent(this);
114 }
115 }
116 /**
117 * Removes an input specimen or observation for this derviation event.
118 * @see #getOriginals()
119 * @return
120 */
121 public void removeOriginal(SpecimenOrObservationBase original) {
122 this.originals.remove(original);
123 }
124
125
126 /**
127 * The specimen or observations that are the output for this derviation event.
128 * @return
129 */
130 public Set<DerivedUnitBase> getDerivatives() {
131 return derivatives;
132 }
133
134
135 /**
136 * Adds a new output specimen or observation for this derviation event.
137 * @see #getDerivatives()
138 * @return
139 */
140 public void addDerivative(DerivedUnitBase derivative) {
141 if (derivative != null){
142 boolean notExisting = derivatives.add(derivative);
143 if (notExisting){
144 derivative.setDerivedFrom(this);
145 }
146 }
147 }
148 /**
149 * Removes an output specimen or observation for this derviation event.
150 * @see #getDerivatives()
151 * @return
152 */
153 public void removeDerivative(DerivedUnitBase derivative) {
154 if (derivative != null){
155 derivative.setDerivedFrom(null);
156 }
157 derivatives.remove(derivative);
158 }
159
160 /**
161 * Returns the derivation event type
162 * @return
163 */
164 public DerivationEventType getType() {
165 return type;
166 }
167
168 public void setType(DerivationEventType type) {
169 this.type = type;
170 }
171
172 //*********** CLONE **********************************/
173
174 /**
175 * Clones <i>this</i> derivation event. This is a shortcut that enables to
176 * create a new instance that differs only slightly from <i>this</i> derivation event
177 * by modifying only some of the attributes.<BR>
178 * This method overrides the clone method from {@link EventBase EventBase}.
179 *
180 * @see EventBase#clone()
181 * @see java.lang.Object#clone()
182 */
183 @Override
184 public Object clone() throws CloneNotSupportedException {
185 try{
186 DerivationEvent result = (DerivationEvent)super.clone();
187 //type
188 result.setType(this.getType());
189 //derivates
190 result.derivatives = new HashSet<DerivedUnitBase>();
191 for(DerivedUnitBase derivative : this.derivatives) {
192 result.addDerivative(derivative);
193 }
194 //originals
195 result.originals = new HashSet<SpecimenOrObservationBase>();
196 for(SpecimenOrObservationBase original : this.originals) {
197 result.addOriginal(original);
198 }
199 //no changes to: -
200 return result;
201 } catch (CloneNotSupportedException e) {
202 logger.warn("Object does not implement cloneable");
203 e.printStackTrace();
204 return null;
205 }
206 }
207 }