Project

General

Profile

Download (3.05 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.persistence.dto;
11

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

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

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

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

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

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

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

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

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

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

    
74
        taxonIdToParentId.put(taxonId, parentId);
75

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

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

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

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

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

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

    
116
}
(1-1/6)