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.common;
|
11
|
|
12
|
|
13
|
import org.apache.log4j.Logger;
|
14
|
|
15
|
import javax.persistence.*;
|
16
|
|
17
|
/**
|
18
|
* @author m.doering
|
19
|
* @version 1.0
|
20
|
* @created 08-Nov-2007 13:06:23
|
21
|
*/
|
22
|
@Entity
|
23
|
public abstract class OrderedTermBase<T extends OrderedTermBase> extends DefinedTermBase implements Comparable<T> {
|
24
|
static Logger logger = Logger.getLogger(OrderedTermBase.class);
|
25
|
|
26
|
//Order index, value < 1 means that this Term is not in order yet
|
27
|
protected int orderIndex;
|
28
|
|
29
|
public OrderedTermBase() {
|
30
|
super();
|
31
|
}
|
32
|
public OrderedTermBase(String term, String label) {
|
33
|
super(term, label);
|
34
|
}
|
35
|
|
36
|
|
37
|
private int getOrderIndex(){
|
38
|
return this.orderIndex;
|
39
|
}
|
40
|
private void setOrderIndex(int orderIndex){
|
41
|
this.orderIndex = orderIndex;
|
42
|
}
|
43
|
|
44
|
/* (non-Javadoc)
|
45
|
* @see java.lang.Comparable#compareTo(java.lang.Object)
|
46
|
*/
|
47
|
public int compareTo(T orderedTerm) {
|
48
|
int orderThat;
|
49
|
int orderThis;
|
50
|
try {
|
51
|
orderThat = orderedTerm.orderIndex;//OLD: this.getVocabulary().getTerms().indexOf(orderedTerm);
|
52
|
orderThis = orderIndex; //OLD: this.getVocabulary().getTerms().indexOf(this);
|
53
|
} catch (RuntimeException e) {
|
54
|
throw e;
|
55
|
}
|
56
|
if (orderThis > orderThat){
|
57
|
return -1;
|
58
|
}else if (orderThis < orderThat){
|
59
|
return 1;
|
60
|
}else {
|
61
|
return 0;
|
62
|
}
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* If this term is lower than the parameter term, true is returned, else false.
|
67
|
* If the parameter term is null, an Exception is thrown.
|
68
|
* @param orderedTerm
|
69
|
* @return boolean result of the comparison
|
70
|
*/
|
71
|
@Transient
|
72
|
public boolean isLower(T orderedTerm){
|
73
|
return (this.compareTo(orderedTerm) < 0 );
|
74
|
}
|
75
|
|
76
|
|
77
|
/**
|
78
|
* If this term is higher than the parameter term, true is returned, else false.
|
79
|
* If the parameter term is null, an Exception is thrown.
|
80
|
* @param orderedTerm
|
81
|
* @return boolean result of the comparison
|
82
|
*/
|
83
|
@Transient
|
84
|
public boolean isHigher(T orderedTerm){
|
85
|
return (this.compareTo(orderedTerm) > 0 );
|
86
|
}
|
87
|
|
88
|
|
89
|
/* (non-Javadoc)
|
90
|
* @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
|
91
|
*/
|
92
|
@Override
|
93
|
public void setVocabulary(TermVocabulary newVocabulary) {
|
94
|
// Hibernate bidirectional cascade hack:
|
95
|
// http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
|
96
|
if(this.vocabulary == newVocabulary){ return;}
|
97
|
if (this.vocabulary != null) {
|
98
|
this.vocabulary.terms.remove(this);
|
99
|
}
|
100
|
if (newVocabulary != null) {
|
101
|
if (OrderedTermVocabulary.class.isAssignableFrom(newVocabulary.getClass())){
|
102
|
OrderedTermVocabulary voc = (OrderedTermVocabulary)newVocabulary;
|
103
|
|
104
|
if (this.orderIndex > 0){
|
105
|
//don't change orderIndex
|
106
|
}else if (voc.getLowestTerm() == null){
|
107
|
this.orderIndex = 1;
|
108
|
}else{
|
109
|
OrderedTermBase otb = voc.getLowestTerm();
|
110
|
this.orderIndex = otb.orderIndex + 1;
|
111
|
}
|
112
|
}
|
113
|
newVocabulary.terms.add(this);
|
114
|
}
|
115
|
this.vocabulary = newVocabulary;
|
116
|
}
|
117
|
|
118
|
|
119
|
/** To be used only by OrderedTermVocabulary*/
|
120
|
@Deprecated
|
121
|
public boolean decreaseIndex(OrderedTermVocabulary<OrderedTermBase> vocabulary){
|
122
|
if (vocabulary.indexChangeAllowed(this) == true){
|
123
|
orderIndex--;
|
124
|
return true;
|
125
|
}else{
|
126
|
return false;
|
127
|
}
|
128
|
}
|
129
|
|
130
|
/** To be used only by OrderedTermVocabulary*/
|
131
|
@Deprecated
|
132
|
public boolean incrementIndex(OrderedTermVocabulary<OrderedTermBase> vocabulary){
|
133
|
if (vocabulary.indexChangeAllowed(this) == true){
|
134
|
orderIndex++;
|
135
|
return true;
|
136
|
}else{
|
137
|
return false;
|
138
|
}
|
139
|
}
|
140
|
|
141
|
public boolean equals(Object o){
|
142
|
if (! OrderedTermBase.class.isAssignableFrom(o.getClass())){
|
143
|
return false;
|
144
|
}else{
|
145
|
OrderedTermBase otb = (OrderedTermBase)o;
|
146
|
if (otb.getUuid().equals(this.getUuid())){
|
147
|
return true;
|
148
|
}else{
|
149
|
return false;
|
150
|
}
|
151
|
}
|
152
|
}
|
153
|
}
|