Project

General

Profile

Download (11.6 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.DefinedTerm;
20
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
21
import eu.etaxonomy.cdm.model.media.Media;
22
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
23
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
24
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
25
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
26
import eu.etaxonomy.cdm.model.occurrence.Collection;
27
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
28
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
29
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
30
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
31
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
32
import eu.etaxonomy.cdm.model.reference.Reference;
33

    
34
/**
35
 * A DTO which is use in the context of the {@link SpecimenTypeDesignationWorkingSetDTO} which is backed up
36
 * <code>SpecimenTypeDesignation.typeSpecimen.derivedFrom.type</code> object graph.
37
 * <p>
38
 * The contained {@link DerivedUnit} either is a {@link MediaSpecimen} with or a {@link DerivedUnit}, depending on the
39
 * "kind of unit" which is defined by the associated <code>DerivationEvent.type</code>:
40
 *
41
 * <ul>
42
 * <li>{@link DerivationEventType#GATHERING_IN_SITU()} -&gt; {@link DerivedUnit}</li>
43
 * <li>{@link DerivationEventTypes#CULTURE_METABOLIC_INACTIVE()} -&gt; {@link DerivedUnit}</li>
44
 * <li>{@link DerivationEventTypes#UNPUBLISHED_IMAGE()} -&gt; {@link MediaSpecimen}</li>
45
 * <li>{@link DerivationEventTypes#PUBLISHED_IMAGE()} -&gt; {@link MediaSpecimen}</li>
46
 * </ul>
47
 *
48
 * @author a.kohlbecker
49
 * @since Jun 22, 2017
50
 *
51
 */
52
public class SpecimenTypeDesignationDTO {
53

    
54
    SpecimenTypeDesignation std;
55

    
56
    /**
57
     * @return the std
58
     */
59
    public SpecimenTypeDesignation asSpecimenTypeDesignation() {
60
        return std;
61
    }
62

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

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

    
92
    public DefinedTerm getKindOfUnit(){
93
        return std.getTypeSpecimen().getKindOfUnit();
94
    }
95

    
96
    public void setKindOfUnit(DefinedTerm kindOfUnit) throws DerivedUnitConversionException{
97

    
98
        std.getTypeSpecimen().setKindOfUnit(kindOfUnit);
99

    
100
        Class<? extends DerivedUnit> requiredSpecimenType = specimenTypeFor(kindOfUnit);
101

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

    
104
            DerivedUnit convertedSpecimen;
105

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

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

    
118

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

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

    
145
    private SpecimenOrObservationType specimenOrObservationTypeFor(DefinedTerm derivationEventType) {
146

    
147
        UUID detUuid = derivationEventType.getUuid();
148

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

    
163
    }
164

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
273
    private MediaRepresentationPart findOrMakeMediaRepresentationPart() {
274

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

    
288
        return part;
289
    }
290

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

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

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

    
328
}
(4-4/6)