Project

General

Profile

Download (6.79 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

    
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.JoinColumn;
18
import javax.persistence.ManyToMany;
19
import javax.persistence.ManyToOne;
20
import javax.persistence.OneToMany;
21
import javax.xml.bind.annotation.XmlAccessType;
22
import javax.xml.bind.annotation.XmlAccessorType;
23
import javax.xml.bind.annotation.XmlElement;
24
import javax.xml.bind.annotation.XmlElementWrapper;
25
import javax.xml.bind.annotation.XmlIDREF;
26
import javax.xml.bind.annotation.XmlRootElement;
27
import javax.xml.bind.annotation.XmlSchemaType;
28
import javax.xml.bind.annotation.XmlType;
29

    
30
import org.apache.log4j.Logger;
31
import org.hibernate.annotations.Cascade;
32
import org.hibernate.annotations.CascadeType;
33
import org.hibernate.envers.Audited;
34
import org.hibernate.search.annotations.IndexedEmbedded;
35

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

    
39
/**
40
 * @author a.mueller
41
 * @date 17.05.2010
42
 *
43
 */
44
@XmlAccessorType(XmlAccessType.FIELD)
45
@XmlType(name = "DerivationEvent", propOrder = {
46
    "originals",
47
    "derivatives",
48
    "institution",
49
    "type"
50
})
51
@XmlRootElement(name = "DerivationEvent")
52
@Entity
53
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
54
//@Indexed
55
@Audited
56
public class DerivationEvent extends EventBase implements Cloneable{
57
	private static final long serialVersionUID = 3661673673962819395L;
58
	private static final Logger logger = Logger.getLogger(DerivationEvent.class);
59

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

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

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

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

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

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

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

    
124
	/**
125
	 * Constructor
126
	 */
127
	protected DerivationEvent() {
128
		super();
129
	}
130

    
131

    
132
	/**
133
	 * The specimen or observations that are the input for this derviation event.
134
	 * @return
135
	 */
136
	public Set<SpecimenOrObservationBase> getOriginals() {
137
		return originals;
138
	}
139

    
140

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

    
164

    
165
	/**
166
	 * The specimen or observations that are the output for this derviation event.
167
	 * @return
168
	 */
169
	public Set<DerivedUnit> getDerivatives() {
170
		return derivatives;
171
	}
172

    
173

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

    
199
    /**
200
     * #4498
201
     * @return
202
     */
203
    public Institution getInstitution() {
204
		return institution;
205
	}
206

    
207
	public void setInstitution(Institution institution) {
208
		this.institution = institution;
209
	}
210

    
211
	/**
212
	 * Returns the derivation event type
213
	 * @return
214
	 */
215
	public DerivationEventType getType() {
216
		return type;
217
	}
218

    
219
	public void setType(DerivationEventType type) {
220
		this.type = type;
221
	}
222

    
223
//*********** CLONE **********************************/
224

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