Project

General

Profile

Download (9.36 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2015 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.ext.geo;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.Collections;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19
import java.util.UUID;
20

    
21
import org.apache.commons.lang.StringUtils;
22
import org.apache.log4j.Logger;
23

    
24
import eu.etaxonomy.cdm.api.service.dto.CondensedDistribution;
25
import eu.etaxonomy.cdm.common.UTF8;
26
import eu.etaxonomy.cdm.model.common.Language;
27
import eu.etaxonomy.cdm.model.description.Distribution;
28
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
29
import eu.etaxonomy.cdm.model.location.NamedArea;
30

    
31
/**
32
 * @author a.kohlbecker
33
 \* @since Jun 24, 2015
34
 *
35
 */
36
public class EuroPlusMedCondensedDistributionComposer extends CondensedDistributionComposerBase {
37

    
38
    @SuppressWarnings("unused")
39
    private static final Logger logger = Logger.getLogger(EuroPlusMedCondensedDistributionComposer.class);
40

    
41
    private final CondensedDistribution condensedDistribution;
42

    
43
    private static Set<UUID> foreignStatusUuids;
44

    
45
    // these status uuids are special for EuroPlusMed and might also be used
46
    private final static UUID REPORTED_IN_ERROR_UUID =  UUID.fromString("38604788-cf05-4607-b155-86db456f7680");
47

    
48
    static {
49

    
50
        // ==================================================
51
        // Mapping as defined in ticket http://dev.e-taxonomy.eu/trac/ticket/3907
52
        // ==================================================
53

    
54
        statusSymbols = new HashMap<UUID, String> ();
55
        // â—� endemic (U+25CF BLACK CIRCLE)
56
        statusSymbols.put(PresenceAbsenceTerm.ENDEMIC_FOR_THE_RELEVANT_AREA().getUuid(), "\u25CF");
57

    
58
        // Lu native (incl. archaeophytes) TODO status archaeophytes?
59
        statusSymbols.put(PresenceAbsenceTerm.NATIVE().getUuid(), "");
60
        statusSymbols.put(PresenceAbsenceTerm.NATIVE_FORMERLY_NATIVE().getUuid(), "");  //wanted? differs from default "ne"
61

    
62
        // ?Lu doubtfully present (U+3F QUESTION MARK)
63
        statusSymbols.put(PresenceAbsenceTerm.INTRODUCED_PRESENCE_QUESTIONABLE().getUuid(), "?");
64
        statusSymbols.put(PresenceAbsenceTerm.NATIVE_PRESENCE_QUESTIONABLE().getUuid(), "?");
65
        statusSymbols.put(PresenceAbsenceTerm.PRESENT_DOUBTFULLY().getUuid(), "?");
66

    
67
        // dLu doubtfully native
68
        statusSymbols.put(PresenceAbsenceTerm.NATIVE_DOUBTFULLY_NATIVE().getUuid(), "d");
69

    
70
        // -Lu absent but reported in error (U+2D HYPHEN-MINUS)
71
        statusSymbols.put(PresenceAbsenceTerm.INTRODUCED_REPORTED_IN_ERROR().getUuid(), UTF8.EN_DASH.toString());
72
        statusSymbols.put(PresenceAbsenceTerm.NATIVE_REPORTED_IN_ERROR().getUuid(), UTF8.EN_DASH.toString());
73
        statusSymbols.put(REPORTED_IN_ERROR_UUID, "-");
74

    
75
        // †Lu (presumably) extinct (U+2020 DAGGER)
76
        // no such status in database!!!
77

    
78
        // [Lu] introduced (casual or naturalized) =  introduced, introduced: naturalized
79
        statusSymbols.put(PresenceAbsenceTerm.INTRODUCED().getUuid(), ""); //wanted? differs from default "i"
80

    
81
        // [aLu] casual alien = introduced: adventitious (casual)
82
        statusSymbols.put(PresenceAbsenceTerm.INTRODUCED_ADVENTITIOUS().getUuid(), "a");
83

    
84
        // [cLu] cultivated
85
        statusSymbols.put(PresenceAbsenceTerm.CULTIVATED() .getUuid(), "c");
86
        statusSymbols.put(PresenceAbsenceTerm.INTRODUCED_CULTIVATED().getUuid(), "c");
87

    
88
        // [nLu] naturalized
89
        statusSymbols.put(PresenceAbsenceTerm.NATURALISED().getUuid(), "n");
90
        statusSymbols.put(PresenceAbsenceTerm.NATURALISED().getUuid(), "n");
91

    
92
        foreignStatusUuids = new HashSet<UUID>();
93
        foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED().getUuid());
94
        foreignStatusUuids.add(PresenceAbsenceTerm.NATURALISED().getUuid());
95
        foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_ADVENTITIOUS().getUuid());
96
        foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_CULTIVATED().getUuid());
97
        foreignStatusUuids.add(PresenceAbsenceTerm.NATURALISED().getUuid());
98
        foreignStatusUuids.add(PresenceAbsenceTerm.CULTIVATED().getUuid());
99

    
100
    }
101

    
102
    public EuroPlusMedCondensedDistributionComposer() {
103
        super();
104
        replaceCommonAreaLabelStart = true;
105
        condensedDistribution = new CondensedDistribution();
106
    }
