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