1
|
/**
|
2
|
* Copyright (C) 2009 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.common;
|
11
|
|
12
|
import java.net.URI;
|
13
|
import java.util.Iterator;
|
14
|
import java.util.NoSuchElementException;
|
15
|
import java.util.Set;
|
16
|
import java.util.SortedSet;
|
17
|
import java.util.TreeSet;
|
18
|
|
19
|
import javax.persistence.Entity;
|
20
|
import javax.persistence.Transient;
|
21
|
import javax.xml.bind.annotation.XmlAccessType;
|
22
|
import javax.xml.bind.annotation.XmlAccessorType;
|
23
|
import javax.xml.bind.annotation.XmlRootElement;
|
24
|
import javax.xml.bind.annotation.XmlType;
|
25
|
|
26
|
import org.apache.log4j.Logger;
|
27
|
import org.hibernate.envers.Audited;
|
28
|
|
29
|
/**
|
30
|
* @author a.mueller
|
31
|
*
|
32
|
*/
|
33
|
@XmlAccessorType(XmlAccessType.FIELD)
|
34
|
@XmlType(name = "OrderedTermVocabulary")
|
35
|
@XmlRootElement(name = "OrderedTermVocabulary")
|
36
|
@Entity
|
37
|
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
|
38
|
//@Indexed(index = "eu.etaxonomy.cdm.model.common.TermVocabulary")
|
39
|
@Audited
|
40
|
public class OrderedTermVocabulary<T extends OrderedTermBase> extends TermVocabulary<T> {
|
41
|
private static final long serialVersionUID = 7871741306306371242L;
|
42
|
@SuppressWarnings("unused")
|
43
|
private static final Logger logger = Logger.getLogger(OrderedTermVocabulary.class);
|
44
|
|
45
|
// ************************* FACTORY METHODS ***********************************************/
|
46
|
|
47
|
|
48
|
/**
|
49
|
* @param type the {@link TermType term type}, must be the same as for all included terms
|
50
|
* @return
|
51
|
* @throws NullPointerException if type is <code>null</code>
|
52
|
*/
|
53
|
public static OrderedTermVocabulary NewInstance(TermType type){
|
54
|
return new OrderedTermVocabulary(type);
|
55
|
}
|
56
|
|
57
|
|
58
|
/**
|
59
|
* @param type the {@link TermType term type}, must be the same as for all included terms
|
60
|
* @param description the description of this vocabulary
|
61
|
* @param label
|
62
|
* @param labelAbbrev
|
63
|
* @param termSourceUri
|
64
|
* @return
|
65
|
* @throws NullPointerException if type is <code>null</code>
|
66
|
*/
|
67
|
public static OrderedTermVocabulary NewInstance(TermType type, String description, String label, String labelAbbrev, URI termSourceUri){
|
68
|
return new OrderedTermVocabulary(type, description, label, labelAbbrev, termSourceUri);
|
69
|
}
|
70
|
|
71
|
|
72
|
//************************ CONSTRUCTOR *****************************************************/
|
73
|
|
74
|
//for hibernate use only
|
75
|
@Deprecated
|
76
|
protected OrderedTermVocabulary() {
|
77
|
super();
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* @param term
|
82
|
* @param label
|
83
|
* @param termSourceUri
|
84
|
*/
|
85
|
protected OrderedTermVocabulary(TermType type) {
|
86
|
super(type);
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* @param term
|
91
|
* @param label
|
92
|
* @param termSourceUri
|
93
|
*/
|
94
|
protected OrderedTermVocabulary(TermType type, String term, String label, String labelAbbrev, URI termSourceUri) {
|
95
|
super(type, term, label, labelAbbrev, termSourceUri);
|
96
|
}
|
97
|
|
98
|
|
99
|
//************************* METHODS **************************************/
|
100
|
|
101
|
@Transient
|
102
|
@Override
|
103
|
public Set<T> getNewTermSet() {
|
104
|
return new TreeSet<T>();
|
105
|
}
|
106
|
|
107
|
@Transient
|
108
|
public SortedSet<T> getOrderedTerms() {
|
109
|
SortedSet<T> result = getSortedSetOfTerms();
|
110
|
return result;
|
111
|
}
|
112
|
|
113
|
public SortedSet<T> getHigherAndEqualTerms(T otb) {
|
114
|
SortedSet<T> result = new TreeSet<T>();
|
115
|
SortedSet<T> sortedSet = getSortedSetOfTerms();
|
116
|
result.addAll( sortedSet.tailSet(otb));
|
117
|
return result;
|
118
|
}
|
119
|
|
120
|
public SortedSet<T> getHigherTerms(T otb) {
|
121
|
SortedSet<T> result = getHigherAndEqualTerms(otb);
|
122
|
for (T setObject : terms){
|
123
|
if (setObject.compareTo(otb) == 0){
|
124
|
result.remove(setObject);
|
125
|
}
|
126
|
}
|
127
|
return result;
|
128
|
}
|
129
|
|
130
|
public SortedSet<T> getLowerAndEqualTerms(T otb) {
|
131
|
SortedSet<T> result = new TreeSet<T>();
|
132
|
result = getLowerTerms(otb);
|
133
|
/*SortedSet<T> sortedSet = getSortedSetOfTerms();
|
134
|
|
135
|
result.addAll( sortedSet.headSet(otb));*/
|
136
|
//getLowerTerms Returns a view of the portion of this set whose elements are STRICTLY less than toElement
|
137
|
for (T setObject : terms){
|
138
|
if (setObject.compareTo(otb) == 0){
|
139
|
result.add(setObject);
|
140
|
}
|
141
|
}
|
142
|
return result;
|
143
|
}
|
144
|
|
145
|
public SortedSet<T> getLowerTerms(T otb) {
|
146
|
/*SortedSet<T> result = getLowerAndEqualTerms(otb);
|
147
|
for (T setObject : terms){
|
148
|
if (setObject.compareTo(otb) == 0){
|
149
|
result.remove(setObject);
|
150
|
}
|
151
|
}*/
|
152
|
SortedSet<T> result = new TreeSet<T>();
|
153
|
SortedSet<T> sortedSet = getSortedSetOfTerms();
|
154
|
//headSet Returns a view of the portion of this set whose elements are STRICTLY less than toElement
|
155
|
result.addAll( sortedSet.headSet(otb));
|
156
|
return result;
|
157
|
}
|
158
|
|
159
|
public SortedSet<T> getEqualTerms(T otb) {
|
160
|
SortedSet<T> result = new TreeSet<T>();
|
161
|
for (T setObject : terms){
|
162
|
if (setObject.compareTo(otb) == 0){
|
163
|
result.add(setObject);
|
164
|
}
|
165
|
}
|
166
|
return result;
|
167
|
}
|
168
|
|
169
|
public T getNextHigherTerm(T otb) {
|
170
|
try {
|
171
|
return getHigherTerms(otb).first();
|
172
|
} catch (NoSuchElementException e) {
|
173
|
return null;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
public T getNextLowerTerm(T otb) {
|
178
|
try {
|
179
|
return getLowerTerms(otb).last();
|
180
|
} catch (NoSuchElementException e) {
|
181
|
return null;
|
182
|
}
|
183
|
}
|
184
|
|
185
|
@Transient
|
186
|
public T getLowestTerm() {
|
187
|
try {
|
188
|
SortedSet<T> sortedSet = getSortedSetOfTerms();
|
189
|
return sortedSet.first();
|
190
|
//return ((SortedSet<T>)terms).first();
|
191
|
} catch (NoSuchElementException e) {
|
192
|
return null;
|
193
|
}
|
194
|
}
|
195
|
|
196
|
@Transient
|
197
|
public T getHighestTerm() {
|
198
|
try {
|
199
|
SortedSet<T> sortedSet = getSortedSetOfTerms();
|
200
|
return sortedSet.last();
|
201
|
} catch (NoSuchElementException e) {
|
202
|
return null;
|
203
|
}
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Adds a term to the the end / lowest
|
208
|
* @see eu.etaxonomy.cdm.model.common.TermVocabulary#addTerm(eu.etaxonomy.cdm.model.common.DefinedTermBase)
|
209
|
*/
|
210
|
@Override
|
211
|
public void addTerm(T term) {
|
212
|
SortedSet<T> sortedTerms = getSortedSetOfTerms();
|
213
|
int lowestOrderIndex;
|
214
|
if (sortedTerms.isEmpty()){
|
215
|
lowestOrderIndex = 0;
|
216
|
}else{
|
217
|
T first = sortedTerms.first();
|
218
|
lowestOrderIndex = first.orderIndex;
|
219
|
}
|
220
|
term.orderIndex = lowestOrderIndex + 1;
|
221
|
super.addTerm(term);
|
222
|
}
|
223
|
|
224
|
public void addTermAbove(T termToBeAdded, T lowerTerm) {
|
225
|
int orderInd = lowerTerm.orderIndex;
|
226
|
termToBeAdded.orderIndex = orderInd;
|
227
|
//increment all orderIndexes of terms below
|
228
|
Iterator<T> iterator = terms.iterator();
|
229
|
while(iterator.hasNext()){
|
230
|
T term = iterator.next();
|
231
|
if (term.orderIndex >= orderInd){ //should always be true
|
232
|
term.orderIndex++;
|
233
|
}
|
234
|
}
|
235
|
super.addTerm(termToBeAdded);
|
236
|
}
|
237
|
|
238
|
public void addTermBelow(T termToBeAdded, T higherTerm) {
|
239
|
int orderInd = higherTerm.orderIndex;
|
240
|
termToBeAdded.orderIndex = orderInd + 1;
|
241
|
//increment all orderIndexes of terms below
|
242
|
Iterator<T> iterator = getLowerTerms(higherTerm).iterator();
|
243
|
while(iterator.hasNext()){
|
244
|
T term = iterator.next();
|
245
|
if (term.orderIndex > orderInd){
|
246
|
term.orderIndex++;
|
247
|
}
|
248
|
}
|
249
|
super.addTerm(termToBeAdded);
|
250
|
}
|
251
|
|
252
|
public void addTermEqualLevel(T termToBeAdded, T equalLevelTerm) throws WrongTermTypeException {
|
253
|
int orderInd = equalLevelTerm.orderIndex;
|
254
|
termToBeAdded.orderIndex = orderInd;
|
255
|
super.addTerm(termToBeAdded);
|
256
|
}
|
257
|
|
258
|
@Override
|
259
|
public void removeTerm(T term) {
|
260
|
if (term == null){
|
261
|
return;
|
262
|
}
|
263
|
if (this.getEqualTerms(term).size() == 0){
|
264
|
Iterator<T> iterator = getLowerTerms(term).iterator();
|
265
|
while (iterator.hasNext()){
|
266
|
T otb = iterator.next();
|
267
|
toBeChangedByObject = otb;
|
268
|
otb.decreaseIndex(this);
|
269
|
toBeChangedByObject = null;
|
270
|
}
|
271
|
}
|
272
|
super.removeTerm(term);
|
273
|
}
|
274
|
|
275
|
@Transient
|
276
|
private T toBeChangedByObject;
|
277
|
|
278
|
public boolean indexChangeAllowed(OrderedTermBase<T> orderedTermBase){
|
279
|
return orderedTermBase == toBeChangedByObject ;
|
280
|
}
|
281
|
|
282
|
|
283
|
@Transient
|
284
|
private SortedSet<T> getSortedSetOfTerms(){
|
285
|
SortedSet<T> sortedSet = new TreeSet<T>();
|
286
|
sortedSet.addAll(terms);
|
287
|
return sortedSet;
|
288
|
}
|
289
|
|
290
|
}
|