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.cdm.model.description;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import javax.persistence.Entity;
|
19
|
import javax.persistence.FetchType;
|
20
|
import javax.persistence.OneToMany;
|
21
|
import javax.persistence.OneToOne;
|
22
|
import javax.persistence.Transient;
|
23
|
import javax.xml.bind.annotation.XmlAccessType;
|
24
|
import javax.xml.bind.annotation.XmlAccessorType;
|
25
|
import javax.xml.bind.annotation.XmlElement;
|
26
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
27
|
import javax.xml.bind.annotation.XmlRootElement;
|
28
|
import javax.xml.bind.annotation.XmlType;
|
29
|
|
30
|
import org.apache.log4j.Logger;
|
31
|
import org.hibernate.annotations.Cascade;
|
32
|
import org.hibernate.annotations.CascadeType;
|
33
|
import org.hibernate.envers.Audited;
|
34
|
|
35
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
36
|
import eu.etaxonomy.cdm.model.common.Representation;
|
37
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
38
|
|
39
|
/**
|
40
|
* The class to arrange {@link Feature features} (characters) in a tree structure.
|
41
|
* Feature trees are essential as interactive multiple-access keys for
|
42
|
* determination process and for systematical output arrangement of
|
43
|
* {@link DescriptionElementBase description elements} according to different goals
|
44
|
* but may also be used to define flat feature subsets for filtering purposes.<BR>
|
45
|
* A feature tree is build on {@link FeatureNode feature nodes}.
|
46
|
* <P>
|
47
|
* This class corresponds partially to ConceptTreeDefType according to the SDD
|
48
|
* schema.
|
49
|
* <P>
|
50
|
* Note: The tree structure of features used for purposes described above has
|
51
|
* nothing in common with the possible hierarchical structure of features
|
52
|
* depending on their grade of precision.
|
53
|
*
|
54
|
* @see MediaKey
|
55
|
*
|
56
|
* @author m.doering
|
57
|
* @created 08-Nov-2007 13:06:16
|
58
|
*/
|
59
|
@XmlAccessorType(XmlAccessType.FIELD)
|
60
|
@XmlType(name = "FeatureTree", propOrder = {
|
61
|
"root",
|
62
|
"representations"
|
63
|
})
|
64
|
@XmlRootElement(name = "FeatureTree")
|
65
|
@Entity
|
66
|
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
|
67
|
//@Indexed(index = "eu.etaxonomy.cdm.model.description.FeatureTree")
|
68
|
@Audited
|
69
|
public class FeatureTree extends IdentifiableEntity<IIdentifiableEntityCacheStrategy> implements Cloneable{
|
70
|
private static final long serialVersionUID = -6713834139003172735L;
|
71
|
private static final Logger logger = Logger.getLogger(FeatureTree.class);
|
72
|
|
73
|
@XmlElement(name = "Root")
|
74
|
@OneToOne(fetch = FetchType.LAZY)
|
75
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
|
76
|
private FeatureNode root;
|
77
|
|
78
|
|
79
|
// TODO needed? FeatureTree was a TermBase until v3.3 but was removed from
|
80
|
//it as TermBase got the termType which does not apply to FeatureTree.
|
81
|
//We need to check how far representations and uri is still required
|
82
|
//or can be deleted. Current implementations seem all to use the title cache
|
83
|
//instead of representation. This may not be correct.
|
84
|
@XmlElementWrapper(name = "Representations")
|
85
|
@XmlElement(name = "Representation")
|
86
|
@OneToMany(fetch=FetchType.EAGER, orphanRemoval=true)
|
87
|
@Cascade( { CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
|
88
|
// @IndexedEmbedded no need for embedding since we are using the DefinedTermBaseClassBridge
|
89
|
private Set<Representation> representations = new HashSet<>();
|
90
|
//make them private for now as we may delete representations in future
|
91
|
//otherwise if we decide to use representations we can make the getters public
|
92
|
private Set<Representation> getRepresentations() {return representations;}
|
93
|
private void setRepresentations(Set<Representation> representations) {this.representations = representations;}
|
94
|
|
95
|
|
96
|
//******************** FACTORY METHODS ******************************************/
|
97
|
|
98
|
|
99
|
/**
|
100
|
* Creates a new feature tree instance with an empty {@link #getRoot() root node}.
|
101
|
*
|
102
|
* @see #NewInstance(UUID)
|
103
|
* @see #NewInstance(List)
|
104
|
*/
|
105
|
public static FeatureTree NewInstance(){
|
106
|
return new FeatureTree();
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Creates a new feature tree instance with an empty {@link #getRoot() root node}
|
111
|
* and assigns to the new feature tree the given
|
112
|
* UUID (universally unique identifier).
|
113
|
*
|
114
|
* @param uuid the universally unique identifier
|
115
|
* @see #NewInstance()
|
116
|
* @see #NewInstance(List)
|
117
|
*/
|
118
|
public static FeatureTree NewInstance(UUID uuid){
|
119
|
FeatureTree result = new FeatureTree();
|
120
|
result.setUuid(uuid);
|
121
|
return result;
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Creates a new feature tree instance with a {@link #getRoot() root node}
|
126
|
* the children of which are the feature nodes build on the base of the
|
127
|
* given list of {@link Feature features}. This corresponds to a flat feature tree.
|
128
|
* For each feature within the list a new {@link FeatureNode feature node} without
|
129
|
* children nodes will be created.
|
130
|
*
|
131
|
* @param featureList the feature list
|
132
|
* @see #NewInstance()
|
133
|
* @see #NewInstance(UUID)
|
134
|
*/
|
135
|
public static FeatureTree NewInstance(List<Feature> featureList){
|
136
|
FeatureTree result = new FeatureTree();
|
137
|
FeatureNode root = result.getRoot();
|
138
|
|
139
|
for (Feature feature : featureList){
|
140
|
FeatureNode child = FeatureNode.NewInstance(feature);
|
141
|
root.addChild(child);
|
142
|
}
|
143
|
|
144
|
return result;
|
145
|
}
|
146
|
|
147
|
|
148
|
// ******************** CONSTRUCTOR *************************************/
|
149
|
|
150
|
/**
|
151
|
* Class constructor: creates a new feature tree instance with an empty
|
152
|
* {@link #getRoot() root node}.
|
153
|
*/
|
154
|
protected FeatureTree() {
|
155
|
super();
|
156
|
root = FeatureNode.NewInstance();
|
157
|
root.setFeatureTree(this);
|
158
|
}
|
159
|
|
160
|
// ****************** GETTER / SETTER **********************************/
|
161
|
|
162
|
/**
|
163
|
* Returns the topmost {@link FeatureNode feature node} (root node) of <i>this</i>
|
164
|
* feature tree. The root node does not have any parent. Since feature nodes
|
165
|
* recursively point to their child nodes the complete feature tree is
|
166
|
* defined by its root node.
|
167
|
*/
|
168
|
public FeatureNode getRoot() {
|
169
|
return root;
|
170
|
}
|
171
|
/**
|
172
|
* @see #getRoot()
|
173
|
*/
|
174
|
public void setRoot(FeatureNode root) {
|
175
|
this.root = root;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Returns the (ordered) list of {@link FeatureNode feature nodes} which are immediate
|
180
|
* children of the root node of <i>this</i> feature tree.
|
181
|
*/
|
182
|
@Transient
|
183
|
public List<FeatureNode> getRootChildren(){
|
184
|
List<FeatureNode> result = new ArrayList<FeatureNode>();
|
185
|
result.addAll(root.getChildNodes());
|
186
|
return result;
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* Computes a set of distinct features that are present in this feature tree
|
191
|
*
|
192
|
* @return
|
193
|
*/
|
194
|
@Transient
|
195
|
public Set<Feature> getDistinctFeatures(){
|
196
|
Set<Feature> features = new HashSet<>();
|
197
|
|
198
|
return root.getDistinctFeaturesRecursive(features);
|
199
|
}
|
200
|
|
201
|
//*********************** CLONE ********************************************************/
|
202
|
|
203
|
/**
|
204
|
* Clones <i>this</i> FeatureTree. This is a shortcut that enables to create
|
205
|
* a new instance that differs only slightly from <i>this</i> FeatureTree by
|
206
|
* modifying only some of the attributes.
|
207
|
* FeatureNodes always belong only to one tree, so all FeatureNodes are cloned to build
|
208
|
* the new FeatureTree
|
209
|
*
|
210
|
*
|
211
|
* @see eu.etaxonomy.cdm.model.common.TermBase#clone()
|
212
|
* @see java.lang.Object#clone()
|
213
|
*/
|
214
|
@Override
|
215
|
public Object clone() {
|
216
|
FeatureTree result;
|
217
|
try {
|
218
|
result = (FeatureTree)super.clone();
|
219
|
}catch (CloneNotSupportedException e) {
|
220
|
logger.warn("Object does not implement cloneable");
|
221
|
e.printStackTrace();
|
222
|
return null;
|
223
|
}
|
224
|
FeatureNode rootClone = this.getRoot().cloneDescendants();
|
225
|
result.root = rootClone;
|
226
|
|
227
|
return result;
|
228
|
|
229
|
}
|
230
|
}
|