ref #6724 SpecimenTypeDesignationWorkingsetEditor with Types sub section for containe...
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / model / registration / SpecimenTypeDesignationDTO.java
1 /**
2 * Copyright (C) 2017 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 package eu.etaxonomy.cdm.vaadin.model.registration;
10
11 import java.net.URI;
12 import java.util.UUID;
13
14 import org.apache.commons.collections.CollectionUtils;
15
16 import eu.etaxonomy.cdm.api.utility.DerivedUnitConversionException;
17 import eu.etaxonomy.cdm.api.utility.DerivedUnitConverter;
18 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
19 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
20 import eu.etaxonomy.cdm.model.media.Media;
21 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
22 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
23 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
24 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
25 import eu.etaxonomy.cdm.model.occurrence.Collection;
26 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
27 import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
28 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
29 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
30 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
31 import eu.etaxonomy.cdm.model.reference.Reference;
32
33 /**
34 * A DTO which is use in the context of the {@link SpecimenTypeDesignationWorkingSetDTO} which is backed up
35 * <code>SpecimenTypeDesignation.typeSpecimen.derivedFrom.type</code> object graph.
36 * <p>
37 * The contained {@link DerivedUnit} either is a {@link MediaSpecimen} with or a {@link DerivedUnit}, depending on the
38 * "kind of unit" which is defined by the associated <code>DerivationEvent.type</code>:
39 *
40 * <ul>
41 * <li>{@link DerivationEventType#GATHERING_IN_SITU()} -&gt; {@link DerivedUnit}</li>
42 * <li>{@link DerivationEventTypes#CULTURE_METABOLIC_INACTIVE()} -&gt; {@link DerivedUnit}</li>
43 * <li>{@link DerivationEventTypes#UNPUBLISHED_IMAGE()} -&gt; {@link MediaSpecimen}</li>
44 * <li>{@link DerivationEventTypes#PUBLISHED_IMAGE()} -&gt; {@link MediaSpecimen}</li>
45 * </ul>
46 *
47 * @author a.kohlbecker
48 * @since Jun 22, 2017
49 *
50 */
51 public class SpecimenTypeDesignationDTO {
52
53 SpecimenTypeDesignation std;
54
55 /**
56 * Creates an new new instance of SpecimenTypeDesignationDTO which is backed up
57 * by an newly instantiated <code>SpecimenTypeDesignation.typeSpecimen.derivedFrom.type</code> object graph.
58 */
59 public SpecimenTypeDesignationDTO(){
60 this(SpecimenTypeDesignation.NewInstance());
61 }
62
63 /**
64 * Creates a new instance of SpecimenTypeDesignationDTO.
65 *
66 * The constructor assures that <code>SpecimenTypeDesignation.typeSpecimen.derivedFrom.type</code> are never null.
67 * That is in case the supplied std parameter or if any of the above listed properties is null the missing instances
68 * are created and added to the object graph.
69 *
70 * @param std
71 */
72 public SpecimenTypeDesignationDTO(SpecimenTypeDesignation std) {
73 this.std = std;
74 if(std.getTypeSpecimen() == null){
75 DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
76 std.setTypeSpecimen(derivedUnit);
77 }
78 if(std.getTypeSpecimen().getDerivedFrom() == null){
79 DerivationEvent derivedFrom = DerivationEvent.NewInstance(DerivationEventType.GATHERING_IN_SITU());
80 std.getTypeSpecimen().setDerivedFrom(derivedFrom);
81 }
82 }
83
84 public DerivationEventType getDerivationEventType(){
85 return std.getTypeSpecimen().getDerivedFrom().getType();
86 }
87
88 public void setDerivationEventType(DerivationEventType derivationEventType) throws DerivedUnitConversionException{
89
90 std.getTypeSpecimen().getDerivedFrom().setType(derivationEventType);
91
92 Class<? extends DerivedUnit> requiredSpecimenType = specimenTypeFor(derivationEventType);
93
94 if(!requiredSpecimenType.equals(std.getTypeSpecimen())){
95
96 DerivedUnit convertedSpecimen;
97
98 if(requiredSpecimenType.equals(MediaSpecimen.class)){
99 DerivedUnitConverter<MediaSpecimen> converter = new DerivedUnitConverter<MediaSpecimen> (std.getTypeSpecimen());
100 convertedSpecimen = converter.convertTo((Class<MediaSpecimen>)requiredSpecimenType, specimenOrObservationTypeFor(derivationEventType));
101 } else {
102 DerivedUnitConverter<DerivedUnit> converter = new DerivedUnitConverter<DerivedUnit> (std.getTypeSpecimen());
103 convertedSpecimen = converter.convertTo((Class<DerivedUnit>)requiredSpecimenType, specimenOrObservationTypeFor(derivationEventType));
104 }
105
106 std.setTypeSpecimen(convertedSpecimen);
107 }
108 }
109
110
111 /**
112 * See constructor doc.
113 *
114 * @param derivationEventType
115 * @return
116 * either <code>DerivedUnit</code> or <code>MediaSpecimen</code> never null.
117 * <code>DerivedUnit</code> is the fall back return value.
118 */
119 private Class<? extends DerivedUnit> specimenTypeFor(DerivationEventType derivationEventType) {
120 UUID detUuid = derivationEventType.getUuid();
121
122 if(detUuid.equals(DerivationEventType.GATHERING_IN_SITU().getUuid())) {
123 return DerivedUnit.class;
124 }
125 if(detUuid.equals(DerivationEventTypes.CULTURE_METABOLIC_INACTIVE().getUuid())) {
126 return DerivedUnit.class;
127 }
128 if(detUuid.equals(DerivationEventTypes.PUBLISHED_IMAGE().getUuid())) {
129 return MediaSpecimen.class;
130 }
131 if(detUuid.equals(DerivationEventTypes.UNPUBLISHED_IMAGE().getUuid())) {
132 return MediaSpecimen.class;
133 }
134 return DerivedUnit.class;
135 }
136
137 private SpecimenOrObservationType specimenOrObservationTypeFor(DerivationEventType derivationEventType) {
138
139 UUID detUuid = derivationEventType.getUuid();
140
141 if(detUuid.equals(DerivationEventType.GATHERING_IN_SITU().getUuid())) {
142 return SpecimenOrObservationType.PreservedSpecimen;
143 }
144 if(detUuid.equals(DerivationEventTypes.CULTURE_METABOLIC_INACTIVE().getUuid())) {
145 return SpecimenOrObservationType.LivingSpecimen;
146 }
147 if(detUuid.equals(DerivationEventTypes.PUBLISHED_IMAGE().getUuid())) {
148 return SpecimenOrObservationType.StillImage;
149 }
150 if(detUuid.equals(DerivationEventTypes.UNPUBLISHED_IMAGE().getUuid())) {
151 return SpecimenOrObservationType.StillImage;
152 }
153 return SpecimenOrObservationType.PreservedSpecimen;
154
155 }
156
157 public SpecimenTypeDesignationStatus getTypeStatus(){
158 return HibernateProxyHelper.deproxy(std.getTypeStatus(), SpecimenTypeDesignationStatus.class);
159 }
160
161 public void setTypeStatus(SpecimenTypeDesignationStatus typeStatus){
162 std.setTypeStatus(typeStatus);
163 }
164
165 public Collection getCollection(){
166 return std.getTypeSpecimen().getCollection();
167 }
168
169 public void setCollection(Collection collection){
170 std.getTypeSpecimen().setCollection(collection);
171 }
172
173 public String getAccessionNumber(){
174 return std.getTypeSpecimen().getAccessionNumber();
175 }
176
177 public void setAccessionNumber(String accessionNumber){
178 std.getTypeSpecimen().setAccessionNumber(accessionNumber);
179 }
180
181 public URI getMediaUri(){
182 if(checkMediaSpecimen()){
183 MediaRepresentationPart part = findMediaRepresentationPart();
184 if(part != null){
185 return part.getUri();
186 }
187 }
188 return null;
189 }
190
191 public void setMediaUri(URI mediaUri){
192 if(checkMediaSpecimen()){
193 MediaRepresentationPart part = findOrMakeMediaRepresentationPart();
194 part.setUri(mediaUri);
195 }
196 }
197
198 public Reference getMediaSpecimenReference(){
199 if(checkMediaSpecimen()){
200 IdentifiableSource source = findMediaSpecimenIdentifieableSource();
201 if(source != null){
202 return source.getCitation();
203 }
204 }
205 return null;
206 }
207
208 public void setMediaSpecimenReference(Reference reference){
209 if(checkMediaSpecimen()){
210 IdentifiableSource source = findOrMakeMediaSpecimenIdentifieableSource();
211 source.setCitation(reference);
212 }
213 }
214
215 public String getMediaSpecimenReferenceDetail(){
216 if(checkMediaSpecimen()){
217 IdentifiableSource source = findMediaSpecimenIdentifieableSource();
218 if(source != null){
219 return source.getCitationMicroReference();
220 }
221 }
222 return null;
223 }
224
225 public void setMediaSpecimenReferenceDetail(String referenceDetail){
226 if(checkMediaSpecimen()){
227 IdentifiableSource source = findOrMakeMediaSpecimenIdentifieableSource();
228 source.setCitationMicroReference(referenceDetail);
229 }
230 }
231
232 /**
233 * @return
234 */
235 private IdentifiableSource findMediaSpecimenIdentifieableSource() {
236 IdentifiableSource source = null;
237 Media media = findMediaSpecimen().getMediaSpecimen();
238 if(media != null){
239 // FIXME use marker type to tag the MediaSpecimenReference and make sure that there is always only one.
240 if(!CollectionUtils.isEmpty(media.getSources())){
241 source = media.getSources().iterator().next();
242 }
243 }
244 return source;
245 }
246
247 /**
248 *
249 */
250 private MediaRepresentationPart findMediaRepresentationPart() {
251
252 Media media = findMediaSpecimen().getMediaSpecimen();
253 if(media != null){
254 if(!CollectionUtils.isEmpty(media.getRepresentations())){
255 MediaRepresentation repr = media.getRepresentations().iterator().next();
256 if(!CollectionUtils.isEmpty(repr.getParts())) {
257 MediaRepresentationPart part = repr.getParts().iterator().next();
258 return part;
259 }
260 }
261 }
262 return null;
263 }
264
265 private MediaRepresentationPart findOrMakeMediaRepresentationPart() {
266
267 if(findMediaSpecimen().getMediaSpecimen() == null){
268 findMediaSpecimen().setMediaSpecimen(Media.NewInstance());
269 }
270 Media media = findMediaSpecimen().getMediaSpecimen();
271 if(CollectionUtils.isEmpty(media.getRepresentations())){
272 media.addRepresentation(MediaRepresentation.NewInstance());
273 }
274 MediaRepresentation repr = media.getRepresentations().iterator().next();
275 if(CollectionUtils.isEmpty(repr.getParts())) {
276 repr.addRepresentationPart(MediaRepresentationPart.NewInstance(null, null));
277 }
278 MediaRepresentationPart part = repr.getParts().iterator().next();
279
280 return part;
281 }
282
283 /**
284 * @return
285 */
286 private IdentifiableSource findOrMakeMediaSpecimenIdentifieableSource() {
287 IdentifiableSource source ;
288 Media media = findMediaSpecimen().getMediaSpecimen();
289 if(media == null){
290 findMediaSpecimen().setMediaSpecimen(Media.NewInstance());
291 }
292 if(CollectionUtils.isEmpty(media.getSources())){
293 source = IdentifiableSource.NewPrimaryMediaSourceInstance(null, null);
294 media.addSource(source);
295 } else {
296 source = media.getSources().iterator().next();
297 }
298 return source;
299 }
300
301 /**
302 * @return
303 */
304 public boolean checkMediaSpecimen() {
305 return findMediaSpecimen() != null;
306 }
307
308 /**
309 * @return
310 */
311 private MediaSpecimen findMediaSpecimen() {
312 DerivedUnit sp = std.getTypeSpecimen();
313 if(MediaSpecimen.class.isAssignableFrom(sp.getClass())){
314 return (MediaSpecimen) sp;
315 }
316 return null;
317 }
318
319 }