2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.model
.term
;
11 import java
.util
.UUID
;
13 import javax
.persistence
.Entity
;
14 import javax
.persistence
.Transient
;
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
.XmlSeeAlso
;
19 import javax
.xml
.bind
.annotation
.XmlType
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.hibernate
.envers
.Audited
;
24 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipTermBase
;
27 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
28 import eu
.etaxonomy
.cdm
.model
.description
.State
;
29 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
30 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
31 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
32 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
36 * @since 08-Nov-2007 13:06:23
38 @XmlAccessorType(XmlAccessType
.FIELD
)
39 @XmlType(name
= "OrderedTermBase", propOrder
= {
43 RelationshipTermBase
.class,
44 PresenceAbsenceTerm
.class,
48 NomenclaturalStatusType
.class,
53 public abstract class OrderedTermBase
<T
extends OrderedTermBase
<T
>>
54 extends DefinedTermBase
<T
> {
56 private static final long serialVersionUID
= 8000797926720467399L;
57 @SuppressWarnings("unused")
58 private static final Logger logger
= Logger
.getLogger(OrderedTermBase
.class);
60 //Order index, value < 1 means that this Term is not in order yet
61 @XmlElement(name
= "OrderIndex")
62 protected int orderIndex
;
65 * Higher ordered terms have a lower order index,
66 * lower ordered terms have a higher order index:
68 * <b>a.oderIndex < b.oderIndex : a > b</b>
69 * @return the order index of a term
71 public int getOrderIndex() {
75 // *********************** CONSTRUCTOR *************************/
77 //for JAXB only, TODO needed?
79 protected OrderedTermBase(){}
81 protected OrderedTermBase(TermType type
) {
84 public OrderedTermBase(TermType type
, String description
, String label
, String labelAbbrev
) {
85 super(type
, description
, label
, labelAbbrev
);
88 // **************************** METHODS ******************************/
91 * Compares this OrderedTermBase with the specified OrderedTermBase for
92 * order. Returns a -1, 0, or +1 if the orderIndex of this object is greater
93 * than, equal to, or less than the specified object. In case the parameter
94 * is <code>null</code> the
96 * <b>Note:</b> The compare logic of this method might appear to be <b>inverse</b>
97 * to the one mentioned in
98 * {@link java.lang.Comparable#compareTo(java.lang.Object)}. This is, because the logic here
99 * is that the lower the orderIndex the higher the term. E.g. the very high {@link Rank}
100 * Kingdom may have an orderIndex close to 1.
103 * the OrderedTermBase to be compared
104 * @throws NullPointerException
105 * if the specified object is null
108 public int compareTo(T orderedTerm
) {
109 return performCompareTo(orderedTerm
, false);
113 * Compares this {@link OrderedTermBase ordered term} with the given {@link OrderedTermBase thatTerm} for
114 * order. Returns a -1, 0, or +1 if the orderId of this object is greater
115 * than, equal to, or less than the specified object.
117 * <b>Note:</b> The compare logic of this method is the <b>inverse logic</b>
118 * of the the one implemented in
119 * {@link java.lang.Comparable#compareTo(java.lang.Object)}
122 * the OrderedTermBase to be compared
123 * @param skipVocabularyCheck
124 * whether to skip checking if both terms to compare are in the
126 * @throws NullPointerException
127 * if the specified object is null
129 protected int performCompareTo(T thatTerm
, boolean skipVocabularyCheck
) {
131 T thatTermLocal
= CdmBase
.deproxy(thatTerm
);
132 if(!skipVocabularyCheck
){
133 if (this.vocabulary
== null || thatTermLocal
.vocabulary
== null){
134 throw new IllegalStateException("An ordered term (" + this.toString() + " or " + thatTermLocal
.toString() + ") of class " + this.getClass() + " or " + thatTermLocal
.getClass() + " does not belong to a vocabulary and therefore can not be compared");
136 if (! this.getVocabulary().getUuid().equals(thatTermLocal
.vocabulary
.getUuid())){
137 throw new IllegalStateException("2 terms do not belong to the same vocabulary and therefore can not be compared: " + this.getTitleCache() + " and " + thatTermLocal
.getTitleCache());
141 int vocCompare
= compareVocabularies(thatTermLocal
);
142 if (vocCompare
!= 0){
149 orderThat
= thatTermLocal
.orderIndex
;
150 orderThis
= orderIndex
;
151 } catch (RuntimeException e
) {
154 if (orderThis
> orderThat
){
156 }else if (orderThis
< orderThat
){
159 if (skipVocabularyCheck
){
160 String errorStr
= "The term %s (ID: %s) is not attached to any vocabulary. This should not happen. "
161 + "Please add the term to an vocabulary";
162 if (this.vocabulary
== null){
163 throw new IllegalStateException(String
.format(errorStr
, this.getLabel(), String
.valueOf(this.getId())));
164 }else if (thatTermLocal
.vocabulary
== null){
165 throw new IllegalStateException(String
.format(errorStr
, thatTermLocal
.getLabel(), String
.valueOf(thatTermLocal
.getId())));
172 protected int compareVocabularies(T thatTerm
) {
173 //if vocabularies are not equal order by voc.uuid to get a defined behavior
174 //ordering terms from 2 different vocabularies is generally not recommended
175 UUID thisVocUuid
= this.vocabulary
== null?
null:this.vocabulary
.getUuid();
176 UUID thatVocUuid
= thatTerm
.getVocabulary() == null?
null:thatTerm
.getVocabulary().getUuid();
177 int vocCompare
= CdmUtils
.nullSafeCompareTo(thisVocUuid
, thatVocUuid
);
182 * If this term is lower than the parameter term, true is returned, else false.
183 * If the parameter term is null, an Exception is thrown.
185 * @return boolean result of the comparison
187 public boolean isLower(T orderedTerm
){
188 return (this.compareTo(orderedTerm
) < 0 );
192 * If this term is higher than the parameter term, true is returned, else false.
193 * If the parameter term is null, an Exception is thrown.
195 * @return boolean result of the comparison
197 public boolean isHigher(T orderedTerm
){
198 return (this.compareTo(orderedTerm
) > 0 );
202 * @deprecated To be used only by OrderedTermVocabulary
205 protected boolean decreaseIndex(OrderedTermVocabulary vocabulary
){
206 if (vocabulary
.indexChangeAllowed(this) == true){
215 * @deprecated To be used only by OrderedTermVocabulary
218 protected boolean incrementIndex(OrderedTermVocabulary vocabulary
){
219 if (vocabulary
.indexChangeAllowed(this) == true){
228 @SuppressWarnings("unchecked")
230 public T
getNextHigherTerm(){ //#3327
231 if (getVocabulary() == null){
234 OrderedTermBase
<T
> result
= CdmBase
.deproxy(getVocabulary(), OrderedTermVocabulary
.class).getNextHigherTerm(this);
239 @SuppressWarnings("unchecked")
241 public T
getNextLowerTerm(){ //#3327
242 if (getVocabulary() == null){
245 OrderedTermBase
<T
> result
= CdmBase
.deproxy(getVocabulary(), OrderedTermVocabulary
.class).getNextLowerTerm(this);
250 //*********************** CLONE ********************************************************/
253 * Clones <i>this</i> OrderedTermBase. This is a shortcut that enables to create
254 * a new instance that differs only slightly from <i>this</i> OrderedTermBase.
256 * @see eu.etaxonomy.cdm.model.term.DefinedTermBase#clone()
257 * @see java.lang.Object#clone()
260 public OrderedTermBase
<T
> clone() {
261 OrderedTermBase
<T
> result
= (OrderedTermBase
<T
>) super.clone();
262 //no changes to orderIndex