Project

General

Profile

Download (11.7 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.stream.Collectors;
18

    
19
import org.joda.time.Partial;
20

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

    
29
public class FieldUnitDTO extends SpecimenOrObservationBaseDTO {
30

    
31
    private static final long serialVersionUID = 3981843956067273220L;
32

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

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

    
42

    
43
	private GatheringEventDTO gatheringEvent;
44

    
45
    private CollectionDTO collection;
46

    
47
    private String catalogNumber;
48

    
49
    private String barcode;
50

    
51
    private String preservationMethod;
52

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

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

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

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

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

    
118
        // --------------------------------------
119

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

    
141

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

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

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

    
157
    @Override
158
    public void updateTreeDependantData() {
159
        TreeLabels treeLabels = assembleLablesFromTree(true, true);
160
        setSummaryLabel(treeLabels.summaryLabel);
161
        setCollectionStatistics(treeLabels.collectionsStatistics);
162
    }
163

    
164
    /**
165
     * Walks the tree of sub-derivatives to collect the summary label and the collection statistics.
166
     * The latter lists all collections with the number of specimens which are involved in this tree.
167
     */
168
    public TreeLabels assembleLablesFromTree(boolean doSummaryLabel, boolean collectionsStatistics) {
169

    
170
        TreeLabels treeLabels = new TreeLabels();
171
        Map<CollectionDTO, List<String> > unitIdenfierLabelsByCollections = new HashMap<>();
172

    
173
        // TODO collectDerivatives(maxDepth)
174
        for(DerivedUnitDTO subDTO : collectDerivatives()) {
175
            CollectionDTO collectionDTO = subDTO.getCollection();
176
            if (collectionDTO != null) {
177
                //combine collection with identifier
178
                String identifier = subDTO.getMostSignificantIdentifier();
179
                if (identifier != null && collectionDTO.getCode()!=null) {
180
                    identifier = (collectionDTO.getCode()!=null?collectionDTO.getCode():"[no collection]")+" "+identifier;
181
                }
182
                if(!unitIdenfierLabelsByCollections.containsKey(collectionDTO)) {
183
                    unitIdenfierLabelsByCollections.put(collectionDTO, new ArrayList<>());
184
                }
185
                unitIdenfierLabelsByCollections.get(collectionDTO).add(identifier);
186
            }
187
        }
188

    
189
        if(doSummaryLabel) {
190
            String summaryLabel = this.label;
191
            List<String> derivativesAccessionNumbers = new ArrayList<>();
192
            for(List<String> labels : unitIdenfierLabelsByCollections.values()) {
193
                derivativesAccessionNumbers.addAll(labels);
194
            }
195
            derivativesAccessionNumbers = derivativesAccessionNumbers.stream().filter(s -> s != null).sorted().collect(Collectors.toList());
196
            if (!derivativesAccessionNumbers.isEmpty()) {
197
                summaryLabel += " (" + String.join(SEPARATOR_STRING, derivativesAccessionNumbers) +  ")";
198
            }
199
            treeLabels.summaryLabel = summaryLabel;
200
        }
201

    
202
        if(collectionsStatistics) {
203
            List<String> collectionStats = new ArrayList<>();
204
            for (CollectionDTO collectionDTO : unitIdenfierLabelsByCollections.keySet()) {
205
                int unitCount = unitIdenfierLabelsByCollections.get(collectionDTO).size();
206
                if (collectionDTO.getCode() != null) {
207
                    collectionStats.add(collectionDTO.getCode() + (unitCount > 1 ? "(" + unitCount + ")" : ""));
208
                }
209
            }
210
            Collections.sort(collectionStats);
211
            treeLabels.collectionsStatistics = String.join(SEPARATOR_STRING, collectionStats);
212
        }
213

    
214
        return treeLabels;
215
    }
216

    
217
    public String getCountry() {
218
        return country;
219
    }
220
    public void setCountry(String country) {
221
        this.country = country;
222
    }
223

    
224
    public String getCollectionStatistics() {
225
        return collectionsStatistics;
226
    }
227

    
228
    public void setCollectionStatistics(String collection) {
229
        this.collectionsStatistics = collection;
230
    }
231

    
232
    public String getCollectingString() {
233
        return collectingString;
234
    }
235
    public void setCollectingString(String collectingString) {
236
        this.collectingString = collectingString;
237
    }
238

    
239
    public Partial getDate() {
240
        return date;
241
    }
242
    public void setDate(Partial date) {
243
        this.date = date;
244
    }
245

    
246
    public boolean isHasType() {
247
        boolean hasType = collectDerivatives()
248
                .stream()
249
                .anyMatch(derivedUnitDTO -> derivedUnitDTO.getSpecimenTypeDesignations() != null && !derivedUnitDTO.getSpecimenTypeDesignations().isEmpty());
250
        return hasType;
251
    }
252

    
253
    public GatheringEventDTO getGatheringEvent() {
254
        return gatheringEvent;
255
    }
256
    public void setGatheringEvent(GatheringEventDTO gatheringEvent) {
257
        this.gatheringEvent = gatheringEvent;
258
    }
259

    
260
    public String getFieldNumber() {
261
        return fieldNumber;
262
    }
263

    
264
    public void setFieldNumber(String fieldNumber) {
265
        this.fieldNumber = fieldNumber;
266
    }
267

    
268
    public String getFieldNotes() {
269
        return fieldNotes;
270
    }
271

    
272
    public void setFieldNotes(String fieldNotes) {
273
        this.fieldNotes = fieldNotes;
274
    }
275

    
276
    public void setCollection(CollectionDTO collection) {
277
        this.collection = collection;
278
    }
279

    
280
    public String getCatalogNumber() {
281
        return catalogNumber;
282
    }
283

    
284
    public void setCatalogNumber(String catalogNumber) {
285
        this.catalogNumber = catalogNumber;
286
    }
287

    
288
    public String getBarcode() {
289
        return barcode;
290
    }
291

    
292
    public void setBarcode(String barcode) {
293
        this.barcode = barcode;
294
    }
295

    
296
    public String getPreservationMethod() {
297
        return preservationMethod;
298
    }
299

    
300
    public void setPreservationMethod(String preservationMethod) {
301
        this.preservationMethod = preservationMethod;
302
    }
303

    
304
    /**
305
     * @return the collection
306
     *
307
     * @deprecated TODO remove as it only duplicates the information contained in the collectionDTO
308
     */
309
    @Deprecated
310
    public String getCollectionCode() {
311
        if (collection != null){
312
            return collection.getCode();
313
        } else {
314
            return null;
315
        }
316
    }
317

    
318
    /**
319
     * @return the collection
320
     */
321
    public CollectionDTO getCollection() {
322
        return collection;
323
    }
324

    
325
    /**
326
     * @param collection the collection to set
327
     */
328
    public void setCollectioDTO(CollectionDTO collection) {
329
        this.collection = collection;
330
    }
331

    
332
    static class TreeLabels {
333
        String summaryLabel = null;
334
        String collectionsStatistics = null;
335
    }
336
}
(15-15/38)