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.HierarchicalContainer;
|
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 HierarchicalContainer {
|
18
|
|
19
|
private static final long serialVersionUID = 102401340698963360L;
|
20
|
public static final String LABEL = "label";
|
21
|
|
22
|
private final List<String> nodeInitStrategy = Arrays.asList(new String[]{
|
23
|
"taxon.sec",
|
24
|
"taxon.name",
|
25
|
"classification"
|
26
|
});
|
27
|
|
28
|
/**
|
29
|
* Creates a new taxon node container
|
30
|
* @param parentNode the parent node which will <b>not</b> be included
|
31
|
* in the result but only its child nodes
|
32
|
*/
|
33
|
public TaxonNodeContainer(TaxonNode parentNode) {
|
34
|
addContainerProperty(LABEL, String.class, "[no taxon]");
|
35
|
if(parentNode==null){
|
36
|
//just load classifications
|
37
|
IClassificationService classificationService = CdmSpringContextHelper.getClassificationService();
|
38
|
List<Classification> classificationList = classificationService.listClassifications(null, null, null, nodeInitStrategy);
|
39
|
for (Classification classification : classificationList) {
|
40
|
TaxonNode rootNode = classification.getRootNode();
|
41
|
Item item = addItem(rootNode);
|
42
|
if(rootNode.getClassification()!=null){
|
43
|
item.getItemProperty(LABEL).setValue(rootNode.getClassification().getName().getText());
|
44
|
}
|
45
|
}
|
46
|
}
|
47
|
else{
|
48
|
addItem(parentNode);
|
49
|
}
|
50
|
// getTaxonNodeList(parentNode);
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
public Collection<?> getChildren(Object itemId) {
|
55
|
List<TaxonNode> childNodes = ((TaxonNode) itemId).getChildNodes();
|
56
|
for (TaxonNode taxonNode : childNodes) {
|
57
|
if(!containsId(taxonNode)){
|
58
|
setParent(taxonNode, itemId);
|
59
|
}
|
60
|
}
|
61
|
return childNodes;
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* {@inheritDoc}
|
66
|
*/
|
67
|
@Override
|
68
|
public Object getParent(Object itemId) {
|
69
|
return ((TaxonNode) itemId).getParent();
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* {@inheritDoc}
|
74
|
*/
|
75
|
@Override
|
76
|
public boolean hasChildren(Object itemId) {
|
77
|
return !((TaxonNode)itemId).getChildNodes().isEmpty();
|
78
|
}
|
79
|
|
80
|
public void getTaxonNodeList(TaxonNode parentNode) {
|
81
|
List<TaxonNode> nodes = new ArrayList<TaxonNode>();
|
82
|
|
83
|
|
84
|
if(parentNode==null){
|
85
|
//just load classifications
|
86
|
IClassificationService classificationService = CdmSpringContextHelper.getClassificationService();
|
87
|
List<Classification> classificationList = classificationService.listClassifications(null, null, null, nodeInitStrategy);
|
88
|
for (Classification classification : classificationList) {
|
89
|
TaxonNode rootNode = classification.getRootNode();
|
90
|
Item item = addItem(rootNode);
|
91
|
setChildrenAllowed(rootNode, false);
|
92
|
nodes.add(rootNode);
|
93
|
if(rootNode.getClassification()!=null){
|
94
|
item.getItemProperty(LABEL).setValue(rootNode.getClassification().getName().getText());
|
95
|
}
|
96
|
}
|
97
|
}
|
98
|
else{
|
99
|
//load child nodes
|
100
|
addChildNodes(parentNode);
|
101
|
}
|
102
|
}
|
103
|
|
104
|
private void addChildNodes(TaxonNode parentNode) {
|
105
|
List<TaxonNode> childNodes = parentNode.getChildNodes();
|
106
|
setChildrenAllowed(parentNode, !childNodes.isEmpty());
|
107
|
boolean hasValidChildren = false;
|
108
|
for (TaxonNode taxonNode : childNodes) {
|
109
|
if(taxonNode!=null && taxonNode.getTaxon()!=null && taxonNode.getTaxon().getName()!=null){
|
110
|
Rank rank = taxonNode.getTaxon().getName().getRank();
|
111
|
if(rank!=null && rank.isHigher(Rank.SPECIES())){
|
112
|
Item item = addItem(taxonNode);
|
113
|
setParent(taxonNode, parentNode);
|
114
|
if(taxonNode.getTaxon()!=null){
|
115
|
item.getItemProperty(LABEL).setValue(taxonNode.getTaxon().getName().getTitleCache());
|
116
|
}
|
117
|
else if(taxonNode.getClassification()!=null){
|
118
|
item.getItemProperty(LABEL).setValue(taxonNode.getClassification().getName().getText());
|
119
|
}
|
120
|
hasValidChildren = true;
|
121
|
|
122
|
addChildNodes(taxonNode);
|
123
|
}
|
124
|
}
|
125
|
}
|
126
|
setChildrenAllowed(parentNode, hasValidChildren);
|
127
|
}
|
128
|
|
129
|
}
|