Project

General

Profile

Download (8.53 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.persistence.Transient;
14
import javax.xml.bind.annotation.XmlAccessType;
15
import javax.xml.bind.annotation.XmlAccessorType;
16
import javax.xml.bind.annotation.XmlElement;
17
import javax.xml.bind.annotation.XmlSeeAlso;
18
import javax.xml.bind.annotation.XmlType;
19

    
20
import org.apache.log4j.Logger;
21
import org.hibernate.envers.Audited;
22

    
23
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
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
    PresenceAbsenceTerm.class,
41
    State.class,
42
    NamedArea.class,
43
    NamedAreaLevel.class,
44
    NomenclaturalStatusType.class,
45
    Rank.class
46
})
47
@Entity
48
@Audited
49
public abstract class OrderedTermBase<T extends OrderedTermBase<?>> extends DefinedTermBase<T> {
50
    private static final long serialVersionUID = 8000797926720467399L;
51
    @SuppressWarnings("unused")
52
    private static final Logger logger = Logger.getLogger(OrderedTermBase.class);
53

    
54
    //Order index, value < 1 means that this Term is not in order yet
55
    @XmlElement(name = "OrderIndex")
56
    protected int orderIndex;
57

    
58
    /**
59
     * Higher ordered terms have a lower order index,
60
     * lower ordered terms have a higher order index:
61
     * <p>
62
     * <b>a.oderIndex &lt; b.oderIndex : a &gt; b</b>
63
     * @return the order index of a term
64
     */
65
    public int getOrderIndex() {
66
        return orderIndex;
67
    }
68

    
69
// *********************** CONSTRUCTOR *************************/
70

    
71
    //for JAXB only, TODO needed?
72
    @Deprecated
73
    protected OrderedTermBase(){}
74

    
75
    protected OrderedTermBase(TermType type) {
76
        super(type);
77
    }
78
    public OrderedTermBase(TermType type, String term, String label, String labelAbbrev) {
79
        super(type, term, label, labelAbbrev);
80
    }
81

    
82
// **************************** METHODS ******************************/
83

    
84
    /**
85
     * Compares this OrderedTermBase with the specified OrderedTermBase for
86
     * order. Returns a -1, 0, or +1 if the orderIndex of this object is greater
87
     * than, equal to, or less than the specified object. In case the parameter
88
     * is <code>null</code> the
89
     * <p>
90
     * <b>Note:</b> The compare logic of this method might appear to be <b>inverse</b>
91
     * to the one mentioned in
92
     * {@link java.lang.Comparable#compareTo(java.lang.Object)}. This is, because the logic here
93
     * is that the lower the orderIndex the higher the term. E.g. the very high {@link Rank}
94
     * Kingdom may have an orderIndex close to 1.
95
     *
96
     * @param orderedTerm
97
     *            the OrderedTermBase to be compared
98
     * @throws NullPointerException
99
     *             if the specified object is null
100
     */
101
    @Override
102
    public int compareTo(T orderedTerm) {
103
        return performCompareTo(orderedTerm, false);
104
    }
105

    
106
    /**
107
     * Compares this OrderedTermBase with the specified OrderedTermBase for
108
     * order. Returns a -1, 0, or +1 if the orderId of this object is greater
109
     * than, equal to, or less than the specified object.
110
     * <p>
111
     * <b>Note:</b> The compare logic of this method is the <b>inverse logic</b>
112
     * of the the one implemented in
113
     * {@link java.lang.Comparable#compareTo(java.lang.Object)}
114
     *
115
     * @param orderedTerm
116
     *            the OrderedTermBase to be compared
117
     * @param skipVocabularyCheck
118
     *            whether to skip checking if both terms to compare are in the
119
     *            same vocabulary
120
     * @throws NullPointerException
121
     *             if the specified object is null
122
     */
123
    protected int performCompareTo(T orderedTerm, boolean skipVocabularyCheck) {
124

    
125
    	OrderedTermBase<?> orderedTermLocal = CdmBase.deproxy(orderedTerm, OrderedTermBase.class);
126
        if(!skipVocabularyCheck){
127
            if (this.vocabulary == null || orderedTermLocal.vocabulary == null){
128
                throw new IllegalStateException("An ordered term (" + this.toString() + " or " + orderedTermLocal.toString() + ") of class " + this.getClass() + " or " + orderedTermLocal.getClass() + " does not belong to a vocabulary and therefore can not be compared");
129
            }
130
            if (! this.getVocabulary().getUuid().equals(orderedTermLocal.vocabulary.getUuid())){
131
               throw new IllegalStateException("2 terms do not belong to the same vocabulary and therefore can not be compared: " + this.getTitleCache() + " and " + orderedTermLocal.getTitleCache());
132
            }
133
        }
134

    
135
        int orderThat;
136
        int orderThis;
137
        try {
138
            orderThat = orderedTermLocal.orderIndex;//OLD: this.getVocabulary().getTerms().indexOf(orderedTerm);
139
            orderThis = orderIndex; //OLD: this.getVocabulary().getTerms().indexOf(this);
140
        } catch (RuntimeException e) {
141
            throw e;
142
        }
143
        if (orderThis > orderThat){
144
            return -1;
145
        }else if (orderThis < orderThat){
146
            return 1;
147
        }else {
148
            return 0;
149
        }
150
    }
151

    
152

    
153
    /**
154
     * If this term is lower than the parameter term, true is returned, else false.
155
     * If the parameter term is null, an Exception is thrown.
156
     * @param orderedTerm
157
     * @return boolean result of the comparison
158
     */
159
    public boolean isLower(T orderedTerm){
160
        return (this.compareTo(orderedTerm) < 0 );
161
    }
162

    
163

    
164
    /**
165
     * If this term is higher than the parameter term, true is returned, else false.
166
     * If the parameter term is null, an Exception is thrown.
167
     * @param orderedTerm
168
     * @return boolean result of the comparison
169
     */
170
    public boolean isHigher(T orderedTerm){
171
        return (this.compareTo(orderedTerm) > 0 );
172
    }
173

    
174

    
175
    /**
176
     * @deprecated To be used only by OrderedTermVocabulary
177
     **/
178
    @Deprecated
179
    protected boolean decreaseIndex(OrderedTermVocabulary<T> vocabulary){
180
        if (vocabulary.indexChangeAllowed(this) == true){
181
            orderIndex--;
182
            return true;
183
        }else{
184
            return false;
185
        }
186
    }
187

    
188
    /**
189
     * @deprecated To be used only by OrderedTermVocabulary
190
     **/
191
    @Deprecated
192
    protected boolean incrementIndex(OrderedTermVocabulary<T> vocabulary){
193
        if (vocabulary.indexChangeAllowed(this) == true){
194
            orderIndex++;
195
            return true;
196
        }else{
197
            return false;
198
        }
199
    }
200

    
201
    @Override
202
    public boolean equals(Object object){
203
        if(this == object) {
204
            return true;
205
        }
206
        if((object == null) || (!OrderedTermBase.class.isAssignableFrom(object.getClass()))) {
207
            return false;
208
        }else{
209
            OrderedTermBase<?> orderedTermBase = (OrderedTermBase<?>)object;
210
            if (orderedTermBase.getUuid().equals(this.getUuid())){
211
                return true;
212
            }else{
213
                return false;
214
            }
215
        }
216
    }
217

    
218
    @SuppressWarnings("unchecked")
219
    @Transient
220
    public T getNextHigherTerm(){  //#3327
221
        if (getVocabulary() == null){
222
            return null;
223
        }else{
224
            OrderedTermBase<T> result = CdmBase.deproxy(getVocabulary(), OrderedTermVocabulary.class).getNextHigherTerm(this);
225
            return (T)result;
226
        }
227
    }
228

    
229
    @SuppressWarnings("unchecked")
230
    @Transient
231
    public T getNextLowerTerm(){ //#3327
232
        if (getVocabulary() == null){
233
            return null;
234
        }else{
235
            OrderedTermBase<T> result = CdmBase.deproxy(getVocabulary(), OrderedTermVocabulary.class).getNextLowerTerm(this);
236
            return (T)result;
237
        }
238
    }
239

    
240
//*********************** CLONE ********************************************************/
241

    
242
    /**
243
     * Clones <i>this</i> OrderedTermBase. This is a shortcut that enables to create
244
     * a new instance that differs only slightly from <i>this</i> OrderedTermBase.
245
     *
246
     * @see eu.etaxonomy.cdm.model.common.DefinedTermBase#clone()
247
     * @see java.lang.Object#clone()
248
     */
249
    @Override
250
    public Object clone() {
251
        OrderedTermBase<?> result = (OrderedTermBase<?>) super.clone();
252
        //no changes to orderIndex
253
        return result;
254
    }
255
}
(52-52/73)