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.List;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.apache.commons.lang.StringUtils;
|
19
|
import org.apache.log4j.Logger;
|
20
|
|
21
|
import eu.etaxonomy.cdm.api.service.dto.CondensedDistribution;
|
22
|
import eu.etaxonomy.cdm.common.UTF8;
|
23
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
24
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
25
|
import eu.etaxonomy.cdm.model.common.Language;
|
26
|
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
|
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.mueller
|
33
|
* @date Apr 05, 2016
|
34
|
*
|
35
|
*/
|
36
|
public class FloraCubaCondensedDistributionComposerOld extends CondensedDistributionComposerBase {
|
37
|
|
38
|
@SuppressWarnings("unused")
|
39
|
private static final Logger logger = Logger.getLogger(FloraCubaCondensedDistributionComposerOld.class);
|
40
|
|
41
|
// private static Set<UUID> foreignStatusUuids;
|
42
|
|
43
|
private UUID uuidInternalArea = UUID.fromString("d0144a6e-0e17-4a1d-bce5-d464a2aa7229"); //Cuba
|
44
|
|
45
|
private String internalAreaSeparator = UTF8.EN_DASH.toString() + " ";
|
46
|
|
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/5682
|
55
|
// ==================================================
|
56
|
|
57
|
statusSymbols = new HashMap<UUID, String> ();
|
58
|
//no entries as we handle symbols now on model level
|
59
|
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* {@inheritDoc}
|
64
|
* @return
|
65
|
*/
|
66
|
@Override
|
67
|
public CondensedDistribution createCondensedDistribution(Collection<Distribution> filteredDistributions,
|
68
|
List<Language> languages) {
|
69
|
|
70
|
CondensedDistribution condensedDistribution = new CondensedDistribution();
|
71
|
|
72
|
//empty
|
73
|
if (filteredDistributions == null || filteredDistributions.isEmpty()){
|
74
|
return condensedDistribution;
|
75
|
}
|
76
|
|
77
|
OrderedTermVocabulary<NamedArea> areaVocabulary = CdmBase.deproxy(filteredDistributions.iterator().next().getArea().getVocabulary(), OrderedTermVocabulary.class);
|
78
|
|
79
|
//deproxy and reverse order
|
80
|
List<NamedArea> areaList = new ArrayList<NamedArea>();
|
81
|
for (DefinedTermBase<NamedArea> dtb : areaVocabulary.getOrderedTerms()){
|
82
|
areaList.add(0, (NamedArea)CdmBase.deproxy(dtb));
|
83
|
}
|
84
|
|
85
|
|
86
|
boolean isFirstAfterInternalArea = false;
|
87
|
for (NamedArea area : areaList){
|
88
|
|
89
|
if (area.getPartOf() != null){
|
90
|
continue; //subarea are handled later
|
91
|
}
|
92
|
|
93
|
StringBuilder areaStatusString = new StringBuilder();
|
94
|
|
95
|
|
96
|
|
97
|
Distribution distribution = getDistribution(area, filteredDistributions);
|
98
|
if (distribution == null){
|
99
|
continue;
|
100
|
}
|
101
|
|
102
|
if (area.getUuid().equals(uuidInternalArea)){
|
103
|
isFirstAfterInternalArea = true;
|
104
|
}else if(isFirstAfterInternalArea && !area.getUuid().equals(uuidInternalArea)){
|
105
|
areaStatusString.append(internalAreaSeparator);
|
106
|
isFirstAfterInternalArea = false;
|
107
|
}
|
108
|
|
109
|
PresenceAbsenceTerm status = distribution.getStatus();
|
110
|
|
111
|
String statusSymbol = statusSymbol(status);
|
112
|
areaStatusString.append(statusSymbol);
|
113
|
|
114
|
String areaLabel = makeAreaLabel(languages, area);
|
115
|
areaStatusString.append(areaLabel);
|
116
|
|
117
|
if(!area.getIncludes().isEmpty()) {
|
118
|
// areaStatusString.append('(');
|
119
|
subAreaLabels(languages, area.getIncludes(), areaStatusString, statusSymbol, areaLabel, filteredDistributions);
|
120
|
// areaStatusString.append(')');
|
121
|
}
|
122
|
|
123
|
|
124
|
// if(isForeignStatus(status)) {
|
125
|
// condensedDistribution.addForeignDistributionItem(status, areaStatusString.toString(), areaLabel);
|
126
|
// } else {
|
127
|
condensedDistribution.addIndigenousDistributionItem(status, areaStatusString.toString(), areaLabel);
|
128
|
// }
|
129
|
|
130
|
}
|
131
|
|
132
|
// }
|
133
|
// //5. order the condensedDistributions alphabetically
|
134
|
// // FIXME
|
135
|
// condensedDistribution.sortForeign();
|
136
|
// condensedDistribution.sortIndigenous();
|
137
|
|
138
|
return condensedDistribution;
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* @param area
|
143
|
* @param filteredDistributions
|
144
|
* @return
|
145
|
*/
|
146
|
private Distribution getDistribution(NamedArea area, Collection<Distribution> filteredDistributions) {
|
147
|
for (Distribution dist : filteredDistributions){
|
148
|
if (dist.getArea() != null && dist.getArea().equals(area)){
|
149
|
return dist;
|
150
|
}
|
151
|
}
|
152
|
return null;
|
153
|
}
|
154
|
|
155
|
|
156
|
// private boolean isForeignStatus(PresenceAbsenceTerm status) {
|
157
|
// return foreignStatusUuids.contains(status.getUuid());
|
158
|
// }
|
159
|
|
160
|
/**
|
161
|
* @param langs
|
162
|
* @param node
|
163
|
* @param areaString
|
164
|
* @param statusSymbol
|
165
|
*/
|
166
|
private void subAreaLabels(List<Language> langs, Collection<NamedArea> subAreas, StringBuilder areaString,
|
167
|
String statusSymbol, String parentLabel,
|
168
|
Collection<Distribution> filteredDistributions) {
|
169
|
//TODO very redundant with main method
|
170
|
List<String> subAreaLabels = new ArrayList<String>();
|
171
|
|
172
|
//deproxy and reverse order
|
173
|
List<NamedArea> areaList = new ArrayList<NamedArea>();
|
174
|
for (DefinedTermBase<NamedArea> dtb : subAreas){
|
175
|
areaList.add(0, (NamedArea)CdmBase.deproxy(dtb));
|
176
|
}
|
177
|
// Collections.sort(areaList);
|
178
|
|
179
|
for(NamedArea area : areaList) {
|
180
|
|
181
|
StringBuilder subAreaString = new StringBuilder();
|
182
|
Distribution distribution = getDistribution(area, filteredDistributions);
|
183
|
if (distribution == null){
|
184
|
continue;
|
185
|
}
|
186
|
|
187
|
|
188
|
PresenceAbsenceTerm status = distribution.getStatus();
|
189
|
String subAreaStatusSymbol = statusSymbol(status);
|
190
|
if (subAreaStatusSymbol != null && !subAreaStatusSymbol.equals(statusSymbol)){
|
191
|
subAreaString.append(subAreaStatusSymbol);
|
192
|
}
|
193
|
|
194
|
String areaLabel = makeAreaLabel(langs, area);
|
195
|
// String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""});
|
196
|
String cleanSubAreaLabel = areaLabel;
|
197
|
subAreaString.append(cleanSubAreaLabel);
|
198
|
|
199
|
if(!area.getIncludes().isEmpty()) {
|
200
|
// subAreaString.append('(');
|
201
|
subAreaLabels(langs, area.getIncludes(), subAreaString, subAreaStatusSymbol, areaLabel, filteredDistributions);
|
202
|
// subAreaString.append(')');
|
203
|
}
|
204
|
|
205
|
subAreaLabels.add(subAreaString.toString());
|
206
|
}
|
207
|
|
208
|
// Collections.sort(subAreaLabels);
|
209
|
if (!subAreaLabels.isEmpty()){
|
210
|
areaString.append("(" + StringUtils.join(subAreaLabels, " ") + ")");
|
211
|
}
|
212
|
|
213
|
}
|
214
|
|
215
|
|
216
|
public String getInternalAreaSeparator() {
|
217
|
return internalAreaSeparator;
|
218
|
}
|
219
|
|
220
|
public void setInternalAreaSeparator(String internalAreaSeparator) {
|
221
|
this.internalAreaSeparator = internalAreaSeparator;
|
222
|
}
|
223
|
|
224
|
|
225
|
|
226
|
|
227
|
|
228
|
|
229
|
}
|