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 https://dev.e-taxonomy.eu/redmine/issues/3907
|
52
|
// ==================================================
|
53
|
|
54
|
statusSymbols = new HashMap<> ();
|
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.CASUAL().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.CASUAL().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
|
@Override
|
109
|
public CondensedDistribution createCondensedDistribution(Collection<Distribution> filteredDistributions,
|
110
|
List<Language> langs) {
|
111
|
|
112
|
//1. group by PresenceAbsenceTerms
|
113
|
Map<PresenceAbsenceTerm, Collection<NamedArea>> areasByStatus = new HashMap<>();
|
114
|
for(Distribution d : filteredDistributions) {
|
115
|
PresenceAbsenceTerm status = d.getStatus();
|
116
|
if(status == null) {
|
117
|
continue;
|
118
|
}
|
119
|
if(!areasByStatus.containsKey(status)) {
|
120
|
areasByStatus.put(status, new HashSet<>());
|
121
|
}
|
122
|
areasByStatus.get(status).add(d.getArea());
|
123
|
}
|
124
|
|
125
|
//2. build the area hierarchy
|
126
|
for(PresenceAbsenceTerm status : areasByStatus.keySet()) {
|
127
|
|
128
|
Map<NamedArea, AreaNode> areaNodeMap = new HashMap<>();
|
129
|
|
130
|
for(NamedArea area : areasByStatus.get(status)) {
|
131
|
AreaNode node;
|
132
|
if(!areaNodeMap.containsKey(area)) {
|
133
|
// putting area into hierarchy as node
|
134
|
node = new AreaNode(area);
|
135
|
areaNodeMap.put(area, node);
|
136
|
} else {
|
137
|
// is parent of another and thus already has a node
|
138
|
node = areaNodeMap.get(area);
|
139
|
}
|
140
|
|
141
|
NamedArea parent = findParentIn(area, areasByStatus.get(status));
|
142
|
if(parent != null) {
|
143
|
AreaNode parentNode;
|
144
|
if(!areaNodeMap.containsKey(parent)) {
|
145
|
parentNode = new AreaNode(parent);
|
146
|
areaNodeMap.put(parent, parentNode);
|
147
|
} else {
|
148
|
parentNode = areaNodeMap.get(parent);
|
149
|
}
|
150
|
parentNode.addSubArea(node);
|
151
|
}
|
152
|
}
|
153
|
|
154
|
//3. find root nodes
|
155
|
Set<AreaNode>hierarchy = new HashSet<>();
|
156
|
for(AreaNode node : areaNodeMap.values()) {
|
157
|
if(!node.hasParent()) {
|
158
|
hierarchy.add(node);
|
159
|
}
|
160
|
}
|
161
|
|
162
|
//4. replace the area by the abbreviated representation and add symbols
|
163
|
for(AreaNode topLevelNode : hierarchy) {
|
164
|
|
165
|
StringBuilder areaStatusString = new StringBuilder();
|
166
|
|
167
|
String statusSymbol = statusSymbol(status);
|
168
|
areaStatusString.append(statusSymbol);
|
169
|
|
170
|
String areaLabel = topLevelNode.area.getPreferredRepresentation(langs).getAbbreviatedLabel();
|
171
|
areaStatusString.append(areaLabel);
|
172
|
|
173
|
if(!topLevelNode.subAreas.isEmpty()) {
|
174
|
areaStatusString.append('(');
|
175
|
subAreaLabels(langs, topLevelNode.subAreas, areaStatusString, statusSymbol, areaLabel);
|
176
|
areaStatusString.append(')');
|
177
|
}
|
178
|
|
179
|
if(isForeignStatus(status)) {
|
180
|
condensedDistribution.addForeignDistributionItem(status, areaStatusString.toString(), areaLabel);
|
181
|
} else {
|
182
|
condensedDistribution.addIndigenousDistributionItem(status, areaStatusString.toString(), areaLabel);
|
183
|
}
|
184
|
|
185
|
}
|
186
|
|
187
|
}
|
188
|
//5. order the condensedDistributions alphabetically
|
189
|
condensedDistribution.sortForeign();
|
190
|
condensedDistribution.sortIndigenous();
|
191
|
|
192
|
return condensedDistribution;
|
193
|
}
|
194
|
|
195
|
|
196
|
private boolean isForeignStatus(PresenceAbsenceTerm status) {
|
197
|
return foreignStatusUuids.contains(status.getUuid());
|
198
|
}
|
199
|
|
200
|
private void subAreaLabels(List<Language> langs, Collection<AreaNode> nodes, StringBuilder areaString,
|
201
|
String statusSymbol, String parentLabel) {
|
202
|
|
203
|
List<String> subAreaLabels = new ArrayList<String>();
|
204
|
|
205
|
for(AreaNode node : nodes) {
|
206
|
|
207
|
StringBuilder subAreaString = new StringBuilder();
|
208
|
|
209
|
subAreaString.append(statusSymbol);
|
210
|
|
211
|
String areaLabel = node.area.getPreferredRepresentation(langs).getAbbreviatedLabel();
|
212
|
if (replaceCommonAreaLabelStart){
|
213
|
String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""});
|
214
|
subAreaString.append(cleanSubAreaLabel);
|
215
|
}else{
|
216
|
subAreaString.append(areaLabel);
|
217
|
}
|
218
|
|
219
|
if(!node.subAreas.isEmpty()) {
|
220
|
subAreaString.append('(');
|
221
|
subAreaLabels(langs, node.subAreas, subAreaString, statusSymbol, areaLabel);
|
222
|
subAreaString.append(')');
|
223
|
}
|
224
|
|
225
|
subAreaLabels.add(subAreaString.toString());
|
226
|
}
|
227
|
Collections.sort(subAreaLabels);
|
228
|
areaString.append(StringUtils.join(subAreaLabels, " "));
|
229
|
}
|
230
|
}
|