1
|
package eu.etaxonomy.cdm.vaadin.container;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.List;
|
6
|
|
7
|
import com.vaadin.data.Item;
|
8
|
import com.vaadin.data.util.HierarchicalContainer;
|
9
|
|
10
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
11
|
import eu.etaxonomy.cdm.model.name.Rank;
|
12
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
13
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
14
|
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
|
15
|
|
16
|
public class TaxonNodeContainer extends HierarchicalContainer {
|
17
|
|
18
|
private static final long serialVersionUID = 102401340698963360L;
|
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
|
addContainerProperty(LABEL, String.class, "[no taxon]");
|
28
|
getTaxonNodeList(parentNode);
|
29
|
}
|
30
|
|
31
|
public void getTaxonNodeList(TaxonNode parentNode) {
|
32
|
List<TaxonNode> nodes = new ArrayList<TaxonNode>();
|
33
|
|
34
|
List<String> nodeInitStrategy = Arrays.asList(new String[]{
|
35
|
"taxon.sec",
|
36
|
"taxon.name",
|
37
|
"classification"
|
38
|
});
|
39
|
|
40
|
if(parentNode==null){
|
41
|
//just load classifications
|
42
|
IClassificationService classificationService = CdmSpringContextHelper.getClassificationService();
|
43
|
List<Classification> classificationList = classificationService.listClassifications(null, null, null, nodeInitStrategy);
|
44
|
for (Classification classification : classificationList) {
|
45
|
TaxonNode rootNode = classification.getRootNode();
|
46
|
Item item = addItem(rootNode);
|
47
|
setChildrenAllowed(rootNode, false);
|
48
|
nodes.add(rootNode);
|
49
|
if(rootNode.getClassification()!=null){
|
50
|
item.getItemProperty(LABEL).setValue(rootNode.getClassification().getName().getText());
|
51
|
}
|
52
|
}
|
53
|
}
|
54
|
else{
|
55
|
//load child nodes
|
56
|
addChildNodes(parentNode);
|
57
|
}
|
58
|
}
|
59
|
|
60
|
private void addChildNodes(TaxonNode parentNode) {
|
61
|
List<TaxonNode> childNodes = parentNode.getChildNodes();
|
62
|
setChildrenAllowed(parentNode, !childNodes.isEmpty());
|
63
|
boolean hasValidChildren = false;
|
64
|
for (TaxonNode taxonNode : childNodes) {
|
65
|
if(taxonNode!=null && taxonNode.getTaxon()!=null && taxonNode.getTaxon().getName()!=null){
|
66
|
Rank rank = taxonNode.getTaxon().getName().getRank();
|
67
|
if(rank!=null && rank.isHigher(Rank.SPECIES())){
|
68
|
Item item = addItem(taxonNode);
|
69
|
setParent(taxonNode, parentNode);
|
70
|
if(taxonNode.getTaxon()!=null){
|
71
|
item.getItemProperty(LABEL).setValue(taxonNode.getTaxon().getName().getTitleCache());
|
72
|
}
|
73
|
else if(taxonNode.getClassification()!=null){
|
74
|
item.getItemProperty(LABEL).setValue(taxonNode.getClassification().getName().getText());
|
75
|
}
|
76
|
hasValidChildren = true;
|
77
|
|
78
|
addChildNodes(taxonNode);
|
79
|
}
|
80
|
}
|
81
|
}
|
82
|
setChildrenAllowed(parentNode, hasValidChildren);
|
83
|
}
|
84
|
|
85
|
}
|