Project

General

Profile

Download (2.44 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.vaadin.container;
2

    
3
import java.util.Collection;
4
import java.util.HashMap;
5
import java.util.Map;
6

    
7
import com.vaadin.data.Item;
8
import com.vaadin.data.util.HierarchicalContainer;
9

    
10
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
11
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
12
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
13

    
14
public class TaxonNodeContainer extends HierarchicalContainer {
15

    
16
	private static final long serialVersionUID = 102401340698963360L;
17
	public static final String LABEL = "titleCache";
18
	private static Map<Object, Object> itemCache = new HashMap<>();
19

    
20
	/**
21
     * Creates a new taxon node container
22
	 * @param roots the root elements of the table
23
	 */
24
	public TaxonNodeContainer(Collection<UuidAndTitleCache<TaxonNode>> roots) {
25
	    addContainerProperty(LABEL, String.class, "[no taxon]");
26
	    for (UuidAndTitleCache<TaxonNode> root: roots) {
27
	        addItem(root);
28
	        addChildItems(root);
29
        }
30
	}
31

    
32
	/**
33
	 * {@inheritDoc}
34
	 */
35
	@Override
36
	public Item addItem(Object itemId) {
37
	    if(itemId instanceof UuidAndTitleCache){
38
	        UuidAndTitleCache<TaxonNode> uuidAndTitleCache = (UuidAndTitleCache<TaxonNode>) itemId;
39
	        Item item = super.addItem(itemId);
40
	        item.getItemProperty(TaxonNodeContainer.LABEL).setValue(uuidAndTitleCache.getTitleCache());
41
	        itemCache.put(((UuidAndTitleCache<TaxonNode>) itemId).getId(), false);
42
	        return item;
43
        }
44
	    return null;
45
	}
46

    
47
    /**
48
     * @param parent
49
     */
50
    public void addChildItems(UuidAndTitleCache<TaxonNode> parent) {
51
        if(itemCache.get(parent.getId()).equals(Boolean.FALSE)){
52
            Collection<UuidAndTitleCache<TaxonNode>> children = CdmSpringContextHelper.getTaxonNodeService().listChildNodesAsUuidAndTitleCache(parent);
53
            setChildrenAllowed(parent, !children.isEmpty());
54
            for (UuidAndTitleCache<TaxonNode> child : children) {
55
                Item childItem = addItem(child);
56
                if(childItem!=null){
57
                    setParent(child, parent);
58
                }
59
                Collection<UuidAndTitleCache<TaxonNode>> grandChildren = CdmSpringContextHelper.getTaxonNodeService().listChildNodesAsUuidAndTitleCache(child);
60
                setChildrenAllowed(child, !grandChildren.isEmpty());
61
            }
62
            itemCache.put(parent.getId(), true);
63
        }
64
    }
65

    
66
    public void clearCache(){
67
        //TODO: when should the cache be cleared?
68
        itemCache = null;
69
    }
70

    
71
}
(7-7/7)