Project

General

Profile

Download (3.83 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2016 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.vaadin.container;
11

    
12
import java.sql.SQLException;
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.List;
18
import java.util.Map;
19

    
20
import com.vaadin.data.Container.Hierarchical;
21
import com.vaadin.data.util.filter.Compare;
22
import com.vaadin.data.util.sqlcontainer.RowId;
23

    
24
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
25
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
26

    
27
/**
28
 * @author pplitzner
29
 * @date 18.10.2016
30
 *
31
 */
32
public class TaxonTreeContainer extends CdmSQLContainer implements Hierarchical{
33

    
34
    private static final long serialVersionUID = 5488629563366944491L;
35

    
36
    private final Collection<RowId> rootItemIds = new HashSet<>();
37
    private final Map<Object, List<Object>> parentChildMap = new HashMap<>();
38
    private final Map<Object, Object> childParentMap = new HashMap<>();
39
    private CdmSQLContainer childrenContainer;
40

    
41
    public TaxonTreeContainer(TaxonNode parentNode) throws SQLException {
42
        super(CdmQueryFactory.generateTaxonTreeQuery("Name", Integer.toString(parentNode.getClassification().getId())));
43
        childrenContainer = new CdmSQLContainer(CdmQueryFactory.generateTaxonTreeQuery("Name", Integer.toString(parentNode.getClassification().getId())));
44
        List<TaxonNode> childNodes = parentNode.getChildNodes();
45
        for (TaxonNode taxonNode : childNodes) {
46
            rootItemIds.add(new RowId(taxonNode.getId()));
47
        }
48
    }
49

    
50
    /**
51
     * {@inheritDoc}
52
     */
53
    @Override
54
    public Collection<?> getChildren(Object itemId) {
55
        List<Object> children = parentChildMap.get(itemId);
56
        if(children==null){
57
            children = updateChildren(itemId);
58
        }
59
        return children;
60
    }
61

    
62
    private List<Object> updateChildren(Object itemId) {
63
        List<Object> children;
64
        Filter childrenOfTaxonFilter = new Compare.Equal("tn.parent_id", Integer.valueOf(itemId.toString()));
65
        childrenContainer.addContainerFilter(childrenOfTaxonFilter);
66
        children = new ArrayList<>();
67
        Collection<?> itemIds = childrenContainer.getItemIds();
68
        for (Object object : itemIds) {
69
            childParentMap.put(object, itemId);
70
            children.add(object);
71
        }
72
        childrenContainer.removeAllContainerFilters();
73

    
74
        parentChildMap.put(itemId, children);
75
        return children;
76
    }
77

    
78
    /**
79
     * {@inheritDoc}
80
     */
81
    @Override
82
    public Object getParent(Object itemId) {
83
        return childParentMap.get(itemId);
84
    }
85

    
86
    /**
87
     * {@inheritDoc}
88
     */
89
    @Override
90
    public boolean hasChildren(Object itemId) {
91
        List<Object> children = parentChildMap.get(itemId);
92
        if(children==null){
93
            children = updateChildren(itemId);
94
        }
95
        return !children.isEmpty();
96
    }
97

    
98
    /**
99
     * {@inheritDoc}
100
     */
101
    @Override
102
    public Collection<?> rootItemIds() {
103
        return rootItemIds;
104
    }
105

    
106
    /**
107
     * {@inheritDoc}
108
     */
109
    @Override
110
    public boolean setParent(Object itemId, Object newParentId) throws UnsupportedOperationException {
111
        return true;
112
    }
113

    
114
    /**
115
     * {@inheritDoc}
116
     */
117
    @Override
118
    public boolean areChildrenAllowed(Object itemId) {
119
        return hasChildren(itemId);
120
    }
121

    
122
    /**
123
     * {@inheritDoc}
124
     */
125
    @Override
126
    public boolean setChildrenAllowed(Object itemId, boolean areChildrenAllowed) throws UnsupportedOperationException {
127
        return true;
128
    }
129

    
130
    /**
131
     * {@inheritDoc}
132
     */
133
    @Override
134
    public boolean isRoot(Object itemId) {
135
        return itemId==null?true:rootItemIds.contains(itemId);
136
    }
137

    
138

    
139

    
140
}
(8-8/8)