Project

General

Profile

Download (6.67 KB) Statistics
| Branch: | Tag: | Revision:
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
import javax.persistence.Entity;
13
import javax.xml.bind.annotation.XmlAccessType;
14
import javax.xml.bind.annotation.XmlAccessorType;
15
import javax.xml.bind.annotation.XmlElement;
16
import javax.xml.bind.annotation.XmlSeeAlso;
17
import javax.xml.bind.annotation.XmlType;
18

    
19
import org.apache.log4j.Logger;
20
import org.hibernate.envers.Audited;
21
import org.hibernate.search.annotations.Indexed;
22

    
23
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
24
import eu.etaxonomy.cdm.model.description.State;
25
import eu.etaxonomy.cdm.model.location.NamedArea;
26
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
27
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
28
import eu.etaxonomy.cdm.model.name.Rank;
29

    
30
/**
31
 * @author m.doering
32
 * @created 08-Nov-2007 13:06:23
33
 */
34
@XmlAccessorType(XmlAccessType.FIELD)
35
@XmlType(name = "OrderedTermBase", propOrder = {
36
    "orderIndex"
37
})
38
@XmlSeeAlso({
39
	RelationshipTermBase.class,
40
	PresenceAbsenceTermBase.class,
41
	State.class,
42
	NamedArea.class,
43
	NamedAreaLevel.class,
44
	NomenclaturalStatusType.class,
45
	Rank.class
46
})
47
@Entity
48
@Indexed(index = "eu.etaxonomy.cdm.model.common.DefinedTermBase")
49
@Audited
50
public abstract class OrderedTermBase<T extends OrderedTermBase> extends DefinedTermBase<T> implements Comparable<T> {
51
	private static final long serialVersionUID = 8000797926720467399L;
52
	@SuppressWarnings("unused")
53
	private static final Logger logger = Logger.getLogger(OrderedTermBase.class);
54
	
55
	//Order index, value < 1 means that this Term is not in order yet
56
	@XmlElement(name = "OrderIndex")
57
	protected int orderIndex;
58
	
59
	/**
60
	 * Higher ordered terms have a lower order index, 
61
	 * lower ordered terms have a higher order index:
62
	 * <p>
63
	 * <b>a.oderIndex &lt; b.oderIndex : a &gt; b</b>
64
	 * @return the order index of a term
65
	 */
66
	public int getOrderIndex() {
67
		return orderIndex;
68
	}
69

    
70
// *********************** CONSTRUCTOR *************************/
71
	
72
	//for JAXB only, TODO needed?
73
    @Deprecated
74
    protected OrderedTermBase(){}
75
	
76
	protected OrderedTermBase(TermType type) {
77
		super(type);
78
	}
79
	public OrderedTermBase(TermType type, String term, String label, String labelAbbrev) {
80
		super(type, term, label, labelAbbrev);
81
	}	
82
	
83
// **************************** METHODS ******************************/	
84
	
85
	/**
86
	 * Compares this OrderedTermBase with the specified OrderedTermBase for
87
	 * order. Returns a negative integer, zero, or a positive integer as the
88
	 * orderId of this object is greater than, equal to, or less than the
89
	 * specified object. This Integer compare logic of this method is the
90
	 * <b>inverse logic</b> of the the one implemented in
91
	 * {@link java.lang.Comparable#compareTo(java.lang.Object)}
92
	 */
93
	public int compareTo(T orderedTerm) {
94
		return performCompareTo(orderedTerm, false);
95
	}
96
	
97
	/**
98
	 * Compares this OrderedTermBase with the specified OrderedTermBase for
99
	 * order. Returns a negative integer, zero, or a positive integer as the
100
	 * orderId of this object is greater than, equal to, or less than the
101
	 * specified object. This Integer compare logic of this method is the
102
	 * <b>inverse logic</b> of the the one implemented in
103
	 * {@link java.lang.Comparable#compareTo(java.lang.Object)}
104
	 */
105
	protected int performCompareTo(T orderedTerm, boolean skipVocabularyCheck) {
106
		orderedTerm = (T) CdmBase.deproxy(orderedTerm, OrderedTermBase.class);
107
		if(!skipVocabularyCheck){
108
			if (this.vocabulary == null || orderedTerm.vocabulary == null){
109
				throw new IllegalStateException("An ordered term (" + this.toString() + " or " + orderedTerm.toString() + ") of class " + this.getClass() + " or " + orderedTerm.getClass() + " does not belong to a vocabulary and therefore can not be compared");
110
			}
111
			if (! this.getVocabulary().getUuid().equals(orderedTerm.vocabulary.getUuid())){
112
				throw new IllegalStateException("2 terms do not belong to the same vocabulary and therefore can not be compared");
113
			}
114
		}
115
		
116
		int orderThat;
117
		int orderThis;
118
		try {
119
			orderThat = orderedTerm.orderIndex;//OLD: this.getVocabulary().getTerms().indexOf(orderedTerm);
120
			orderThis = orderIndex; //OLD: this.getVocabulary().getTerms().indexOf(this);
121
		} catch (RuntimeException e) {
122
			throw e;
123
		}
124
		if (orderThis > orderThat){
125
			return -1;
126
		}else if (orderThis < orderThat){
127
			return 1;
128
		}else {
129
			return 0;
130
		}
131
	}
132
	
133
//	public int compareTo(IdentifiableEntity o) {
134
//		if (o instanceof OrderedTermBase){
135
//			return compareTo((OrderedTermBase)o);
136
//		}else{
137
//			return super.compareTo(o);
138
//		}
139
//	}
140
	
141
	/**
142
	 * If this term is lower than the parameter term, true is returned, else false.
143
	 * If the parameter term is null, an Exception is thrown.
144
	 * @param orderedTerm
145
	 * @return boolean result of the comparison
146
	 */
147
	public boolean isLower(T orderedTerm){
148
		return (this.compareTo(orderedTerm) < 0 );
149
	}
150

    
151
	
152
	/**
153
	 * If this term is higher than the parameter term, true is returned, else false.
154
	 * If the parameter term is null, an Exception is thrown.
155
	 * @param orderedTerm
156
	 * @return boolean result of the comparison
157
	 */
158
	public boolean isHigher(T orderedTerm){
159
		return (this.compareTo(orderedTerm) > 0 );
160
	}
161
	
162
	
163
	/** 
164
	 * @deprecated To be used only by OrderedTermVocabulary
165
	 **/
166
	@Deprecated
167
	protected boolean decreaseIndex(OrderedTermVocabulary<T> vocabulary){
168
		if (vocabulary.indexChangeAllowed(this) == true){
169
			orderIndex--;
170
			return true;
171
		}else{
172
			return false;
173
		}
174
	}
175
	
176
	/** 
177
	 * @deprecated To be used only by OrderedTermVocabulary
178
	 **/
179
	@Deprecated
180
	protected boolean incrementIndex(OrderedTermVocabulary<T> vocabulary){
181
		if (vocabulary.indexChangeAllowed(this) == true){
182
			orderIndex++;
183
			return true;
184
		}else{
185
			return false;
186
		}
187
	}
188
	
189
	@Override
190
	public boolean equals(Object object){
191
		if(this == object)
192
			return true;
193
		if((object == null) || (!OrderedTermBase.class.isAssignableFrom(object.getClass()))) {
194
			return false;
195
		}else{
196
			OrderedTermBase orderedTermBase = (OrderedTermBase)object;
197
			if (orderedTermBase.getUuid().equals(this.getUuid())){
198
				return true;
199
			}else{
200
				return false;
201
			}
202
		}
203
	}
204
	
205
//*********************** CLONE ********************************************************/
206
	
207
	/** 
208
	 * Clones <i>this</i> OrderedTermBase. This is a shortcut that enables to create
209
	 * a new instance that differs only slightly from <i>this</i> OrderedTermBase.
210
	 * 
211
	 * @see eu.etaxonomy.cdm.model.common.DefinedTermBase#clone()
212
	 * @see java.lang.Object#clone()
213
	 */
214
	@Override
215
	public Object clone() {
216
		OrderedTermBase result = (OrderedTermBase) super.clone();
217
		//no changes to orderIndex
218
		return result;
219
	}
220
}
(49-49/70)