Project

General

Profile

« Previous | Next » 

Revision d3c71277

Added by Andreas Müller about 8 years ago

First implementatio for Cuban Condensed Distribution String #5682

View differences:

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;
15 14
import java.util.HashMap;
16 15
import java.util.HashSet;
17 16
import java.util.List;
......
23 22
import org.apache.log4j.Logger;
24 23

  
25 24
import eu.etaxonomy.cdm.api.service.dto.CondensedDistribution;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26 26
import eu.etaxonomy.cdm.model.common.Language;
27
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
27 28
import eu.etaxonomy.cdm.model.description.Distribution;
28 29
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
29 30
import eu.etaxonomy.cdm.model.location.NamedArea;
......
116 117

  
117 118

  
118 119

  
119
        foreignStatusUuids = new HashSet<UUID>();
120
        foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED().getUuid());
121
        foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_NATURALIZED().getUuid());
122
        foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_ADVENTITIOUS().getUuid());
123
        foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_CULTIVATED().getUuid());
124
        foreignStatusUuids.add(PresenceAbsenceTerm.NATURALISED().getUuid());
125
        foreignStatusUuids.add(PresenceAbsenceTerm.CULTIVATED().getUuid());
120
//        foreignStatusUuids = new HashSet<UUID>();
121
//        foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED().getUuid());
122
//        foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_NATURALIZED().getUuid());
123
//        foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_ADVENTITIOUS().getUuid());
124
//        foreignStatusUuids.add(PresenceAbsenceTerm.INTRODUCED_CULTIVATED().getUuid());
125
//        foreignStatusUuids.add(PresenceAbsenceTerm.NATURALISED().getUuid());
126
//        foreignStatusUuids.add(PresenceAbsenceTerm.CULTIVATED().getUuid());
126 127

  
127 128
    }
