Revision c83d209e
Added by Andreas Kohlbecker almost 7 years ago
cdmlib-ext/src/main/java/eu/etaxonomy/cdm/ext/geo/FloraCubaCondensedDistributionComposer.java | ||
---|---|---|
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 FloraCubaCondensedDistributionComposer implements ICondensedDistributionComposer { |
|
37 |
|
|
38 |
private static final Logger logger = Logger.getLogger(FloraCubaCondensedDistributionComposer.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 FloraCubaCondensedDistributionComposer() { |
|
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 |
// FIXME |
|
193 |
condensedDistribution.sortForeign(); |
|
194 |
condensedDistribution.sortIndigenous(); |
|
195 |
|
|
196 |
return condensedDistribution; |
|
197 |
} |
|
198 |
|
|
199 |
/** |
|
200 |
* @param status |
|
201 |
* @return |
|
202 |
*/ |
|
203 |
private String statusSymbol(PresenceAbsenceTerm status) { |
|
204 |
if(status == null) { |
|
205 |
return ""; |
|
206 |
} |
|
207 |
String symbol = statusSymbols.get(status.getUuid()); |
|
208 |
if(symbol == null) { |
|
209 |
symbol = ""; |
|
210 |
} |
|
211 |
return symbol; |
|
212 |
} |
|
213 |
|
|
214 |
private boolean isForeignStatus(PresenceAbsenceTerm status) { |
|
215 |
return foreignStatusUuids.contains(status.getUuid()); |
|
216 |
} |
|
217 |
|
|
218 |
/** |
|
219 |
* @param langs |
|
220 |
* @param node |
|
221 |
* @param areaString |
|
222 |
* @param statusSymbol |
|
223 |
*/ |
|
224 |
private void subAreaLabels(List<Language> langs, Collection<AreaNode> nodes, StringBuilder areaString, String statusSymbol, String parentLabel) { |
|
225 |
|
|
226 |
List<String> subAreaLabels = new ArrayList<String>(); |
|
227 |
|
|
228 |
for(AreaNode node : nodes) { |
|
229 |
|
|
230 |
StringBuilder subAreaString = new StringBuilder(); |
|
231 |
|
|
232 |
subAreaString.append(statusSymbol); |
|
233 |
|
|
234 |
String areaLabel = node.area.getPreferredRepresentation(langs).getAbbreviatedLabel(); |
|
235 |
String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""}); |
|
236 |
subAreaString.append(cleanSubAreaLabel); |
|
237 |
|
|
238 |
if(!node.subAreas.isEmpty()) { |
|
239 |
subAreaString.append('('); |
|
240 |
subAreaLabels(langs, node.subAreas, subAreaString, statusSymbol, areaLabel); |
|
241 |
subAreaString.append(')'); |
|
242 |
} |
|
243 |
|
|
244 |
subAreaLabels.add(subAreaString.toString()); |
|
245 |
} |
|
246 |
Collections.sort(subAreaLabels); |
|
247 |
areaString.append(StringUtils.join(subAreaLabels, " ")); |
|
248 |
|
|
249 |
} |
|
250 |
|
|
251 |
/** |
|
252 |
* Searches for the parent are of the area given as parameter in |
|
253 |
* the Collection of areas. |
|
254 |
* |
|
255 |
* @parent area |
|
256 |
* The area whose parent area is to be searched |
|
257 |
* @param collection |
|
258 |
* The areas to search in. |
|
259 |
* |
|
260 |
* @return |
|
261 |
* Either the parent if it has been found or null. |
|
262 |
*/ |
|
263 |
private NamedArea findParentIn(NamedArea area, Collection<NamedArea> areas) { |
|
264 |
NamedArea parent = area.getPartOf(); |
|
265 |
if(parent != null && areas.contains(parent)){ |
|
266 |
return parent; |
|
267 |
} |
|
268 |
return null; |
|
269 |
} |
|
270 |
|
|
271 |
class AreaNode { |
|
272 |
|
|
273 |
private final NamedArea area; |
|
274 |
private AreaNode parent = null; |
|
275 |
private final Set<AreaNode> subAreas = new HashSet<AreaNode>(); |
|
276 |
|
|
277 |
/** |
|
278 |
* @param area |
|
279 |
*/ |
|
280 |
public AreaNode(NamedArea area) { |
|
281 |
this.area = area; |
|
282 |
} |
|
283 |
|
|
284 |
public void addSubArea(AreaNode subArea) { |
|
285 |
subAreas.add(subArea); |
|
286 |
subArea.parent = this; |
|
287 |
} |
|
288 |
|
|
289 |
public AreaNode getParent() { |
|
290 |
return parent; |
|
291 |
} |
|
292 |
|
|
293 |
public boolean hasParent() { |
|
294 |
return getParent() != null; |
|
295 |
} |
|
296 |
|
|
297 |
public Collection<NamedArea> getSubareas() { |
|
298 |
Collection<NamedArea> areas = new HashSet<NamedArea>(); |
|
299 |
for(AreaNode node : subAreas) { |
|
300 |
areas.add(node.area); |
|
301 |
} |
|
302 |
return areas; |
|
303 |
} |
|
304 |
} |
|
305 |
|
|
306 |
} |
Also available in: Unified diff
CondensedDistributionCompose for FloraCuba