Project

General

Profile

Download (2.88 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
package eu.etaxonomy.cdm.persistence.dto;
10

    
11
import java.io.Serializable;
12
import java.util.Comparator;
13
import java.util.UUID;
14

    
15
import eu.etaxonomy.cdm.model.common.OrderIndexComparator;
16
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
17
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
18

    
19
/**
20
 * @author k.luther
21
 * @since 18.03.2010
22
 *
23
 */
24
public class TaxonNodeDtoByRankAndNameComparator implements Serializable, Comparator<TaxonNodeDto> {
25
	private static final long serialVersionUID = 2596641007876609704L;
26

    
27
	@Override
28
    public int compare(TaxonNodeDto node1, TaxonNodeDto node2) {
29

    
30
	    boolean node1Excluded = node1.isExcluded();
31
	    boolean node2Excluded = node2.isExcluded();
32
	    boolean node1Unplaced = node1.isUnplaced();
33
	    boolean node2Unplaced = node2.isUnplaced();
34

    
35

    
36
		if (node1.getUuid().equals(node2.getUuid())){
37
			return 0;
38
		}
39
		//They should both be put to the end (first unplaced then excluded)
40
		if (node2Excluded && !node1Excluded){
41
		    return -1;
42
		}
43
		if (node2Unplaced && !(node1Unplaced || node1Excluded)){
44
		    return -1;
45
		}
46

    
47
		if (node1Excluded && !node2Excluded){
48
            return 1;
49
        }
50
        if (node1Unplaced && !(node2Unplaced || node2Excluded)){
51
            return 1;
52
        }
53

    
54
        if (node1Unplaced && node2Excluded){
55
            return -1;
56
        }
57
        if (node2Unplaced && node1Excluded){
58
            return 1;
59
        }
60

    
61

    
62

    
63

    
64
		Integer rankTax1 = node1.getRankOrderIndex();
65
		Integer rankTax2 = node2.getRankOrderIndex();
66

    
67
		//first compare ranks, if ranks are equal (or both null) compare names or taxon title cache if names are null
68
		int rankOrder = OrderIndexComparator.instance().compare(rankTax1, rankTax2);
69

    
70
		if (rankOrder == 0) {
71
			if (node1.getTitleCache() != null && node2.getTitleCache() != null){
72
				//same rank, order by name
73
				int result = node1.getNameTitleCache().compareTo(node2.getNameTitleCache());
74
				if (result == 0){
75
					return node1.getTaxonUuid().compareTo(node2.getTaxonUuid());
76
				}else{
77
					return result;
78
				}
79
			}else {
80
				//this is maybe not 100% correct, we need to compare name cases, but it is a very rare case
81
				return node1.getTaxonTitleCache().compareTo(node2.getTaxonTitleCache());
82
			}
83
		}else{
84
			//rankTax2.isHigher(rankTax1)
85
			return rankOrder;
86
		}
87
	}
88

    
89
    /**
90
     * @param taxon1
91
     * @return
92
     */
93
    public String getTaxonTitle(TaxonBase<?> taxon, TaxonNode node) {
94
        return (taxon == null) ? node.getUuid().toString(): taxon.getTitleCache();
95
    }
96

    
97
    /**
98
     * @param taxon
99
     * @param node
100
     * @return
101
     */
102
    private UUID getTaxonUuid(TaxonBase<?> taxon, TaxonNode node) {
103
        return (taxon == null) ? node.getUuid(): taxon.getUuid();
104
    }
105

    
106

    
107

    
108
}
(11-11/16)