Project

General

Profile

« Previous | Next » 

Revision 4dccf2d6

Added by Andreas Müller about 3 years ago

fix #8858 Improve presence/absence handling for PresenceAbsenceTerm.compareTo

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/term/OrderedTermBase.java
110 110
    }
111 111

  
112 112
    /**
113
     * Compares this OrderedTermBase with the specified OrderedTermBase for
114
     * order. Returns a -1, 0, or +1 if the orderId of this object is greater
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 115
     * than, equal to, or less than the specified object.
116 116
     * <p>
117 117
     * <b>Note:</b> The compare logic of this method is the <b>inverse logic</b>
......
126 126
     * @throws NullPointerException
127 127
     *             if the specified object is null
128 128
     */
129
    protected int performCompareTo(T thatTerm, boolean skipVocabularyCheck) {
129
    protected int performCompareTo(T thatTerm , boolean skipVocabularyCheck ) {
130 130

  
131
    	OrderedTermBase<?> thatTermLocal = CdmBase.deproxy(thatTerm);
131
    	T thatTermLocal = CdmBase.deproxy(thatTerm);
132 132
    	if(!skipVocabularyCheck){
133 133
            if (this.vocabulary == null || thatTermLocal.vocabulary == null){
134 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");
......
138 138
            }
139 139
        }
140 140

  
141
    	//if vocabularies are not equal order by voc.uuid to get a defined behavior
142
    	//ordering terms from 2 different vocabularies is generally not recommended
143
    	UUID thisVocUuid = this.vocabulary == null? null:this.vocabulary.getUuid();
144
        UUID thatVocUuid = thatTermLocal.getVocabulary() == null? null:thatTermLocal.getVocabulary().getUuid();
145
        int vocCompare = CdmUtils.nullSafeCompareTo(thisVocUuid, thatVocUuid);
141
    	int vocCompare = compareVocabularies(thatTermLocal);
146 142
        if (vocCompare != 0){
147 143
            return vocCompare;
148 144
        }
......
150 146
        int orderThat;
151 147
        int orderThis;
152 148
        try {
153
            orderThat = thatTermLocal.orderIndex;//OLD: this.getVocabulary().getTerms().indexOf(orderedTerm);
154
            orderThis = orderIndex; //OLD: this.getVocabulary().getTerms().indexOf(this);
149
            orderThat = thatTermLocal.orderIndex;
150
            orderThis = orderIndex;
155 151
        } catch (RuntimeException e) {
156 152
            throw e;
157 153
        }
......
163 159
            if (skipVocabularyCheck){
164 160
                String errorStr = "The term %s (ID: %s) is not attached to any vocabulary. This should not happen. "
165 161
                        + "Please add the term to an vocabulary";
166
                if (this.getVocabulary() == null){
162
                if (this.vocabulary == null){
167 163
                    throw new IllegalStateException(String.format(errorStr, this.getLabel(), String.valueOf(this.getId())));
168 164
                }else if (thatTermLocal.vocabulary == null){
169 165
                    throw new IllegalStateException(String.format(errorStr, thatTermLocal.getLabel(), String.valueOf(thatTermLocal.getId())));
170 166
                }
171
                return this.getVocabulary().getUuid().compareTo(thatTermLocal.vocabulary.getUuid());
172
            }else{
173
                return 0;
174 167
            }
168
            return 0;
175 169
        }
176 170
    }
177 171

  
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);
178
        return vocCompare;
179
    }
180

  
178 181
    /**
179 182
     * If this term is lower than the parameter term, true is returned, else false.
180 183
     * If the parameter term is null, an Exception is thrown.

Also available in: Unified diff