1
|
/**
|
2
|
* Copyright (C) 2009 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.model.taxon;
|
10
|
|
11
|
import java.util.Comparator;
|
12
|
import java.util.StringTokenizer;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
|
16
|
import eu.etaxonomy.cdm.common.AbstractStringComparator;
|
17
|
import eu.etaxonomy.cdm.common.UTF8;
|
18
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
19
|
import eu.etaxonomy.cdm.model.name.INonViralName;
|
20
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
21
|
|
22
|
/**
|
23
|
* Comparator that compares two TaxonNode instances by the titleCache of their referenced names.
|
24
|
* @author a.kohlbecker
|
25
|
* @since 24.06.2009
|
26
|
*
|
27
|
*/
|
28
|
public class TaxonNodeByNameComparator extends AbstractStringComparator<TaxonNode> implements Comparator<TaxonNode>, ITaxonNodeComparator<TaxonNode> {
|
29
|
|
30
|
private static final String HYBRID_SIGN = UTF8.HYBRID.toString();
|
31
|
|
32
|
private static final Logger logger = Logger.getLogger(TaxonNodeByNameComparator.class);
|
33
|
|
34
|
private boolean ignoreHybridSign = true;
|
35
|
private boolean sortInfraGenericFirst = true;
|
36
|
|
37
|
@Override
|
38
|
public int compare(TaxonNode node1, TaxonNode node2) {
|
39
|
if (node1 == null && node2 == null) {
|
40
|
return 0;
|
41
|
}
|
42
|
else if (node1 == null) {
|
43
|
return 1;
|
44
|
}
|
45
|
else if (node2 == null) {
|
46
|
return -1;
|
47
|
}
|
48
|
if (node1.equals(node2)){
|
49
|
return 0;
|
50
|
}
|
51
|
boolean node1Excluded = node1.isExcluded();
|
52
|
boolean node2Excluded = node2.isExcluded();
|
53
|
boolean node1Unplaced = node1.isUnplaced();
|
54
|
boolean node2Unplaced = node2.isUnplaced();
|
55
|
|
56
|
//They should both be put to the end (first unplaced then excluded)
|
57
|
if (node2Excluded && !node1Excluded){
|
58
|
return -1;
|
59
|
}
|
60
|
if (node2Unplaced && !(node1Unplaced || node1Excluded)){
|
61
|
return -1;
|
62
|
}
|
63
|
|
64
|
if (node1Excluded && !node2Excluded){
|
65
|
return 1;
|
66
|
}
|
67
|
if (node1Unplaced && !(node2Unplaced || node2Excluded)){
|
68
|
return 1;
|
69
|
}
|
70
|
|
71
|
if (node1Unplaced && node2Excluded){
|
72
|
return -1;
|
73
|
}
|
74
|
if (node2Unplaced && node1Excluded){
|
75
|
return 1;
|
76
|
}
|
77
|
|
78
|
String titleCache1 = createSortableTitleCache(node1);
|
79
|
String titleCache2 = createSortableTitleCache(node2);
|
80
|
|
81
|
if(isIgnoreHybridSign()) {
|
82
|
if (logger.isTraceEnabled()){logger.trace("ignoring Hybrid Signs: " + HYBRID_SIGN);}
|
83
|
titleCache1 = titleCache1.replace(HYBRID_SIGN, "");
|
84
|
titleCache2 = titleCache2.replace(HYBRID_SIGN, "");
|
85
|
}
|
86
|
|
87
|
titleCache1 = applySubstitutionRules(titleCache1);
|
88
|
titleCache2 = applySubstitutionRules(titleCache2);
|
89
|
|
90
|
// 1
|
91
|
StringTokenizer s2 = new StringTokenizer(titleCache1, "\"");
|
92
|
if (s2.countTokens()>0){
|
93
|
titleCache1 = "";
|
94
|
}
|
95
|
while(s2.hasMoreTokens()){
|
96
|
titleCache1 += s2.nextToken();
|
97
|
}
|
98
|
|
99
|
// 2
|
100
|
s2 = new StringTokenizer(titleCache2, "\"");
|
101
|
if (s2.countTokens()>0){
|
102
|
titleCache2 = "";
|
103
|
}
|
104
|
|
105
|
while(s2.hasMoreTokens()){
|
106
|
titleCache2 += s2.nextToken();
|
107
|
}
|
108
|
|
109
|
int result = titleCache1.compareTo(titleCache2);
|
110
|
if (result != 0){
|
111
|
return result;
|
112
|
}else{
|
113
|
return node1.getUuid().compareTo(node2.getUuid());
|
114
|
}
|
115
|
}
|
116
|
|
117
|
|
118
|
private String createSortableTitleCache(TaxonNode taxonNode) {
|
119
|
|
120
|
String titleCache = null;
|
121
|
if(taxonNode.getTaxon() != null && taxonNode.getTaxon().getName() != null ){
|
122
|
TaxonName name = HibernateProxyHelper.deproxy(taxonNode.getTaxon().getName(), TaxonName.class);
|
123
|
|
124
|
if (name.isNonViral()){
|
125
|
if (logger.isTraceEnabled()){logger.trace(name + " isNonViralName");}
|
126
|
INonViralName nonViralName = name;
|
127
|
if (nonViralName.getGenusOrUninomial() != null){
|
128
|
titleCache = nonViralName.getGenusOrUninomial();
|
129
|
if ((name.isSpecies() || name.isInfraSpecific()) && nonViralName.getSpecificEpithet() != null){
|
130
|
titleCache = titleCache + " " + nonViralName.getSpecificEpithet();
|
131
|
}
|
132
|
if (name.isInfraSpecific() && nonViralName.getSpecificEpithet() != null
|
133
|
&& nonViralName.getInfraSpecificEpithet() != null){
|
134
|
if (logger.isTraceEnabled()){logger.trace(name + " isInfraSpecific");}
|
135
|
titleCache = titleCache + " " + nonViralName.getInfraSpecificEpithet();
|
136
|
if (nonViralName.getSpecificEpithet().equals(nonViralName.getInfraSpecificEpithet())){
|
137
|
titleCache = nonViralName.getNameCache() + " "+nonViralName.getAuthorshipCache();
|
138
|
}
|
139
|
}
|
140
|
if (name.isInfraGeneric() && nonViralName.getInfraGenericEpithet() != null){
|
141
|
if (logger.isTraceEnabled()){logger.trace(name + " isInfraGeneric");}
|
142
|
titleCache = titleCache + " " + nonViralName.getInfraGenericEpithet();
|
143
|
}
|
144
|
if (nonViralName.isSpeciesAggregate() && nonViralName.getSpecificEpithet() != null){
|
145
|
if (logger.isTraceEnabled()){logger.trace(name + " isSpeciesAggregate");}
|
146
|
titleCache = nonViralName.getGenusOrUninomial() + " " + nonViralName.getSpecificEpithet();
|
147
|
}
|
148
|
}
|
149
|
|
150
|
}
|
151
|
if (titleCache == null){
|
152
|
if (logger.isTraceEnabled()){logger.trace("titleCache still null, using name.getTitleCache()");}
|
153
|
titleCache = name.getTitleCache();
|
154
|
}
|
155
|
}
|
156
|
if (titleCache == null){
|
157
|
if (logger.isTraceEnabled()){logger.trace("titleCache still null, using taxonNode id");}
|
158
|
titleCache = String.valueOf(taxonNode.getId());
|
159
|
}
|
160
|
if (logger.isTraceEnabled()){logger.trace("SortableTitleCache: " + titleCache);}
|
161
|
// System.out.println(titleCache);
|
162
|
return titleCache;
|
163
|
}
|
164
|
|
165
|
|
166
|
@Override
|
167
|
public boolean isIgnoreHybridSign() {
|
168
|
return ignoreHybridSign;
|
169
|
}
|
170
|
|
171
|
@Override
|
172
|
public void setIgnoreHybridSign(boolean ignore) {
|
173
|
this.ignoreHybridSign = ignore;
|
174
|
}
|
175
|
|
176
|
public boolean isSortInfraGenericFirst() {
|
177
|
return sortInfraGenericFirst;
|
178
|
}
|
179
|
|
180
|
public void setSortInfraGenericFirst(boolean infraGenericFirst) {
|
181
|
this.sortInfraGenericFirst = infraGenericFirst;
|
182
|
}
|
183
|
|
184
|
}
|