Project

General

Profile

Download (9.06 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.vaadin.util.converter;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.Collections;
14
import java.util.Comparator;
15
import java.util.HashMap;
16
import java.util.LinkedHashMap;
17
import java.util.LinkedList;
18
import java.util.List;
19
import java.util.Map;
20

    
21
import eu.etaxonomy.cdm.model.common.Language;
22
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
23
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
24
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
25
import eu.etaxonomy.cdm.model.name.TaxonName;
26
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
27
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
28
import eu.etaxonomy.cdm.vaadin.model.EntityReference;
29
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
30

    
31
/**
32
 * Converts a collection of TypeDesignations, which should belong to the
33
 * same name of course, into a string representation.
34
 *
35
 * Order of TypeDesignations in the resulting string:
36
 *  Type, Holotype, Lectotype, Epitypes
37
 * @author a.kohlbecker
38
 * @since Mar 10, 2017
39
 *
40
 */
41
public class TypeDesignationConverter {
42

    
43

    
44
    private final String separator = ", ";
45

    
46
    private Collection<TypeDesignationBase> typeDesignations;
47
    private Map<TypeDesignationStatusBase<?>, Collection<EntityReference>> orderedStringsByType;
48
    private LinkedHashMap<String, Collection<EntityReference>> orderedRepresentations = new LinkedHashMap<>();
49
    private EntityReference typifiedName;
50

    
51
    private String finalString = null;
52

    
53

    
54

    
55
    /**
56
     * @param taxonName
57
     * @throws RegistrationValidationException
58
     *
59
     */
60
    public TypeDesignationConverter(Collection<TypeDesignationBase> typeDesignations) throws RegistrationValidationException {
61
        this.typeDesignations = typeDesignations;
62
        orderedStringsByType = new HashMap<>();
63
        typeDesignations.forEach(td -> putString(td.getTypeStatus(), new EntityReference(td.getId(), stringify(td))));
64
        orderedRepresentations = buildOrderedRepresentations();
65
        this.typifiedName = findTypifiedName();
66
    }
67

    
68
    private LinkedHashMap buildOrderedRepresentations(){
69

    
70
        // 1. order by SpecimenType, NameType
71

    
72
        // SpecimenTypes.........
73
        // Order SpecimenTypes by GatheringEvent
74

    
75
        List<TypeDesignationStatusBase<?>> keyList = new LinkedList<>(orderedStringsByType.keySet());
76

    
77
        Collections.sort(keyList, new Comparator<TypeDesignationStatusBase>() {
78
            @Override
79
            public int compare(TypeDesignationStatusBase o1, TypeDesignationStatusBase o2) {
80
                // fix inverted order of cdm terms by -1*
81
                return -1 * o1.compareTo(o2);
82
            }
83
        });
84
        // NameTypes.........
85

    
86
        keyList.forEach(key -> orderedRepresentations.put(getTypeDesignationStytusLabel(key), orderedStringsByType.get(key)));
87
        return orderedRepresentations;
88
    }
89

    
90
    public TypeDesignationConverter buildString(){
91

    
92
        StringBuilder sb = new StringBuilder();
93

    
94
        if(getTypifiedNameCache() != null){
95
            sb.append(getTypifiedNameCache()).append(": ");
96
        }
97

    
98
        List<String> keyList = new LinkedList<>(orderedRepresentations.keySet());
99

    
100

    
101
        keyList.forEach(key -> {
102
            sb.append(key).append(": ");
103
            orderedRepresentations.get(key).forEach(isAndString -> {
104
                sb.append(isAndString.getLabel());
105
                if(sb.length() > 0){
106
                    sb.append(separator);
107
                }
108
            });
109
        });
110

    
111
        finalString  = sb.toString();
112
        return this;
113
    }
114

    
115
    public Map<String, Collection<EntityReference>> getOrderedTypeDesignationRepresentations() {
116
        return orderedRepresentations;
117
    }
118

    
119
    /**
120
     * FIXME use the validation framework validators and to store the validation problems!!!
121
     *
122
     * @return
123
     * @throws RegistrationValidationException
124
     */
125
    private EntityReference findTypifiedName() throws RegistrationValidationException {
126

    
127
        List<String> problems = new ArrayList<>();
128

    
129
        TaxonName typifiedName = null;
130

    
131
        for(TypeDesignationBase<?> typeDesignation : typeDesignations){
132
            typeDesignation.getTypifiedNames();
133
            if(typeDesignation.getTypifiedNames().isEmpty()){
134

    
135
                //TODO instead throw RegistrationValidationException()
136
                problems.add("Missing typifiedName in " + typeDesignation.toString());
137
                continue;
138
            }
139
            if(typeDesignation.getTypifiedNames().size() > 1){
140
              //TODO instead throw RegistrationValidationException()
141
                problems.add("Multiple typifiedName in " + typeDesignation.toString());
142
                continue;
143
            }
144
            if(typifiedName == null){
145
                // remember
146
                typifiedName = typeDesignation.getTypifiedNames().iterator().next();
147
            } else {
148
                // compare
149
                TaxonName otherTypifiedName = typeDesignation.getTypifiedNames().iterator().next();
150
                if(typifiedName.getId() != otherTypifiedName.getId()){
151
                  //TODO instead throw RegistrationValidationException()
152
                    problems.add("Multiple typifiedName in " + typeDesignation.toString());
153
                }
154
            }
155

    
156
        }
157
        if(!problems.isEmpty()){
158
            // FIXME use the validation framework
159
            throw new RegistrationValidationException("Inconsistent type designations", problems);
160
        }
161

    
162
        if(typifiedName != null){
163
            return new EntityReference(typifiedName.getId(), typifiedName.getTitleCache());
164
        }
165
        return null;
166
    }
167

    
168

    
169
    /**
170
     * @return the title cache of the typifying name or <code>null</code>
171
     */
172
    public String getTypifiedNameCache() {
173
        if(typifiedName != null){
174
            return typifiedName.getLabel();
175
        }
176
        return null;
177
    }
178

    
179
    /**
180
     * @return the title cache of the typifying name or <code>null</code>
181
     */
182
    public EntityReference getTypifiedName() {
183

    
184
       return typifiedName;
185

    
186
    }
187

    
188
    /**
189
     * @param key
190
     * @return
191
     */
192
    protected String getTypeDesignationStytusLabel(TypeDesignationStatusBase<?> key) {
193
        String typeLable;
194
        if(key.equals( SpecimenTypeDesignationStatus.TYPE())){
195
            typeLable = "Type";
196
        } else {
197
            typeLable = key.getPreferredRepresentation(Language.DEFAULT()).getLabel();
198
        }
199
        return typeLable;
200
    }
201

    
202
    /**
203
     * @param td
204
     * @return
205
     */
206
    private String stringify(TypeDesignationBase td) {
207

    
208
        if(td instanceof NameTypeDesignation){
209
            return stringify((NameTypeDesignation)td);
210
        } else {
211
            return stringify((SpecimenTypeDesignation)td);
212
        }
213
    }
214

    
215

    
216
    /**
217
     * @param td
218
     * @return
219
     */
220
    protected String stringify(NameTypeDesignation td) {
221

    
222
        StringBuffer sb = new StringBuffer();
223

    
224
        if(td.getTypeName() != null){
225
            sb.append(td.getTypeName().getTitleCache());
226
        }
227
        if(td.getCitation() != null){
228
            sb.append(" ").append(td.getCitation().getTitleCache());
229
            if(td.getCitationMicroReference() != null){
230
                sb.append(":").append(td.getCitationMicroReference());
231
            }
232
        }
233
        if(td.isNotDesignated()){
234
            sb.append(" not designated");
235
        }
236
        if(td.isRejectedType()){
237
            sb.append(" rejected");
238
        }
239
        if(td.isConservedType()){
240
            sb.append(" conserved");
241
        }
242
        return sb.toString();
243
    }
244

    
245
    /**
246
     * @param td
247
     * @return
248
     */
249
    private String stringify(SpecimenTypeDesignation td) {
250
        StringBuffer sb = new StringBuffer();
251

    
252
        if(td.getTypeSpecimen() != null){
253
            String nameTitleCache = td.getTypeSpecimen().getTitleCache();
254
            if(getTypifiedNameCache() != null){
255
                nameTitleCache = nameTitleCache.replace(getTypifiedNameCache(), "");
256
            }
257
            sb.append(nameTitleCache);
258
        }
259

    
260
        if(td.getCitation() != null){
261
            sb.append(" ").append(td.getCitation().getTitleCache());
262
            if(td.getCitationMicroReference() != null){
263
                sb.append(" :").append(td.getCitationMicroReference());
264
            }
265
        }
266
        if(td.isNotDesignated()){
267
            sb.append(" not designated");
268
        }
269

    
270
        return sb.toString();
271
    }
272

    
273
    private void putString(TypeDesignationStatusBase<?> status, EntityReference idAndString){
274
        // the cdm orderd term bases are ordered invers, fixing this for here
275
        if(status == null){
276
            status = SpecimenTypeDesignationStatus.TYPE();
277
        }
278
        if(!orderedStringsByType.containsKey(status)){
279
            orderedStringsByType.put(status, new ArrayList<EntityReference>());
280
        }
281
        orderedStringsByType.get(status).add(idAndString);
282
    }
283

    
284
    public String print(){
285
        return finalString;
286
    }
287
}
(4-4/5)