1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.cdm.model.common;
|
5
|
|
6
|
import java.util.Iterator;
|
7
|
import java.util.NoSuchElementException;
|
8
|
import java.util.Set;
|
9
|
import java.util.SortedSet;
|
10
|
import java.util.TreeSet;
|
11
|
|
12
|
import javax.persistence.Entity;
|
13
|
import javax.persistence.Transient;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
/**
|
18
|
* @author a.mueller
|
19
|
*
|
20
|
*/
|
21
|
@Entity
|
22
|
public class OrderedTermVocabulary<T extends OrderedTermBase> extends TermVocabulary<T> {
|
23
|
private static final Logger logger = Logger.getLogger(OrderedTermVocabulary.class);
|
24
|
|
25
|
/**
|
26
|
*
|
27
|
*/
|
28
|
public OrderedTermVocabulary() {
|
29
|
super();
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* @param term
|
34
|
* @param label
|
35
|
* @param termSourceUri
|
36
|
*/
|
37
|
public OrderedTermVocabulary(String term, String label, String termSourceUri) {
|
38
|
super(term, label, termSourceUri);
|
39
|
}
|
40
|
|
41
|
@Override
|
42
|
@Transient
|
43
|
protected Set<T> getNewTermSet(){
|
44
|
return new TreeSet<T>();
|
45
|
}
|
46
|
|
47
|
@Transient
|
48
|
public SortedSet<T> getOrderedTerms(T otb) {
|
49
|
SortedSet<T> result = new TreeSet<T>();
|
50
|
result.addAll(terms);
|
51
|
return result;
|
52
|
}
|
53
|
|
54
|
@Transient
|
55
|
public SortedSet<T> getHigherAndEqualTerms(T otb) {
|
56
|
SortedSet<T> result = new TreeSet<T>();
|
57
|
SortedSet<T> sortedSet = new TreeSet<T>();
|
58
|
sortedSet.addAll(terms);
|
59
|
result.addAll( sortedSet.tailSet(otb));
|
60
|
return result;
|
61
|
}
|
62
|
@Transient
|
63
|
public SortedSet<T> getHigherTerms(T otb) {
|
64
|
SortedSet<T> result = getHigherAndEqualTerms(otb);
|
65
|
for (T setObject : terms){
|
66
|
if (setObject.compareTo(otb) == 0){
|
67
|
result.remove(setObject);
|
68
|
}
|
69
|
}
|
70
|
return result;
|
71
|
}
|
72
|
|
73
|
@Transient
|
74
|
public SortedSet<T> getLowerAndEqualTerms(T otb) {
|
75
|
SortedSet<T> result = new TreeSet<T>();
|
76
|
SortedSet<T> sortedSet = new TreeSet<T>();
|
77
|
sortedSet.addAll(terms);
|
78
|
result.addAll( sortedSet.headSet(otb));
|
79
|
return result;
|
80
|
}
|
81
|
|
82
|
@Transient
|
83
|
public SortedSet<T> getLowerTerms(T otb) {
|
84
|
SortedSet<T> result = getLowerAndEqualTerms(otb);
|
85
|
for (T setObject : terms){
|
86
|
if (setObject.compareTo(otb) == 0){
|
87
|
result.remove(setObject);
|
88
|
}
|
89
|
}
|
90
|
return result;
|
91
|
}
|
92
|
|
93
|
@Transient
|
94
|
public SortedSet<T> getEqualTerms(T otb) {
|
95
|
SortedSet<T> result = new TreeSet<T>();
|
96
|
for (T setObject : terms){
|
97
|
if (setObject.compareTo(otb) == 0){
|
98
|
result.add(setObject);
|
99
|
}
|
100
|
}
|
101
|
return result;
|
102
|
}
|
103
|
|
104
|
@Transient
|
105
|
public T getNextHigherTerm(T otb) {
|
106
|
try {
|
107
|
return getHigherTerms(otb).first();
|
108
|
} catch (NoSuchElementException e) {
|
109
|
return null;
|
110
|
}
|
111
|
}
|
112
|
|
113
|
@Transient
|
114
|
public T getNextLowerTerm(T otb) {
|
115
|
try {
|
116
|
return getLowerTerms(otb).last();
|
117
|
} catch (NoSuchElementException e) {
|
118
|
return null;
|
119
|
}
|
120
|
}
|
121
|
|
122
|
@Transient
|
123
|
public T getLowestTerm() {
|
124
|
try {
|
125
|
SortedSet<T> sortedSet = new TreeSet<T>();
|
126
|
sortedSet.addAll(terms);
|
127
|
return sortedSet.first();
|
128
|
//return ((SortedSet<T>)terms).first();
|
129
|
} catch (NoSuchElementException e) {
|
130
|
return null;
|
131
|
}
|
132
|
}
|
133
|
|
134
|
@Transient
|
135
|
public T getHighestTerm() {
|
136
|
try {
|
137
|
SortedSet<T> sortedSet = new TreeSet<T>();
|
138
|
sortedSet.addAll(terms);
|
139
|
return sortedSet.last();
|
140
|
} catch (NoSuchElementException e) {
|
141
|
return null;
|
142
|
}
|
143
|
}
|
144
|
|
145
|
@Override
|
146
|
public void addTerm(T term) throws WrongTermTypeException {
|
147
|
SortedSet sortedTerms = ((SortedSet<T>)terms);
|
148
|
int lowestOrderIndex;
|
149
|
if (sortedTerms.size() == 0){
|
150
|
lowestOrderIndex = 0;
|
151
|
}else{
|
152
|
Object first = (T)sortedTerms.first();
|
153
|
lowestOrderIndex = ((T)first).orderIndex;
|
154
|
}
|
155
|
term.orderIndex = lowestOrderIndex + 1;
|
156
|
super.addTerm(term);
|
157
|
}
|
158
|
|
159
|
public void addTermAbove(T termToBeAdded, T lowerTerm) throws WrongTermTypeException {
|
160
|
int orderInd = lowerTerm.orderIndex;
|
161
|
termToBeAdded.orderIndex = orderInd;
|
162
|
//increment all orderIndexes of terms below
|
163
|
Iterator<T> iterator = terms.iterator();
|
164
|
while(iterator.hasNext()){
|
165
|
T term = iterator.next();
|
166
|
if (term.orderIndex >= orderInd){ //should always be true
|
167
|
term.orderIndex++;
|
168
|
}
|
169
|
}
|
170
|
super.addTerm(termToBeAdded);
|
171
|
}
|
172
|
|
173
|
public void addTermBelow(T termToBeAdded, T higherTerm) throws WrongTermTypeException {
|
174
|
int orderInd = higherTerm.orderIndex;
|
175
|
termToBeAdded.orderIndex = orderInd + 1;
|
176
|
//increment all orderIndexes of terms below
|
177
|
Iterator<T> iterator = getLowerTerms(higherTerm).iterator();
|
178
|
while(iterator.hasNext()){
|
179
|
T term = iterator.next();
|
180
|
if (term.orderIndex > orderInd){
|
181
|
term.orderIndex++;
|
182
|
}
|
183
|
}
|
184
|
super.addTerm(termToBeAdded);
|
185
|
}
|
186
|
|
187
|
// public void addTermEqualLevel(T termToBeAdded, T equalLevelTerm) throws WrongTermTypeException {
|
188
|
// int orderInd = equalLevelTerm.orderIndex;
|
189
|
// termToBeAdded.orderIndex = orderInd;
|
190
|
// super.addTerm(termToBeAdded);
|
191
|
// }
|
192
|
|
193
|
@Override
|
194
|
public void removeTerm(T term) {
|
195
|
if (term == null){
|
196
|
return;
|
197
|
}
|
198
|
if (this.getEqualTerms(term).size() == 0){
|
199
|
Iterator<T> iterator = getLowerTerms(term).iterator();
|
200
|
while (iterator.hasNext()){
|
201
|
T otb = iterator.next();
|
202
|
toBeChangedByObject = otb;
|
203
|
otb.decreaseIndex(this);
|
204
|
toBeChangedByObject = null;
|
205
|
}
|
206
|
}
|
207
|
term.setVocabulary(null);
|
208
|
}
|
209
|
|
210
|
private T toBeChangedByObject;
|
211
|
|
212
|
public boolean indexChangeAllowed(T otb){
|
213
|
return otb == toBeChangedByObject ;
|
214
|
}
|
215
|
|
216
|
}
|