Revision 441eafbf
Added by Andreas Müller about 8 years ago
cdmlib-ext/src/main/java/eu/etaxonomy/cdm/ext/geo/FloraCubaCondensedDistributionComposer.java | ||
---|---|---|
11 | 11 |
|
12 | 12 |
import java.util.ArrayList; |
13 | 13 |
import java.util.Collection; |
14 |
import java.util.Collections; |
|
14 | 15 |
import java.util.HashMap; |
15 | 16 |
import java.util.HashSet; |
16 | 17 |
import java.util.List; |
... | ... | |
39 | 40 |
@SuppressWarnings("unused") |
40 | 41 |
private static final Logger logger = Logger.getLogger(FloraCubaCondensedDistributionComposer.class); |
41 | 42 |
|
42 |
private final CondensedDistribution condensedDistribution; |
|
43 |
|
|
44 | 43 |
private static Map<UUID, String> statusSymbols; |
45 | 44 |
|
46 | 45 |
private static Set<UUID> foreignStatusUuids; |
... | ... | |
51 | 50 |
static { |
52 | 51 |
|
53 | 52 |
// ================================================== |
54 |
// Mapping as defined in ticket http://dev.e-taxonomy.eu/trac/ticket/3907
|
|
53 |
// Mapping as defined in ticket http://dev.e-taxonomy.eu/trac/ticket/5682
|
|
55 | 54 |
// ================================================== |
56 | 55 |
|
57 | 56 |
statusSymbols = new HashMap<UUID, String> (); |
... | ... | |
90 | 89 |
statusSymbols.put(PresenceAbsenceTerm.CULTIVATED_REPORTED_IN_ERROR().getUuid(), "-c"); |
91 | 90 |
|
92 | 91 |
|
93 |
|
|
94 | 92 |
//Cuba specific |
95 | 93 |
//occasionally cultivated |
96 | 94 |
statusSymbols.put(UUID.fromString("936c3f9a-6099-4322-9792-0a72c6c2ce25"), "(c)"); |
... | ... | |
116 | 114 |
statusSymbols.put(UUID.fromString("71b72e24-c2b6-44a5-bdab-39f083bf0f06"), "-d"); |
117 | 115 |
|
118 | 116 |
|
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 | 117 |
} |
138 | 118 |
|
139 | 119 |
/** |
... | ... | |
143 | 123 |
@Override |
144 | 124 |
public CondensedDistribution createCondensedDistribution(Collection<Distribution> filteredDistributions, |
145 | 125 |
List<Language> langs) { |
126 |
CondensedDistribution condensedDistribution = new CondensedDistribution(); |
|
146 | 127 |
|
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 |
|
|
128 |
//empty |
|
197 | 129 |
if (filteredDistributions == null || filteredDistributions.isEmpty()){ |
198 | 130 |
return condensedDistribution; |
199 | 131 |
} |
132 |
|
|
200 | 133 |
OrderedTermVocabulary<NamedArea> areaVocabulary = CdmBase.deproxy(filteredDistributions.iterator().next().getArea().getVocabulary(), OrderedTermVocabulary.class); |
201 |
for (NamedArea area : areaVocabulary.getOrderedTerms()){ |
|
134 |
|
|
135 |
List<NamedArea> areaList = new ArrayList<NamedArea>(areaVocabulary.getOrderedTerms()); |
|
136 |
Collections.reverse(areaList); |
|
137 |
|
|
138 |
for (NamedArea area : areaList){ |
|
202 | 139 |
|
203 | 140 |
if (area.getPartOf() != null){ |
204 | 141 |
continue; //subarea are handled later |
... | ... | |
214 | 151 |
String statusSymbol = statusSymbol(status); |
215 | 152 |
areaStatusString.append(statusSymbol); |
216 | 153 |
|
217 |
String areaLabel = area.getIdInVocabulary(); //TODO if not exist use abbrevLabel
|
|
154 |
String areaLabel = makeAreaLabel(langs, area);
|
|
218 | 155 |
areaStatusString.append(areaLabel); |
219 | 156 |
|
220 | 157 |
if(!area.getIncludes().isEmpty()) { |
221 |
areaStatusString.append('('); |
|
222 |
subAreaLabels(langs, area.getIncludes(), areaStatusString, statusSymbol, areaLabel); |
|
223 |
areaStatusString.append(')'); |
|
158 |
// areaStatusString.append('(');
|
|
159 |
subAreaLabels(langs, area.getIncludes(), areaStatusString, statusSymbol, areaLabel, filteredDistributions);
|
|
160 |
// areaStatusString.append(')');
|
|
224 | 161 |
} |
225 | 162 |
|
226 | 163 |
// if(isForeignStatus(status)) { |
... | ... | |
269 | 206 |
return symbol; |
270 | 207 |
} |
271 | 208 |
|
272 |
private boolean isForeignStatus(PresenceAbsenceTerm status) { |
|
273 |
return foreignStatusUuids.contains(status.getUuid()); |
|
274 |
} |
|
209 |
// private boolean isForeignStatus(PresenceAbsenceTerm status) {
|
|
210 |
// return foreignStatusUuids.contains(status.getUuid());
|
|
211 |
// }
|
|
275 | 212 |
|
276 | 213 |
/** |
277 | 214 |
* @param langs |
... | ... | |
279 | 216 |
* @param areaString |
280 | 217 |
* @param statusSymbol |
281 | 218 |
*/ |
282 |
private void subAreaLabels(List<Language> langs, Collection<NamedArea> nodes, StringBuilder areaString, String statusSymbol, String parentLabel) { |
|
219 |
private void subAreaLabels(List<Language> langs, Collection<NamedArea> subAreas, StringBuilder areaString, |
|
220 |
String statusSymbol, String parentLabel, |
|
221 |
Collection<Distribution> filteredDistributions) { |
|
283 | 222 |
//TODO very redundant with main method |
284 | 223 |
List<String> subAreaLabels = new ArrayList<String>(); |
285 | 224 |
|
286 |
for(NamedArea area : nodes) { |
|
225 |
List<NamedArea> areaList = new ArrayList<NamedArea>(subAreas); |
|
226 |
Collections.reverse(areaList); |
|
227 |
|
|
228 |
for(NamedArea area : areaList) { |
|
287 | 229 |
|
288 | 230 |
StringBuilder subAreaString = new StringBuilder(); |
231 |
Distribution distribution = getDistribution(area, filteredDistributions); |
|
232 |
if (distribution == null){ |
|
233 |
continue; |
|
234 |
} |
|
235 |
|
|
289 | 236 |
|
290 |
subAreaString.append(statusSymbol); |
|
237 |
PresenceAbsenceTerm status = distribution.getStatus(); |
|
238 |
String subAreaStatusSymbol = statusSymbol(status); |
|
239 |
if (subAreaStatusSymbol != null && !subAreaStatusSymbol.equals(statusSymbol)){ |
|
240 |
subAreaString.append(subAreaStatusSymbol); |
|
241 |
} |
|
291 | 242 |
|
292 |
String areaLabel = area.getIdInVocabulary() != null ? area.getIdInVocabulary() :area.getPreferredRepresentation(langs).getAbbreviatedLabel();
|
|
243 |
String areaLabel = makeAreaLabel(langs, area);
|
|
293 | 244 |
String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""}); |
294 | 245 |
subAreaString.append(cleanSubAreaLabel); |
295 | 246 |
|
296 | 247 |
if(!area.getIncludes().isEmpty()) { |
297 |
subAreaString.append('('); |
|
298 |
subAreaLabels(langs, area.getIncludes(), subAreaString, statusSymbol, areaLabel);
|
|
299 |
subAreaString.append(')'); |
|
248 |
// subAreaString.append('(');
|
|
249 |
subAreaLabels(langs, area.getIncludes(), subAreaString, subAreaStatusSymbol, areaLabel, filteredDistributions);
|
|
250 |
// subAreaString.append(')');
|
|
300 | 251 |
} |
301 | 252 |
|
302 | 253 |
subAreaLabels.add(subAreaString.toString()); |
303 | 254 |
} |
304 |
// Collections.sort(subAreaLabels); |
|
305 |
areaString.append(StringUtils.join(subAreaLabels, " ")); |
|
306 | 255 |
|
256 |
// Collections.sort(subAreaLabels); |
|
257 |
if (!subAreaLabels.isEmpty()){ |
|
258 |
areaString.append("(" + StringUtils.join(subAreaLabels, " ") + ")"); |
|
259 |
} |
|
260 |
|
|
261 |
} |
|
262 |
|
|
263 |
/** |
|
264 |
* @param langs |
|
265 |
* @param area |
|
266 |
* @return |
|
267 |
*/ |
|
268 |
private String makeAreaLabel(List<Language> langs, NamedArea area) { |
|
269 |
return area.getIdInVocabulary() != null ? area.getIdInVocabulary() :area.getPreferredRepresentation(langs).getAbbreviatedLabel(); |
|
307 | 270 |
} |
308 | 271 |
|
309 | 272 |
/** |
Also available in: Unified diff
Improve condensed string for Cuba (still not yet ready) #5682