Project

General

Profile

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

    
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Collection;
6
import java.util.List;
7

    
8
import com.vaadin.data.Item;
9
import com.vaadin.data.util.IndexedContainer;
10

    
11
import eu.etaxonomy.cdm.api.service.IClassificationService;
12
import eu.etaxonomy.cdm.model.name.Rank;
13
import eu.etaxonomy.cdm.model.taxon.Classification;
14
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
15
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
16

    
17
public class TaxonNodeContainer extends IndexedContainer {
18

    
19
	public static final String LABEL = "label";
20
	
21
	/**
22
	 * Creates a new taxon node container
23
	 * @param parentNode the parent node which will <b>not</b> be included
24
	 * in the result but only its child nodes
25
	 */
26
	public TaxonNodeContainer(TaxonNode parentNode) {
27
		List<TaxonNode> taxonNodeList = getTaxonNodeList(parentNode);
28
		addContainerProperty(LABEL, String.class, "[no taxon]");
29
		for (TaxonNode taxonNode : taxonNodeList) {
30
			Item item = addItem(taxonNode);
31
			if(taxonNode.getTaxon()!=null){
32
				item.getItemProperty(LABEL).setValue(taxonNode.getTaxon().getName().getTitleCache());
33
			}
34
			else if(taxonNode.getClassification()!=null){
35
				item.getItemProperty(LABEL).setValue(taxonNode.getClassification().getName().getText());
36
			}
37
		}
38
	}
39
	
40
	public List<TaxonNode> getTaxonNodeList(TaxonNode parentNode) {
41
		List<TaxonNode> nodes = new ArrayList<TaxonNode>();
42
		
43
		List<String> nodeInitStrategy = Arrays.asList(new String[]{
44
	            "taxon.sec",
45
	            "taxon.name",
46
	            "classification"
47
	    });
48

    
49
		if(parentNode==null){
50
			//just load classifications
51
			IClassificationService classificationService = CdmSpringContextHelper.getClassificationService();
52
			List<Classification> classificationList = classificationService.listClassifications(null, null, null, nodeInitStrategy);
53
			for (Classification classification : classificationList) {
54
				nodes.add(classification.getRootNode());
55
			}
56
		}
57
		else{
58
			//load child nodes
59
			nodes.addAll(addChildNodes(parentNode));
60
		}
61
		return nodes;
62
	}
63

    
64
	private Collection<? extends TaxonNode> addChildNodes(TaxonNode parentNode) {
65
		List<TaxonNode> nodes = new ArrayList<TaxonNode>();
66
		for (TaxonNode taxonNode : parentNode.getChildNodes()) {
67
			if(taxonNode!=null && taxonNode.getTaxon()!=null && taxonNode.getTaxon().getName()!=null){
68
				Rank rank = taxonNode.getTaxon().getName().getRank();
69
				if(rank!=null && rank.isHigher(Rank.SPECIES())){
70
					nodes.add(taxonNode);
71
					addChildNodes(taxonNode);
72
				}
73
			}
74
		}
75
		return nodes;
76
	}
77
	
78
}
(5-5/5)