Project

General

Profile

Download (11.7 KB) Statistics
| Branch: | Tag: | Revision:
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
     * @return the std
57
     */
58
    public SpecimenTypeDesignation asSpecimenTypeDesignation() {
59
        return std;
60
    }
61

    
62
    /**
63
     * Creates an new new instance of SpecimenTypeDesignationDTO which is backed up
64
     * by an newly instantiated <code>SpecimenTypeDesignation.typeSpecimen.derivedFrom.type</code> object graph.
65
     */
66
    public SpecimenTypeDesignationDTO(){
67
        this(SpecimenTypeDesignation.NewInstance());
68
    }
69

    
70
    /**
71
     * Creates a new instance of SpecimenTypeDesignationDTO.
72
     *
73
     * The constructor assures that <code>SpecimenTypeDesignation.typeSpecimen.derivedFrom.type</code> are never null.
74
     * That is in case the supplied std parameter or if any of the above listed properties is null the missing instances
75
     * are created and added to the object graph.
76
     *
77
     * @param std
78
     */
79
    public SpecimenTypeDesignationDTO(SpecimenTypeDesignation std) {
80
        this.std = std;
81
        if(std.getTypeSpecimen() == null){
82
            DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
83
            std.setTypeSpecimen(derivedUnit);
84
        }
85
        if(std.getTypeSpecimen().getDerivedFrom() == null){
86
            DerivationEvent derivedFrom = DerivationEvent.NewInstance(DerivationEventType.GATHERING_IN_SITU());
87
            std.getTypeSpecimen().setDerivedFrom(derivedFrom);
88
        }
89
    }
90

    
91
    public DerivationEventType getDerivationEventType(){
92
        return std.getTypeSpecimen().getDerivedFrom().getType();
93
    }
94

    
95
    public void setDerivationEventType(DerivationEventType derivationEventType) throws DerivedUnitConversionException{
96

    
97
        std.getTypeSpecimen().getDerivedFrom().setType(derivationEventType);
98

    
99
        Class<? extends DerivedUnit> requiredSpecimenType = specimenTypeFor(derivationEventType);
100

    
101
        if(!requiredSpecimenType.equals(std.getTypeSpecimen())){
102

    
103
            DerivedUnit convertedSpecimen;
104

    
105
            if(requiredSpecimenType.equals(MediaSpecimen.class)){
106
                DerivedUnitConverter<MediaSpecimen> converter = new DerivedUnitConverter<MediaSpecimen> (std.getTypeSpecimen());
107
                convertedSpecimen = converter.convertTo((Class<MediaSpecimen>)requiredSpecimenType, specimenOrObservationTypeFor(derivationEventType));
108
            } else {
109
                DerivedUnitConverter<DerivedUnit> converter = new DerivedUnitConverter<DerivedUnit> (std.getTypeSpecimen());
110
                convertedSpecimen = converter.convertTo((Class<DerivedUnit>)requiredSpecimenType, specimenOrObservationTypeFor(derivationEventType));
111
            }
112

    
113
            std.setTypeSpecimen(convertedSpecimen);
114
        }
115
    }
116

    
117

    
118
    /**
119
     * See constructor doc.
120
     *
121
     * @param derivationEventType
122
     * @return
123
     *      either <code>DerivedUnit</code> or <code>MediaSpecimen</code> never null.
124
     *  <code>DerivedUnit</code> is the fall back return value.
125
     */
126
    private Class<?  extends DerivedUnit> specimenTypeFor(DerivationEventType derivationEventType) {
127
        UUID detUuid = derivationEventType.getUuid();
128

    
129
        if(detUuid.equals(DerivationEventType.GATHERING_IN_SITU().getUuid())) {
130
            return DerivedUnit.class;
131
        }
132
        if(detUuid.equals(DerivationEventTypes.CULTURE_METABOLIC_INACTIVE().getUuid())) {
133
            return DerivedUnit.class;
134
        }
135
        if(detUuid.equals(DerivationEventTypes.PUBLISHED_IMAGE().getUuid())) {
136
            return MediaSpecimen.class;
137
        }
138
        if(detUuid.equals(DerivationEventTypes.UNPUBLISHED_IMAGE().getUuid())) {
139
            return MediaSpecimen.class;
140
        }
141
        return DerivedUnit.class;
142
    }
143

    
144
    private SpecimenOrObservationType specimenOrObservationTypeFor(DerivationEventType derivationEventType) {
145

    
146
        UUID detUuid = derivationEventType.getUuid();
147

    
148
        if(detUuid.equals(DerivationEventType.GATHERING_IN_SITU().getUuid())) {
149
            return SpecimenOrObservationType.PreservedSpecimen;
150
        }
151
        if(detUuid.equals(DerivationEventTypes.CULTURE_METABOLIC_INACTIVE().getUuid())) {
152
            return SpecimenOrObservationType.LivingSpecimen;
153
        }
154
        if(detUuid.equals(DerivationEventTypes.PUBLISHED_IMAGE().getUuid())) {
155
            return SpecimenOrObservationType.StillImage;
156
        }
157
        if(detUuid.equals(DerivationEventTypes.UNPUBLISHED_IMAGE().getUuid())) {
158
            return SpecimenOrObservationType.StillImage;
159
        }
160
       return SpecimenOrObservationType.PreservedSpecimen;
161

    
162
    }
163

    
164
    public SpecimenTypeDesignationStatus getTypeStatus(){
165
        return HibernateProxyHelper.deproxy(std.getTypeStatus(), SpecimenTypeDesignationStatus.class);
166
    }
