Project

General

Profile

Download (16.6 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2015 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.api.service.dto;
10

    
11
import java.io.Serializable;
12
import java.util.AbstractMap;
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.Comparator;
16
import java.util.EnumSet;
17
import java.util.HashSet;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Set;
21
import java.util.TreeSet;
22

    
23
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
24
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
25
import eu.etaxonomy.cdm.model.description.DescriptionBase;
26
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
27
import eu.etaxonomy.cdm.model.description.Feature;
28
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
29
import eu.etaxonomy.cdm.model.description.TextData;
30
import eu.etaxonomy.cdm.model.media.Media;
31
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
32
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
33
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
34
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
35
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
36
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
37
import eu.etaxonomy.cdm.model.term.DefinedTerm;
38
import eu.etaxonomy.cdm.model.term.TermBase;
39
import eu.etaxonomy.cdm.ref.TypedEntityReference;
40

    
41

    
42
public abstract class SpecimenOrObservationBaseDTO extends TypedEntityReference<SpecimenOrObservationBase<?>>{
43

    
44
    private static final long serialVersionUID = -7597690654462090732L;
45

    
46
    private int id;
47
    private TreeSet<AbstractMap.SimpleEntry<String, String>> characterData;
48
    private DerivationTreeSummaryDTO derivationTreeSummary;
49
    protected String taxonName;
50

    
51
    protected String summaryLabel;
52
    protected boolean hasDetailImage;
53
    private boolean hasCharacterData;
54
    private boolean hasDna;
55
    private boolean hasSpecimenScan;
56

    
57
    private SpecimenOrObservationType recordBase;
58
    private TermBase kindOfUnit;
59
    private CollectionDTO collection;
60
    private String catalogNumber;
61
    private String collectorsNumber;
62
    private String barcode;
63
    private String preservationMethod;
64
    private String individualCount;
65
    private Set<DerivedUnitDTO> derivatives;
66

    
67
    private Set<SpecimenTypeDesignationDTO> specimenTypeDesignations;
68

    
69
    private DerivationEventDTO derivationEvent;
70

    
71
    // TODO use DTO !!!
72
    private Set<IdentifiableSource> sources;
73
    private List<MediaDTO> listOfMedia = new ArrayList<>();
74

    
75
    private DefinedTerm sex;
76

    
77
    private DefinedTerm lifeStage;
78

    
79
    protected SpecimenOrObservationBaseDTO(SpecimenOrObservationBase<?> specimenOrObservation) {
80
        super(HibernateProxyHelper.getClassWithoutInitializingProxy(specimenOrObservation), specimenOrObservation.getUuid(), specimenOrObservation.getTitleCache());
81
        this.id = specimenOrObservation.getId();
82
        Set<Media> collectedMedia = collectMedia(specimenOrObservation);
83
        addMediaAsDTO(collectedMedia);
84
        setKindOfUnit(specimenOrObservation.getKindOfUnit());
85
        setSex(specimenOrObservation.getSex());
86
        setIndividualCount(specimenOrObservation.getIndividualCount());
87
        lifeStage = specimenOrObservation.getLifeStage();
88
        if (specimenOrObservation instanceof DerivedUnit){
89
            DerivedUnit derivedUnit = (DerivedUnit)specimenOrObservation;
90
            if (derivedUnit.getSpecimenTypeDesignations() != null){
91
                setSpecimenTypeDesignations(derivedUnit.getSpecimenTypeDesignations());
92
            }
93
        }
94
    }
95

    
96
    public void setCollection(CollectionDTO collection) {
97
        this.collection = collection;
98
    }
99

    
100
    public String getCatalogNumber() {
101
        return catalogNumber;
102
    }
103

    
104
    public void setCatalogNumber(String catalogNumber) {
105
        this.catalogNumber = catalogNumber;
106
    }
107

    
108
    public String getCollectorsNumber() {
109
        return collectorsNumber;
110
    }
111

    
112
    public void setCollectorsNumber(String collectorsNumber) {
113
        this.collectorsNumber = collectorsNumber;
114
    }
115

    
116
    public String getBarcode() {
117
        return barcode;
118
    }
119

    
120
    public void setBarcode(String barcode) {
121
        this.barcode = barcode;
122
    }
123

    
124
    public String getPreservationMethod() {
125
        return preservationMethod;
126
    }
127

    
128
    public void setPreservationMethod(String preservationMethod) {
129
        this.preservationMethod = preservationMethod;
130
    }
131

    
132
    public Set<SpecimenTypeDesignationDTO> getSpecimenTypeDesignations() {
133
        return specimenTypeDesignations;
134
    }
135

    
136
    public void setSpecimenTypeDesignations(Set<SpecimenTypeDesignation> specimenTypeDesignations) {
137
        this.specimenTypeDesignations = new HashSet<>();
138
        for (SpecimenTypeDesignation typeDes: specimenTypeDesignations){
139
            if (typeDes != null){
140
                this.specimenTypeDesignations.add(new SpecimenTypeDesignationDTO(typeDes));
141
            }
142
        }
143

    
144
    }
145

    
146

    
147
    public Set<IdentifiableSource> getSources() {
148
        return sources;
149
    }
150

    
151
    public void setSources(Set<IdentifiableSource> sources) {
152
        this.sources = sources;
153
    }
154

    
155

    
156
    /**
157
     * @return the derivateDataDTO
158
     */
159
    public DerivationTreeSummaryDTO getDerivationTreeSummary() {
160
        return derivationTreeSummary;
161
    }
162

    
163
    /**
164
     * @param derivationTreeSummary the derivateDataDTO to set
165
     */
166
    public void setDerivationTreeSummary(DerivationTreeSummaryDTO derivationTreeSummary) {
167
        this.derivationTreeSummary = derivationTreeSummary;
168
        if(derivationTreeSummary != null) {
169
            setHasSpecimenScan(isHasSpecimenScan() || !derivationTreeSummary.getSpecimenScans().isEmpty());
170
            setHasDetailImage(isHasDetailImage() || !derivationTreeSummary.getDetailImages().isEmpty());
171
            setHasDna(isHasDna() || !derivationTreeSummary.getMolecularDataList().isEmpty());
172
        }
173
    }
174

    
175
    /**
176
     * @return the characterData
177
     */
178
    public TreeSet<AbstractMap.SimpleEntry<String, String>> getCharacterData() {
179
        return characterData;
180
    }
181

    
182
    public void addCharacterData(String character, String state){
183
      if(characterData==null){
184
          characterData = new TreeSet<AbstractMap.SimpleEntry<String,String>>(new PairComparator());
185
      }
186
      characterData.add(new AbstractMap.SimpleEntry<>(character, state));
187
    }
188

    
189
    private class PairComparator implements Comparator<AbstractMap.SimpleEntry<String,String>>, Serializable {
190

    
191
        private static final long serialVersionUID = -8589392050761963540L;
192

    
193
        @Override
194
        public int compare(AbstractMap.SimpleEntry<String, String> o1, AbstractMap.SimpleEntry<String, String> o2) {
195
            if(o1==null && o2!=null){
196
                return -1;
197
            }
198
            if(o1!=null && o2==null){
199
                return 1;
200
            }
201
            if(o1!=null && o2!=null){
202
                return o1.getKey().compareTo(o2.getKey());
203
            }
204
            return 0;
205
        }
206
    }
207

    
208
    /**
209
     * @return the hasCharacterData
210
     */
211
    public boolean isHasCharacterData() {
212
        return hasCharacterData;
213
    }
214

    
215
    /**
216
     * @param hasCharacterData the hasCharacterData to set
217
     */
218
    public void setHasCharacterData(boolean hasCharacterData) {
219
        this.hasCharacterData = hasCharacterData;
220
    }
221

    
222
    /**
223
     * @return the hasDna
224
     */
225
    public boolean isHasDna() {
226
        return hasDna;
227
    }
228

    
229
    /**
230
     * @param hasDna the hasDna to set
231
     */
232
    public void setHasDna(boolean hasDna) {
233
        this.hasDna = hasDna;
234
    }
235

    
236
    /**
237
     * @return the hasDetailImage
238
     */
239
    public boolean isHasDetailImage() {
240
        return hasDetailImage;
241
    }
242

    
243
    /**
244
     * @param hasDetailImage the hasDetailImage to set
245
     */
246
    public void setHasDetailImage(boolean hasDetailImage) {
247
        this.hasDetailImage = hasDetailImage;
248
    }
249

    
250
    /**
251
     * @return the hasSpecimenScan
252
     */
253
    public boolean isHasSpecimenScan() {
254
        return hasSpecimenScan;
255
    }
256

    
257
    /**
258
     * @param hasSpecimenScan the hasSpecimenScan to set
259
     */
260
    public void setHasSpecimenScan(boolean hasSpecimenScan) {
261
        this.hasSpecimenScan = hasSpecimenScan;
262
    }
263
    /**
264
     * @return The summary of all DerivedUnit identifiers with the label of
265
     * this SpecimenOrObservationBase.
266
     * This label is usually being user for citing the unit in publications.
267
     */
268
    public String getSummaryLabel() {
269
        return summaryLabel;
270
    }
271

    
272
    /**
273
     * Summary of all DerivedUnit identifiers with the label of this SpecimenOrObservationBase.
274
     * This label is usually being user for citing the unit in publications.
275
     */
276
    public void setSummaryLabel(String summaryLabel) {
277
        this.summaryLabel = summaryLabel;
278
    }
279

    
280

    
281
    public SpecimenOrObservationType getRecordBase() {
282
        return recordBase;
283
    }
284
    public void setRecordBase(SpecimenOrObservationType specimenOrObservationType) {
285
        this.recordBase = specimenOrObservationType;
286
    }
287

    
288
    /**
289
     * @return the collection
290
     *
291
     * @deprecated TODO remove as it only duplicates the information contained in the collectionDTO
292
     */
293
    @Deprecated
294
    public String getCollectionCode() {
295
        if (collection != null){
296
            return collection.getCode();
297
        } else {
298
            return null;
299
        }
300
    }
301

    
302
    /**
303
     * @return the collection
304
     */
305
    public CollectionDTO getCollection() {
306
        return collection;
307
    }
308
    /**
309
     * @param collection the collection to set
310
     */
311
    public void setCollectioDTO(CollectionDTO collection) {
312
        this.collection = collection;
313
    }
314

    
315

    
316

    
317
    public Set<DerivedUnitDTO> getDerivatives() {
318
        if (this.derivatives == null){
319
            this.derivatives = new HashSet<>();
320
        }
321
        return derivatives;
322
    }
323

    
324
    public void setDerivatives(Set<DerivedUnitDTO> derivatives) {
325
        this.derivatives = derivatives;
326
    }
327

    
328
    public void addDerivative(DerivedUnitDTO derivate){
329
        if (this.derivatives == null){
330
            this.derivatives = new HashSet<>();
331
        }
332
        this.derivatives.add(derivate);
333
    }
334
    public void addAllDerivatives(Set<DerivedUnitDTO> derivatives){
335
        if (this.derivatives == null){
336
            this.derivatives = new HashSet<>();
337
        }
338
        this.derivatives.addAll(derivatives);
339
    }
340

    
341
    /**
342
     * Recursively collects all derivatives from this.
343
     */
344
    public Collection<DerivedUnitDTO> collectDerivatives() {
345
        return collectDerivatives(new HashSet<>());
346
    }
347

    
348
    /**
349
     * private partner method to {@link #collectDerivatives()} for recursive calls.
350
     *
351
     * @param dtos
352
     */
353
    private Collection<DerivedUnitDTO> collectDerivatives(Set<DerivedUnitDTO> dtos) {
354
        dtos.addAll(getDerivatives());
355
        if(derivatives != null) {
356
            for(SpecimenOrObservationBaseDTO subDto : derivatives) {
357
                dtos.addAll(subDto.collectDerivatives(dtos));
358
            }
359
        }
360
        return dtos;
361
    }
362

    
363

    
364

    
365
    public DerivationEventDTO getDerivationEvent() {
366
        return derivationEvent;
367
    }
368

    
369
    public void setDerivationEvent(DerivationEventDTO derivationEvent) {
370
        this.derivationEvent = derivationEvent;
371
    }
372

    
373

    
374
    /**
375
     * @return the listOfMedia
376
     */
377
    public List<MediaDTO> getListOfMedia() {
378
        return listOfMedia;
379
    }
380

    
381
    /**
382
     * @param listOfMedia the listOfMedia to set
383
     */
384
    public void setListOfMedia(List<MediaDTO> listOfMedia) {
385
        this.listOfMedia = listOfMedia;
386
    }
387

    
388

    
389
    protected Set<Media> collectMedia(SpecimenOrObservationBase<?> specimenOrObservation){
390
        Set<Media> collectedMedia = new HashSet<>();
391
        Set<SpecimenDescription> descriptions = specimenOrObservation.getSpecimenDescriptionImageGallery();
392
        for (DescriptionBase<?> desc : descriptions){
393
            if (desc instanceof SpecimenDescription){
394
                SpecimenDescription specimenDesc = (SpecimenDescription)desc;
395
                for (DescriptionElementBase element : specimenDesc.getElements()){
396
                    if (element.isInstanceOf(TextData.class)&& element.getFeature().equals(Feature.IMAGE())) {
397
                        for (Media media :element.getMedia()){
398
                            collectedMedia.add(media);
399
                        }
400
                    }
401
                }
402
            }
403
        }
404
        return collectedMedia;
405
    }
406

    
407
    private void addMediaAsDTO(Set<Media> media) {
408
        for(Media m : media) {
409
            m.getAllTitles(); // initialize all titles!!!
410
            for (MediaRepresentation rep :m.getRepresentations()){
411
                for(MediaRepresentationPart p : rep.getParts()){
412
                    if(p.getUri() != null){
413
                        MediaDTO dto = new MediaDTO(m.getUuid());
414
                        dto.setUri(p.getUri().toString());
415
                        getListOfMedia().add(dto);
416
                    }
417
                }
418
            }
419
        }
420
    }
421

    
422
    /**
423
     * @param sob
424
     *      The Unit to assemble the derivatives information for
425
     * @param maxDepth
426
     *   The maximum number of derivation events levels up to which derivatives are to be assembled.
427
     *   <code>NULL</code> means infinitely.
428
     * @param includeTypes
429
     *      Allows for positive filtering by {@link SpecimenOrObservationType}.
430
     *      Filter is disabled when <code>NULL</code>. This only affects the derivatives assembled in the
431
     *      {@link #derivatives} list. The <code>unitLabelsByCollection</code> are always collected for the
432
     *      whole bouquet of derivatives.
433
     * @param unitLabelsByCollection
434
     *      A map to record the unit labels (most significant identifier + collection code) per collection.
435
     *      Optional parameter, may be <code>NULL</code>.
436
     */
437
    protected void assembleDerivatives(SpecimenOrObservationBase<?> sob,
438
            Integer maxDepth, EnumSet<SpecimenOrObservationType> includeTypes,
439
            Map<eu.etaxonomy.cdm.model.occurrence.Collection, List<String>> unitLabelsByCollection) {
440

    
441
        boolean doDescend = maxDepth == null || maxDepth > 0;
442
        Integer nextLevelMaxDepth = maxDepth != null ? maxDepth - 1 : null;
443
        for (DerivedUnit derivedUnit : sob.collectDerivedUnits()) {
444
            if(!derivedUnit.isPublish()){
445
                continue;
446
            }
447

    
448
            if(unitLabelsByCollection != null) {
449
                // collect accession numbers for citation
450
                // collect collections for herbaria column
451
                eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();
452
                if (collection != null) {
453
                    //combine collection with identifier
454
                    String identifier = derivedUnit.getMostSignificantIdentifier();
455
                    if (identifier != null && collection.getCode()!=null) {
456
                        identifier = (collection.getCode()!=null?collection.getCode():"[no collection]")+" "+identifier;
457
                    }
458
                    if(!unitLabelsByCollection.containsKey(collection)) {
459
                        unitLabelsByCollection.put(collection, new ArrayList<>());
460
                    }
461
                    unitLabelsByCollection.get(collection).add(identifier);
462
                }
463
            }
464

    
465
            if (doDescend && (includeTypes == null || includeTypes.contains(derivedUnit.getRecordBasis()))) {
466
                DerivedUnitDTO derivedUnitDTO = DerivedUnitDTO.fromEntity(derivedUnit, nextLevelMaxDepth, includeTypes, null);
467
                addDerivative(derivedUnitDTO);
468
                setHasCharacterData(isHasCharacterData() || derivedUnitDTO.isHasCharacterData());
469
                // NOTE! the flags setHasDetailImage, setHasDna, setHasSpecimenScan are also set in
470
                // setDerivateDataDTO(), see below
471
                setHasDetailImage(isHasDetailImage() || derivedUnitDTO.isHasDetailImage());
472
                setHasDna(isHasDna() || derivedUnitDTO.isHasDna());
473
                setHasSpecimenScan(isHasSpecimenScan() || derivedUnitDTO.isHasSpecimenScan());
474
            }
475
        }
476
    }
477

    
478
    public TermBase getKindOfUnit() {
479
        return kindOfUnit;
480
    }
481
    public void setKindOfUnit(TermBase kindOfUnit) {
482
        this.kindOfUnit = HibernateProxyHelper.deproxy(kindOfUnit);
483
    }
484
    public DefinedTerm getSex() {
485
        return sex;
486
    }
487
    public void setSex(DefinedTerm sex) {
488
        this.sex = sex;
489
    }
490
    public DefinedTerm getLifeStage() {
491
        return lifeStage;
492
    }
493
    public void setLifeStage(DefinedTerm lifeStage) {
494
        this.lifeStage = lifeStage;
495
    }
496

    
497

    
498
    public int getId() {
499
        return id;
500
    }
501

    
502
    public String getIndividualCount() {
503
        return individualCount;
504
    }
505

    
506
    public void setIndividualCount(String individualCount) {
507
        this.individualCount = individualCount;
508
    }
509

    
510

    
511
}
(27-27/36)