Project

General

Profile

Download (3.15 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

    
14
import eu.etaxonomy.cdm.compare.common.OrderIndexComparator;
15
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
16
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
17
import eu.etaxonomy.cdm.strategy.cache.TagEnum;
18
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
19

    
20
/**
21
 * @author k.luther
22
 * @since 18.03.2010
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
		if (node1.getUuid().equals(node2.getUuid())){
36
			return 0;
37
		}
38
		//They should both be put to the end (first unplaced then excluded)
39
		if (node2Excluded && !node1Excluded){
40
		    return -1;
41
		}
42
		if (node2Unplaced && !(node1Unplaced || node1Excluded)){
43
		    return -1;
44
		}
45

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

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

    
60
		Integer rankTax1 = node1.getRankOrderIndex();
61
		Integer rankTax2 = node2.getRankOrderIndex();
62

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

    
66
		if (rankOrder == 0) {
67
			if (node1.getTaggedTitle() != null && node2.getTaggedTitle() != null){
68
				//same rank, order by name
69
			    String sortableName1 = "";
70
			    for (TaggedText tagged: node1.getTaggedTitle()){
71
			        if (tagged.getType().equals(TagEnum.name)){
72
			            sortableName1 += " " + tagged.getText();
73
			        }
74
			    }
75

    
76
			    String sortableName2 = "";
77
                for (TaggedText tagged: node2.getTaggedTitle()){
78
                    if (tagged.getType().equals(TagEnum.name)){
79
                        sortableName2 += " " + tagged.getText();
80
                    }
81
                }
82
				int result = sortableName1.compareTo(sortableName2);
83
				if (result == 0){
84
					return node1.getTaxonUuid().compareTo(node2.getTaxonUuid());
85
				}else{
86
					return result;
87
				}
88
			}else {
89
				//this is maybe not 100% correct, we need to compare name cases, but it is a very rare case
90
				return node1.getTaxonTitleCache().compareTo(node2.getTaxonTitleCache());
91
			}
92
		}else{
93
			//rankTax2.isHigher(rankTax1)
94
			return rankOrder;
95
		}
96
	}
97

    
98
    public String getTaxonTitle(TaxonBase<?> taxon, TaxonNode node) {
99
        return (taxon == null) ? node.getUuid().toString(): taxon.getTitleCache();
100
    }
101
}
(20-20/29)