Project

General

Profile

Download (9.34 KB) Statistics
| Branch: | Tag: | Revision:
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.DefinedTermBase;
27
import eu.etaxonomy.cdm.model.common.Language;
28
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
29
import eu.etaxonomy.cdm.model.common.Representation;
30
import eu.etaxonomy.cdm.model.description.Distribution;
31
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
32
import eu.etaxonomy.cdm.model.location.NamedArea;
33

    
34
/**
35
 * @author a.mueller
36
 * @date Apr 05, 2016
37
 *
38
 */
39
public class FloraCubaCondensedDistributionComposer implements ICondensedDistributionComposer {
40

    
41
    @SuppressWarnings("unused")
42
    private static final Logger logger = Logger.getLogger(FloraCubaCondensedDistributionComposer.class);
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/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> langs) {
69
        CondensedDistribution condensedDistribution = new CondensedDistribution();
70

    
71
        //empty
72
        if (filteredDistributions == null || filteredDistributions.isEmpty()){
73
            return condensedDistribution;
74
        }
75

    
76
        OrderedTermVocabulary<NamedArea> areaVocabulary = CdmBase.deproxy(filteredDistributions.iterator().next().getArea().getVocabulary(), OrderedTermVocabulary.class);
77

    
78
        //deproxy and reverse order
79
        List<NamedArea> areaList = new ArrayList<NamedArea>();
80
        for (DefinedTermBase<NamedArea> dtb : areaVocabulary.getOrderedTerms()){
81
            areaList.add(0, (NamedArea)CdmBase.deproxy(dtb));
82
        }
83

    
84
        for (NamedArea area : areaList){
85

    
86
            if (area.getPartOf() != null){
87
                continue;  //subarea are handled later
88
            }
89
            StringBuilder areaStatusString = new StringBuilder();
90
            Distribution distribution = getDistribution(area, filteredDistributions);
91
            if (distribution == null){
92
                continue;
93
            }
94

    
95
            PresenceAbsenceTerm status = distribution.getStatus();
96

    
97
            String statusSymbol = statusSymbol(status);
98
            areaStatusString.append(statusSymbol);
99

    
100
            String areaLabel = makeAreaLabel(langs, area);
101
            areaStatusString.append(areaLabel);
102

    
103
            if(!area.getIncludes().isEmpty()) {
104
//                areaStatusString.append('(');
105
                subAreaLabels(langs, area.getIncludes(), areaStatusString, statusSymbol, areaLabel, filteredDistributions);
106
//                areaStatusString.append(')');
107
            }
108

    
109
//            if(isForeignStatus(status)) {
110
//                condensedDistribution.addForeignDistributionItem(status, areaStatusString.toString(), areaLabel);
111
//            } else {
112
                condensedDistribution.addIndigenousDistributionItem(status, areaStatusString.toString(), areaLabel);
113
//            }
114

    
115
        }
116

    
117
//        }
118
//        //5. order the condensedDistributions alphabetically
119
//        // FIXME
120
//        condensedDistribution.sortForeign();
121
//        condensedDistribution.sortIndigenous();
122

    
123
        return condensedDistribution;
124
    }
125

    
126
    /**
127
     * @param area
128
     * @param filteredDistributions
129
     * @return
130
     */
131
    private Distribution getDistribution(NamedArea area, Collection<Distribution> filteredDistributions) {
132
        for (Distribution dist : filteredDistributions){
133
            if (dist.getArea() != null && dist.getArea().equals(area)){
134
                return dist;
135
            }
136
        }
137
        return null;
138
    }
139

    
140
    /**
141
     * @param status
142
     * @return
143
     */
144
    private String statusSymbol(PresenceAbsenceTerm status) {
145
        if(status == null) {
146
            return "";
147
        }
148
        String symbol = statusSymbols.get(status.getUuid());
149
        if(symbol != null) {
150
            return symbol;
151
        }else if (status.getSymbol() != null){
152
            return status.getSymbol();
153
        }else if (status.getIdInVocabulary() != null){
154
            return status.getIdInVocabulary();
155
        }else {
156
            Representation r = status.getPreferredRepresentation((Language)null);
157
            if (r != null){
158
                String abbrevLabel = r.getAbbreviatedLabel();
159
                if (abbrevLabel != null){
160
                    return abbrevLabel;
161
                }
162
            }
163
        }
164

    
165
        return "n.a.";
166
    }
