Project

General

Profile

Download (12.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2016 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.Collections;
13
import java.util.EnumSet;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.stream.Collectors;
19

    
20
import org.joda.time.Partial;
21

    
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.model.agent.AgentBase;
24
import eu.etaxonomy.cdm.model.location.NamedArea;
25
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
26
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
27
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
28
import eu.etaxonomy.cdm.strategy.cache.common.IdentifiableEntityDefaultCacheStrategy;
29

    
30
public class FieldUnitDTO extends SpecimenOrObservationBaseDTO {
31

    
32
    private static final long serialVersionUID = 3981843956067273220L;
33

    
34
    private static final String SEPARATOR_STRING = ", ";
35

    
36
	private String country; // TODO remove this obsolete copy of gatheringEvent.timeperiod
37
	private Partial date; // TODO remove this obsolete copy of gatheringEvent.timeperiod
38
	private String collectingString;
39
	private String collectionsStatistics;
40
	private String fieldNumber;
41
	private String fieldNotes;
42

    
43

    
44
	private GatheringEventDTO gatheringEvent;
45

    
46
    private CollectionDTO collection;
47

    
48
    private String catalogNumber;
49

    
50
    private String barcode;
51

    
52
    private String preservationMethod;
53

    
54
	public static FieldUnitDTO fromEntity(FieldUnit entity){
55
        return FieldUnitDTO.fromEntity(entity, null);
56
	}
57

    
58
	/**
59
     * Factory method for the construction of a FieldUnitDTO.
60
     * <p>
61
     * The direct derivatives are added to the field {@link #getDerivatives() derivates}.
62
     *
63
     *
64
     * @param fieldUnit
65
     *     The FieldUnit entity to create a DTO for. Is null save.
66
     * @param maxDepth
67
     *     The max number of levels to walk into the derivation tree, <code>null</code> means unlimited.
68
     * @param typeIncludeFilter
69
     *     Set of SpecimenOrObservationType to be included into the collection of {@link #getDerivatives() derivative DTOs}
70
     */
71
    public static FieldUnitDTO fromEntity(FieldUnit entity, Integer maxDepth, EnumSet<SpecimenOrObservationType> typeIncludeFilter){
72
        if(entity == null) {
73
            return null;
74
        }
75
        return new FieldUnitDTO(entity, maxDepth, typeIncludeFilter);
76
    }
77

    
78
	/**
79
     * Factory method for the construction of a FieldUnitDTO.
80
     * <p>
81
     * The direct derivatives are added to the field {@link #getDerivatives() derivates}.
82
     *
83
     *
84
     * @param fieldUnit
85
     *     The FieldUnit entity to create a DTO for. Is null save.
86
     * @param typeIncludeFilter
87
     *     Set of SpecimenOrObservationType to be included into the collection of {@link #getDerivatives() derivative DTOs}
88
     * @deprecated use {@link #fromEntity(FieldUnit, Integer, EnumSet)}
89
     */
90
    @Deprecated
91
	public static FieldUnitDTO fromEntity(FieldUnit entity, EnumSet<SpecimenOrObservationType> typeIncludeFilter){
92
        return fromEntity(entity, null, typeIncludeFilter);
93
    }
94

    
95
	/**
96
     * The direct derivatives are added to the field {@link #getDerivatives() derivates}.
97
	 *
98
	 * @param fieldUnit
99
	 *     The FieldUnit entity to create a DTO for
100
	 * @param maxDepth
101
     *   The maximum number of derivation events levels up to which derivatives are to be collected.
102
     *   <code>null</code> means infinitely.
103
	 * @param typeIncludeFilter
104
	 *     Set of SpecimenOrObservationType to be included into the collection of {@link #getDerivatives() derivative DTOs}
105
	 */
106
    private FieldUnitDTO(FieldUnit fieldUnit, Integer maxDepth, EnumSet<SpecimenOrObservationType> typeIncludeFilter ) {
107
        super(fieldUnit);
108

    
109
        setFieldNotes(fieldUnit.getFieldNotes());
110
        setFieldNumber(fieldUnit.getFieldNumber());
111
        if(typeIncludeFilter == null) {
112
            typeIncludeFilter = EnumSet.allOf(SpecimenOrObservationType.class);
113
        }
114
        if (fieldUnit.getGatheringEvent() != null){
115
            gatheringEvent = GatheringEventDTO.newInstance(fieldUnit.getGatheringEvent());
116
        }
117
        setRecordBase(fieldUnit.getRecordBasis());
118

    
119
        // --------------------------------------
120

    
121
        if (fieldUnit.getGatheringEvent() != null) {
122
            GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
123
            // Country
124
            NamedArea country = gatheringEvent.getCountry();
125
            setCountry(country != null ? country.getLabel() : null);
126
            // Collection
127
            AgentBase<?> collector = gatheringEvent.getCollector();
128
            String fieldNumber = fieldUnit.getFieldNumber();
129
            String collectionString = "";
130
            if (collector != null || fieldNumber != null) {
131
                collectionString += collector != null ? collector : "";
132
                if (!collectionString.isEmpty()) {
133
                    collectionString += " ";
134
                }
135
                collectionString += (fieldNumber != null ? fieldNumber : "");
136
                collectionString = collectionString.trim();
137
            }
138
            setCollectingString(collectionString);
139
            setDate(gatheringEvent.getGatheringDate());
140
        }
141

    
142

    
143
        // assemble derivate data DTO
144
        DerivationTreeSummaryDTO derivateDataDTO = DerivationTreeSummaryDTO.fromEntity(fieldUnit, null);
145
        setDerivationTreeSummary(derivateDataDTO);
146

    
147
        // assemble citation
148
        String summaryLabel = fieldUnit.getTitleCache();
149
        if((CdmUtils.isBlank(summaryLabel) || summaryLabel.equals(IdentifiableEntityDefaultCacheStrategy.TITLE_CACHE_GENERATION_NOT_IMPLEMENTED))
150
                && !fieldUnit.isProtectedTitleCache()){
151
            fieldUnit.setTitleCache(null);
152
            summaryLabel = fieldUnit.getTitleCache();
153
        }
154

    
155
        addAllDerivatives(assembleDerivatives(fieldUnit, maxDepth, typeIncludeFilter));
156
    }
157

    
158
    @Override
159
    public void updateTreeDependantData(Set<DerivedUnitDTO> derivatives) {
160
        TreeLabels treeLabels = assembleLablesFromTree(true, true);
161
        setSummaryLabel(treeLabels.summaryLabel);
162
        setCollectionStatistics(treeLabels.collectionsStatistics);
163
        for (DerivedUnitDTO derivative: derivatives) {
164
			this.setHasDna(this.isHasDna() || derivative.isHasDna());
165
			this.setHasDetailImage(this.isHasDetailImage() || derivative.isHasDetailImage());
166
			this.setHasSpecimenScan(isHasSpecimenScan()|| derivative.isHasSpecimenScan());
167
			this.setHasCharacterData(isHasCharacterData()||derivative.isHasCharacterData());
168
		}
169
    }
170

    
171
    /**
172
     * Walks the tree of sub-derivatives to collect the summary label and the collection statistics.
173
     * The latter lists all collections with the number of specimens which are involved in this tree.
174
     */
175
    public TreeLabels assembleLablesFromTree(boolean doSummaryLabel, boolean collectionsStatistics) {
176

    
177
        TreeLabels treeLabels = new TreeLabels();
178
        Map<CollectionDTO, List<String> > unitIdenfierLabelsByCollections = new HashMap<>();
179

    
180
        // TODO collectDerivatives(maxDepth)
181
        for(DerivedUnitDTO subDTO : collectDerivatives()) {
182
            CollectionDTO collectionDTO = subDTO.getCollection();
183
            if (collectionDTO != null) {
184
                //combine collection with identifier
185
                String identifier = subDTO.getMostSignificantIdentifier();
186
                if (identifier != null && collectionDTO.getCode()!=null) {
187
                    identifier = (collectionDTO.getCode()!=null?collectionDTO.getCode():"[no collection]")+" "+identifier;
188
                }
189
                if(!unitIdenfierLabelsByCollections.containsKey(collectionDTO)) {
190
                    unitIdenfierLabelsByCollections.put(collectionDTO, new ArrayList<>());
191
                }
192
                unitIdenfierLabelsByCollections.get(collectionDTO).add(identifier);
193
            }
194
        }
195

    
196
        if(doSummaryLabel) {
197
            String summaryLabel = this.label;
198
            List<String> derivativesAccessionNumbers = new ArrayList<>();
199
            for(List<String> labels : unitIdenfierLabelsByCollections.values()) {
200
                derivativesAccessionNumbers.addAll(labels);
201
            }
202
            derivativesAccessionNumbers = derivativesAccessionNumbers.stream().filter(s -> s != null).sorted().collect(Collectors.toList());
203
            if (!derivativesAccessionNumbers.isEmpty()) {
204
                summaryLabel += " (" + String.join(SEPARATOR_STRING, derivativesAccessionNumbers) +  ")";
205
            }
206
            treeLabels.summaryLabel = summaryLabel;
207
        }
208

    
209
        if(collectionsStatistics) {
210
            List<String> collectionStats = new ArrayList<>();
211
            for (CollectionDTO collectionDTO : unitIdenfierLabelsByCollections.keySet()) {
212
                int unitCount = unitIdenfierLabelsByCollections.get(collectionDTO).size();
213
                if (collectionDTO.getCode() != null) {
214
                    collectionStats.add(collectionDTO.getCode() + (unitCount > 1 ? "(" + unitCount + ")" : ""));
215
                }
216
            }
217
            Collections.sort(collectionStats);
218
            treeLabels.collectionsStatistics = String.join(SEPARATOR_STRING, collectionStats);
219
        }
220

    
221
        return treeLabels;
222
    }
223

    
224
    public String getCountry() {
225
        return country;
226
    }
227
    public void setCountry(String country) {
228
        this.country = country;
229
    }
230

    
231
    public String getCollectionStatistics() {
232
        return collectionsStatistics;
233
    }
234

    
235
    public void setCollectionStatistics(String collection) {
236
        this.collectionsStatistics = collection;
237
    }
238

    
239
    public String getCollectingString() {
240
        return collectingString;
241
    }
242
    public void setCollectingString(String collectingString) {
243
        this.collectingString = collectingString;
244
    }
245

    
246
    public Partial getDate() {
247
        return date;
248
    }
249
    public void setDate(Partial date) {
250
        this.date = date;
251
    }
252

    
253
    public boolean isHasType() {
254
        boolean hasType = collectDerivatives()
255
                .stream()
256
                .anyMatch(derivedUnitDTO -> derivedUnitDTO.getSpecimenTypeDesignations() != null && !derivedUnitDTO.getSpecimenTypeDesignations().isEmpty());
257
        return hasType;
258
    }
259

    
260
    public GatheringEventDTO getGatheringEvent() {
261
        return gatheringEvent;
262
    }
263
    public void setGatheringEvent(GatheringEventDTO gatheringEvent) {
264
        this.gatheringEvent = gatheringEvent;
265
    }
266

    
267
    public String getFieldNumber() {
268
        return fieldNumber;
269
    }
270

    
271
    public void setFieldNumber(String fieldNumber) {
272
        this.fieldNumber = fieldNumber;
273
    }
274

    
275
    public String getFieldNotes() {
276
        return fieldNotes;
277
    }
278

    
279
    public void setFieldNotes(String fieldNotes) {
280
        this.fieldNotes = fieldNotes;
281
    }
282

    
283
    public void setCollection(CollectionDTO collection) {
284
        this.collection = collection;
285
    }
286

    
287
    public String getCatalogNumber() {
288
        return catalogNumber;
289
    }
290

    
291
    public void setCatalogNumber(String catalogNumber) {
292
        this.catalogNumber = catalogNumber;
293
    }
294

    
295
    public String getBarcode() {
296
        return barcode;
297
    }
298

    
299
    public void setBarcode(String barcode) {
300
        this.barcode = barcode;
301
    }
302

    
303
    public String getPreservationMethod() {
304
        return preservationMethod;
305
    }
306

    
307
    public void setPreservationMethod(String preservationMethod) {
308
        this.preservationMethod = preservationMethod;
309
    }
310

    
311
    /**
312
     * @return the collection
313
     *
314
     * @deprecated TODO remove as it only duplicates the information contained in the collectionDTO
315
     */
316
    @Deprecated
317
    public String getCollectionCode() {
318
        if (collection != null){
319
            return collection.getCode();
320
        } else {
321
            return null;
322
        }
323
    }
324

    
325
    /**
326
     * @return the collection
327
     */
328
    public CollectionDTO getCollection() {
329
        return collection;
330
    }
331

    
332
    /**
333
     * @param collection the collection to set
334
     */
335
    public void setCollectioDTO(CollectionDTO collection) {
336
        this.collection = collection;
337
    }
338

    
339
    static class TreeLabels {
340
        String summaryLabel = null;
341
        String collectionsStatistics = null;
342
    }
343
}
(20-20/47)