Project

General

Profile

Download (5.5 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.LinkedList;
17
import java.util.List;
18
import java.util.Map;
19

    
20
import eu.etaxonomy.cdm.model.common.Language;
21
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
22
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
23
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
24
import eu.etaxonomy.cdm.model.name.TaxonName;
25
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
26
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
27

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

    
40

    
41
    private final String separator = ", ";
42

    
43
    private Collection<TypeDesignationBase> typeDesignations;
44
    private Map<TypeDesignationStatusBase<?>, Collection<String>> orderedStrings;
45

    
46
    private String finalString = null;
47

    
48
    private String typifiedNameCache = null;
49

    
50
    /**
51
     * @param taxonNameBase
52
     *
53
     */
54
    public TypeDesignationConverter(Collection<TypeDesignationBase> typeDesignations, TaxonName taxonNameBase) {
55
        this.typeDesignations = typeDesignations;
56
        orderedStrings = new HashMap<>(typeDesignations.size());
57
        if(taxonNameBase != null){
58
            this.typifiedNameCache = taxonNameBase.getTitleCache();
59
        }
60
    }
61

    
62
    private void putString(TypeDesignationStatusBase<?> status, String string){
63
        // the cdm orderd term bases are ordered invers, fixing this for here
64
        if(status == null){
65
            status = SpecimenTypeDesignationStatus.TYPE();
66
        }
67
        if(!orderedStrings.containsKey(status)){
68
            orderedStrings.put(status, new ArrayList<String>());
69
        }
70
        orderedStrings.get(status).add(string);
71
    }
72

    
73

    
74
    public TypeDesignationConverter buildString(){
75

    
76
        typeDesignations.forEach(td -> putString(td.getTypeStatus(), stringify(td)));
77

    
78
        StringBuilder sb = new StringBuilder();
79

    
80
        if(typifiedNameCache != null){
81
            sb.append(typifiedNameCache).append(": ");
82
        }
83
        List<TypeDesignationStatusBase<?>> keyList = new LinkedList<>(orderedStrings.keySet());
84

    
85
        Collections.sort(keyList, new Comparator<TypeDesignationStatusBase>() {
86
            @Override
87
            public int compare(TypeDesignationStatusBase o1, TypeDesignationStatusBase o2) {
88
                // fix inverted order of cdm terms by -1*
89
                return -1 * o1.compareTo(o2);
90
            }
91
        });
92

    
93
        keyList.forEach(key -> {
94
            if(key.equals( SpecimenTypeDesignationStatus.TYPE())){
95
                sb.append("Type");
96
            } else {
97
                sb.append(key.getPreferredRepresentation(Language.DEFAULT()));
98
            }
99
            sb.append(": ");
100
            orderedStrings.get(key).forEach(str -> {
101
                sb.append(str);
102
                if(sb.length() > 0){
103
                    sb.append(separator);
104
                }
105
            });
106
        });
107

    
108
        finalString  = sb.toString();
109
        return this;
110
    }
111

    
112
    /**
113
     * @param td
114
     * @return
115
     */
116
    private String stringify(TypeDesignationBase td) {
117

    
118
        if(td instanceof NameTypeDesignation){
119
            return stringify((NameTypeDesignation)td);
120
        } else {
121
            return stringify((SpecimenTypeDesignation)td);
122
        }
123
    }
124

    
125

    
126
    /**
127
     * @param td
128
     * @return
129
     */
130
    protected String stringify(NameTypeDesignation td) {
131

    
132
        StringBuffer sb = new StringBuffer();
133

    
134
        if(td.getTypeName() != null){
135
            sb.append(td.getTypeName().getTitleCache());
136
        }
137
        if(td.getCitation() != null){
138
            sb.append(" ").append(td.getCitation().getTitleCache());
139
            if(td.getCitationMicroReference() != null){
140
                sb.append(":").append(td.getCitationMicroReference());
141
            }
142
        }
143
        if(td.isNotDesignated()){
144
            sb.append(" not designated");
145
        }
146
        if(td.isRejectedType()){
147
            sb.append(" rejected");
148
        }
149
        if(td.isConservedType()){
150
            sb.append(" conserved");
151
        }
152
        return sb.toString();
153
    }
154

    
155
    /**
156
     * @param td
157
     * @return
158
     */
159
    private String stringify(SpecimenTypeDesignation td) {
160
        StringBuffer sb = new StringBuffer();
161

    
162
        if(td.getTypeSpecimen() != null){
163
            String nameTitleCache = td.getTypeSpecimen().getTitleCache();
164
            if(typifiedNameCache != null){
165
                nameTitleCache = nameTitleCache.replace(typifiedNameCache, "");
166
            }
167
            sb.append(nameTitleCache);
168
        }
169

    
170
        if(td.getCitation() != null){
171
            sb.append(" ").append(td.getCitation().getTitleCache());
172
            if(td.getCitationMicroReference() != null){
173
                sb.append(" :").append(td.getCitationMicroReference());
174
            }
175
        }
176
        if(td.isNotDesignated()){
177
            sb.append(" not designated");
178
        }
179

    
180
        return sb.toString();
181
    }
182

    
183
    public String print(){
184
        return finalString;
185
    }
186
}
(3-3/4)