Project

General

Profile

Download (5.33 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.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
	@ManyToMany(fetch = FetchType.LAZY,mappedBy="derivationEvents")
62
	@IndexedEmbedded(depth = 3)
63
	protected Set<SpecimenOrObservationBase> originals = new HashSet<SpecimenOrObservationBase>();
64
	
65
	@XmlElementWrapper(name = "Derivatives")
66
	@XmlElement(name = "Derivative")
67
	@XmlIDREF
68
	@XmlSchemaType(name = "IDREF")
69
	@OneToMany(fetch=FetchType.LAZY,mappedBy="derivationEvent")
70
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
71
	protected Set<DerivedUnitBase> derivatives = new HashSet<DerivedUnitBase>();
72
	
73
	@XmlElement(name = "DerivationEventType")
74
    @XmlIDREF
75
    @XmlSchemaType(name = "IDREF")
76
    @ManyToOne(fetch = FetchType.LAZY)
77
	private DerivationEventType type;
78
	
79
	/**
80
	 * Factory method
81
	 * @return
82
	 */
83
	public static DerivationEvent NewInstance(){
84
		return new DerivationEvent();
85
	}
86
	
87
	/**
88
	 * Constructor
89
	 */
90
	protected DerivationEvent() {
91
		super();
92
	}
93
	
94

    
95
	/**
96
	 * The specimen or observations that are the input for this derviation event.
97
	 * @return
98
	 */
99
	public Set<SpecimenOrObservationBase> getOriginals() {
100
		return originals;
101
	}
102

    
103
	
104
	/**
105
	 * Adds a new input specimen or observation for this derviation event.
106
	 * @see #getOriginals()
107
	 * @return
108
	 */
109
	public void addOriginal(SpecimenOrObservationBase original) {
110
		if (! this.originals.contains(original)){
111
			this.originals.add(original);
112
			original.addDerivationEvent(this);
113
		}
114
	}
115
	/**
116
	 * Removes an input specimen or observation for this derviation event.
117
	 * @see #getOriginals()
118
	 * @return
119
	 */
120
	public void removeOriginal(SpecimenOrObservationBase original) {
121
		this.originals.remove(original);
122
	}
123
	
124
	
125
	/**
126
	 * The specimen or observations that are the output for this derviation event.
127
	 * @return
128
	 */
129
	public Set<DerivedUnitBase> getDerivatives() {
130
		return derivatives;
131
	}
132
	
133
	
134
	/**
135
	 * Adds a new output specimen or observation for this derviation event.
136
	 * @see #getDerivatives()
137
	 * @return
138
	 */
139
	public void addDerivative(DerivedUnitBase derivative) {
140
		if (derivative != null){
141
			boolean notExisting = derivatives.add(derivative);
142
			if (notExisting){
143
				derivative.setDerivedFrom(this);
144
			}
145
		}
146
	}
147
	/**
148
	 * Removes an output specimen or observation for this derviation event.
149
	 * @see #getDerivatives()
150
	 * @return
151
	 */
152
	public void removeDerivative(DerivedUnitBase derivative) {
153
		if (derivative != null){
154
			derivative.setDerivedFrom(null);
155
		}
156
		derivatives.remove(derivative);
157
	}
158

    
159
	/**
160
	 * Returns the derivation event type
161
	 * @return
162
	 */
163
	public DerivationEventType getType() {
164
		return type;
165
	}
166
	
167
	public void setType(DerivationEventType type) {
168
		this.type = type;
169
	}
170
	
171
//*********** CLONE **********************************/	
172
	
173
	/** 
174
	 * Clones <i>this</i> derivation event. This is a shortcut that enables to
175
	 * create a new instance that differs only slightly from <i>this</i> derivation event
176
	 * by modifying only some of the attributes.<BR>
177
	 * This method overrides the clone method from {@link EventBase EventBase}.
178
	 * 
179
	 * @see EventBase#clone()
180
	 * @see java.lang.Object#clone()
181
	 */
182
	@Override
183
	public Object clone() throws CloneNotSupportedException {
184
		try{
185
			DerivationEvent result = (DerivationEvent)super.clone();
186
			//type
187
			result.setType(this.getType());
188
			//derivates
189
			result.derivatives = new HashSet<DerivedUnitBase>();
190
			for(DerivedUnitBase derivative : this.derivatives) {
191
				result.addDerivative(derivative);
192
			}
193
			//originals
194
			result.originals = new HashSet<SpecimenOrObservationBase>();
195
			for(SpecimenOrObservationBase original : this.originals) {
196
				result.addOriginal(original);
197
			}
198
			//no changes to: -
199
			return result;
200
		} catch (CloneNotSupportedException e) {
201
			logger.warn("Object does not implement cloneable");
202
			e.printStackTrace();
203
			return null;
204
		}
205
	}
206
}
(2-2/17)