1 |
0a5d50a0
|
Andreas Müller
|
/**
|
2 |
1412a4c4
|
Katja Luther
|
* 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 |
d1cf4a26
|
Katja Luther
|
*/
|
9 |
1412a4c4
|
Katja Luther
|
|
10 |
0a5d50a0
|
Andreas Müller
|
package eu.etaxonomy.cdm.model.common;
|
11 |
|
|
|
12 |
f88ffe46
|
Andreas Müller
|
import java.net.URI;
|
13 |
0a5d50a0
|
Andreas Müller
|
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 |
230dda5c
|
Andreas Müller
|
import javax.persistence.Entity;
|
20 |
3fd06fea
|
Andreas Müller
|
import javax.persistence.Transient;
|
21 |
01b7ddbf
|
a.babadshanjan
|
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 |
230dda5c
|
Andreas Müller
|
|
26 |
0a5d50a0
|
Andreas Müller
|
import org.apache.log4j.Logger;
|
27 |
ee91bcd9
|
ben.clark
|
import org.hibernate.envers.Audited;
|
28 |
0a5d50a0
|
Andreas Müller
|
|
29 |
|
|
/**
|
30 |
|
|
* @author a.mueller
|
31 |
|
|
*
|
32 |
|
|
*/
|
33 |
01b7ddbf
|
a.babadshanjan
|
@XmlAccessorType(XmlAccessType.FIELD)
|
34 |
|
|
@XmlType(name = "OrderedTermVocabulary")
|
35 |
|
|
@XmlRootElement(name = "OrderedTermVocabulary")
|
36 |
230dda5c
|
Andreas Müller
|
@Entity
|
37 |
c70a7f0f
|
Andreas Kohlbecker
|
//@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 |
ee91bcd9
|
ben.clark
|
@Audited
|
40 |
5a0834fb
|
Andreas Müller
|
public class OrderedTermVocabulary<T extends OrderedTermBase> extends TermVocabulary<T> {
|
41 |
0d575644
|
Andreas Müller
|
private static final long serialVersionUID = 7871741306306371242L;
|
42 |
|
|
@SuppressWarnings("unused")
|
43 |
0a5d50a0
|
Andreas Müller
|
private static final Logger logger = Logger.getLogger(OrderedTermVocabulary.class);
|
44 |
|
|
|
45 |
99d3fb37
|
Andreas Müller
|
// ************************* FACTORY METHODS ***********************************************/
|
46 |
884b63a3
|
Andreas Müller
|
|
47 |
d1cf4a26
|
Katja Luther
|
|
48 |
884b63a3
|
Andreas Müller
|
/**
|
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 |
d1cf4a26
|
Katja Luther
|
|
57 |
|
|
|
58 |
533cbb43
|
Andreas Müller
|
/**
|
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 |
d1cf4a26
|
Katja Luther
|
* @param label
|
62 |
533cbb43
|
Andreas Müller
|
* @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 |
99d3fb37
|
Andreas Müller
|
}
|
70 |
d1cf4a26
|
Katja Luther
|
|
71 |
|
|
|
72 |
|
|
//************************ CONSTRUCTOR *****************************************************/
|
73 |
99d3fb37
|
Andreas Müller
|
|
74 |
9e3239f6
|
Andreas Müller
|
//for hibernate use only
|
75 |
|
|
@Deprecated
|
76 |
117547a0
|
Andreas Müller
|
protected OrderedTermVocabulary() {
|
77 |
99d3fb37
|
Andreas Müller
|
super();
|
78 |
|
|
}
|
79 |
d1cf4a26
|
Katja Luther
|
|
80 |
884b63a3
|
Andreas Müller
|
/**
|
81 |
|
|
* @param term
|
82 |
|
|
* @param label
|
83 |
|
|
* @param termSourceUri
|
84 |
|
|
*/
|
85 |
|
|
protected OrderedTermVocabulary(TermType type) {
|
86 |
|
|
super(type);
|
87 |
|
|
}
|
88 |
d1cf4a26
|
Katja Luther
|
|
89 |
0a5d50a0
|
Andreas Müller
|
/**
|
90 |
|
|
* @param term
|
91 |
|
|
* @param label
|
92 |
|
|
* @param termSourceUri
|
93 |
|
|
*/
|
94 |
533cbb43
|
Andreas Müller
|
protected OrderedTermVocabulary(TermType type, String term, String label, String labelAbbrev, URI termSourceUri) {
|
95 |
|
|
super(type, term, label, labelAbbrev, termSourceUri);
|
96 |
0a5d50a0
|
Andreas Müller
|
}
|
97 |
f6765014
|
ben.clark
|
|
98 |
99d3fb37
|
Andreas Müller
|
|
99 |
d1cf4a26
|
Katja Luther
|
//************************* METHODS **************************************/
|
100 |
|
|
|
101 |
b70a7f94
|
Andreas Kohlbecker
|
@Transient
|
102 |
f6765014
|
ben.clark
|
@Override
|
103 |
|
|
public Set<T> getNewTermSet() {
|
104 |
0a5d50a0
|
Andreas Müller
|
return new TreeSet<T>();
|
105 |
|
|
}
|
106 |
d1cf4a26
|
Katja Luther
|
|
107 |
f480cc13
|
Andreas Kohlbecker
|
@Transient
|
108 |
|
|
public SortedSet<T> getOrderedTerms() {
|
109 |
a1c4f027
|
Andreas Müller
|
SortedSet<T> result = getSortedSetOfTerms();
|
110 |
32b86483
|
Andreas Müller
|
return result;
|
111 |
|
|
}
|
112 |
a1c4f027
|
Andreas Müller
|
|
113 |
fb2451a1
|
Andreas Müller
|
public SortedSet<T> getHigherAndEqualTerms(T otb) {
|
114 |
5a0834fb
|
Andreas Müller
|
SortedSet<T> result = new TreeSet<>();
|
115 |
a1c4f027
|
Andreas Müller
|
SortedSet<T> sortedSet = getSortedSetOfTerms();
|
116 |
b51607bc
|
Andreas Müller
|
result.addAll( sortedSet.tailSet(otb));
|
117 |
fb2451a1
|
Andreas Müller
|
return result;
|
118 |
|
|
}
|
119 |
ee91bcd9
|
ben.clark
|
|
120 |
0a5d50a0
|
Andreas Müller
|
public SortedSet<T> getHigherTerms(T otb) {
|
121 |
fb2451a1
|
Andreas Müller
|
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 |
0a5d50a0
|
Andreas Müller
|
}
|
129 |
fb2451a1
|
Andreas Müller
|
|
130 |
|
|
public SortedSet<T> getLowerAndEqualTerms(T otb) {
|
131 |
5a9b66ae
|
Andreas Müller
|
SortedSet<T> result = new TreeSet<>();
|
132 |
d1cf4a26
|
Katja Luther
|
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 |
522b8889
|
Katja Luther
|
for (T setObject : terms){
|
138 |
|
|
if (setObject.compareTo(otb) == 0){
|
139 |
|
|
result.add(setObject);
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
fb2451a1
|
Andreas Müller
|
return result;
|
143 |
|
|
}
|
144 |
d1cf4a26
|
Katja Luther
|
|
145 |
0a5d50a0
|
Andreas Müller
|
public SortedSet<T> getLowerTerms(T otb) {
|
146 |
d1cf4a26
|
Katja Luther
|
/*SortedSet<T> result = getLowerAndEqualTerms(otb);
|
147 |
fb2451a1
|
Andreas Müller
|
for (T setObject : terms){
|
148 |
|
|
if (setObject.compareTo(otb) == 0){
|
149 |
|
|
result.remove(setObject);
|
150 |
|
|
}
|
151 |
d1cf4a26
|
Katja Luther
|
}*/
|
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 |
fb2451a1
|
Andreas Müller
|
return result;
|
157 |
0a5d50a0
|
Andreas Müller
|
}
|
158 |
aebcb6a1
|
Andreas Müller
|
|
159 |
|
|
public SortedSet<T> getEqualTerms(T otb) {
|
160 |
5a9b66ae
|
Andreas Müller
|
SortedSet<T> result = new TreeSet<>();
|
161 |
aebcb6a1
|
Andreas Müller
|
for (T setObject : terms){
|
162 |
|
|
if (setObject.compareTo(otb) == 0){
|
163 |
|
|
result.add(setObject);
|
164 |
|
|
}
|
165 |
|
|
}
|
166 |
|
|
return result;
|
167 |
|
|
}
|
168 |
d1cf4a26
|
Katja Luther
|
|
169 |
0a5d50a0
|
Andreas Müller
|
public T getNextHigherTerm(T otb) {
|
170 |
|
|
try {
|
171 |
aebcb6a1
|
Andreas Müller
|
return getHigherTerms(otb).first();
|
172 |
0a5d50a0
|
Andreas Müller
|
} catch (NoSuchElementException e) {
|
173 |
|
|
return null;
|
174 |
|
|
}
|
175 |
|
|
}
|
176 |
d1cf4a26
|
Katja Luther
|
|
177 |
0a5d50a0
|
Andreas Müller
|
public T getNextLowerTerm(T otb) {
|
178 |
|
|
try {
|
179 |
aebcb6a1
|
Andreas Müller
|
return getLowerTerms(otb).last();
|
180 |
0a5d50a0
|
Andreas Müller
|
} catch (NoSuchElementException e) {
|
181 |
|
|
return null;
|
182 |
|
|
}
|
183 |
|
|
}
|
184 |
b70a7f94
|
Andreas Kohlbecker
|
|
185 |
|
|
@Transient
|
186 |
0a5d50a0
|
Andreas Müller
|
public T getLowestTerm() {
|
187 |
|
|
try {
|
188 |
a1c4f027
|
Andreas Müller
|
SortedSet<T> sortedSet = getSortedSetOfTerms();
|
189 |
99491025
|
Andreas Müller
|
return sortedSet.first();
|
190 |
|
|
//return ((SortedSet<T>)terms).first();
|
191 |
0a5d50a0
|
Andreas Müller
|
} catch (NoSuchElementException e) {
|
192 |
|
|
return null;
|
193 |
|
|
}
|
194 |
|
|
}
|
195 |
d1cf4a26
|
Katja Luther
|
|
196 |
b70a7f94
|
Andreas Kohlbecker
|
@Transient
|
197 |
0a5d50a0
|
Andreas Müller
|
public T getHighestTerm() {
|
198 |
|
|
try {
|
199 |
a1c4f027
|
Andreas Müller
|
SortedSet<T> sortedSet = getSortedSetOfTerms();
|
200 |
b51607bc
|
Andreas Müller
|
return sortedSet.last();
|
201 |
0a5d50a0
|
Andreas Müller
|
} catch (NoSuchElementException e) {
|
202 |
|
|
return null;
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
d1cf4a26
|
Katja Luther
|
|
206 |
a24c7b5d
|
Andreas Müller
|
/**
|
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 |
d1cf4a26
|
Katja Luther
|
@Override
|
211 |
|
|
public void addTerm(T term) {
|
212 |
a1c4f027
|
Andreas Müller
|
SortedSet<T> sortedTerms = getSortedSetOfTerms();
|
213 |
fb2451a1
|
Andreas Müller
|
int lowestOrderIndex;
|
214 |
9f840a1e
|
Andreas Müller
|
if (sortedTerms.isEmpty()){
|
215 |
aebcb6a1
|
Andreas Müller
|
lowestOrderIndex = 0;
|
216 |
fb2451a1
|
Andreas Müller
|
}else{
|
217 |
f6765014
|
ben.clark
|
T first = sortedTerms.first();
|
218 |
|
|
lowestOrderIndex = first.orderIndex;
|
219 |
fb2451a1
|
Andreas Müller
|
}
|
220 |
0a5d50a0
|
Andreas Müller
|
term.orderIndex = lowestOrderIndex + 1;
|
221 |
d1cf4a26
|
Katja Luther
|
super.addTerm(term);
|
222 |
0a5d50a0
|
Andreas Müller
|
}
|
223 |
|
|
|
224 |
892efc69
|
Andreas Kohlbecker
|
public void addTermAbove(T termToBeAdded, T lowerTerm) {
|
225 |
aebcb6a1
|
Andreas Müller
|
int orderInd = lowerTerm.orderIndex;
|
226 |
0a5d50a0
|
Andreas Müller
|
termToBeAdded.orderIndex = orderInd;
|
227 |
d1cf4a26
|
Katja Luther
|
//increment all orderIndexes of terms below
|
228 |
0a5d50a0
|
Andreas Müller
|
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 |
892efc69
|
Andreas Kohlbecker
|
public void addTermBelow(T termToBeAdded, T higherTerm) {
|
239 |
aebcb6a1
|
Andreas Müller
|
int orderInd = higherTerm.orderIndex;
|
240 |
0a5d50a0
|
Andreas Müller
|
termToBeAdded.orderIndex = orderInd + 1;
|
241 |
d1cf4a26
|
Katja Luther
|
//increment all orderIndexes of terms below
|
242 |
aebcb6a1
|
Andreas Müller
|
Iterator<T> iterator = getLowerTerms(higherTerm).iterator();
|
243 |
0a5d50a0
|
Andreas Müller
|
while(iterator.hasNext()){
|
244 |
|
|
T term = iterator.next();
|
245 |
|
|
if (term.orderIndex > orderInd){
|
246 |
|
|
term.orderIndex++;
|
247 |
|
|
}
|
248 |
|
|
}
|
249 |
|
|
super.addTerm(termToBeAdded);
|
250 |
aebcb6a1
|
Andreas Müller
|
}
|
251 |
d1cf4a26
|
Katja Luther
|
|
252 |
f6765014
|
ben.clark
|
public void addTermEqualLevel(T termToBeAdded, T equalLevelTerm) throws WrongTermTypeException {
|
253 |
|
|
int orderInd = equalLevelTerm.orderIndex;
|
254 |
|
|
termToBeAdded.orderIndex = orderInd;
|
255 |
|
|
super.addTerm(termToBeAdded);
|
256 |
|
|
}
|
257 |
d1cf4a26
|
Katja Luther
|
|
258 |
0a5d50a0
|
Andreas Müller
|
@Override
|
259 |
|
|
public void removeTerm(T term) {
|
260 |
fb2451a1
|
Andreas Müller
|
if (term == null){
|
261 |
|
|
return;
|
262 |
|
|
}
|
263 |
aebcb6a1
|
Andreas Müller
|
if (this.getEqualTerms(term).size() == 0){
|
264 |
0a5d50a0
|
Andreas Müller
|
Iterator<T> iterator = getLowerTerms(term).iterator();
|
265 |
|
|
while (iterator.hasNext()){
|
266 |
d1cf4a26
|
Katja Luther
|
T otb = iterator.next();
|
267 |
0a5d50a0
|
Andreas Müller
|
toBeChangedByObject = otb;
|
268 |
|
|
otb.decreaseIndex(this);
|
269 |
|
|
toBeChangedByObject = null;
|
270 |
|
|
}
|
271 |
|
|
}
|
272 |
f6765014
|
ben.clark
|
super.removeTerm(term);
|
273 |
0a5d50a0
|
Andreas Müller
|
}
|
274 |
d1cf4a26
|
Katja Luther
|
|
275 |
ee91bcd9
|
ben.clark
|
@Transient
|
276 |
0a5d50a0
|
Andreas Müller
|
private T toBeChangedByObject;
|
277 |
d1cf4a26
|
Katja Luther
|
|
278 |
2a25d458
|
Patrick Plitzner
|
public boolean indexChangeAllowed(OrderedTermBase orderedTermBase){
|
279 |
ee91bcd9
|
ben.clark
|
return orderedTermBase == toBeChangedByObject ;
|
280 |
0a5d50a0
|
Andreas Müller
|
}
|
281 |
d1cf4a26
|
Katja Luther
|
|
282 |
|
|
|
283 |
a1c4f027
|
Andreas Müller
|
@Transient
|
284 |
|
|
private SortedSet<T> getSortedSetOfTerms(){
|
285 |
|
|
SortedSet<T> sortedSet = new TreeSet<T>();
|
286 |
|
|
sortedSet.addAll(terms);
|
287 |
|
|
return sortedSet;
|
288 |
|
|
}
|
289 |
0a5d50a0
|
Andreas Müller
|
|
290 |
|
|
}
|