merge-update from trunk
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / molecular / DnaSample.java
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.molecular;
11
12
13 import java.util.HashSet;
14 import java.util.Set;
15
16 import javax.persistence.Entity;
17 import javax.persistence.FetchType;
18 import javax.persistence.ManyToOne;
19 import javax.persistence.OneToMany;
20 import javax.persistence.Transient;
21 import javax.validation.constraints.NotNull;
22 import javax.xml.bind.annotation.XmlAccessType;
23 import javax.xml.bind.annotation.XmlAccessorType;
24 import javax.xml.bind.annotation.XmlElement;
25 import javax.xml.bind.annotation.XmlElementWrapper;
26 import javax.xml.bind.annotation.XmlIDREF;
27 import javax.xml.bind.annotation.XmlRootElement;
28 import javax.xml.bind.annotation.XmlSchemaType;
29 import javax.xml.bind.annotation.XmlType;
30
31 import org.apache.log4j.Logger;
32 import org.hibernate.annotations.Cascade;
33 import org.hibernate.annotations.CascadeType;
34 import org.hibernate.envers.Audited;
35 import org.hibernate.search.annotations.ContainedIn;
36 import org.hibernate.search.annotations.Indexed;
37 import org.hibernate.search.annotations.IndexedEmbedded;
38
39 import eu.etaxonomy.cdm.model.occurrence.Collection;
40 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
41 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
42 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
43 import eu.etaxonomy.cdm.strategy.cache.common.IdentifiableEntityDefaultCacheStrategy;
44
45 /**
46 * A DNA Sample is the extracted DNA of a given tissue sample. It may be stored in
47 * a DNA Bank and should then be handled as a collection unit.
48 * DNA Sample are used to determine their {@link Sequence DNA sequences}
49 * starting with a process called {@link Amplification amplification}.
50 *
51 * @author m.doering
52 * @created 08-Nov-2007
53 */
54 @XmlAccessorType(XmlAccessType.FIELD)
55 @XmlType(name = "DnaSample", propOrder = {
56 "sequences",
57 "amplifications",
58 "dnaQuality"
59 })
60 @XmlRootElement(name = "DnaSample")
61 @Entity
62 @Indexed(index = "eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase")
63 @Audited
64 public class DnaSample extends DerivedUnit implements Cloneable {
65 private static final long serialVersionUID = -2978411330023671805L;
66 @SuppressWarnings("unused")
67 private static final Logger logger = Logger.getLogger(DnaSample.class);
68
69 // ****************** FACTORY METHOD *****************/
70
71 /**
72 * Factory method
73 * @return
74 */
75 public static DnaSample NewInstance(){
76 return new DnaSample();
77 }
78
79
80 // ************** ATTRIBUTES ****************************/
81
82 // @XmlElement(name = "BankNumber")
83 // private String bankNumber;
84
85 @XmlElementWrapper(name = "Sequences")
86 @XmlElement(name = "sequence")
87 @XmlIDREF
88 @XmlSchemaType(name = "IDREF")
89 @OneToMany(fetch = FetchType.LAZY)
90 @Cascade(CascadeType.SAVE_UPDATE)
91 private Set<Sequence> sequences = new HashSet<Sequence>();
92
93
94 @XmlElementWrapper(name = "Amplifications")
95 @XmlElement(name = "Amplification")
96 @OneToMany(mappedBy="dnaSample", fetch = FetchType.LAZY)
97 @Cascade( { CascadeType.SAVE_UPDATE, CascadeType.DELETE})
98 @ContainedIn
99 @NotNull
100 private Set<Amplification> amplifications = new HashSet<Amplification>();
101
102 @XmlElement(name = "DnaQuality", required = true)
103 @XmlIDREF
104 @XmlSchemaType(name = "IDREF")
105 @ManyToOne(fetch = FetchType.LAZY)
106 @IndexedEmbedded
107 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
108 private DnaQuality dnaQuality;
109
110
111 // ******************* CONSTRUCTOR *************************/
112
113 /**
114 * Constructor
115 */
116 protected DnaSample() { //protected for Javassist, otherwise private
117 super(SpecimenOrObservationType.DnaSample);
118 this.cacheStrategy = new IdentifiableEntityDefaultCacheStrategy<DerivedUnit>();
119 }
120
121 //************ GETTER / SETTER **********************************/
122
123 //sequencings
124 public Set<Sequence> getSequences() {
125 return sequences;
126 }
127
128 public void addSequence(Sequence sequence) {
129 if (sequence.getDnaSample() != null){
130 sequence.getDnaSample().removeSequence(sequence);
131 }
132 this.sequences.add(sequence);
133 sequence.setDnaSample(this);
134 }
135
136 public void removeSequence(Sequence sequence) {
137 sequence.setDnaSample(null);
138 this.sequences.remove(sequence);
139 }
140
141
142
143 //amplifications
144 public Set<Amplification> getAmplifications() {
145 return amplifications;
146 }
147
148 public void addAmplification(Amplification amplification) {
149 this.amplifications.add(amplification);
150 amplification.setDnaSample(this);
151 }
152
153 public void removeAmplification(Amplification amplification) {
154 this.amplifications.remove(amplification);
155 }
156
157 public DnaQuality getDnaQuality() {
158 return dnaQuality;
159 }
160
161 public void setDnaQuality(DnaQuality dnaQuality) {
162 this.dnaQuality = dnaQuality;
163 }
164
165
166 // ************* Convenience Getter / Setter ************/
167
168
169 @Transient
170 public Collection getStoredAt(){
171 return this.getCollection();
172 }
173
174 public void setStoredAt(Collection storedAt){
175 this.setCollection(storedAt);
176 }
177
178 @Transient
179 public Set<SpecimenOrObservationBase> getExtractedFrom(){
180 return getOriginals();
181 }
182
183 @Transient
184 public String getBankNumber(){
185 return this.getCatalogNumber();
186 }
187
188 public void setBankNumber(String bankNumber){
189 this.setCatalogNumber(bankNumber);
190 }
191
192
193 //*********** CLONE **********************************/
194
195 /**
196 * Clones <i>this</i> dna sample. This is a shortcut that enables to
197 * create a new instance that differs only slightly from <i>this</i> dna sample
198 * by modifying only some of the attributes.<BR>
199 * This method overrides the clone method from {@link Specimen Specimen}.
200 * @throws CloneNotSupportedException
201 *
202 * @see Specimen#clone()
203 * @see DerivedUnit#clone()
204 * @see eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity#clone()
205 * @see java.lang.Object#clone()
206 */
207 @Override
208 public DnaSample clone() {
209 DnaSample result = (DnaSample)super.clone();
210 //sequenceSet
211 result.sequences = new HashSet<Sequence>();
212 for(Sequence sequence : this.sequences) {
213 result.addSequence((Sequence)sequence.clone());
214 }
215 //no changes to: bankNumber
216 return result;
217 }
218 }