128 129

  
......
139 140
            List<Language> langs) {
140 141

  
141 142
        //1. order by PresenceAbsenceTerms
142
        Map<PresenceAbsenceTerm, Collection<NamedArea>> byStatus = new HashMap<PresenceAbsenceTerm, Collection<NamedArea>>();
143
        for(Distribution d : filteredDistributions) {
144
            PresenceAbsenceTerm status = d.getStatus();
145
            if(status == null) {
146
                continue;
147
            }
148
            if(!byStatus.containsKey(status)) {
149
                byStatus.put(status, new HashSet<NamedArea>());
150
            }
151
            byStatus.get(status).add(d.getArea());
143
//        Map<PresenceAbsenceTerm, Collection<NamedArea>> byStatus = new HashMap<PresenceAbsenceTerm, Collection<NamedArea>>();
144
//        for(Distribution d : filteredDistributions) {
145
//            PresenceAbsenceTerm status = d.getStatus();
146
//            if(status == null) {
147
//                continue;
148
//            }
149
//            if(!byStatus.containsKey(status)) {
150
//                byStatus.put(status, new HashSet<NamedArea>());
151
//            }
152
//            byStatus.get(status).add(d.getArea());
153
//        }
154

  
155
//        //2. build the area hierarchy
156
//        for(PresenceAbsenceTerm status : byStatus.keySet()) {
157
//
158
//            Map<NamedArea, AreaNode> areaNodeMap = new HashMap<NamedArea, AreaNode>();
159
//
160
//            for(NamedArea area : byStatus.get(status)) {
161
//                AreaNode node;
162
//                if(!areaNodeMap.containsKey(area)) {
163
//                    // putting area into hierarchy as node
164
//                    node = new AreaNode(area);
165
//                    areaNodeMap.put(area, node);
166
//                } else {
167
//                    //  is parent of another and thus already has a node
168
//                    node = areaNodeMap.get(area);
169
//                }
170
//
171
//                NamedArea parent = findParentIn(area, byStatus.get(status));
172
//                if(parent != null) {
173
//                    AreaNode parentNode;
174
//                    if(!areaNodeMap.containsKey(parent)) {
175
//                        parentNode = new AreaNode(parent);
176
//                        areaNodeMap.put(parent, parentNode);
177
//                    } else {
178
//                        parentNode = areaNodeMap.get(parent);
179
//                    }
180
//                    parentNode.addSubArea(node);
181
//                }
182
//            }
183
//
184
//            //3. find root nodes
185
//            Set<AreaNode>hierarchy = new HashSet<AreaNode>();
186
//            for(AreaNode node : areaNodeMap.values()) {
187
//                if(!node.hasParent()) {
188
//                    hierarchy.add(node);
189
//                }
190
//            }
191

  
192
        if (filteredDistributions == null || filteredDistributions.isEmpty()){
193
            return condensedDistribution;
152 194
        }
195
        OrderedTermVocabulary<NamedArea> areaVocabulary = CdmBase.deproxy(filteredDistributions.iterator().next().getArea().getVocabulary(), OrderedTermVocabulary.class);
196
        for (NamedArea area : areaVocabulary.getOrderedTerms()){
153 197

  
154
        //2. build the area hierarchy
155
        for(PresenceAbsenceTerm status : byStatus.keySet()) {
156

  
157
            Map<NamedArea, AreaNode> areaNodeMap = new HashMap<NamedArea, AreaNode>();
158

  
159
            for(NamedArea area : byStatus.get(status)) {
160
                AreaNode node;
161
                if(!areaNodeMap.containsKey(area)) {
162
                    // putting area into hierarchy as node
163
                    node = new AreaNode(area);
164
                    areaNodeMap.put(area, node);
165
                } else {
166
                    //  is parent of another and thus already has a node
167
                    node = areaNodeMap.get(area);
168
                }
169

  
170
                NamedArea parent = findParentIn(area, byStatus.get(status));
171
                if(parent != null) {
172
                    AreaNode parentNode;
173
                    if(!areaNodeMap.containsKey(parent)) {
174
                        parentNode = new AreaNode(parent);
175
                        areaNodeMap.put(parent, parentNode);
176
                    } else {
177
                        parentNode = areaNodeMap.get(parent);
178
                    }
179
                    parentNode.addSubArea(node);
180
                }
198
            if (area.getPartOf() != null){
199
                continue;  //subarea are handled later
181 200
            }
201
            StringBuilder areaStatusString = new StringBuilder();
202
            Distribution distribution = getDistribution(area, filteredDistributions);
203
            PresenceAbsenceTerm status = distribution.getStatus();
182 204

  
183
            //3. find root nodes
184
            Set<AreaNode>hierarchy = new HashSet<AreaNode>();
185
            for(AreaNode node : areaNodeMap.values()) {
186
                if(!node.hasParent()) {
187
                    hierarchy.add(node);
188
                }
189
            }
190

  
191
            //4. replace the area by the abbreviated representation and add symbols
192
            for(AreaNode node : hierarchy) {
193

  
194
                StringBuilder areaStatusString = new StringBuilder();
205
            String statusSymbol = statusSymbol(status);
206
            areaStatusString.append(statusSymbol);
195 207

  
196
                String statusSymbol = statusSymbol(status);
197
                areaStatusString.append(statusSymbol);
198

  
199
                String areaLabel = node.area.getPreferredRepresentation(langs).getAbbreviatedLabel();
200
                areaStatusString.append(areaLabel);
201

  
202
                if(!node.subAreas.isEmpty()) {
203
                    areaStatusString.append('(');
204
                    subAreaLabels(langs, node.subAreas, areaStatusString, statusSymbol, areaLabel);
205
                    areaStatusString.append(')');
206
                }
207

  
208
                if(isForeignStatus(status)) {
209
                    condensedDistribution.addForeignDistributionItem(status, areaStatusString.toString(), areaLabel);
210
                } else {
211
                    condensedDistribution.addIndigenousDistributionItem(status, areaStatusString.toString(), areaLabel);
212
                }
208
            String areaLabel = area.getIdInVocabulary();  //TODO if not exist use abbrevLabel
209
            areaStatusString.append(areaLabel);
213 210

  
211
            if(!area.getIncludes().isEmpty()) {
212
                areaStatusString.append('(');
213
                subAreaLabels(langs, area.getIncludes(), areaStatusString, statusSymbol, areaLabel);
214
                areaStatusString.append(')');
214 215
            }
215 216

  
217
//            if(isForeignStatus(status)) {
218
//                condensedDistribution.addForeignDistributionItem(status, areaStatusString.toString(), areaLabel);
219
//            } else {
220
                condensedDistribution.addIndigenousDistributionItem(status, areaStatusString.toString(), areaLabel);
221
//            }
222

  
216 223
        }
224

  
225
//        }
217 226
//        //5. order the condensedDistributions alphabetically
218 227
//        // FIXME
219
        condensedDistribution.sortForeign();
220
        condensedDistribution.sortIndigenous();
228
//        condensedDistribution.sortForeign();
229
//        condensedDistribution.sortIndigenous();
221 230

  
222 231
        return condensedDistribution;
223 232
    }
224 233

  
234
    /**
235
     * @param area
236
     * @param filteredDistributions
237
     * @return
238
     */
239
    private Distribution getDistribution(NamedArea area, Collection<Distribution> filteredDistributions) {
240
        for (Distribution dist : filteredDistributions){
241
            if (dist.getArea() != null && dist.getArea().equals(area)){
242
                return dist;
243
            }
244
        }
245
        return null;
246
    }
247

  
225 248
    /**
226 249
     * @param status
227 250
     * @return
......
247 270
     * @param areaString
248 271
     * @param statusSymbol
249 272
     */
250
    private void subAreaLabels(List<Language> langs, Collection<AreaNode> nodes, StringBuilder areaString, String statusSymbol, String parentLabel) {
251

  
273
    private void subAreaLabels(List<Language> langs, Collection<NamedArea> nodes, StringBuilder areaString, String statusSymbol, String parentLabel) {
274
        //TODO very redundant with main method
252 275
        List<String> subAreaLabels = new ArrayList<String>();
253 276

  
254
        for(AreaNode node : nodes) {
277
        for(NamedArea area : nodes) {
255 278

  
256 279
            StringBuilder subAreaString = new StringBuilder();
257 280

  
258 281
            subAreaString.append(statusSymbol);
259 282

  
260
            String areaLabel = node.area.getPreferredRepresentation(langs).getAbbreviatedLabel();
283
            String areaLabel = area.getIdInVocabulary() != null ? area.getIdInVocabulary() :area.getPreferredRepresentation(langs).getAbbreviatedLabel();
261 284
            String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""});
262 285
            subAreaString.append(cleanSubAreaLabel);
263 286

  
264
            if(!node.subAreas.isEmpty()) {
287
            if(!area.getIncludes().isEmpty()) {
265 288
                subAreaString.append('(');
266
                subAreaLabels(langs, node.subAreas, subAreaString, statusSymbol, areaLabel);
289
                subAreaLabels(langs, area.getIncludes(), subAreaString, statusSymbol, areaLabel);
267 290
                subAreaString.append(')');
268 291
            }
269 292

  
270 293
            subAreaLabels.add(subAreaString.toString());
271 294
        }
272
        Collections.sort(subAreaLabels);
295
//        Collections.sort(subAreaLabels);
273 296
        areaString.append(StringUtils.join(subAreaLabels, " "));
274 297

  
275 298
    }

Also available in: Unified diff