Project

General

Profile

Download (8.41 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.Set;
18
import java.util.UUID;
19

    
20
import org.apache.commons.lang.StringUtils;
21
import org.apache.log4j.Logger;
22

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

    
32
/**
33
 * @author a.mueller
34
 * @date Apr 05, 2016
35
 *
36
 */
37
public class FloraCubaCondensedDistributionComposer extends CondensedDistributionComposerBase {
38

    
39
    @SuppressWarnings("unused")
40
    private static final Logger logger = Logger.getLogger(FloraCubaCondensedDistributionComposer.class);
41

    
42
    private static Set<UUID> foreignStatusUuids;
43

    
44
    // these status uuids are special for EuroPlusMed and might also be used
45
    private final static UUID REPORTED_IN_ERROR_UUID =  UUID.fromString("38604788-cf05-4607-b155-86db456f7680");
46

    
47
    static {
48

    
49
        // ==================================================
50
        // Mapping as defined in ticket http://dev.e-taxonomy.eu/trac/ticket/5682
51
        // ==================================================
52

    
53
       statusSymbols = new HashMap<UUID, String> ();
54
       //no entries as we handle symbols now on model level
55

    
56
    }
57

    
58
    /**
59
     * {@inheritDoc}
60
     * @return
61
     */
62
    @Override
63
    public CondensedDistribution createCondensedDistribution(Collection<Distribution> filteredDistributions,
64
            List<Language> langs) {
65

    
66
        CondensedDistribution condensedDistribution = new CondensedDistribution();
67

    
68
        //empty
69
        if (filteredDistributions == null || filteredDistributions.isEmpty()){
70
            return condensedDistribution;
71
        }
72

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

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

    
81
        for (NamedArea area : areaList){
82

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

    
92
            PresenceAbsenceTerm status = distribution.getStatus();
93

    
94
            String statusSymbol = statusSymbol(status);
95
            areaStatusString.append(statusSymbol);
96

    
97
            String areaLabel = makeAreaLabel(langs, area);
98
            areaStatusString.append(areaLabel);
99

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

    
106
//            if(isForeignStatus(status)) {
107
//                condensedDistribution.addForeignDistributionItem(status, areaStatusString.toString(), areaLabel);
108
//            } else {
109
                condensedDistribution.addIndigenousDistributionItem(status, areaStatusString.toString(), areaLabel);
110
//            }
111

    
112
        }
113

    
114
//        }
115
//        //5. order the condensedDistributions alphabetically
116
//        // FIXME
117
//        condensedDistribution.sortForeign();
118
//        condensedDistribution.sortIndigenous();
119

    
120
        return condensedDistribution;
121
    }
122

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

    
137

    
138
//    private boolean isForeignStatus(PresenceAbsenceTerm status) {
139
//        return foreignStatusUuids.contains(status.getUuid());
140
//    }
141

    
142
    /**
143
     * @param langs
144
     * @param node
145
     * @param areaString
146
     * @param statusSymbol
147
     */
148
    private void subAreaLabels(List<Language> langs, Collection<NamedArea> subAreas, StringBuilder areaString,
149
            String statusSymbol, String parentLabel,
150
            Collection<Distribution> filteredDistributions) {
151
        //TODO very redundant with main method
152
        List<String> subAreaLabels = new ArrayList<String>();
153

    
154
        //deproxy and reverse order
155
        List<NamedArea> areaList = new ArrayList<NamedArea>();
156
        for (DefinedTermBase<NamedArea> dtb : subAreas){
157
            areaList.add(0, (NamedArea)CdmBase.deproxy(dtb));
158
        }
159

    
160
        for(NamedArea area : areaList) {
161

    
162
            StringBuilder subAreaString = new StringBuilder();
163
            Distribution distribution = getDistribution(area, filteredDistributions);
164
            if (distribution == null){
165
                continue;
166
            }
167

    
168

    
169
            PresenceAbsenceTerm status = distribution.getStatus();
170
            String subAreaStatusSymbol = statusSymbol(status);
171
            if (subAreaStatusSymbol != null && !subAreaStatusSymbol.equals(statusSymbol)){
172
                subAreaString.append(subAreaStatusSymbol);
173
            }
174

    
175
            String areaLabel = makeAreaLabel(langs, area);
176
//            String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""});
177
            String cleanSubAreaLabel = areaLabel;
178
            subAreaString.append(cleanSubAreaLabel);
179

    
180
            if(!area.getIncludes().isEmpty()) {
181
//                subAreaString.append('(');
182
                subAreaLabels(langs, area.getIncludes(), subAreaString, subAreaStatusSymbol, areaLabel, filteredDistributions);
183
//                subAreaString.append(')');
184
            }
185

    
186
            subAreaLabels.add(subAreaString.toString());
187
        }
188

    
189
//      Collections.sort(subAreaLabels);
190
        if (!subAreaLabels.isEmpty()){
191
            areaString.append("(" + StringUtils.join(subAreaLabels, " ") + ")");
192
        }
193

    
194
    }
195

    
196
    /**
197
     * @param langs
198
     * @param area
199
     * @return
200
     */
201
    private String makeAreaLabel(List<Language> langs, NamedArea area) {
202
        return area.getIdInVocabulary() != null ? area.getIdInVocabulary() :area.getPreferredRepresentation(langs).getAbbreviatedLabel();
203
    }
204

    
205
    /**
206
     * Searches for the parent are of the area given as parameter in
207
     * the Collection of areas.
208
     *
209
     * @parent area
210
     *      The area whose parent area is to be searched
211
     * @param collection
212
     *      The areas to search in.
213
     *
214
     * @return
215
     *      Either the parent if it has been found or null.
216
     */
217
    private NamedArea findParentIn(NamedArea area, Collection<NamedArea> areas) {
218
        NamedArea parent = area.getPartOf();
219
        if(parent != null && areas.contains(parent)){
220
            return parent;
221
        }
222
        return null;
223
    }
224

    
225
    class AreaNode {
226

    
227
        private final NamedArea area;
228
        private AreaNode parent = null;
229
        private final Set<AreaNode> subAreas = new HashSet<AreaNode>();
230

    
231
        /**
232
         * @param area
233
         */
234
        public AreaNode(NamedArea area) {
235
            this.area = area;
236
        }
237

    
238
        public void addSubArea(AreaNode subArea) {
239
            subAreas.add(subArea);
240
            subArea.parent = this;
241
        }
242

    
243
        public AreaNode getParent() {
244
            return parent;
245
        }
246

    
247
        public boolean hasParent() {
248
            return getParent() != null;
249
        }
250

    
251
        public Collection<NamedArea> getSubareas() {
252
            Collection<NamedArea> areas = new HashSet<NamedArea>();
253
            for(AreaNode node : subAreas) {
254
                areas.add(node.area);
255
            }
256
            return areas;
257
        }
258
    }
259

    
260
}
(6-6/13)