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.compare.taxon;
|
10
|
|
11
|
import java.util.StringTokenizer;
|
12
|
|
13
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.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
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
22
|
import eu.etaxonomy.cdm.model.taxon.TaxonNodeStatus;
|
23
|
|
24
|
/**
|
25
|
* Comparator that compares two TaxonNode instances by the titleCache of their
|
26
|
* referenced names.
|
27
|
*
|
28
|
* @author a.kohlbecker
|
29
|
* @since 24.06.2009
|
30
|
*/
|
31
|
public class TaxonNodeByNameComparator
|
32
|
extends AbstractStringComparator<TaxonNode>
|
33
|
implements ITaxonNodeComparator<TaxonNode> {
|
34
|
|
35
|
private static final String HYBRID_SIGN = UTF8.HYBRID.toString();
|
36
|
|
37
|
private static final Logger logger = LogManager.getLogger(TaxonNodeByNameComparator.class);
|
38
|
|
39
|
private boolean ignoreHybridSign = true;
|
40
|
private boolean sortInfraGenericFirst = true;
|
41
|
|
42
|
@Override
|
43
|
public int compare(TaxonNode node1, TaxonNode node2) {
|
44
|
if (node1 == null && node2 == null) {
|
45
|
return 0;
|
46
|
} else if (node1 == null) {
|
47
|
return 1;
|
48
|
} else if (node2 == null) {
|
49
|
return -1;
|
50
|
}
|
51
|
|
52
|
if (node1.equals(node2)){
|
53
|
return 0;
|
54
|
}
|
55
|
int nodeResult = compareNodes(node1, node2);
|
56
|
if (nodeResult != 0){
|
57
|
return nodeResult;
|
58
|
}
|
59
|
return compareNames(node1, node2);
|
60
|
}
|
61
|
|
62
|
protected int compareNames(TaxonNode node1, TaxonNode node2) {
|
63
|
String titleCache1 = createSortableTitleCache(node1);
|
64
|
String titleCache2 = createSortableTitleCache(node2);
|
65
|
|
66
|
if(isIgnoreHybridSign()) {
|
67
|
if (logger.isTraceEnabled()){logger.trace("ignoring Hybrid Signs: " + HYBRID_SIGN);}
|
68
|
titleCache1 = titleCache1.replace(HYBRID_SIGN, "");
|
69
|
titleCache2 = titleCache2.replace(HYBRID_SIGN, "");
|
70
|
}
|
71
|
|
72
|
titleCache1 = applySubstitutionRules(titleCache1);
|
73
|
titleCache2 = applySubstitutionRules(titleCache2);
|
74
|
|
75
|
// 1
|
76
|
StringTokenizer s2 = new StringTokenizer(titleCache1, "\"");
|
77
|
if (s2.countTokens()>0){
|
78
|
titleCache1 = "";
|
79
|
}
|
80
|
while(s2.hasMoreTokens()){
|
81
|
titleCache1 += s2.nextToken();
|
82
|
}
|
83
|
|
84
|
// 2
|
85
|
s2 = new StringTokenizer(titleCache2, "\"");
|
86
|
if (s2.countTokens()>0){
|
87
|
titleCache2 = "";
|
88
|
}
|
89
|
|
90
|
while(s2.hasMoreTokens()){
|
91
|
titleCache2 += s2.nextToken();
|
92
|
}
|
93
|
|
94
|
int result = titleCache1.compareTo(titleCache2);
|
95
|
if (result != 0){
|
96
|
return result;
|
97
|
}else{
|
98
|
return node1.getUuid().compareTo(node2.getUuid());
|
99
|
}
|
100
|
}
|
101
|
|
102
|
protected int compareNodes(TaxonNode node1, TaxonNode node2) {
|
103
|
|
104
|
TaxonNodeStatus status1 = node1.getStatus();
|
105
|
TaxonNodeStatus status2 = node2.getStatus();
|
106
|
|
107
|
if (CdmUtils.nullSafeEqual(status1, status2)){
|
108
|
return 0;
|
109
|
}else if (status1 == null){
|
110
|
return 1;
|
111
|
}else if (status2 == null){
|
112
|
return -1;
|
113
|
}else {
|
114
|
return status1.compareTo(status2);
|
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
|
|
162
|
return titleCache;
|
163
|
}
|
164
|
|
165
|
@Override
|
166
|
public boolean isIgnoreHybridSign() {
|
167
|
return ignoreHybridSign;
|
168
|
}
|
169
|
|
170
|
@Override
|
171
|
public void setIgnoreHybridSign(boolean ignore) {
|
172
|
this.ignoreHybridSign = ignore;
|
173
|
}
|
174
|
|
175
|
public boolean isSortInfraGenericFirst() {
|
176
|
return sortInfraGenericFirst;
|
177
|
}
|
178
|
|
179
|
public void setSortInfraGenericFirst(boolean infraGenericFirst) {
|
180
|
this.sortInfraGenericFirst = infraGenericFirst;
|
181
|
}
|
182
|
}
|