root/trunk/cdmlib/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/occurrence/DerivationEvent.java

Revision 10919, 5.4 kB (checked in by a.mueller, 18 months ago)

merge 3.0.2 to trunk

  • Property svn:keywords set to Id
Line 
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
10package eu.etaxonomy.cdm.model.occurrence;
11
12import java.util.HashSet;
13import java.util.Set;
14
15import javax.persistence.Entity;
16import javax.persistence.FetchType;
17import javax.persistence.ManyToMany;
18import javax.persistence.ManyToOne;
19import javax.persistence.OneToMany;
20import javax.xml.bind.annotation.XmlAccessType;
21import javax.xml.bind.annotation.XmlAccessorType;
22import javax.xml.bind.annotation.XmlElement;
23import javax.xml.bind.annotation.XmlElementWrapper;
24import javax.xml.bind.annotation.XmlIDREF;
25import javax.xml.bind.annotation.XmlRootElement;
26import javax.xml.bind.annotation.XmlSchemaType;
27import javax.xml.bind.annotation.XmlType;
28
29import org.apache.log4j.Logger;
30import org.hibernate.annotations.Cascade;
31import org.hibernate.annotations.CascadeType;
32import org.hibernate.envers.Audited;
33import org.hibernate.search.annotations.Indexed;
34import org.hibernate.search.annotations.IndexedEmbedded;
35
36import 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
53public 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        @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
62        @ManyToMany(fetch = FetchType.LAZY,mappedBy="derivationEvents")
63        @IndexedEmbedded(depth = 3)
64        protected Set<SpecimenOrObservationBase> originals = new HashSet<SpecimenOrObservationBase>();
65       
66        @XmlElementWrapper(name = "Derivatives")
67        @XmlElement(name = "Derivative")
68        @XmlIDREF
69        @XmlSchemaType(name = "IDREF")
70        @OneToMany(fetch=FetchType.LAZY, mappedBy="derivedFrom")
71        @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
72        protected Set<DerivedUnitBase> derivatives = new HashSet<DerivedUnitBase>();
73       
74        @XmlElement(name = "DerivationEventType")
75    @XmlIDREF
76    @XmlSchemaType(name = "IDREF")
77    @ManyToOne(fetch = FetchType.LAZY)
78        private DerivationEventType type;
79       
80        /**
81         * Factory method
82         * @return
83         */
84        public static DerivationEvent NewInstance(){
85                return new DerivationEvent();
86        }
87       
88        /**
89         * Constructor
90         */
91        protected DerivationEvent() {
92                super();
93        }
94       
95
96        /**
97         * The specimen or observations that are the input for this derviation event.
98         * @return
99         */
100        public Set<SpecimenOrObservationBase> getOriginals() {
101                return originals;
102        }
103
104       
105        /**
106         * Adds a new input specimen or observation for this derviation event.
107         * @see #getOriginals()
108         * @return
109         */
110        public void addOriginal(SpecimenOrObservationBase original) {
111                if (! this.originals.contains(original)){
112                        this.originals.add(original);
113                        original.addDerivationEvent(this);
114                }
115        }
116        /**
117         * Removes an input specimen or observation for this derviation event.
118         * @see #getOriginals()
119         * @return
120         */
121        public void removeOriginal(SpecimenOrObservationBase original) {
122                this.originals.remove(original);
123        }
124       
125       
126        /**
127         * The specimen or observations that are the output for this derviation event.
128         * @return
129         */
130        public Set<DerivedUnitBase> getDerivatives() {
131                return derivatives;
132        }
133       
134       
135        /**
136         * Adds a new output specimen or observation for this derviation event.
137         * @see #getDerivatives()
138         * @return
139         */
140        public void addDerivative(DerivedUnitBase derivative) {
141                if (derivative != null){
142                        boolean notExisting = derivatives.add(derivative);
143                        if (notExisting){
144                                derivative.setDerivedFrom(this);
145                        }
146                }
147        }
148        /**
149         * Removes an output specimen or observation for this derviation event.
150         * @see #getDerivatives()
151         * @return
152         */
153        public void removeDerivative(DerivedUnitBase derivative) {
154                if (derivative != null){
155                        derivative.setDerivedFrom(null);
156                }
157                derivatives.remove(derivative);
158        }
159
160        /**
161         * Returns the derivation event type
162         * @return
163         */
164        public DerivationEventType getType() {
165                return type;
166        }
167       
168        public void setType(DerivationEventType type) {
169                this.type = type;
170        }
171       
172//*********** CLONE **********************************/
173       
174        /**
175         * Clones <i>this</i> derivation event. This is a shortcut that enables to
176         * create a new instance that differs only slightly from <i>this</i> derivation event
177         * by modifying only some of the attributes.<BR>
178         * This method overrides the clone method from {@link EventBase EventBase}.
179         *
180         * @see EventBase#clone()
181         * @see java.lang.Object#clone()
182         */
183        @Override
184        public Object clone() throws CloneNotSupportedException {
185                try{
186                        DerivationEvent result = (DerivationEvent)super.clone();
187                        //type
188                        result.setType(this.getType());
189                        //derivates
190                        result.derivatives = new HashSet<DerivedUnitBase>();
191                        for(DerivedUnitBase derivative : this.derivatives) {
192                                result.addDerivative(derivative);
193                        }
194                        //originals
195                        result.originals = new HashSet<SpecimenOrObservationBase>();
196                        for(SpecimenOrObservationBase original : this.originals) {
197                                result.addOriginal(original);
198                        }
199                        //no changes to: -
200                        return result;
201                } catch (CloneNotSupportedException e) {
202                        logger.warn("Object does not implement cloneable");
203                        e.printStackTrace();
204                        return null;
205                }
206        }
207}
Note: See TracBrowser for help on using the browser.