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