Project

General

Profile

Download (13.2 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.util.ArrayList;
12
import java.util.Collection;
13
import java.util.Collections;
14
import java.util.EnumSet;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19
import java.util.regex.Pattern;
20

    
21
import eu.etaxonomy.cdm.common.CdmUtils;
22
import eu.etaxonomy.cdm.common.URI;
23
import eu.etaxonomy.cdm.format.CdmFormatterFactory;
24
import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
25
import eu.etaxonomy.cdm.format.description.DefaultCategoricalDescriptionBuilder;
26
import eu.etaxonomy.cdm.format.description.DefaultQuantitativeDescriptionBuilder;
27
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.description.CategoricalData;
30
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
31
import eu.etaxonomy.cdm.model.description.QuantitativeData;
32
import eu.etaxonomy.cdm.model.media.Media;
33
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
34
import eu.etaxonomy.cdm.model.name.TaxonName;
35
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
36
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
37
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
38
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
39
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.ref.TypedEntityReference;
42

    
43
/**
44
 * @author pplitzner
45
 * @since Mar 26, 2015
46
 */
47
public class DerivedUnitDTO extends SpecimenOrObservationBaseDTO{
48

    
49
    private static final long serialVersionUID = 2345864166579381295L;
50

    
51
    private String accessionNumber;
52
    private String specimenShortTitle;
53
    private TypedEntityReference<TaxonName> storedUnder;
54
    private URI preferredStableUri;
55

    
56
    private List<TypedEntityReference<Taxon>> associatedTaxa;
57
    private Map<String, List<String>> types;
58

    
59
    private String originalLabelInfo;
60
    private String exsiccatum;
61
    private String mostSignificantIdentifier;
62

    
63
    private CollectionDTO collection;
64

    
65
    private String catalogNumber;
66

    
67
    private String barcode;
68

    
69
    private String preservationMethod;
70

    
71
    /**
72
     * Constructs a new DerivedUnitDTO. All derivatives of the passed <code>DerivedUnit entity</code> will be collected and
73
     * added as DerivedUnitDTO to the {@link SpecimenOrObservationBaseDTO#getDerivatives() derivative DTOs}.
74
     *
75
     * @param entity
76
     *   The entity to create the dto for
77
     *
78
     * @return <code>null</code> or the new DerivedUnitDTO
79
     */
80
    public static DerivedUnitDTO fromEntity(DerivedUnit entity){
81
        return fromEntity(entity, null, null);
82
    }
83

    
84
    /**
85
     * Constructs a new DerivedUnitDTO. All derivatives of the passed <code>DerivedUnit entity</code> will be collected and
86
     * added as DerivedUnitDTO to the {@link SpecimenOrObservationBaseDTO#getDerivatives() derivative DTOs}.
87
     *
88
     * @param entity
89
     *   The entity to create the dto for
90
     * @param maxDepth
91
     *   The maximum number of derivation events levels up to which derivatives are to be collected.
92
     *   <code>NULL</code> means infinitely.
93
     * @param specimenOrObservationTypeFilter
94
     *     Set of SpecimenOrObservationType to be included into the collection of {@link #getDerivatives() derivative DTOs}
95
     * @return
96
     *  The DTO
97
     */
98
    public static DerivedUnitDTO fromEntity(DerivedUnit entity, Integer maxDepth,
99
            EnumSet<SpecimenOrObservationType> specimenOrObservationTypeFilter){
100

    
101
        if(entity == null) {
102
            return null;
103
        }
104
        DerivedUnitDTO dto =  new DerivedUnitDTO(entity);
105

    
106
        // ---- assemble derivation tree summary
107
        //      this data should be sufficient in clients for showing the unit in a list view
108
        dto.setDerivationTreeSummary(DerivationTreeSummaryDTO.fromEntity(entity, dto.getSpecimenShortTitle()));
109

    
110
        // ---- assemble derivatives
111
        //      this data is is often only required for clients in order to show the details of the derivation tree
112
        dto.addAllDerivatives(dto.assembleDerivatives(entity, maxDepth, specimenOrObservationTypeFilter));
113

    
114
        return dto;
115
    }
116

    
117
    /**
118
     * @param derivedUnit
119
     */
120
    public DerivedUnitDTO(DerivedUnit derivedUnit) {
121
        super(derivedUnit);
122

    
123
        // experimental feature, not yet exposed in method signature
124
        boolean cleanAccessionNumber = false;
125
        accessionNumber = derivedUnit.getAccessionNumber();
126
        preferredStableUri = derivedUnit.getPreferredStableUri();
127
        if (derivedUnit.getCollection() != null){
128
            setCollectioDTO(CollectionDTO.fromCollection(HibernateProxyHelper.deproxy(derivedUnit.getCollection())));
129
            if(cleanAccessionNumber && getCollection().getCode() != null) {
130
                accessionNumber = accessionNumber.replaceFirst("^" + Pattern.quote(getCollection().getCode()) + "-", "");
131
            }
132
        }
133
        setBarcode(derivedUnit.getBarcode());
134
        setCatalogNumber(derivedUnit.getCatalogNumber());
135
        setDerivationEvent(DerivationEventDTO.fromEntity(derivedUnit.getDerivedFrom()));
136
        if (derivedUnit.getPreservation()!= null){
137
            setPreservationMethod(derivedUnit.getPreservation().getMaterialMethodText());
138
        }
139
        setRecordBase(derivedUnit.getRecordBasis());
140
        setSources(derivedUnit.getSources());
141
        setSpecimenTypeDesignations(derivedUnit.getSpecimenTypeDesignations());
142

    
143
        // -------------------------------------------------------------
144

    
145
        mostSignificantIdentifier = derivedUnit.getMostSignificantIdentifier();
146

    
147
        //specimenShortTitle
148
        setSpecimenShortTitle(composeSpecimenShortTitle(derivedUnit));
149

    
150

    
151
        //preferred stable URI
152
        setPreferredStableUri(derivedUnit.getPreferredStableUri());
153

    
154
        // label
155
        setSummaryLabel(derivedUnit.getTitleCache());
156

    
157
        // character state data
158
        if(derivedUnit.characterData() != null) {
159
            Collection<DescriptionElementBase> characterDataForSpecimen = derivedUnit.characterData();
160
            for (DescriptionElementBase descriptionElementBase : characterDataForSpecimen) {
161
                String character = descriptionElementBase.getFeature().getLabel();
162
                ArrayList<Language> languages = new ArrayList<>(Collections.singleton(Language.DEFAULT()));
163
                if (descriptionElementBase instanceof QuantitativeData) {
164
                    QuantitativeData quantitativeData = (QuantitativeData) descriptionElementBase;
165
                    DefaultQuantitativeDescriptionBuilder builder = new DefaultQuantitativeDescriptionBuilder();
166
                    String state = builder.build(quantitativeData, languages).getText(Language.DEFAULT());
167
                    addCharacterData(character, state);
168
                }
169
                else if(descriptionElementBase instanceof CategoricalData){
170
                    CategoricalData categoricalData = (CategoricalData) descriptionElementBase;
171
                    DefaultCategoricalDescriptionBuilder builder = new DefaultCategoricalDescriptionBuilder();
172
                    String state = builder.build(categoricalData, languages).getText(Language.DEFAULT());
173
                    addCharacterData(character, state);
174
                }
175
            }
176
        }
177

    
178
        // check type designations
179
        Collection<SpecimenTypeDesignation> specimenTypeDesignations = derivedUnit.getSpecimenTypeDesignations();
180
        for (SpecimenTypeDesignation specimenTypeDesignation : specimenTypeDesignations) {
181
            TypeDesignationStatusBase<?> typeStatus = specimenTypeDesignation.getTypeStatus();
182
            Set<TaxonName> typifiedNames = specimenTypeDesignation.getTypifiedNames();
183
            List<String> typedTaxaNames = new ArrayList<>();
184
            for (TaxonName taxonName : typifiedNames) {
185
                typedTaxaNames.add(taxonName.getTitleCache());
186
            }
187
            addTypes(typeStatus!=null?typeStatus.getLabel():"", typedTaxaNames);
188
        }
189

    
190
        if(derivedUnit.getStoredUnder() != null) {
191
            storedUnder = TypedEntityReference.fromEntity(derivedUnit.getStoredUnder());
192
        }
193
        originalLabelInfo = derivedUnit.getOriginalLabelInfo();
194
        exsiccatum = derivedUnit.getExsiccatum();
195

    
196
    }
197

    
198
    protected String composeSpecimenShortTitle(DerivedUnit derivedUnit) {
199
        FormatKey collectionKey = FormatKey.COLLECTION_CODE;
200
        String specimenShortTitle = CdmFormatterFactory.format(derivedUnit, collectionKey);
201
        if (CdmUtils.isBlank(specimenShortTitle)) {
202
            collectionKey = FormatKey.COLLECTION_NAME;
203
        }
204
        if(CdmUtils.isNotBlank(derivedUnit.getMostSignificantIdentifier())){
205
            specimenShortTitle = CdmFormatterFactory.format(derivedUnit, new FormatKey[] {
206
                    collectionKey,
207
                    FormatKey.SPACE,
208
                    FormatKey.MOST_SIGNIFICANT_IDENTIFIER
209
                    });
210
            if(!specimenShortTitle.isEmpty() && derivedUnit instanceof MediaSpecimen) {
211
                Media media = ((MediaSpecimen)derivedUnit).getMediaSpecimen();
212
                if(media != null && !CdmUtils.isBlank(media.getTitleCache()) ) {
213
                    if(media.getTitle() != null && !media.getTitle().getText().isEmpty()) {
214
                        specimenShortTitle += " (" + media.getTitle().getText() + ")";
215
                    }
216
                }
217
            }
218
        }
219
        if(CdmUtils.isBlank(specimenShortTitle)){
220
            specimenShortTitle = derivedUnit.getTitleCache();
221
        }
222
        if(CdmUtils.isBlank(specimenShortTitle)){  //should not be necessary as titleCache should never be empty
223
            specimenShortTitle = derivedUnit.getUuid().toString();
224
        }
225
        return specimenShortTitle;
226
    }
227

    
228
    @Override
229
    protected Set<Media> collectMedia(SpecimenOrObservationBase<?> specimenOrObservation){
230
        Set<Media> collectedMedia = super.collectMedia(specimenOrObservation);
231
        if(specimenOrObservation instanceof MediaSpecimen) {
232
            if(((MediaSpecimen)specimenOrObservation).getMediaSpecimen() != null) {
233
            collectedMedia.add(((MediaSpecimen)specimenOrObservation).getMediaSpecimen());
234
            }
235
        }
236
        return collectedMedia;
237
    }
238

    
239

    
240
    public String getAccessionNumber() {
241
        return accessionNumber;
242
    }
243
    public void setAccessionNumber(String accessionNumber) {
244
        this.accessionNumber = accessionNumber;
245
    }
246

    
247
    public Map<String, List<String>> getTypes() {
248
        return types;
249
    }
250
    public void addTypes(String typeStatus, List<String> typedTaxa){
251
        if(types==null){
252
            types = new HashMap<>();
253
        }
254
        types.put(typeStatus, typedTaxa);
255
    }
256

    
257
    public List<TypedEntityReference<Taxon>> getAssociatedTaxa() {
258
        return associatedTaxa;
259
    }
260
    public void addAssociatedTaxon(Taxon taxon){
261
        if(associatedTaxa==null){
262
            associatedTaxa = new ArrayList<>();
263
        }
264
        associatedTaxa.add(TypedEntityReference.fromEntity(taxon));
265
    }
266

    
267
    public void setPreferredStableUri(URI preferredStableUri) {
268
        this.preferredStableUri = preferredStableUri;
269
    }
270
    public URI getPreferredStableUri() {
271
        return preferredStableUri;
272
    }
273

    
274
    public String getSpecimenShortTitle() {
275
        return specimenShortTitle;
276
    }
277
    public void setSpecimenShortTitle(String specimenIdentifier) {
278
        this.specimenShortTitle = specimenIdentifier;
279
    }
280

    
281
    public String getMostSignificantIdentifier() {
282
        return mostSignificantIdentifier;
283
    }
284
    public void setMostSignificantIdentifier(String mostSignificantIdentifier) {
285
        this.mostSignificantIdentifier = mostSignificantIdentifier;
286
    }
287

    
288
    public TypedEntityReference<TaxonName> getStoredUnder() {
289
        return storedUnder;
290
    }
291
    public void setStoredUnder(TypedEntityReference<TaxonName> storedUnder) {
292
        this.storedUnder = storedUnder;
293
    }
294

    
295
    public String getOriginalLabelInfo() {
296
        return originalLabelInfo;
297
    }
298
    public void setOriginalLabelInfo(String originalLabelInfo) {
299
        this.originalLabelInfo = originalLabelInfo;
300
    }
301

    
302
    public String getExsiccatum() {
303
        return exsiccatum;
304
    }
305
    public void setExsiccatum(String exsiccatum) {
306
        this.exsiccatum = exsiccatum;
307
    }
308

    
309
    public String getCatalogNumber() {
310
        return catalogNumber;
311
    }
312
    public void setCatalogNumber(String catalogNumber) {
313
        this.catalogNumber = catalogNumber;
314
    }
315

    
316
    public String getBarcode() {
317
        return barcode;
318
    }
319
    public void setBarcode(String barcode) {
320
        this.barcode = barcode;
321
    }
322

    
323
    public String getPreservationMethod() {
324
        return preservationMethod;
325
    }
326
    public void setPreservationMethod(String preservationMethod) {
327
        this.preservationMethod = preservationMethod;
328
    }
329

    
330
    public CollectionDTO getCollection() {
331
        return collection;
332
    }
333
    public void setCollectioDTO(CollectionDTO collection) {
334
        this.collection = collection;
335
    }
336

    
337
    @Override
338
    protected void updateTreeDependantData() {
339
        // TODO DerivationTreeSummaryDTO should be updated here once it is refactored so that it can operate on dtos
340
    }
341
}
(9-9/44)