Project

General

Profile

Download (2.99 KB) Statistics
| Branch: | Tag: | Revision:
1 f4d35a72 Andreas Kohlbecker
/**
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 869c3f85 Andreas Kohlbecker
import java.util.List;
15 f4d35a72 Andreas Kohlbecker
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 53db84af Andreas Müller
 * @since Sep 3, 2015
24 f4d35a72 Andreas Kohlbecker
 *
25
 */
26
public class ClassificationLookupDTO {
27
28 4820d02b Andreas Müller
    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 f4d35a72 Andreas Kohlbecker
    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 869c3f85 Andreas Kohlbecker
    public Map<Rank, Set<Integer>> getTaxonIdByRank() {
44 f4d35a72 Andreas Kohlbecker
        return taxonIdByRank;
45
    }
46
47
    /**
48
     * @return the childTaxonMap
49
     */
50 869c3f85 Andreas Kohlbecker
    public Map<Integer, Set<Integer>> getChildTaxonMap() {
51 f4d35a72 Andreas Kohlbecker
        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 09023b8d Andreas Kohlbecker
        if(idsForRank != null) {
91
            for(Integer taxonId : idsForRank) {
92
                Integer parentId = taxonIdToParentId.get(taxonId);
93
                taxonIdToParentId.remove(taxonId);
94
                childTaxonMap.remove(parentId);
95
            }
96 f4d35a72 Andreas Kohlbecker
        }
97
    }
98
99 869c3f85 Andreas Kohlbecker
    /**
100 09023b8d Andreas Kohlbecker
     * Drops all ranks from the classifiacationLookupDTO except those
101
     * listed in ranks.
102
     *
103
     * @param includeRanks
104 869c3f85 Andreas Kohlbecker
     */
105 09023b8d Andreas Kohlbecker
    public void filterInclude(List<Rank> includeRanks) {
106
107 4820d02b Andreas Müller
       Set<Rank> rankSet = new HashSet<>(taxonIdByRank.keySet());
108 09023b8d Andreas Kohlbecker
       for(Rank rank : rankSet) {
109
           if(!includeRanks.contains(rank)) {
110
               dropRank(rank);
111
           }
112 869c3f85 Andreas Kohlbecker
       }
113
    }
114
115 f4d35a72 Andreas Kohlbecker
}