Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.ext.geo;
11

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

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

    
25
import eu.etaxonomy.cdm.api.service.dto.CondensedDistribution;
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
 * @date Jun 24, 2015
34
 *
35
 */
36
public class EuroPlusMedCondensedDistributionComposer implements ICondensedDistributionComposer {
37

    
38
    private static final Logger logger = Logger.getLogger(EuroPlusMedCondensedDistributionComposer.class);
39

    
40
    private final CondensedDistribution condensedDistribution;
41

    
42
    private static Map<UUID, String> statusSymbols;
43

    
44
    private static Set<UUID> foreignStatusUuids;
45

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

    
49
    static {
50

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

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

    
59
        // Lu native (incl. archaeophytes) TODO status archaeophytes?
60
        statusSymbols.put(PresenceAbsenceTerm.NATIVE().getUuid(), "");
61
        statusSymbols.put(PresenceAbsenceTerm.NATIVE_FORMERLY_NATIVE().getUuid(), "");
62

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

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

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

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

    
79
        // [Lu] introduced (casual or naturalized) =  introduced, introduced: naturalized
80
        statusSymbols.put(PresenceAbsenceTerm.INTRODUCED().getUuid(), "");
81

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

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

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

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

    
101
    }
102

    
103
    public EuroPlusMedCondensedDistributionComposer() {
104
        condensedDistribution = new CondensedDistribution();
105
    }
106

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

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

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

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

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

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

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

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

    
168
                StringBuilder areaStatusString = new StringBuilder();
169

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

    
173
                String areaLabel = node.area.getPreferredRepresentation(langs).getAbbreviatedLabel();
174
                areaStatusString.append(areaLabel);
175

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

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

    
188
            }
189

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

    
195
        return condensedDistribution;
196
    }
197

    
198
    /**
199
     * @param status
200
     * @return
201
     */
202
    private String statusSymbol(PresenceAbsenceTerm status) {
203
        if(status == null) {
204
            return "";
205
        }
206
        String symbol = statusSymbols.get(status.getUuid());
207
        if(symbol == null) {
208
            symbol = "";
209
        }
210
        return symbol;
211
    }
212

    
213
    private boolean isForeignStatus(PresenceAbsenceTerm status) {
214
        return foreignStatusUuids.contains(status.getUuid());
215
    }
216

    
217
    /**
218
     * @param langs
219
     * @param node
220
     * @param areaString
221
     * @param statusSymbol
222
     */
223
    private void subAreaLabels(List<Language> langs, Collection<AreaNode> nodes, StringBuilder areaString, String statusSymbol, String parentLabel) {
224

    
225
        List<String> subAreaLabels = new ArrayList<String>();
226

    
227
        for(AreaNode node : nodes) {
228

    
229
            StringBuilder subAreaString = new StringBuilder();
230

    
231
            subAreaString.append(statusSymbol);
232

    
233
            String areaLabel = node.area.getPreferredRepresentation(langs).getAbbreviatedLabel();
234
            String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""});
235
            subAreaString.append(cleanSubAreaLabel);
236

    
237
            if(!node.subAreas.isEmpty()) {
238
                subAreaString.append('(');
239
                subAreaLabels(langs, node.subAreas, subAreaString, statusSymbol, areaLabel);
240
                subAreaString.append(')');
241
            }
242

    
243
            subAreaLabels.add(subAreaString.toString());
244
        }
245
        Collections.sort(subAreaLabels);
246
        areaString.append(StringUtils.join(subAreaLabels, " "));
247

    
248
    }
249

    
250
    /**
251
     * Searches for the parent are of the area given as parameter in
252
     * the Collection of areas.
253
     *
254
     * @parent area
255
     *      The area whose parent area is to be searched
256
     * @param collection
257
     *      The areas to search in.
258
     *
259
     * @return
260
     *      Either the parent if it has been found or null.
261
     */
262
    private NamedArea findParentIn(NamedArea area, Collection<NamedArea> areas) {
263
        NamedArea parent = area.getPartOf();
264
        if(parent != null && areas.contains(parent)){
265
            return parent;
266
        }
267
        return null;
268
    }
269

    
270
    class AreaNode {
271

    
272
        private final NamedArea area;
273
        private AreaNode parent = null;
274
        private final Set<AreaNode> subAreas = new HashSet<AreaNode>();
275

    
276
        /**
277
         * @param area
278
         */
279
        public AreaNode(NamedArea area) {
280
            this.area = area;
281
        }
282

    
283
        public void addSubArea(AreaNode subArea) {
284
            subAreas.add(subArea);
285
            subArea.parent = this;
286
        }
287

    
288
        public AreaNode getParent() {
289
            return parent;
290
        }
291

    
292
        public boolean hasParent() {
293
            return getParent() != null;
294
        }
295

    
296
        public Collection<NamedArea> getSubareas() {
297
            Collection<NamedArea> areas = new HashSet<NamedArea>();
298
            for(AreaNode node : subAreas) {
299
                areas.add(node.area);
300
            }
301
            return areas;
302
        }
303
    }
304

    
305
}
(4-4/12)