757f11d8d0a95a2b41c5d06f4cad90c0582c576a
[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.OneToMany;
19 import javax.persistence.Transient;
20 import javax.validation.constraints.NotNull;
21 import javax.xml.bind.annotation.XmlAccessType;
22 import javax.xml.bind.annotation.XmlAccessorType;
23 import javax.xml.bind.annotation.XmlElement;
24 import javax.xml.bind.annotation.XmlElementWrapper;
25 import javax.xml.bind.annotation.XmlIDREF;
26 import javax.xml.bind.annotation.XmlRootElement;
27 import javax.xml.bind.annotation.XmlSchemaType;
28 import javax.xml.bind.annotation.XmlType;
29
30 import org.apache.log4j.Logger;
31 import org.hibernate.annotations.Cascade;
32 import org.hibernate.annotations.CascadeType;
33 import org.hibernate.envers.Audited;
34 import org.hibernate.search.annotations.ContainedIn;
35 import org.hibernate.search.annotations.Indexed;
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 * @created 08-Nov-2007
51 */
52 @XmlAccessorType(XmlAccessType.FIELD)
53 @XmlType(name = "DnaSample", propOrder = {
54 "sequences",
55 "amplifications"
56 })
57 @XmlRootElement(name = "DnaSample")
58 @Entity
59 @Indexed(index = "eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase")
60 @Audited
61 public class DnaSample extends DerivedUnit implements Cloneable {
62 private static final long serialVersionUID = -2978411330023671805L;
63 @SuppressWarnings("unused")
64 private static final Logger logger = Logger.getLogger(DnaSample.class);
65
66 // ****************** FACTORY METHOD *****************/
67
68 /**
69 * Factory method
70 * @return
71 */
72 public static DnaSample NewInstance(){
73 return new DnaSample();
74 }
75
76 // ************** ATTRIBUTES ****************************/
77
78 // @XmlElement(name = "BankNumber")
79 // private String bankNumber;
80
81 @XmlElementWrapper(name = "Sequences")
82 @XmlElement(name = "sequence")
83 @XmlIDREF
84 @XmlSchemaType(name = "IDREF")
85 @OneToMany(fetch = FetchType.LAZY)
86 @Cascade(CascadeType.SAVE_UPDATE)
87 private Set<Sequence> sequences = new HashSet<Sequence>();
88
89
90 @XmlElementWrapper(name = "Amplifications")
91 @XmlElement(name = "Amplification")
92 @OneToMany(mappedBy="dnaSample", fetch = FetchType.LAZY)
93 @Cascade( { CascadeType.SAVE_UPDATE, CascadeType.DELETE})
94 @ContainedIn
95 @NotNull
96 private Set<Amplification> amplifications = new HashSet<Amplification>();
97
98
99 // ******************* CONSTRUCTOR *************************/
100
101 /**
102 * Constructor
103 */
104 private DnaSample() {
105 super(SpecimenOrObservationType.DnaSample);
106 this.cacheStrategy = new IdentifiableEntityDefaultCacheStrategy<DerivedUnit>();
107 }
108
109 //************ GETTER / SETTER **********************************/
110
111 //sequencings
112 public Set<Sequence> getSequences() {
113 return sequences;
114 }
115
116 public void addSequence(Sequence sequence) {
117 if (sequence.getDnaSample() != null){
118 sequence.getDnaSample().removeSequence(sequence);
119 }
120 this.sequences.add(sequence);
121 sequence.setDnaSample(this);
122 }
123
124 public void removeSequence(Sequence sequence) {
125 sequence.setDnaSample(null);
126 this.sequences.remove(sequence);
127 }
128
129
130
131 //amplifications
132 public Set<Amplification> getAmplifications() {
133 return amplifications;
134 }
135
136 public void addAmplification(Amplification amplification) {
137 this.amplifications.add(amplification);
138 amplification.setDnaSample(this);
139 }
140
141 public void removeAmplification(Amplification amplification) {
142 this.amplifications.remove(amplification);
143 }
144
145 // ************* Convenience Getter / Setter ************/
146
147 @Transient
148 public Collection getStoredAt(){
149 return this.getCollection();
150 }
151
152 public void setStoredAt(Collection storedAt){
153 this.setCollection(storedAt);
154 }
155
156 @Transient
157 public Set<SpecimenOrObservationBase> getExtractedFrom(){
158 return getOriginals();
159 }
160
161 @Transient
162 public String getBankNumber(){
163 return this.getCatalogNumber();
164 }
165
166 public void setBankNumber(String bankNumber){
167 this.setCatalogNumber(bankNumber);
168 }
169
170
171 //*********** CLONE **********************************/
172
173 /**
174 * Clones <i>this</i> dna sample. This is a shortcut that enables to
175 * create a new instance that differs only slightly from <i>this</i> dna sample
176 * by modifying only some of the attributes.<BR>
177 * This method overrides the clone method from {@link Specimen Specimen}.
178 * @throws CloneNotSupportedException
179 *
180 * @see Specimen#clone()
181 * @see DerivedUnit#clone()
182 * @see eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity#clone()
183 * @see java.lang.Object#clone()
184 */
185 @Override
186 public DnaSample clone() {
187 DnaSample result = (DnaSample)super.clone();
188 //sequenceSet
189 result.sequences = new HashSet<Sequence>();
190 for(Sequence sequence : this.sequences) {
191 result.addSequence(sequence);
192 }
193 //no changes to: bankNumber
194 return result;
195 }
196 }