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.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.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.common.Language;
|
27
|
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
|
28
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
29
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
30
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
31
|
|
32
|
/**
|
33
|
* @author a.mueller
|
34
|
* @date Apr 05, 2016
|
35
|
*
|
36
|
*/
|
37
|
public class FloraCubaCondensedDistributionComposer implements ICondensedDistributionComposer {
|
38
|
|
39
|
@SuppressWarnings("unused")
|
40
|
private static final Logger logger = Logger.getLogger(FloraCubaCondensedDistributionComposer.class);
|
41
|
|
42
|
private final CondensedDistribution condensedDistribution;
|
43
|
|
44
|
private static Map<UUID, String> statusSymbols;
|
45
|
|
46
|
private static Set<UUID> foreignStatusUuids;
|
47
|
|
48
|
// these status uuids are special for EuroPlusMed and might also be used
|
49
|
private final static UUID REPORTED_IN_ERROR_UUID = UUID.fromString("38604788-cf05-4607-b155-86db456f7680");
|
50
|
|
51
|
static {
|
52
|
|
53
|
// ==================================================
|
54
|
// Mapping as defined in ticket http://dev.e-taxonomy.eu/trac/ticket/3907
|
55
|
// ==================================================
|
56
|
|
57
|
statusSymbols = new HashMap<UUID, String> ();
|
58
|
// ● endemic (U+25CF BLACK CIRCLE)
|
59
|
statusSymbols.put(PresenceAbsenceTerm.ENDEMIC_FOR_THE_RELEVANT_AREA().getUuid(), "\u25CF");
|
60
|
|
61
|
// Lu native (incl. archaeophytes) TODO status archaeophytes?
|
62
|
statusSymbols.put(PresenceAbsenceTerm.NATIVE().getUuid(), "");
|
63
|
// statusSymbols.put(PresenceAbsenceTerm.NATIVE_FORMERLY_NATIVE().getUuid(), "");
|
64
|
|
65
|
// ?Lu doubtfully present (U+3F QUESTION MARK)
|
66
|
// statusSymbols.put(PresenceAbsenceTerm.INTRODUCED_PRESENCE_QUESTIONABLE().getUuid(), "?");
|
67
|
statusSymbols.put(PresenceAbsenceTerm.NATIVE_PRESENCE_QUESTIONABLE().getUuid(), "?");
|
68
|
// statusSymbols.put(PresenceAbsenceTerm.PRESENT_DOUBTFULLY().getUuid(), "?");
|
69
|
|
70
|
// dLu doubtfully native
|
71
|
statusSymbols.put(PresenceAbsenceTerm.NATIVE_DOUBTFULLY_NATIVE().getUuid(), "d");
|
72
|
|
73
|
// -Lu absent but reported in error (U+2D HYPHEN-MINUS)
|
74
|
// statusSymbols.put(PresenceAbsenceTerm.INTRODUCED_REPORTED_IN_ERROR().getUuid(), "-");
|
75
|
statusSymbols.put(PresenceAbsenceTerm.NATIVE_REPORTED_IN_ERROR().getUuid(), "-");
|
76
|
statusSymbols.put(REPORTED_IN_ERROR_UUID, "-");
|
77
|
|
78
|
// [aLu] casual alien = introduced: adventitious (casual)
|
79
|
statusSymbols.put(PresenceAbsenceTerm.INTRODUCED_ADVENTITIOUS().getUuid(), "a");
|
80
|
|
81
|
// [cLu] cultivated
|
82
|
statusSymbols.put(PresenceAbsenceTerm.CULTIVATED() .getUuid(), "c");
|
83
|
// statusSymbols.put(PresenceAbsenceTerm.INTRODUCED_CULTIVATED().getUuid(), "c");
|
84
|
|
85
|
// [nLu] naturalized
|
86
|
statusSymbols.put(PresenceAbsenceTerm.NATURALISED().getUuid(), "n");
|
87
|
// statusSymbols.put(PresenceAbsenceTerm.INTRODUCED_NATURALIZED().getUuid(), "n");
|
88
|
|
89
|
statusSymbols.put(PresenceAbsenceTerm.CULTIVATED_PRESENCE_QUESTIONABLE().getUuid(), "?c");
|
90
|
statusSymbols.put(PresenceAbsenceTerm.CULTIVATED_REPORTED_IN_ERROR().getUuid(), "-c");
|
91
|
|
92
|
|
93
|
|
94
|
//Cuba specific
|
95
|
//occasionally cultivated
|
96
|
statusSymbols.put(UUID.fromString("936c3f9a-6099-4322-9792-0a72c6c2ce25"), "(c)");
|
97
|
//endemic, doubtfully present
|
98
|
statusSymbols.put(UUID.fromString("5f954f08-267a-4928-b073-12328f74c187"), "?e");
|
99
|
//non-native and doubtfully naturalised
|
100
|
statusSymbols.put(UUID.fromString("a1e26234-831e-4190-9fe3-011aca09ddba"), "p");
|
101
|
//rare casual
|
102
|
statusSymbols.put(UUID.fromString("8914ce0d-7d31-4c88-8317-985f2b3a493b"), "(a)");
|
103
|
//?non-native and doubtfully naturalised
|
104
|
statusSymbols.put(UUID.fromString("9e0b413b-5a68-4e5b-91f2-227b4f832466"), "?p");
|
105
|
//?adventive (casual) alien
|
106
|
statusSymbols.put(UUID.fromString("c42ca644-1773-4230-a2ee-328a5d4a21ab"), "?a");
|
107
|
//endemic, reported in error
|
108
|
statusSymbols.put(UUID.fromString("679b215d-c231-4ee2-ae12-3ffc3dd528ad"), "-e");
|
109
|
//naturalised, reported in error
|
110
|
statusSymbols.put(UUID.fromString("8d918a37-3add-4e1c-a233-c37dbee209aa"), "-n");
|
111
|
//non-native and doubtfully naturalised, reported in error
|
112
|
statusSymbols.put(UUID.fromString("b9153d90-9e31-465a-a28c-79077a8ed4c2"), "-p");
|
113
|
//adventive alien , reported in error
|
114
|
statusSymbols.put(UUID.fromString("9b910b7b-43e3-4260-961c-6063b11cb7dc"), "-a");
|
115
|
//doubtfully native: reported in error
|
116
|
statusSymbols.put(UUID.fromString("71b72e24-c2b6-44a5-bdab-39f083bf0f06"), "-d");
|
117
|
|
118
|
|
119
|
|
120
|
|
121
|
|
122
|
|
123
|
|
124
|
|
125
|
// foreignStatusUuids = new HashSet<UUID>();
|
126
|
// foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED().getUuid());
|
127
|
// foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_NATURALIZED().getUuid());
|
128
|
// foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_ADVENTITIOUS().getUuid());
|
129
|
// foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_CULTIVATED().getUuid());
|
130
|
// foreignStatusUuids.add(PresenceAbsenceTerm.NATURALISED().getUuid());
|
131
|
// foreignStatusUuids.add(PresenceAbsenceTerm.CULTIVATED().getUuid());
|
132
|
|
133
|
}
|
134
|
|
135
|
public FloraCubaCondensedDistributionComposer() {
|
136
|
condensedDistribution = new CondensedDistribution();
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* {@inheritDoc}
|
141
|
* @return
|
142
|
*/
|
143
|
@Override
|
144
|
public CondensedDistribution createCondensedDistribution(Collection<Distribution> filteredDistributions,
|
145
|
List<Language> langs) {
|
146
|
|
147
|
//1. order by PresenceAbsenceTerms
|
148
|
// Map<PresenceAbsenceTerm, Collection<NamedArea>> byStatus = new HashMap<PresenceAbsenceTerm, Collection<NamedArea>>();
|
149
|
// for(Distribution d : filteredDistributions) {
|
150
|
// PresenceAbsenceTerm status = d.getStatus();
|
151
|
// if(status == null) {
|
152
|
// continue;
|
153
|
// }
|
154
|
// if(!byStatus.containsKey(status)) {
|
155
|
// byStatus.put(status, new HashSet<NamedArea>());
|
156
|
// }
|
157
|
// byStatus.get(status).add(d.getArea());
|
158
|
// }
|
159
|
|
160
|
// //2. build the area hierarchy
|
161
|
// for(PresenceAbsenceTerm status : byStatus.keySet()) {
|
162
|
//
|
163
|
// Map<NamedArea, AreaNode> areaNodeMap = new HashMap<NamedArea, AreaNode>();
|
164
|
//
|
165
|
// for(NamedArea area : byStatus.get(status)) {
|
166
|
// AreaNode node;
|
167
|
// if(!areaNodeMap.containsKey(area)) {
|
168
|
// // putting area into hierarchy as node
|
169
|
// node = new AreaNode(area);
|
170
|
// areaNodeMap.put(area, node);
|
171
|
// } else {
|
172
|
// // is parent of another and thus already has a node
|
173
|
// node = areaNodeMap.get(area);
|
174
|
// }
|
175
|
//
|
176
|
// NamedArea parent = findParentIn(area, byStatus.get(status));
|
177
|
// if(parent != null) {
|
178
|
// AreaNode parentNode;
|
179
|
// if(!areaNodeMap.containsKey(parent)) {
|
180
|
// parentNode = new AreaNode(parent);
|
181
|
// areaNodeMap.put(parent, parentNode);
|
182
|
// } else {
|
183
|
// parentNode = areaNodeMap.get(parent);
|
184
|
// }
|
185
|
// parentNode.addSubArea(node);
|
186
|
// }
|
187
|
// }
|
188
|
//
|
189
|
// //3. find root nodes
|
190
|
// Set<AreaNode>hierarchy = new HashSet<AreaNode>();
|
191
|
// for(AreaNode node : areaNodeMap.values()) {
|
192
|
// if(!node.hasParent()) {
|
193
|
// hierarchy.add(node);
|
194
|
// }
|
195
|
// }
|
196
|
|
197
|
if (filteredDistributions == null || filteredDistributions.isEmpty()){
|
198
|
return condensedDistribution;
|
199
|
}
|
200
|
OrderedTermVocabulary<NamedArea> areaVocabulary = CdmBase.deproxy(filteredDistributions.iterator().next().getArea().getVocabulary(), OrderedTermVocabulary.class);
|
201
|
for (NamedArea area : areaVocabulary.getOrderedTerms()){
|
202
|
|
203
|
if (area.getPartOf() != null){
|
204
|
continue; //subarea are handled later
|
205
|
}
|
206
|
StringBuilder areaStatusString = new StringBuilder();
|
207
|
Distribution distribution = getDistribution(area, filteredDistributions);
|
208
|
PresenceAbsenceTerm status = distribution.getStatus();
|
209
|
|
210
|
String statusSymbol = statusSymbol(status);
|
211
|
areaStatusString.append(statusSymbol);
|
212
|
|
213
|
String areaLabel = area.getIdInVocabulary(); //TODO if not exist use abbrevLabel
|
214
|
areaStatusString.append(areaLabel);
|
215
|
|
216
|
if(!area.getIncludes().isEmpty()) {
|
217
|
areaStatusString.append('(');
|
218
|
subAreaLabels(langs, area.getIncludes(), areaStatusString, statusSymbol, areaLabel);
|
219
|
areaStatusString.append(')');
|
220
|
}
|
221
|
|
222
|
// if(isForeignStatus(status)) {
|
223
|
// condensedDistribution.addForeignDistributionItem(status, areaStatusString.toString(), areaLabel);
|
224
|
// } else {
|
225
|
condensedDistribution.addIndigenousDistributionItem(status, areaStatusString.toString(), areaLabel);
|
226
|
// }
|
227
|
|
228
|
}
|
229
|
|
230
|
// }
|
231
|
// //5. order the condensedDistributions alphabetically
|
232
|
// // FIXME
|
233
|
// condensedDistribution.sortForeign();
|
234
|
// condensedDistribution.sortIndigenous();
|
235
|
|
236
|
return condensedDistribution;
|
237
|
}
|
238
|
|
239
|
/**
|
240
|
* @param area
|
241
|
* @param filteredDistributions
|
242
|
* @return
|
243
|
*/
|
244
|
private Distribution getDistribution(NamedArea area, Collection<Distribution> filteredDistributions) {
|
245
|
for (Distribution dist : filteredDistributions){
|
246
|
if (dist.getArea() != null && dist.getArea().equals(area)){
|
247
|
return dist;
|
248
|
}
|
249
|
}
|
250
|
return null;
|
251
|
}
|
252
|
|
253
|
/**
|
254
|
* @param status
|
255
|
* @return
|
256
|
*/
|
257
|
private String statusSymbol(PresenceAbsenceTerm status) {
|
258
|
if(status == null) {
|
259
|
return "";
|
260
|
}
|
261
|
String symbol = statusSymbols.get(status.getUuid());
|
262
|
if(symbol == null) {
|
263
|
symbol = "";
|
264
|
}
|
265
|
return symbol;
|
266
|
}
|
267
|
|
268
|
private boolean isForeignStatus(PresenceAbsenceTerm status) {
|
269
|
return foreignStatusUuids.contains(status.getUuid());
|
270
|
}
|
271
|
|
272
|
/**
|
273
|
* @param langs
|
274
|
* @param node
|
275
|
* @param areaString
|
276
|
* @param statusSymbol
|
277
|
*/
|
278
|
private void subAreaLabels(List<Language> langs, Collection<NamedArea> nodes, StringBuilder areaString, String statusSymbol, String parentLabel) {
|
279
|
//TODO very redundant with main method
|
280
|
List<String> subAreaLabels = new ArrayList<String>();
|
281
|
|
282
|
for(NamedArea area : nodes) {
|
283
|
|
284
|
StringBuilder subAreaString = new StringBuilder();
|
285
|
|
286
|
subAreaString.append(statusSymbol);
|
287
|
|
288
|
String areaLabel = area.getIdInVocabulary() != null ? area.getIdInVocabulary() :area.getPreferredRepresentation(langs).getAbbreviatedLabel();
|
289
|
String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""});
|
290
|
subAreaString.append(cleanSubAreaLabel);
|
291
|
|
292
|
if(!area.getIncludes().isEmpty()) {
|
293
|
subAreaString.append('(');
|
294
|
subAreaLabels(langs, area.getIncludes(), subAreaString, statusSymbol, areaLabel);
|
295
|
subAreaString.append(')');
|
296
|
}
|
297
|
|
298
|
subAreaLabels.add(subAreaString.toString());
|
299
|
}
|
300
|
// Collections.sort(subAreaLabels);
|
301
|
areaString.append(StringUtils.join(subAreaLabels, " "));
|
302
|
|
303
|
}
|
304
|
|
305
|
/**
|
306
|
* Searches for the parent are of the area given as parameter in
|
307
|
* the Collection of areas.
|
308
|
*
|
309
|
* @parent area
|
310
|
* The area whose parent area is to be searched
|
311
|
* @param collection
|
312
|
* The areas to search in.
|
313
|
*
|
314
|
* @return
|
315
|
* Either the parent if it has been found or null.
|
316
|
*/
|
317
|
private NamedArea findParentIn(NamedArea area, Collection<NamedArea> areas) {
|
318
|
NamedArea parent = area.getPartOf();
|
319
|
if(parent != null && areas.contains(parent)){
|
320
|
return parent;
|
321
|
}
|
322
|
return null;
|
323
|
}
|
324
|
|
325
|
class AreaNode {
|
326
|
|
327
|
private final NamedArea area;
|
328
|
private AreaNode parent = null;
|
329
|
private final Set<AreaNode> subAreas = new HashSet<AreaNode>();
|
330
|
|
331
|
/**
|
332
|
* @param area
|
333
|
*/
|
334
|
public AreaNode(NamedArea area) {
|
335
|
this.area = area;
|
336
|
}
|
337
|
|
338
|
public void addSubArea(AreaNode subArea) {
|
339
|
subAreas.add(subArea);
|
340
|
subArea.parent = this;
|
341
|
}
|
342
|
|
343
|
public AreaNode getParent() {
|
344
|
return parent;
|
345
|
}
|
346
|
|
347
|
public boolean hasParent() {
|
348
|
return getParent() != null;
|
349
|
}
|
350
|
|
351
|
public Collection<NamedArea> getSubareas() {
|
352
|
Collection<NamedArea> areas = new HashSet<NamedArea>();
|
353
|
for(AreaNode node : subAreas) {
|
354
|
areas.add(node.area);
|
355
|
}
|
356
|
return areas;
|
357
|
}
|
358
|
}
|
359
|
|
360
|
}
|