Project

General

Profile

Download (7.03 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.logging.log4j.LogManager;import org.apache.logging.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 = LogManager.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
	 */
119
	public static DerivationEvent NewSimpleInstance(SpecimenOrObservationBase original,
120
	        DerivedUnit derivative, DerivationEventType type){
121
		DerivationEvent result = NewInstance(type);
122
		result.addOriginal(original);
123
		result.addDerivative(derivative);
124
		return result;
125
	}
126

    
127
// ************************* CONSTRUCTOR ****************************/
128

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

    
136
// ********************* GETTER / SETTER / ADDER **********************/
137

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

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

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

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

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

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

    
224
//*********** CLONE **********************************/
225

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