167

    
168
    public void setTypeStatus(SpecimenTypeDesignationStatus typeStatus){
169
        std.setTypeStatus(typeStatus);
170
    }
171

    
172
    public Collection getCollection(){
173
        return std.getTypeSpecimen().getCollection();
174
    }
175

    
176
    public void setCollection(Collection collection){
177
        std.getTypeSpecimen().setCollection(collection);
178
    }
179

    
180
    public String getAccessionNumber(){
181
        return std.getTypeSpecimen().getAccessionNumber();
182
    }
183

    
184
    public void setAccessionNumber(String accessionNumber){
185
        std.getTypeSpecimen().setAccessionNumber(accessionNumber);
186
    }
187

    
188
    public URI getMediaUri(){
189
        if(checkMediaSpecimen()){
190
            MediaRepresentationPart part = findMediaRepresentationPart();
191
            if(part != null){
192
                return part.getUri();
193
            }
194
        }
195
        return null;
196
    }
197

    
198
    public void setMediaUri(URI mediaUri){
199
        if(checkMediaSpecimen()){
200
            MediaRepresentationPart part = findOrMakeMediaRepresentationPart();
201
            part.setUri(mediaUri);
202
        }
203
    }
204

    
205
    public Reference getMediaSpecimenReference(){
206
        if(checkMediaSpecimen()){
207
            IdentifiableSource source = findMediaSpecimenIdentifieableSource();
208
            if(source != null){
209
                return source.getCitation();
210
            }
211
        }
212
        return null;
213
    }
214

    
215
    public void setMediaSpecimenReference(Reference reference){
216
        if(checkMediaSpecimen()){
217
            IdentifiableSource source = findOrMakeMediaSpecimenIdentifieableSource();
218
            source.setCitation(reference);
219
        }
220
    }
221

    
222
    public String getMediaSpecimenReferenceDetail(){
223
        if(checkMediaSpecimen()){
224
            IdentifiableSource source = findMediaSpecimenIdentifieableSource();
225
            if(source != null){
226
                return source.getCitationMicroReference();
227
            }
228
        }
229
        return null;
230
    }
231

    
232
    public void setMediaSpecimenReferenceDetail(String referenceDetail){
233
        if(checkMediaSpecimen()){
234
            IdentifiableSource source = findOrMakeMediaSpecimenIdentifieableSource();
235
            source.setCitationMicroReference(referenceDetail);
236
        }
237
    }
238

    
239
    /**
240
     * @return
241
     */
242
    private IdentifiableSource findMediaSpecimenIdentifieableSource() {
243
        IdentifiableSource source = null;
244
        Media media = findMediaSpecimen().getMediaSpecimen();
245
        if(media != null){
246
            // FIXME use marker type to tag the MediaSpecimenReference and make sure that there is always only one.
247
            if(!CollectionUtils.isEmpty(media.getSources())){
248
                source = media.getSources().iterator().next();
249
            }
250
        }
251
        return source;
252
    }
253

    
254
    /**
255
     *
256
     */
257
    private MediaRepresentationPart findMediaRepresentationPart() {
258

    
259
        Media media = findMediaSpecimen().getMediaSpecimen();
260
        if(media != null){
261
            if(!CollectionUtils.isEmpty(media.getRepresentations())){
262
                MediaRepresentation repr = media.getRepresentations().iterator().next();
263
                if(!CollectionUtils.isEmpty(repr.getParts())) {
264
                    MediaRepresentationPart part = repr.getParts().iterator().next();
265
                    return part;
266
                }
267
            }
268
        }
269
        return null;
270
    }
271

    
272
    private MediaRepresentationPart findOrMakeMediaRepresentationPart() {
273

    
274
        if(findMediaSpecimen().getMediaSpecimen() == null){
275
            findMediaSpecimen().setMediaSpecimen(Media.NewInstance());
276
        }
277
        Media media = findMediaSpecimen().getMediaSpecimen();
278
        if(CollectionUtils.isEmpty(media.getRepresentations())){
279
            media.addRepresentation(MediaRepresentation.NewInstance());
280
        }
281
        MediaRepresentation repr = media.getRepresentations().iterator().next();
282
        if(CollectionUtils.isEmpty(repr.getParts())) {
283
            repr.addRepresentationPart(MediaRepresentationPart.NewInstance(null, null));
284
        }
285
        MediaRepresentationPart part = repr.getParts().iterator().next();
286

    
287
        return part;
288
    }
289

    
290
    /**
291
     * @return
292
     */
293
    private IdentifiableSource findOrMakeMediaSpecimenIdentifieableSource() {
294
        IdentifiableSource source ;
295
        Media media = findMediaSpecimen().getMediaSpecimen();
296
        if(media == null){
297
            findMediaSpecimen().setMediaSpecimen(Media.NewInstance());
298
        }
299
        if(CollectionUtils.isEmpty(media.getSources())){
300
           source = IdentifiableSource.NewPrimaryMediaSourceInstance(null, null);
301
           media.addSource(source);
302
        } else {
303
            source = media.getSources().iterator().next();
304
        }
305
        return source;
306
    }
307

    
308
    /**
309
     * @return
310
     */
311
    public boolean checkMediaSpecimen() {
312
        return findMediaSpecimen() != null;
313
    }
314

    
315
    /**
316
     * @return
317
     */
318
    private MediaSpecimen findMediaSpecimen() {
319
        DerivedUnit sp = std.getTypeSpecimen();
320
        if(MediaSpecimen.class.isAssignableFrom(sp.getClass())){
321
            return (MediaSpecimen) sp;
322
        }
323
        return null;
324
    }
325

    
326
}
(4-4/6)