Project

General

Profile

Download (7.54 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2015 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.ext.geo;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.UUID;
16

    
17
import org.apache.commons.lang.StringUtils;
18
import org.apache.log4j.Logger;
19

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

    
30
/**
31
 * @author a.mueller
32
 * @date Apr 05, 2016
33
 *
34
 */
35
public class FloraCubaCondensedDistributionComposerOld extends CondensedDistributionComposerBase {
36

    
37
    @SuppressWarnings("unused")
38
    private static final Logger logger = Logger.getLogger(FloraCubaCondensedDistributionComposerOld.class);
39

    
40
//    private static Set<UUID> foreignStatusUuids;
41

    
42
    private UUID uuidInternalArea = UUID.fromString("d0144a6e-0e17-4a1d-bce5-d464a2aa7229");  //Cuba
43

    
44
    private String internalAreaSeparator = UTF8.EN_DASH.toString() + " ";
45

    
46

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

    
50
    static {
51

    
52
        // ==================================================
53
        // Mapping as defined in ticket http://dev.e-taxonomy.eu/trac/ticket/5682
54
        // ==================================================
55

    
56
       statusSymbols = new HashMap<UUID, String> ();
57
       //no entries as we handle symbols now on model level
58

    
59
    }
60

    
61
    /**
62
     * {@inheritDoc}
63
     * @return
64
     */
65
    @Override
66
    public CondensedDistribution createCondensedDistribution(Collection<Distribution> filteredDistributions,
67
            List<Language> languages) {
68

    
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

    
85
        boolean isFirstAfterInternalArea = false;
86
        for (NamedArea area : areaList){
87

    
88
            if (area.getPartOf() != null){
89
                continue;  //subarea are handled later
90
            }
91

    
92
            StringBuilder areaStatusString = new StringBuilder();
93

    
94

    
95

    
96
            Distribution distribution = getDistribution(area, filteredDistributions);
97
            if (distribution == null){
98
                continue;
99
            }
100

    
101
            if (area.getUuid().equals(uuidInternalArea)){
102
                isFirstAfterInternalArea = true;
103
            }else if(isFirstAfterInternalArea && !area.getUuid().equals(uuidInternalArea)){
104
                areaStatusString.append(internalAreaSeparator);
105
                isFirstAfterInternalArea = false;
106
            }
107

    
108
            PresenceAbsenceTerm status = distribution.getStatus();
109

    
110
            String statusSymbol = statusSymbol(status);
111
            areaStatusString.append(statusSymbol);
112

    
113
            String areaLabel = makeAreaLabel(languages, area);
114
            areaStatusString.append(areaLabel);
115

    
116
            if(!area.getIncludes().isEmpty()) {
117
//                areaStatusString.append('(');
118
                subAreaLabels(languages, area.getIncludes(), areaStatusString, statusSymbol, areaLabel, filteredDistributions);
119
//                areaStatusString.append(')');
120
            }
121

    
122

    
123
//            if(isForeignStatus(status)) {
124
//                condensedDistribution.addForeignDistributionItem(status, areaStatusString.toString(), areaLabel);
125
//            } else {
126
                condensedDistribution.addIndigenousDistributionItem(status, areaStatusString.toString(), areaLabel);
127
//            }
128

    
129
        }
130

    
131
//        }
132
//        //5. order the condensedDistributions alphabetically
133
//        // FIXME
134
//        condensedDistribution.sortForeign();
135
//        condensedDistribution.sortIndigenous();
136

    
137
        return condensedDistribution;
138
    }
139

    
140
    /**
141
     * @param area
142
     * @param filteredDistributions
143
     * @return
144
     */
145
    private Distribution getDistribution(NamedArea area, Collection<Distribution> filteredDistributions) {
146
        for (Distribution dist : filteredDistributions){
147
            if (dist.getArea() != null && dist.getArea().equals(area)){
148
                return dist;
149
            }
150
        }
151
        return null;
152
    }
153

    
154

    
155
//    private boolean isForeignStatus(PresenceAbsenceTerm status) {
156
//        return foreignStatusUuids.contains(status.getUuid());
157
//    }
158

    
159
    /**
160
     * @param langs
161
     * @param node
162
     * @param areaString
163
     * @param statusSymbol
164
     */
165
    private void subAreaLabels(List<Language> langs, Collection<NamedArea> subAreas, StringBuilder areaString,
166
            String statusSymbol, String parentLabel,
167
            Collection<Distribution> filteredDistributions) {
168
        //TODO very redundant with main method
169
        List<String> subAreaLabels = new ArrayList<String>();
170

    
171
        //deproxy and reverse order
172
        List<NamedArea> areaList = new ArrayList<NamedArea>();
173
        for (DefinedTermBase<NamedArea> dtb : subAreas){
174
            areaList.add(0, (NamedArea)CdmBase.deproxy(dtb));
175
        }
176
//        Collections.sort(areaList);
177

    
178
        for(NamedArea area : areaList) {
179

    
180
            StringBuilder subAreaString = new StringBuilder();
181
            Distribution distribution = getDistribution(area, filteredDistributions);
182
            if (distribution == null){
183
                continue;
184
            }
185

    
186

    
187
            PresenceAbsenceTerm status = distribution.getStatus();
188
            String subAreaStatusSymbol = statusSymbol(status);
189
            if (subAreaStatusSymbol != null && !subAreaStatusSymbol.equals(statusSymbol)){
190
                subAreaString.append(subAreaStatusSymbol);
191
            }
192

    
193
            String areaLabel = makeAreaLabel(langs, area);
194
//            String cleanSubAreaLabel = StringUtils.replaceEach(areaLabel, new String[] {parentLabel, "(", ")"}, new String[] {"", "", ""});
195
            String cleanSubAreaLabel = areaLabel;
196
            subAreaString.append(cleanSubAreaLabel);
197

    
198
            if(!area.getIncludes().isEmpty()) {
199
//                subAreaString.append('(');
200
                subAreaLabels(langs, area.getIncludes(), subAreaString, subAreaStatusSymbol, areaLabel, filteredDistributions);
201
//                subAreaString.append(')');
202
            }
203

    
204
            subAreaLabels.add(subAreaString.toString());
205
        }
206

    
207
//      Collections.sort(subAreaLabels);
208
        if (!subAreaLabels.isEmpty()){
209
            areaString.append("(" + StringUtils.join(subAreaLabels, " ") + ")");
210
        }
211

    
212
    }
213

    
214

    
215
    public String getInternalAreaSeparator() {
216
        return internalAreaSeparator;
217
    }
218

    
219
    public void setInternalAreaSeparator(String internalAreaSeparator) {
220
        this.internalAreaSeparator = internalAreaSeparator;
221
    }
222

    
223

    
224

    
225

    
226

    
227

    
228
}
(7-7/14)