Project

General

Profile

Download (2.99 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.persistence.dto;
10

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

    
18
import eu.etaxonomy.cdm.model.name.Rank;
19
import eu.etaxonomy.cdm.model.taxon.Classification;
20

    
21
/**
22
 * @author a.kohlbecker
23
 * @since Sep 3, 2015
24
 *
25
 */
26
public class ClassificationLookupDTO {
27

    
28
    private final Map<Integer, Integer> taxonIdToParentId = new HashMap<>();
29
    private final Map<Rank,Set<Integer>> taxonIdByRank = new HashMap<>();
30
    private final Map<Integer,Set<Integer>> childTaxonMap = new HashMap<>();
31
    private Classification classification = null;
32

    
33
    /**
34
     * @return the taxonIds
35
     */
36
    public Set<Integer> getTaxonIds() {
37
        return taxonIdToParentId.keySet();
38
    }
39

    
40
    /**
41
     * @return the taxonIdByRank
42
     */
43
    public Map<Rank, Set<Integer>> getTaxonIdByRank() {
44
        return taxonIdByRank;
45
    }
46

    
47
    /**
48
     * @return the childTaxonMap
49
     */
50
    public Map<Integer, Set<Integer>> getChildTaxonMap() {
51
        return childTaxonMap;
52
    }
53

    
54
    /**
55
     * @return the classification
56
     */
57
    public Classification getClassification() {
58
        return classification;
59
    }
60

    
61
    /**
62
     *
63
     * @param classification
64
     *      Must never be null the ClassificationLookupDTO always specific to one
65
     *      Classification.
66
     */
67
    public ClassificationLookupDTO(Classification classification) {
68
        this.classification  = classification;
69
    }
70

    
71
    public void add(Integer taxonId, Rank rank, Integer parentId) {
72

    
73
        taxonIdToParentId.put(taxonId, parentId);
74

    
75
        if(!childTaxonMap.containsKey(parentId)) {
76
            childTaxonMap.put(parentId, new HashSet<Integer>());
77
        }
78
        childTaxonMap.get(parentId).add(taxonId);
79

    
80
        if(!taxonIdByRank.containsKey(rank)) {
81
            taxonIdByRank.put(rank, new HashSet<Integer>());
82
        }
83
        taxonIdByRank.get(rank).add(taxonId);
84
    }
85

    
86
    public void dropRank(Rank rank) {
87
        Collection<Integer> idsForRank = taxonIdByRank.get(rank);
88
        taxonIdByRank.remove(rank);
89

    
90
        if(idsForRank != null) {
91
            for(Integer taxonId : idsForRank) {
92
                Integer parentId = taxonIdToParentId.get(taxonId);
93
                taxonIdToParentId.remove(taxonId);
94
                childTaxonMap.remove(parentId);
95
            }
96
        }
97
    }
98

    
99
    /**
100
     * Drops all ranks from the classifiacationLookupDTO except those
101
     * listed in ranks.
102
     *
103
     * @param includeRanks
104
     */
105
    public void filterInclude(List<Rank> includeRanks) {
106

    
107
       Set<Rank> rankSet = new HashSet<>(taxonIdByRank.keySet());
108
       for(Rank rank : rankSet) {
109
           if(!includeRanks.contains(rank)) {
110
               dropRank(rank);
111
           }
112
       }
113
    }
114

    
115
}
(2-2/16)