Fixes an issue where update of title caches did not work for cloned instances.
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / OrderedTermBase.java
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.Modifier;
24 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
25 import eu.etaxonomy.cdm.model.description.State;
26 import eu.etaxonomy.cdm.model.location.NamedArea;
27 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
28 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
29 import eu.etaxonomy.cdm.model.name.Rank;
30
31 /**
32 * @author m.doering
33 * @version 1.0
34 * @created 08-Nov-2007 13:06:23
35 */
36 @XmlAccessorType(XmlAccessType.FIELD)
37 @XmlType(name = "OrderedTermBase", propOrder = {
38 "orderIndex"
39 })
40 @XmlSeeAlso({
41 RelationshipTermBase.class,
42 Modifier.class,
43 PresenceAbsenceTermBase.class,
44 State.class,
45 NamedArea.class,
46 NamedAreaLevel.class,
47 NomenclaturalStatusType.class,
48 Rank.class
49 })
50 @Entity
51 @Indexed(index = "eu.etaxonomy.cdm.model.common.DefinedTermBase")
52 @Audited
53 public abstract class OrderedTermBase<T extends OrderedTermBase> extends DefinedTermBase<T> implements Comparable<T> {
54 private static final long serialVersionUID = 8000797926720467399L;
55 @SuppressWarnings("unused")
56 private static final Logger logger = Logger.getLogger(OrderedTermBase.class);
57
58 //Order index, value < 1 means that this Term is not in order yet
59 @XmlElement(name = "OrderIndex")
60 protected int orderIndex;
61
62 /**
63 * Higher ordered terms have a lower order index,
64 * lower ordered terms have a higher order index:
65 * <p>
66 * <b>a.oderIndex &lt; b.oderIndex : a &gt; b</b>
67 * @return the order index of a term
68 */
69 public int getOrderIndex() {
70 return orderIndex;
71 }
72
73 public OrderedTermBase() {
74 super();
75 }
76 public OrderedTermBase(String term, String label, String labelAbbrev) {
77 super(term, label, labelAbbrev);
78 }
79
80 /**
81 * Compares this OrderedTermBase with the specified OrderedTermBase for
82 * order. Returns a negative integer, zero, or a positive integer as the
83 * orderId of this object is greater than, equal to, or less than the
84 * specified object. This Integer compare logic of this method is the
85 * <b>inverse logic</b> of the the one implemented in
86 * {@link java.lang.Comparable#compareTo(java.lang.Object)}
87 */
88 public int compareTo(T orderedTerm) {
89 if (this.vocabulary == null || ! this.vocabulary.getUuid().equals(orderedTerm.vocabulary.getUuid())){
90 throw new IllegalStateException("2 terms do not belong to the same vocabulary and therefore can not be compared");
91 }
92
93 int orderThat;
94 int orderThis;
95 try {
96 orderThat = orderedTerm.orderIndex;//OLD: this.getVocabulary().getTerms().indexOf(orderedTerm);
97 orderThis = orderIndex; //OLD: this.getVocabulary().getTerms().indexOf(this);
98 } catch (RuntimeException e) {
99 throw e;
100 }
101 if (orderThis > orderThat){
102 return -1;
103 }else if (orderThis < orderThat){
104 return 1;
105 }else {
106 return 0;
107 }
108 }
109
110 // public int compareTo(IdentifiableEntity o) {
111 // if (o instanceof OrderedTermBase){
112 // return compareTo((OrderedTermBase)o);
113 // }else{
114 // return super.compareTo(o);
115 // }
116 // }
117
118 /**
119 * If this term is lower than the parameter term, true is returned, else false.
120 * If the parameter term is null, an Exception is thrown.
121 * @param orderedTerm
122 * @return boolean result of the comparison
123 */
124 public boolean isLower(T orderedTerm){
125 return (this.compareTo(orderedTerm) < 0 );
126 }
127
128
129 /**
130 * If this term is higher than the parameter term, true is returned, else false.
131 * If the parameter term is null, an Exception is thrown.
132 * @param orderedTerm
133 * @return boolean result of the comparison
134 */
135 public boolean isHigher(T orderedTerm){
136 return (this.compareTo(orderedTerm) > 0 );
137 }
138
139
140 /**
141 * @deprecated To be used only by OrderedTermVocabulary
142 **/
143 @Deprecated
144 protected boolean decreaseIndex(OrderedTermVocabulary<T> vocabulary){
145 if (vocabulary.indexChangeAllowed(this) == true){
146 orderIndex--;
147 return true;
148 }else{
149 return false;
150 }
151 }
152
153 /**
154 * @deprecated To be used only by OrderedTermVocabulary
155 **/
156 @Deprecated
157 protected boolean incrementIndex(OrderedTermVocabulary<T> vocabulary){
158 if (vocabulary.indexChangeAllowed(this) == true){
159 orderIndex++;
160 return true;
161 }else{
162 return false;
163 }
164 }
165
166 @Override
167 public boolean equals(Object object){
168 if(this == object)
169 return true;
170 if((object == null) || (!OrderedTermBase.class.isAssignableFrom(object.getClass()))) {
171 return false;
172 }else{
173 OrderedTermBase orderedTermBase = (OrderedTermBase)object;
174 if (orderedTermBase.getUuid().equals(this.getUuid())){
175 return true;
176 }else{
177 return false;
178 }
179 }
180 }
181 }