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 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(), "");
|
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(), "-");
|
72
|
statusSymbols.put(PresenceAbsenceTerm.NATIVE_REPORTED_IN_ERROR().getUuid(), "-");
|
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(), "");
|
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.INTRODUCED_NATURALIZED().getUuid(), "n");
|
91
|
|
92
|
foreignStatusUuids = new HashSet<UUID>();
|
93
|
foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED().getUuid());
|
94
|
foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_NATURALIZED().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
|
condensedDistribution = new CondensedDistribution();
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* {@inheritDoc}
|
108
|
* @return
|
109
|
*/
|
110
|
@Override
|
111
|
public CondensedDistribution createCondensedDistribution(Collection<Distribution> filteredDistributions,
|
112
|
List<Language> langs) {
|
113
|
|
114
|
//1. order by PresenceAbsenceTerms
|
115
|
Map<PresenceAbsenceTerm, Collection<NamedArea>> byStatus = new HashMap<PresenceAbsenceTerm, Collection<NamedArea>>();
|
116
|
for(Distribution d : filteredDistributions) {
|
117
|
PresenceAbsenceTerm status = d.getStatus();
|
118
|
if(status == null) {
|
119
|
continue;
|
120
|
}
|
121
|
if(!byStatus.containsKey(status)) {
|
122
|
byStatus.put(status, new HashSet<NamedArea>());
|
123
|
}
|
124
|
byStatus.get(status).add(d.getArea());
|
125
|
}
|
126
|
|
127
|
//2. build the area hierarchy
|
128
|
for(PresenceAbsenceTerm status : byStatus.keySet()) {
|
129
|
|
130
|
Map<NamedArea, AreaNode> areaNodeMap = new HashMap<NamedArea, AreaNode>();
|
131
|
|
132
|
for(NamedArea area : byStatus.get(status)) {
|
133
|
AreaNode node;
|
134
|
if(!areaNodeMap.containsKey(area)) {
|
135
|
// putting area into hierarchy as node
|
136
|
node = new AreaNode(area);
|
137
|
areaNodeMap.put(area, node);
|
138
|
} else {
|
139
|
// is parent of another and thus already has a node
|
140
|
node = areaNodeMap.get(area);
|
141
|
}
|
142
|
|
143
|
NamedArea parent = findParentIn(area, byStatus.get(status));
|
144
|
if(parent != null) {
|
145
|
AreaNode parentNode;
|
146
|
if(!areaNodeMap.containsKey(parent)) {
|
147
|
parentNode = new AreaNode(parent);
|
148
|
areaNodeMap.put(parent, parentNode);
|
149
|
} else {
|
150
|
parentNode = areaNodeMap.get(parent);
|
151
|
}
|
152
|
parentNode.addSubArea(node);
|
153
|
}
|
154
|
}
|
155
|
|
156
|
//3. find root nodes
|
157
|
Set<AreaNode>hierarchy = new HashSet<AreaNode>();
|
158
|
for(AreaNode node : areaNodeMap.values()) {
|
159
|
if(!node.hasParent()) {
|
160
|
hierarchy.add(node);
|
161
|
}
|
162
|
}
|
163
|
|
164
|
//4. replace the area by the abbreviated representation and add symbols
|
165
|
for(AreaNode node : hierarchy) {
|
166
|
|
167
|
StringBuilder areaStatusString = new StringBuilder();
|
168
|
|
169
|
String statusSymbol = statusSymbol(status);
|
170
|
areaStatusString.append(statusSymbol);
|
171
|
|
172
|
String areaLabel = node.area.getPreferredRepresentation(langs).getAbbreviatedLabel();
|
173
|
areaStatusString.append(areaLabel);
|
174
|
|
175
|
if(!node.subAreas.isEmpty()) {
|
176
|
areaStatusString.append('(');
|
177
|
subAreaLabels(langs, node.subAreas, areaStatusString, statusSymbol, areaLabel);
|
178
|
areaStatusString.append(')');
|
179
|
}
|
180
|
|
181
|
if(isForeignStatus(status)) {
|
182
|
condensedDistribution.addForeignDistributionItem(status, areaStatusString.toString(), areaLabel);
|
183
|
} else {
|
184
|
condensedDistribution.addIndigenousDistributionItem(status, areaStatusString.toString(), areaLabel);
|
185
|
}
|
186
|
|
187
|
}
|
188
|
|
189
|
}
|
190
|
//5. order the condensedDistributions alphabetically
|
191
|
condensedDistribution.sortForeign();
|
192
|
condensedDistribution.sortIndigenous();
|
193
|
|
194
|
return condensedDistribution;
|
195
|
}
|
196
|
|
197
|
|
198
|
private boolean isForeignStatus(PresenceAbsenceTerm status) {
|
199
|
return foreignStatusUuids.contains(status.getUuid());
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* @param langs
|
204
|
* @param node
|
205
|
* @param areaString
|
206
|
* @param statusSymbol
|
207
|
*/
|
208
|
private void subAreaLabels(List<Language> langs, Collection<AreaNode> nodes, StringBuilder areaString, String statusSymbol, String parentLabel) {
|
209
|
|
210
|
List<String> subAreaLabels = new ArrayList<String>();
|
211
|
|
212
|
for(AreaNode node : nodes) {
|
213
|
|
214
|
StringBuilder subAreaString = new StringBuilder();
|
215
|
|
216
|
subAreaString.append(statusSymbol);
|
217
|
|
218
|
String areaLabel = node.area.getPreferredRepresentation(langs).getAbbreviatedLabel();
|
219
|
String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""});
|
220
|
subAreaString.append(cleanSubAreaLabel);
|
221
|
|
222
|
if(!node.subAreas.isEmpty()) {
|
223
|
subAreaString.append('(');
|
224
|
subAreaLabels(langs, node.subAreas, subAreaString, statusSymbol, areaLabel);
|
225
|
subAreaString.append(')');
|
226
|
}
|
227
|
|
228
|
subAreaLabels.add(subAreaString.toString());
|
229
|
}
|
230
|
Collections.sort(subAreaLabels);
|
231
|
areaString.append(StringUtils.join(subAreaLabels, " "));
|
232
|
|
233
|
}
|
234
|
|
235
|
/**
|
236
|
* Searches for the parent are of the area given as parameter in
|
237
|
* the Collection of areas.
|
238
|
*
|
239
|
* @parent area
|
240
|
* The area whose parent area is to be searched
|
241
|
* @param collection
|
242
|
* The areas to search in.
|
243
|
*
|
244
|
* @return
|
245
|
* Either the parent if it has been found or null.
|
246
|
*/
|
247
|
private NamedArea findParentIn(NamedArea area, Collection<NamedArea> areas) {
|
248
|
NamedArea parent = area.getPartOf();
|
249
|
if(parent != null && areas.contains(parent)){
|
250
|
return parent;
|
251
|
}
|
252
|
return null;
|
253
|
}
|
254
|
|
255
|
class AreaNode {
|
256
|
|
257
|
private final NamedArea area;
|
258
|
private AreaNode parent = null;
|
259
|
private final Set<AreaNode> subAreas = new HashSet<AreaNode>();
|
260
|
|
261
|
/**
|
262
|
* @param area
|
263
|
*/
|
264
|
public AreaNode(NamedArea area) {
|
265
|
this.area = area;
|
266
|
}
|
267
|
|
268
|
public void addSubArea(AreaNode subArea) {
|
269
|
subAreas.add(subArea);
|
270
|
subArea.parent = this;
|
271
|
}
|
272
|
|
273
|
public AreaNode getParent() {
|
274
|
return parent;
|
275
|
}
|
276
|
|
277
|
public boolean hasParent() {
|
278
|
return getParent() != null;
|
279
|
}
|
280
|
|
281
|
public Collection<NamedArea> getSubareas() {
|
282
|
Collection<NamedArea> areas = new HashSet<NamedArea>();
|
283
|
for(AreaNode node : subAreas) {
|
284
|
areas.add(node.area);
|
285
|
}
|
286
|
return areas;
|
287
|
}
|
288
|
}
|
289
|
|
290
|
}
|