Project

General

Profile

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

    
28
import org.apache.log4j.Logger;
29
import org.hibernate.annotations.Cascade;
30
import org.hibernate.annotations.CascadeType;
31

    
32
import eu.etaxonomy.cdm.model.common.EventBase;
33

    
34
@XmlAccessorType(XmlAccessType.FIELD)
35
@XmlType(name = "DerivationEvent", propOrder = {
36
    "originals",
37
    "derivatives",
38
    "type"
39
})
40
@XmlRootElement(name = "DerivationEvent")
41
@Entity
42
public class DerivationEvent extends EventBase implements Cloneable{
43
	
44
	static Logger logger = Logger.getLogger(DerivationEvent.class);
45

    
46
	@XmlElementWrapper(name = "Originals")
47
	@XmlElement(name = "Original")
48
	@XmlIDREF
49
	@XmlSchemaType(name = "IDREF")
50
	private Set<SpecimenOrObservationBase> originals = getNewOriginalsSet();
51
	
52
	@XmlElementWrapper(name = "Derivatives")
53
	@XmlElement(name = "Derivative")
54
	@XmlIDREF
55
	@XmlSchemaType(name = "IDREF")
56
	protected Set<DerivedUnitBase> derivatives = getNewDerivatesSet();
57
	
58
	@XmlElement(name = "DerivationEventType")
59
    @XmlIDREF
60
    @XmlSchemaType(name = "IDREF")
61
	private DerivationEventType type;
62
	
63
	/**
64
	 * Factory method
65
	 * @return
66
	 */
67
	public static DerivationEvent NewInstance(){
68
		return new DerivationEvent();
69
	}
70
	
71
	/**
72
	 * Constructor
73
	 */
74
	protected DerivationEvent() {
75
		super();
76
	}
77
	
78
	@ManyToMany(mappedBy="derivationEvents")
79
	@Cascade({CascadeType.SAVE_UPDATE})
80
	public Set<SpecimenOrObservationBase> getOriginals() {
81
		return originals;
82
	}
83
	protected void setOriginals(Set<SpecimenOrObservationBase> originals) {
84
		this.originals = originals;
85
	}
86
	public void addOriginal(SpecimenOrObservationBase original) {
87
		if (! this.originals.contains(original)){
88
			this.originals.add(original);
89
			original.addDerivationEvent(this);
90
		}
91
	}
92
	public void removeOriginal(SpecimenOrObservationBase original) {
93
		this.originals.remove(original);
94
	}
95
	
96
	
97
	@OneToMany(mappedBy="derivationEvent")
98
	@Cascade({CascadeType.SAVE_UPDATE})
99
	public Set<DerivedUnitBase> getDerivatives() {
100
		return derivatives;
101
	}
102
	protected void setDerivatives(Set<DerivedUnitBase> derivatives) {
103
		this.derivatives = derivatives;
104
	}
105
	public void addDerivative(DerivedUnitBase derivative) {
106
		if (derivative != null){
107
			derivative.setDerivedFrom(this);
108
		}
109
	}
110
	public void removeDerivative(DerivedUnitBase derivative) {
111
		if (derivative != null){
112
			derivative.setDerivedFrom(null);
113
		}
114
	}
115

    
116
	
117
	@ManyToOne
118
	public DerivationEventType getType() {
119
		return type;
120
	}
121
	public void setType(DerivationEventType type) {
122
		this.type = type;
123
	}
124
	
125
	
126
//*********** CLONE **********************************/	
127
	
128
	/** 
129
	 * Clones <i>this</i> derivation event. This is a shortcut that enables to
130
	 * create a new instance that differs only slightly from <i>this</i> derivation event
131
	 * by modifying only some of the attributes.<BR>
132
	 * This method overrides the clone method from {@link EventBase EventBase}.
133
	 * 
134
	 * @see EventBase#clone()
135
	 * @see java.lang.Object#clone()
136
	 */
137
	@Override
138
	public DerivationEvent clone(){
139
		try{
140
			DerivationEvent result = (DerivationEvent)super.clone();
141
			//type
142
			result.setType(this.getType());
143
			//derivates
144
			Set<DerivedUnitBase> derivates = getNewDerivatesSet();
145
			derivates.addAll(this.derivatives);
146
			result.setDerivatives(derivates);
147
			//originals
148
			Set<SpecimenOrObservationBase> originals = getNewOriginalsSet();
149
			originals.addAll(this.originals);
150
			result.setOriginals(this.getOriginals());
151
			//no changes to: -
152
			return result;
153
		} catch (CloneNotSupportedException e) {
154
			logger.warn("Object does not implement cloneable");
155
			e.printStackTrace();
156
			return null;
157
		}
158
	}
159
	
160
	private static Set<DerivedUnitBase> getNewDerivatesSet(){
161
		return new HashSet<DerivedUnitBase>();
162
	}
163

    
164
	private static Set<SpecimenOrObservationBase> getNewOriginalsSet(){
165
		return new HashSet<SpecimenOrObservationBase>();
166
	}
167
	
168
}
(2-2/15)