Project

General

Profile

Download (8.78 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
 * @since 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
            if (skipVocabularyCheck){
149
                String errorStr = "The term %s (ID: %s) is not attached to any vocabulary. This should not happen. "
150
                        + "Please add the term to an vocabulary";
151
                if (this.getVocabulary() == null){
152
                    throw new IllegalStateException(String.format(errorStr, this.getLabel(), String.valueOf(this.getId())));
153
                }else if (orderedTermLocal.vocabulary == null){
154
                    throw new IllegalStateException(String.format(errorStr, orderedTermLocal.getLabel(), String.valueOf(orderedTermLocal.getId())));
155
                }
156
                return this.getVocabulary().getUuid().compareTo(orderedTermLocal.vocabulary.getUuid());
157
            }else{
158
                return 0;
159
            }
160
        }
161
    }
162

    
163

    
164
    /**
165
     * If this term is lower 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 isLower(T orderedTerm){
171
        return (this.compareTo(orderedTerm) < 0 );
172
    }
173

    
174

    
175
    /**
176
     * If this term is higher than the parameter term, true is returned, else false.
177
     * If the parameter term is null, an Exception is thrown.
178
     * @param orderedTerm
179
     * @return boolean result of the comparison
180
     */
181
    public boolean isHigher(T orderedTerm){
182
        return (this.compareTo(orderedTerm) > 0 );
183
    }
184

    
185

    
186
    /**
187
     * @deprecated To be used only by OrderedTermVocabulary
188
     **/
189
    @Deprecated
190
    protected boolean decreaseIndex(OrderedTermVocabulary vocabulary){
191
        if (vocabulary.indexChangeAllowed(this) == true){
192
            orderIndex--;
193
            return true;
194
        }else{
195
            return false;
196
        }
197
    }
198

    
199
    /**
200
     * @deprecated To be used only by OrderedTermVocabulary
201
     **/
202
    @Deprecated
203
    protected boolean incrementIndex(OrderedTermVocabulary vocabulary){
204
        if (vocabulary.indexChangeAllowed(this) == true){
205
            orderIndex++;
206
            return true;
207
        }else{
208
            return false;
209
        }
210
    }
211

    
212

    
213
    @SuppressWarnings("unchecked")
214
    @Transient
215
    public T getNextHigherTerm(){  //#3327
216
        if (getVocabulary() == null){
217
            return null;
218
        }else{
219
            OrderedTermBase<T> result = CdmBase.deproxy(getVocabulary(), OrderedTermVocabulary.class).getNextHigherTerm(this);
220
            return (T)result;
221
        }
222
    }
223

    
224
    @SuppressWarnings("unchecked")
225
    @Transient
226
    public T getNextLowerTerm(){ //#3327
227
        if (getVocabulary() == null){
228
            return null;
229
        }else{
230
            OrderedTermBase<T> result = CdmBase.deproxy(getVocabulary(), OrderedTermVocabulary.class).getNextLowerTerm(this);
231
            return (T)result;
232
        }
233
    }
234

    
235
//*********************** CLONE ********************************************************/
236

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