167

    
168
//    private boolean isForeignStatus(PresenceAbsenceTerm status) {
169
//        return foreignStatusUuids.contains(status.getUuid());
170
//    }
171

    
172
    /**
173
     * @param langs
174
     * @param node
175
     * @param areaString
176
     * @param statusSymbol
177
     */
178
    private void subAreaLabels(List<Language> langs, Collection<NamedArea> subAreas, StringBuilder areaString,
179
            String statusSymbol, String parentLabel,
180
            Collection<Distribution> filteredDistributions) {
181
        //TODO very redundant with main method
182
        List<String> subAreaLabels = new ArrayList<String>();
183

    
184
        //deproxy and reverse order
185
        List<NamedArea> areaList = new ArrayList<NamedArea>();
186
        for (DefinedTermBase<NamedArea> dtb : subAreas){
187
            areaList.add(0, (NamedArea)CdmBase.deproxy(dtb));
188
        }
189

    
190
        for(NamedArea area : areaList) {
191

    
192
            StringBuilder subAreaString = new StringBuilder();
193
            Distribution distribution = getDistribution(area, filteredDistributions);
194
            if (distribution == null){
195
                continue;
196
            }
197

    
198

    
199
            PresenceAbsenceTerm status = distribution.getStatus();
200
            String subAreaStatusSymbol = statusSymbol(status);
201
            if (subAreaStatusSymbol != null && !subAreaStatusSymbol.equals(statusSymbol)){
202
                subAreaString.append(subAreaStatusSymbol);
203
            }
204

    
205
            String areaLabel = makeAreaLabel(langs, area);
206
//            String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""});
207
            String cleanSubAreaLabel = areaLabel;
208
            subAreaString.append(cleanSubAreaLabel);
209

    
210
            if(!area.getIncludes().isEmpty()) {
211
//                subAreaString.append('(');
212
                subAreaLabels(langs, area.getIncludes(), subAreaString, subAreaStatusSymbol, areaLabel, filteredDistributions);
213
//                subAreaString.append(')');
214
            }
215

    
216
            subAreaLabels.add(subAreaString.toString());
217
        }
218

    
219
//      Collections.sort(subAreaLabels);
220
        if (!subAreaLabels.isEmpty()){
221
            areaString.append("(" + StringUtils.join(subAreaLabels, " ") + ")");
222
        }
223

    
224
    }
225

    
226
    /**
227
     * @param langs
228
     * @param area
229
     * @return
230
     */
231
    private String makeAreaLabel(List<Language> langs, NamedArea area) {
232
        return area.getIdInVocabulary() != null ? area.getIdInVocabulary() :area.getPreferredRepresentation(langs).getAbbreviatedLabel();
233
    }
234

    
235
    /**
236
     * Searches for the parent are of the area given as parameter in
237
     * the Collection of areas.
238
     *
239
     * @parent area
240
     *      The area whose parent area is to be searched
241
     * @param collection
242
     *      The areas to search in.
243
     *
244
     * @return
245
     *      Either the parent if it has been found or null.
246
     */
247
    private NamedArea findParentIn(NamedArea area, Collection<NamedArea> areas) {
248
        NamedArea parent = area.getPartOf();
249
        if(parent != null && areas.contains(parent)){
250
            return parent;
251
        }
252
        return null;
253
    }
254

    
255
    class AreaNode {
256

    
257
        private final NamedArea area;
258
        private AreaNode parent = null;
259
        private final Set<AreaNode> subAreas = new HashSet<AreaNode>();
260

    
261
        /**
262
         * @param area
263
         */
264
        public AreaNode(NamedArea area) {
265
            this.area = area;
266
        }
267

    
268
        public void addSubArea(AreaNode subArea) {
269
            subAreas.add(subArea);
270
            subArea.parent = this;
271
        }
272

    
273
        public AreaNode getParent() {
274
            return parent;
275
        }
276

    
277
        public boolean hasParent() {
278
            return getParent() != null;
279
        }
280

    
281
        public Collection<NamedArea> getSubareas() {
282
            Collection<NamedArea> areas = new HashSet<NamedArea>();
283
            for(AreaNode node : subAreas) {
284
                areas.add(node.area);
285
            }
286
            return areas;
287
        }
288
    }
289

    
290
}
(5-5/12)