1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.model;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Collections;
|
14
|
import java.util.Comparator;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
20
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
21
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
22
|
import eu.etaxonomy.cdm.model.description.Feature;
|
23
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
24
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
25
|
import eu.etaxonomy.cdm.model.term.TermTree;
|
26
|
|
27
|
/**
|
28
|
* @author n.hoffmann
|
29
|
* @created Dec 2, 2010
|
30
|
*/
|
31
|
public class FeatureNodeContainerTree {
|
32
|
|
33
|
/** Constant <code>comparator</code> */
|
34
|
// TODO we will do this with a ViewerSorter
|
35
|
public static Comparator<DescriptionElementBase> comparator = new Comparator<DescriptionElementBase>() {
|
36
|
|
37
|
@Override
|
38
|
public int compare(DescriptionElementBase o1, DescriptionElementBase o2) {
|
39
|
if (o1.equals(o2)){
|
40
|
return 0;
|
41
|
}
|
42
|
int result = DescriptionHelper.getLabel(o1).compareTo(DescriptionHelper.getLabel(o2));
|
43
|
if (result == 0){
|
44
|
return o1.getUuid().compareTo(o2.getUuid());
|
45
|
}
|
46
|
return result;
|
47
|
}
|
48
|
};
|
49
|
|
50
|
private Set<FeatureNodeContainer> featureNodeContainers = new HashSet<>();
|
51
|
|
52
|
private FeatureNodeContainer root;
|
53
|
private DescriptionBase description;
|
54
|
private TermTree featureTree;
|
55
|
|
56
|
public FeatureNodeContainerTree(DescriptionBase description, TermTree featureTree){
|
57
|
this.description = description;
|
58
|
this.featureTree = featureTree;
|
59
|
|
60
|
root = new FeatureNodeContainer(this);
|
61
|
|
62
|
buildTree();
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Traverses the given {@link FeatureNode} and computes a FeatureNodeContainer branch if the
|
67
|
* given {@link TaxonDescription} has elements for the given feature node or any of its children
|
68
|
* or null if there are no elements.
|
69
|
*/
|
70
|
public void buildTree(){
|
71
|
List<TermNode> children = featureTree.getRootChildren();
|
72
|
|
73
|
for(TermNode featureNode : children){
|
74
|
root.findLeaves(featureNode);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* <p>getFeatureNodeContainerForDescriptionElement</p>
|
80
|
*
|
81
|
* @param descriptionElement a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
|
82
|
* @return a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
|
83
|
*/
|
84
|
public FeatureNodeContainer getFeatureNodeContainerForDescriptionElement (DescriptionElementBase descriptionElement) {
|
85
|
List<FeatureNodeContainer> leafs = root.getLeafs();
|
86
|
|
87
|
for(FeatureNodeContainer container : leafs){
|
88
|
if(container.getDescriptionElements().contains(descriptionElement)){
|
89
|
return container;
|
90
|
}
|
91
|
}
|
92
|
return null;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* Returns all elements contained in the given {@link DescriptionBase description} that have the given {@link Feature feature}.
|
97
|
*
|
98
|
* @param description
|
99
|
* @param feature
|
100
|
* @return a list of description elements with the given feature alphabetically sorted by {@link DescriptionHelper#getLabel(Object)}
|
101
|
*/
|
102
|
public List<DescriptionElementBase> getDescriptionsElementsForFeature(Feature feature) {
|
103
|
List<DescriptionElementBase> featureElements = new ArrayList<DescriptionElementBase>();
|
104
|
|
105
|
Set<DescriptionElementBase> elements = description.getElements();
|
106
|
|
107
|
if (elements != null) {
|
108
|
for (DescriptionElementBase element : elements) {
|
109
|
Feature elementFeature = HibernateProxyHelper.deproxy(element.getFeature());
|
110
|
|
111
|
if (feature.equals(elementFeature)) {
|
112
|
featureElements.add(element);
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
|
117
|
if(featureElements.size() != 0){
|
118
|
Collections.sort(featureElements, comparator);
|
119
|
}
|
120
|
|
121
|
return featureElements;
|
122
|
}
|
123
|
|
124
|
|
125
|
public FeatureNodeContainer getFeatureNodeContainer(Feature feature) {
|
126
|
List<FeatureNodeContainer> leafs = root.getLeafs();
|
127
|
|
128
|
for(FeatureNodeContainer container : leafs){
|
129
|
if(container.getFeature().equals(feature)){
|
130
|
return container;
|
131
|
}
|
132
|
}
|
133
|
return null;
|
134
|
}
|
135
|
|
136
|
public FeatureNodeContainer getFeatureNodeContainer(TermNode featureNode){
|
137
|
List<FeatureNodeContainer> leafs = root.getLeafs();
|
138
|
|
139
|
for(FeatureNodeContainer container : leafs){
|
140
|
if(container.getFeatureNode().equals(featureNode)){
|
141
|
return container;
|
142
|
}
|
143
|
}
|
144
|
return null;
|
145
|
}
|
146
|
|
147
|
public void addContainer(FeatureNodeContainer container){
|
148
|
featureNodeContainers.add(container);
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
*
|
153
|
*/
|
154
|
public void removeContainer(FeatureNodeContainer container) {
|
155
|
featureNodeContainers.remove(container);
|
156
|
}
|
157
|
|
158
|
public FeatureNodeContainer getRoot() {
|
159
|
return root;
|
160
|
}
|
161
|
|
162
|
public DescriptionBase getDescription() {
|
163
|
return description;
|
164
|
}
|
165
|
|
166
|
public TermTree getFeatureTree() {
|
167
|
return featureTree;
|
168
|
}
|
169
|
|
170
|
}
|