107

    
108
    /**
109
     * {@inheritDoc}
110
     * @return
111
     */
112
    @Override
113
    public CondensedDistribution createCondensedDistribution(Collection<Distribution> filteredDistributions,
114
            List<Language> langs) {
115

    
116
        //1. group by PresenceAbsenceTerms
117
        Map<PresenceAbsenceTerm, Collection<NamedArea>> areasByStatus = new HashMap<PresenceAbsenceTerm, Collection<NamedArea>>();
118
        for(Distribution d : filteredDistributions) {
119
            PresenceAbsenceTerm status = d.getStatus();
120
            if(status == null) {
121
                continue;
122
            }
123
            if(!areasByStatus.containsKey(status)) {
124
                areasByStatus.put(status, new HashSet<NamedArea>());
125
            }
126
            areasByStatus.get(status).add(d.getArea());
127
        }
128

    
129
        //2. build the area hierarchy
130
        for(PresenceAbsenceTerm status : areasByStatus.keySet()) {
131

    
132
            Map<NamedArea, AreaNode> areaNodeMap = new HashMap<NamedArea, AreaNode>();
133

    
134
            for(NamedArea area : areasByStatus.get(status)) {
135
                AreaNode node;
136
                if(!areaNodeMap.containsKey(area)) {
137
                    // putting area into hierarchy as node
138
                    node = new AreaNode(area);
139
                    areaNodeMap.put(area, node);
140
                } else {
141
                    //  is parent of another and thus already has a node
142
                    node = areaNodeMap.get(area);
143
                }
144

    
145
                NamedArea parent = findParentIn(area, areasByStatus.get(status));
146
                if(parent != null) {
147
                    AreaNode parentNode;
148
                    if(!areaNodeMap.containsKey(parent)) {
149
                        parentNode = new AreaNode(parent);
150
                        areaNodeMap.put(parent, parentNode);
151
                    } else {
152
                        parentNode = areaNodeMap.get(parent);
153
                    }
154
                    parentNode.addSubArea(node);
155
                }
156
            }
157

    
158
            //3. find root nodes
159
            Set<AreaNode>hierarchy = new HashSet<>();
160
            for(AreaNode node : areaNodeMap.values()) {
161
                if(!node.hasParent()) {
162
                    hierarchy.add(node);
163
                }
164
            }
165

    
166
            //4. replace the area by the abbreviated representation and add symbols
167
            for(AreaNode topLevelNode : hierarchy) {
168

    
169
                StringBuilder areaStatusString = new StringBuilder();
170

    
171
                String statusSymbol = statusSymbol(status);
172
                areaStatusString.append(statusSymbol);
173

    
174
                String areaLabel = topLevelNode.area.getPreferredRepresentation(langs).getAbbreviatedLabel();
175
                areaStatusString.append(areaLabel);
176

    
177
                if(!topLevelNode.subAreas.isEmpty()) {
178
                    areaStatusString.append('(');
179
                    subAreaLabels(langs, topLevelNode.subAreas, areaStatusString, statusSymbol, areaLabel);
180
                    areaStatusString.append(')');
181
                }
182

    
183
                if(isForeignStatus(status)) {
184
                    condensedDistribution.addForeignDistributionItem(status, areaStatusString.toString(), areaLabel);
185
                } else {
186
                    condensedDistribution.addIndigenousDistributionItem(status, areaStatusString.toString(), areaLabel);
187
                }
188

    
189
            }
190

    
191
        }
192
        //5. order the condensedDistributions alphabetically
193
        condensedDistribution.sortForeign();
194
        condensedDistribution.sortIndigenous();
195

    
196
        return condensedDistribution;
197
    }
198

    
199

    
200
    private boolean isForeignStatus(PresenceAbsenceTerm status) {
201
        return foreignStatusUuids.contains(status.getUuid());
202
    }
203

    
204
    /**
205
     * @param langs
206
     * @param node
207
     * @param areaString
208
     * @param statusSymbol
209
     */
210
    private void subAreaLabels(List<Language> langs, Collection<AreaNode> nodes, StringBuilder areaString, String statusSymbol, String parentLabel) {
211

    
212
        List<String> subAreaLabels = new ArrayList<String>();
213

    
214
        for(AreaNode node : nodes) {
215

    
216
            StringBuilder subAreaString = new StringBuilder();
217

    
218
            subAreaString.append(statusSymbol);
219

    
220
            String areaLabel = node.area.getPreferredRepresentation(langs).getAbbreviatedLabel();
221
            if (replaceCommonAreaLabelStart){
222
                String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""});
223
                subAreaString.append(cleanSubAreaLabel);
224
            }else{
225
                subAreaString.append(areaLabel);
226
            }
227

    
228
            if(!node.subAreas.isEmpty()) {
229
                subAreaString.append('(');
230
                subAreaLabels(langs, node.subAreas, subAreaString, statusSymbol, areaLabel);
231
                subAreaString.append(')');
232
            }
233

    
234
            subAreaLabels.add(subAreaString.toString());
235
        }
236
        Collections.sort(subAreaLabels);
237
        areaString.append(StringUtils.join(subAreaLabels, " "));
238

    
239
    }
240

    
241

    
242

    
243
}
(5-5/14)