Project

General

Profile

Download (6.99 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
package eu.etaxonomy.cdm.model.occurrence;
10

    
11
import java.util.HashSet;
12
import java.util.Set;
13

    
14
import javax.persistence.Entity;
15
import javax.persistence.FetchType;
16
import javax.persistence.JoinColumn;
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.IndexedEmbedded;
34

    
35
import eu.etaxonomy.cdm.model.agent.Institution;
36
import eu.etaxonomy.cdm.model.common.EventBase;
37

    
38
/**
39
 * @author a.mueller
40
 * @since 17.05.2010
41
 */
42
@XmlAccessorType(XmlAccessType.FIELD)
43
@XmlType(name = "DerivationEvent", propOrder = {
44
    "originals",
45
    "derivatives",
46
    "institution",
47
    "type"
48
})
49
@XmlRootElement(name = "DerivationEvent")
50
@Entity
51
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
52
//@Indexed
53
@Audited
54
public class DerivationEvent extends EventBase {
55

    
56
	private static final long serialVersionUID = 3661673673962819395L;
57
	private static final Logger logger = Logger.getLogger(DerivationEvent.class);
58

    
59
	@XmlElementWrapper(name = "Originals")
60
	@XmlElement(name = "Original")
61
	@XmlIDREF
62
	@XmlSchemaType(name = "IDREF")
63
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
64
	@ManyToMany(fetch = FetchType.LAZY,mappedBy="derivationEvents")
65
	@IndexedEmbedded(depth = 3)
66
	protected Set<SpecimenOrObservationBase> originals = new HashSet<>();
67

    
68
	@XmlElementWrapper(name = "Derivatives")
69
	@XmlElement(name = "Derivative")
70
	@XmlIDREF
71
	@XmlSchemaType(name = "IDREF")
72
	@OneToMany(fetch=FetchType.LAZY, mappedBy="derivedFrom")
73
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
74
	protected Set<DerivedUnit> derivatives = new HashSet<>();
75

    
76
	@XmlElement(name = "Institution")
77
	@XmlIDREF
78
	@XmlSchemaType(name = "IDREF")
79
	@ManyToOne(fetch = FetchType.LAZY)
80
	@IndexedEmbedded
81
	@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
82
	@JoinColumn(name="institution_id")
83
	private Institution institution;
84

    
85
	@XmlElement(name = "DerivationEventType")
86
    @XmlIDREF
87
    @XmlSchemaType(name = "IDREF")
88
    @ManyToOne(fetch = FetchType.LAZY)
89
	private DerivationEventType type;
90

    
91
// ********************* FACTORY  ****************************/
92

    
93
	/**
94
	 * Factory method
95
	 * @deprecated Use {@link #NewInstance(DerivationEventType)} or any other
96
	 * factory method instead to make sure,
97
	 * the derivation event type is always set.
98
	 */
99
	@Deprecated
100
	public static DerivationEvent NewInstance(){
101
		return new DerivationEvent();
102
	}
103

    
104
	// ********************* FACTORY  ****************************/
105

    
106
	/**
107
	 * Factory method
108
	 * @return
109
	 */
110
	public static DerivationEvent NewInstance(DerivationEventType type){
111
		DerivationEvent result = new DerivationEvent();
112
		result.setType(type);
113
		return result;
114
	}
115

    
116
	/**
117
	 * Factory method
118
	 * @return
119
	 */
120
	public static DerivationEvent NewSimpleInstance(SpecimenOrObservationBase original,
121
	        DerivedUnit derivative, DerivationEventType type){
122
		DerivationEvent result = NewInstance(type);
123
		result.addOriginal(original);
124
		result.addDerivative(derivative);
125
		return result;
126
	}
127

    
128
// ************************* CONSTRUCTOR ****************************/
129

    
130
	/**
131
	 * Constructor
132
	 */
133
	protected DerivationEvent() {
134
		super();
135
	}
136

    
137
// ********************* GETTER / SETTER / ADDER **********************/
138

    
139
	/**
140
	 * The specimen or observations that are the input for this derviation event.
141
	 * @return
142
	 */
143
	public Set<SpecimenOrObservationBase> getOriginals() {
144
		return originals;
145
	}
146

    
147
	/**
148
	 * Adds a new input specimen or observation for this derviation event.
149
	 * @see #getOriginals()
150
	 * @return
151
	 */
152
	public void addOriginal(SpecimenOrObservationBase original) {
153
		if (! this.originals.contains(original)){
154
			this.originals.add(original);
155
			original.addDerivationEvent(this);
156
		}
157
	}
158
	/**
159
	 * Removes an input specimen or observation for this derviation event.
160
	 * @see #getOriginals()
161
	 * @return
162
	 */
163
	public void removeOriginal(SpecimenOrObservationBase original) {
164
	    if (this.originals.contains(original)){
165
	        this.originals.remove(original);
166
	        original.removeDerivationEvent(this);
167
	    }
168
	}
169

    
170
	/**
171
	 * The specimen or observations that are the output for this derviation event.
172
	 * @return
173
	 */
174
	public Set<DerivedUnit> getDerivatives() {
175
		return derivatives;
176
	}
177

    
178
	/**
179
	 * Adds a new output specimen or observation for this derivation event.
180
	 * @see #getDerivatives()
181
	 * @return
182
	 */
183
	public void addDerivative(DerivedUnit derivative) {
184
		if (derivative != null){
185
			boolean notExisting = derivatives.add(derivative);
186
			if (notExisting){
187
				derivative.setDerivedFrom(this);
188
			}
189
		}
190
	}
191
	/**
192
	 * Removes an output specimen or observation for this derviation event.
193
	 * @see #getDerivatives()
194
	 * @return
195
	 */
196
	public void removeDerivative(DerivedUnit derivative) {
197
		if (derivative != null){
198
			derivative.setDerivedFrom(null);
199
		}
200
		derivatives.remove(derivative);
201
	}
202

    
203
    /**
204
     * #4498
205
     * @return
206
     */
207
    public Institution getInstitution() {
208
		return institution;
209
	}
210
	public void setInstitution(Institution institution) {
211
		this.institution = institution;
212
	}
213

    
214
	/**
215
	 * Returns the derivation event type
216
	 * @return
217
	 */
218
	public DerivationEventType getType() {
219
		return type;
220
	}
221
	public void setType(DerivationEventType type) {
222
		this.type = type;
223
	}
224

    
225
//*********** CLONE **********************************/
226

    
227
	/**
228
	 * Clones <i>this</i> derivation event. This is a shortcut that enables to
229
	 * create a new instance that differs only slightly from <i>this</i> derivation event
230
	 * by modifying only some of the attributes.<BR>
231
	 * This method overrides the clone method from {@link EventBase EventBase}.
232
	 *
233
	 * @see EventBase#clone()
234
	 * @see java.lang.Object#clone()
235
	 */
236
	@Override
237
	public DerivationEvent clone() throws CloneNotSupportedException {
238
		try{
239
			DerivationEvent result = (DerivationEvent)super.clone();
240
			//type
241
			result.setType(this.getType());
242
			//derivates
243
			result.derivatives = new HashSet<DerivedUnit>();
244
			for(DerivedUnit derivative : this.derivatives) {
245
				result.addDerivative(derivative);
246
			}
247
			//originals
248
			result.originals = new HashSet<>();
249
			for(SpecimenOrObservationBase<?> original : this.originals) {
250
				result.addOriginal(original);
251
			}
252
			//no changes to: -
253
			return result;
254
		} catch (CloneNotSupportedException e) {
255
			logger.warn("Object does not implement cloneable");
256
			e.printStackTrace();
257
			return null;
258
		}
259
	}
260
}
(2-2/14)