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.api.service;
|
10
|
|
11
|
import java.io.Serializable;
|
12
|
import java.util.Comparator;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import eu.etaxonomy.cdm.model.name.Rank;
|
16
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
17
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
18
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
19
|
|
20
|
/**
|
21
|
* @author k.luther
|
22
|
* @since 18.03.2010
|
23
|
*
|
24
|
*/
|
25
|
public class TaxonNodeDtoByRankAndNameComparator implements Serializable, Comparator<TaxonNodeDto> {
|
26
|
private static final long serialVersionUID = 2596641007876609704L;
|
27
|
|
28
|
@Override
|
29
|
public int compare(TaxonNodeDto node1, TaxonNodeDto node2) {
|
30
|
|
31
|
boolean node1Excluded = node1.isExcluded();
|
32
|
boolean node2Excluded = node2.isExcluded();
|
33
|
boolean node1Unplaced = node1.isUnplaced();
|
34
|
boolean node2Unplaced = node2.isUnplaced();
|
35
|
|
36
|
|
37
|
if (node1.getUuid().equals(node2.getUuid())){
|
38
|
return 0;
|
39
|
}
|
40
|
//They should both be put to the end (first unplaced then excluded)
|
41
|
if (node2Excluded && !node1Excluded){
|
42
|
return -1;
|
43
|
}
|
44
|
if (node2Unplaced && !(node1Unplaced || node1Excluded)){
|
45
|
return -1;
|
46
|
}
|
47
|
|
48
|
if (node1Excluded && !node2Excluded){
|
49
|
return 1;
|
50
|
}
|
51
|
if (node1Unplaced && !(node2Unplaced || node2Excluded)){
|
52
|
return 1;
|
53
|
}
|
54
|
|
55
|
if (node1Unplaced && node2Excluded){
|
56
|
return -1;
|
57
|
}
|
58
|
if (node2Unplaced && node1Excluded){
|
59
|
return 1;
|
60
|
}
|
61
|
|
62
|
|
63
|
|
64
|
|
65
|
Rank rankTax1 = node1.getRank();
|
66
|
Rank rankTax2 = node2.getRank();
|
67
|
|
68
|
//first compare ranks, if ranks are equal (or both null) compare names or taxon title cache if names are null
|
69
|
if (rankTax1 == null && rankTax2 != null){
|
70
|
return 1;
|
71
|
}else if(rankTax2 == null && rankTax1 != null){
|
72
|
return -1;
|
73
|
}else if (rankTax1 != null && rankTax1.isHigher(rankTax2)){
|
74
|
return -1;
|
75
|
}else if (rankTax1 == null && rankTax2 == null || rankTax1.equals(rankTax2)) {
|
76
|
if (node1.getTitleCache() != null && node2.getTitleCache() != null){
|
77
|
//same rank, order by name
|
78
|
int result = node1.getNameTitleCache().compareTo(node2.getNameTitleCache());
|
79
|
if (result == 0){
|
80
|
return node1.getTaxonUuid().compareTo(node2.getTaxonUuid());
|
81
|
}else{
|
82
|
return result;
|
83
|
}
|
84
|
}else {
|
85
|
//this is maybe not 100% correct, we need to compare name cases, but it is a very rare case
|
86
|
return node1.getTaxonTitleCache().compareTo(node2.getTaxonTitleCache());
|
87
|
}
|
88
|
}else{
|
89
|
//rankTax2.isHigher(rankTax1)
|
90
|
return 1;
|
91
|
}
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* @param taxon1
|
96
|
* @return
|
97
|
*/
|
98
|
public String getTaxonTitle(TaxonBase<?> taxon, TaxonNode node) {
|
99
|
return (taxon == null) ? node.getUuid().toString(): taxon.getTitleCache();
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* @param taxon
|
104
|
* @param node
|
105
|
* @return
|
106
|
*/
|
107
|
private UUID getTaxonUuid(TaxonBase<?> taxon, TaxonNode node) {
|
108
|
return (taxon == null) ? node.getUuid(): taxon.getUuid();
|
109
|
}
|
110
|
|
111
|
|
112
|
|
113
|
}
|