Project

General

Profile

« Previous | Next » 

Revision a59c51d2

Added by Andreas Kohlbecker almost 11 years ago

fixing bug in filterDistributions()

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/DescriptionServiceImpl.java
291 291
        distList.clear();
292 292
        distList.addAll(filteredDistributions);
293 293

  
294
        //ordering the areas
294
        //order by areas
295 295
        tree.orderAsTree(distList, omitLevels);
296 296
        tree.sortChildren();
297 297
        return tree;
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/utility/DescriptionUtility.java
9 9
*/
10 10
package eu.etaxonomy.cdm.api.utility;
11 11

  
12
import java.util.ArrayList;
12 13
import java.util.Collection;
13 14
import java.util.HashMap;
14 15
import java.util.HashSet;
15 16
import java.util.Map;
16 17
import java.util.Set;
17 18

  
19
import eu.etaxonomy.cdm.model.common.CdmBase;
18 20
import eu.etaxonomy.cdm.model.common.MarkerType;
19 21
import eu.etaxonomy.cdm.model.description.Distribution;
20 22
import eu.etaxonomy.cdm.model.location.NamedArea;
......
46 48
     */
47 49
    public static Collection<Distribution> filterDistributions(Collection<Distribution> distributions) {
48 50

  
49
        Map<String, Distribution> computedDistributions = new HashMap<String, Distribution>(distributions.size());
50
        Map<String, Distribution> otherDistributions = new HashMap<String, Distribution>(distributions.size());
51
        Map<String, Set<Distribution>> computedDistributions = new HashMap<String, Set<Distribution>>(distributions.size());
52
        Map<String, Set<Distribution>> otherDistributions = new HashMap<String, Set<Distribution>>(distributions.size());
53
        Set<Distribution> removeCandidates = new HashSet<Distribution>();
51 54

  
52 55
        // 1. sort by computed / not computed
53 56
        for(Distribution distribution : distributions){
54 57
            if(distribution.hasMarker(MarkerType.COMPUTED(), true)){
55
                computedDistributions.put(areaKey(distribution), distribution);
58
                if(!computedDistributions.containsKey(areaKey(distribution))){
59
                    computedDistributions.put(areaKey(distribution), new HashSet<Distribution>());
60
                }
61
                computedDistributions.get(areaKey(distribution)).add(distribution);
56 62
            } else {
57
                otherDistributions.put(areaKey(distribution), distribution);
63
                if(!otherDistributions.containsKey(areaKey(distribution))){
64
                    otherDistributions.put(areaKey(distribution), new HashSet<Distribution>());
65
                }
66
                otherDistributions.get(areaKey(distribution)).add(distribution);
58 67
            }
59 68
        }
60 69

  
61
        // if no computed elements return all
62
        if(computedDistributions.size() == 0){
63
            return otherDistributions.values();
64
        }
70
        // if there are computed elements apply the filter rules
71
        if(computedDistributions.size() > 0){
65 72

  
66
        // 2. apply the filter rules
67
        // remove all not computed areas for which a computed area exists
68
        for(String keyComputed : computedDistributions.keySet()){
69
            otherDistributions.remove(otherDistributions);
70
        }
71
        Set<Distribution> removeCandidates = new HashSet<Distribution>();
72
        for(Distribution distribution : computedDistributions.values()){
73
            if(distribution.getArea() != null){
74
                NamedArea parentArea = distribution.getArea().getPartOf();
75
                while(parentArea != null){
76
                    Distribution parentDistribution = computedDistributions.get(areaKey(parentArea));
77
                    if(parentDistribution != null && parentDistribution.getStatus().equals(distribution.getStatus())){
78
                        removeCandidates.add(parentDistribution);
73
            // 2. apply the filter rules
74
            // prepare removal of all not computed areas for which a computed area exists
75
            for(String keyComputed : computedDistributions.keySet()){
76
                otherDistributions.remove(otherDistributions);
77
            }
79 78

  
79
            for(Distribution distribution : valuesOfAllInnerSets(computedDistributions.values())){
80
                if(distribution.getArea() != null){
81
                    NamedArea parentArea = distribution.getArea().getPartOf();
82
                    while(parentArea != null){
83
                        for(Distribution parentDistribution : computedDistributions.get(areaKey(parentArea))) {
84
                            if(parentDistribution != null && parentDistribution.getStatus().equals(distribution.getStatus())){
85
                                removeCandidates.add(parentDistribution);
86
                            }
87
                        }
88
                        parentArea = parentArea.getPartOf();
80 89
                    }
81
                    parentArea = parentArea.getPartOf();
82 90
                }
83 91
            }
84 92
        }
93

  
94
        // finally remove computed distributions if necessary and combine computed and non computed distributions again
95
        Set<Distribution> filteredDistributions = new HashSet<Distribution>(otherDistributions.size() + computedDistributions.size());
96
        filteredDistributions.addAll(valuesOfAllInnerSets(computedDistributions.values()));
85 97
        for(Distribution distribution : removeCandidates){
86 98
            computedDistributions.remove(areaKey(distribution));
87 99
        }
88 100

  
89
        // finally combine computed and non computed distributions again
90
        Set<Distribution> filteredDistributions = new HashSet<Distribution>(otherDistributions.size() + computedDistributions.size());
91
        filteredDistributions.addAll(otherDistributions.values());
92
        filteredDistributions.addAll(computedDistributions.values());
101
        filteredDistributions.addAll(valuesOfAllInnerSets(otherDistributions.values()));
93 102

  
94 103
        return filteredDistributions;
95 104

  
96 105
    }
97 106

  
107
    private static <T extends CdmBase> Collection<T> valuesOfAllInnerSets(Collection<Set<T>> collectionOfSets){
108
        Collection<T> allValues = new ArrayList<T>();
109
        for(Set<T> set : collectionOfSets){
110
            allValues.addAll(set);
111
        }
112
        return allValues;
113
    }
114

  
98 115
    private static String areaKey(NamedArea area){
99 116
        return String.valueOf(area.getUuid());
100 117
    }

Also available in: Unified diff