Project

General

Profile

Download (6 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2021 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.strategy.cache.occurrence;
10

    
11
import java.util.Comparator;
12
import java.util.SortedSet;
13
import java.util.TreeSet;
14
import java.util.UUID;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.common.CdmUtils;
19
import eu.etaxonomy.cdm.model.agent.Institution;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.occurrence.Collection;
22
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
23
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
24
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
25

    
26
/**
27
 * Formatting class for DerivedUnits.
28
 *
29
 * Note: this class is mostly a copy from the orignal class DerivedUnitFacadeCacheStrategy
30
 *       in cdmlib-service. (#9678)
31
 *
32
 * @author a.mueller
33
 * @since 18.06.2021
34
 */
35
public class DerivedUnitDefaultCacheStrategy
36
        extends OccurrenceCacheStrategyBase<DerivedUnit>{
37

    
38
    private static final long serialVersionUID = -3905309456296895952L;
39
    @SuppressWarnings("unused")
40
    private static final Logger logger = Logger.getLogger(DerivedUnitDefaultCacheStrategy.class);
41

    
42
    private static final UUID uuid = UUID.fromString("2746bca3-f58a-4b5d-b4ec-ece9785731fe");
43

    
44
    @Override
45
    protected UUID getUuid() {return uuid;}
46

    
47
    private boolean skipFieldUnit = false;
48
    private boolean addTrailingDot = true;
49

    
50
    private static final FieldUnitDefaultCacheStrategy fieldUnitCacheStrategy
51
        = FieldUnitDefaultCacheStrategy.NewInstance(false, false);
52

    
53
    @Override
54
    protected String doGetTitleCache(DerivedUnit specimen) {
55
        String result = "";
56

    
57
        SortedSet<FieldUnit> fieldUnits = getFieldUnits(specimen);
58
        if(!skipFieldUnit){
59
            for (FieldUnit fieldUnit : fieldUnits){
60
                result = CdmUtils.concat("; ", fieldUnitCacheStrategy.getTitleCache(fieldUnit, true));
61
            }
62
        }
63

    
64
        // NOTE: regarding the string representations of MediaTypes, see https://dev.e-taxonomy.eu/redmine/issues/7608
65

    
66
        //exsiccatum
67
        String exsiccatum = null;
68
        exsiccatum = specimen.getExsiccatum();
69
        result = CdmUtils.concat("; ", result, exsiccatum);
70

    
71
        //Herbarium & identifier
72
        String barcode = getSpecimenLabel(specimen);
73
        if (isNotBlank(barcode)) {
74
            result = (result + " (" +  barcode + ")").trim();
75
        }
76

    
77
        //result
78
        if(!skipFieldUnit){
79
            for (FieldUnit fieldUnit : fieldUnits){
80
                result = addPlantDescription(result, fieldUnit);
81
            }
82
        }
83

    
84

    
85
        if (addTrailingDot){
86
            result = CdmUtils.addTrailingDotIfNotExists(result);
87
        }
88

    
89
        return result;
90
    }
91

    
92
    private SortedSet<FieldUnit> getFieldUnits(DerivedUnit specimen) {
93
        SortedSet<FieldUnit> result = new TreeSet<>(getFieldUnitComparator());
94
        if (specimen.getOriginals() != null){
95
            for (SpecimenOrObservationBase<?> sob : specimen.getOriginals()){
96
                if (sob.isInstanceOf(FieldUnit.class)){
97
                    result.add(CdmBase.deproxy(sob, FieldUnit.class));
98
                }else{
99
                    result.addAll(getFieldUnits(CdmBase.deproxy(sob, DerivedUnit.class)));
100
                }
101
            }
102
        }
103
        return result;
104
    }
105

    
106
    //very preliminary comparator to guarantee a defined order
107
    private Comparator<FieldUnit> getFieldUnitComparator() {
108
        return new Comparator<FieldUnit>() {
109

    
110
            @Override
111
            public int compare(FieldUnit o1, FieldUnit o2) {
112
                return o1.getUuid().compareTo(o2.getUuid());
113
            }
114
        };
115
    }
116

    
117
    /**
118
     * Produces the collection barcode which is the combination of the collection code and
119
     * accession number.
120
     *
121
     * @param result
122
     * @param derivedUnit
123
     * @return
124
     */
125
    public String getSpecimenLabel(DerivedUnit derivedUnit) {
126
        String code = getCode(derivedUnit);
127
        String identifier = getUnitNumber(derivedUnit /*, code*/);
128
        String collectionData = CdmUtils.concat(" ", code, identifier);
129
        return collectionData;
130
    }
131

    
132

    
133
    /**
134
     * Computes the unit number which might be an accession number, barcode, catalogue number, ...
135
     * In future if the unit number starts with the same string as the barcode
136
     * it might be replaced.
137
     * @param derivedUnit the derived unit facade
138
     */
139
    private String getUnitNumber(DerivedUnit derivedUnit) {
140
        String result;
141
        if (isNotBlank(derivedUnit.getAccessionNumber())){
142
            result = derivedUnit.getAccessionNumber();
143
        }else if (isNotBlank(derivedUnit.getBarcode())){
144
            result = derivedUnit.getBarcode();
145
        }else{
146
            result = derivedUnit.getCatalogNumber();
147
        }
148
        String code = getCode(derivedUnit);
149
        if(result != null){
150
            result = result.trim();
151
            if(isNotBlank(code) && result.startsWith(code + " ")){
152
                result = result.replaceAll("^" + code + "\\s", "");
153
            }
154
        }
155
        return result;
156
    }
157

    
158
    private String getCode(DerivedUnit derivedUnit) {
159
        String code = "";
160
        if(derivedUnit.getCollection() != null){
161
            code = derivedUnit.getCollection().getCode();
162
            if (isBlank(code)){
163
                code = derivedUnit.getCollection().getName();
164
            }
165
            if (isBlank(code)){
166
                Institution institution = derivedUnit.getCollection().getInstitute();
167
                if (institution != null){
168
                    code = institution.getCode();
169
                }
170
                if (isBlank(code)){
171
                    Collection superCollection = derivedUnit.getCollection().getSuperCollection();
172
                    if (superCollection != null){
173
                        code = superCollection.getCode();
174
                    }
175
                }
176
            }
177
        }
178
        return code;
179
    }
180
}
(2-2/6)