Project

General

Profile

Download (2.79 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
public class ClassificationLookupDTO {
26

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

    
32
    public Set<Integer> getTaxonIds() {
33
        return taxonIdToParentId.keySet();
34
    }
35

    
36
    public Map<Rank, Set<Integer>> getTaxonIdByRank() {
37
        return taxonIdByRank;
38
    }
39

    
40
    public Map<Integer, Set<Integer>> getChildTaxonMap() {
41
        return childTaxonMap;
42
    }
43

    
44
    public Classification getClassification() {
45
        return classification;
46
    }
47

    
48
    /**
49
     *
50
     * @param classification
51
     *      Must never be null the ClassificationLookupDTO always specific to one
52
     *      Classification.
53
     */
54
    public ClassificationLookupDTO(Classification classification) {
55
        this.classification  = classification;
56
    }
57

    
58
    public void add(Integer taxonId, Rank rank, Integer parentId) {
59

    
60
        taxonIdToParentId.put(taxonId, parentId);
61

    
62
        if(!childTaxonMap.containsKey(parentId)) {
63
            childTaxonMap.put(parentId, new HashSet<>());
64
        }
65
        childTaxonMap.get(parentId).add(taxonId);
66

    
67
        if(!taxonIdByRank.containsKey(rank)) {
68
            taxonIdByRank.put(rank, new HashSet<>());
69
        }
70
        taxonIdByRank.get(rank).add(taxonId);
71
    }
72

    
73
    public void dropRank(Rank rank) {
74
        Collection<Integer> idsForRank = taxonIdByRank.get(rank);
75
        taxonIdByRank.remove(rank);
76

    
77
        if(idsForRank != null) {
78
            for(Integer taxonId : idsForRank) {
79
                Integer parentId = taxonIdToParentId.get(taxonId);
80
                taxonIdToParentId.remove(taxonId);
81
                childTaxonMap.remove(parentId);
82
            }
83
        }
84
    }
85

    
86
    /**
87
     * Drops all ranks from the classifiacationLookupDTO except those
88
     * listed in ranks.
89
     *
90
     * @param includeRanks
91
     */
92
    public void filterInclude(List<Rank> includeRanks) {
93

    
94
       Set<Rank> rankSet = new HashSet<>(taxonIdByRank.keySet());
95
       for(Rank rank : rankSet) {
96
           if(!includeRanks.contains(rank)) {
97
               dropRank(rank);
98
           }
99
       }
100
    }
101

    
102
}
(4-4/26)