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