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