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.TaxonNameBase;
|
21
|
|
22
|
/**
|
23
|
* Comparator that compares two TaxonNode instances by the titleCache of their referenced names.
|
24
|
* @author a.kohlbecker
|
25
|
* @date 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 o1, TaxonNode o2) {
|
39
|
|
40
|
if (o1 == null && o2 == null) {
|
41
|
return 0;
|
42
|
}
|
43
|
else if (o1 == null) {
|
44
|
return 1;
|
45
|
}
|
46
|
else if (o2 == null) {
|
47
|
return -1;
|
48
|
}
|
49
|
if (o1.equals(o2)){
|
50
|
return 0;
|
51
|
}
|
52
|
|
53
|
String titleCache1 = createSortableTitleCache(o1);
|
54
|
String titleCache2 = createSortableTitleCache(o2);
|
55
|
|
56
|
if(isIgnoreHybridSign()) {
|
57
|
if (logger.isTraceEnabled()){logger.trace("ignoring Hybrid Signs: " + HYBRID_SIGN);}
|
58
|
titleCache1 = titleCache1.replace(HYBRID_SIGN, "");
|
59
|
titleCache2 = titleCache2.replace(HYBRID_SIGN, "");
|
60
|
}
|
61
|
|
62
|
titleCache1 = applySubstitutionRules(titleCache1);
|
63
|
titleCache2 = applySubstitutionRules(titleCache2);
|
64
|
|
65
|
// 1
|
66
|
StringTokenizer s2 = new StringTokenizer(titleCache1, "\"");
|
67
|
if (s2.countTokens()>0){
|
68
|
titleCache1 = "";
|
69
|
}
|
70
|
while(s2.hasMoreTokens()){
|
71
|
titleCache1 += s2.nextToken();
|
72
|
}
|
73
|
|
74
|
// 2
|
75
|
s2 = new StringTokenizer(titleCache2, "\"");
|
76
|
if (s2.countTokens()>0){
|
77
|
titleCache2 = "";
|
78
|
}
|
79
|
|
80
|
while(s2.hasMoreTokens()){
|
81
|
titleCache2 += s2.nextToken();
|
82
|
}
|
83
|
|
84
|
int result = titleCache1.compareTo(titleCache2);
|
85
|
if (result != 0){
|
86
|
return result;
|
87
|
}else{
|
88
|
return o1.getUuid().compareTo(o2.getUuid());
|
89
|
}
|
90
|
}
|
91
|
|
92
|
|
93
|
private String createSortableTitleCache(TaxonNode taxonNode) {
|
94
|
|
95
|
String titleCache = null;
|
96
|
if(taxonNode.getTaxon() != null && taxonNode.getTaxon().getName() != null ){
|
97
|
TaxonNameBase<?,?> name = HibernateProxyHelper.deproxy(taxonNode.getTaxon().getName(), TaxonNameBase.class);
|
98
|
|
99
|
if (name.isNonViral()){
|
100
|
if (logger.isTraceEnabled()){logger.trace(name + " isNonViralName");}
|
101
|
INonViralName nonViralName = name;
|
102
|
if (nonViralName.getGenusOrUninomial() != null){
|
103
|
titleCache = nonViralName.getGenusOrUninomial();
|
104
|
if (name.isSpecies() && nonViralName.getSpecificEpithet() != null){
|
105
|
titleCache = titleCache + " " + nonViralName.getSpecificEpithet();
|
106
|
}
|
107
|
if (name.isInfraSpecific() && nonViralName.getSpecificEpithet() != null
|
108
|
&& nonViralName.getInfraSpecificEpithet() != null){
|
109
|
if (logger.isTraceEnabled()){logger.trace(name + " isInfraSpecific");}
|
110
|
titleCache = titleCache + " " + nonViralName.getInfraSpecificEpithet();
|
111
|
if (nonViralName.getSpecificEpithet().equals(nonViralName.getInfraSpecificEpithet())){
|
112
|
titleCache = nonViralName.getNameCache() + " "+nonViralName.getAuthorshipCache();
|
113
|
}
|
114
|
}
|
115
|
if (name.isInfraGeneric() && nonViralName.getInfraGenericEpithet() != null){
|
116
|
if (logger.isTraceEnabled()){logger.trace(name + " isInfraGeneric");}
|
117
|
titleCache = titleCache + " " + nonViralName.getInfraGenericEpithet();
|
118
|
}
|
119
|
if (nonViralName.isSpeciesAggregate() && nonViralName.getSpecificEpithet() != null){
|
120
|
if (logger.isTraceEnabled()){logger.trace(name + " isSpeciesAggregate");}
|
121
|
titleCache = nonViralName.getGenusOrUninomial() + " " + nonViralName.getSpecificEpithet();
|
122
|
}
|
123
|
}
|
124
|
|
125
|
}
|
126
|
if (titleCache == null){
|
127
|
if (logger.isTraceEnabled()){logger.trace("titleCache still null, using name.getTitleCache()");}
|
128
|
titleCache = name.getTitleCache();
|
129
|
}
|
130
|
}
|
131
|
if (titleCache == null){
|
132
|
if (logger.isTraceEnabled()){logger.trace("titleCache still null, using taxonNode id");}
|
133
|
titleCache = String.valueOf(taxonNode.getId());
|
134
|
}
|
135
|
if (logger.isTraceEnabled()){logger.trace("SortableTitleCache: " + titleCache);}
|
136
|
// System.out.println(titleCache);
|
137
|
return titleCache;
|
138
|
}
|
139
|
|
140
|
|
141
|
@Override
|
142
|
public boolean isIgnoreHybridSign() {
|
143
|
return ignoreHybridSign;
|
144
|
}
|
145
|
|
146
|
@Override
|
147
|
public void setIgnoreHybridSign(boolean ignore) {
|
148
|
this.ignoreHybridSign = ignore;
|
149
|
}
|
150
|
|
151
|
public boolean isSortInfraGenericFirst() {
|
152
|
return sortInfraGenericFirst;
|
153
|
}
|
154
|
|
155
|
public void setSortInfraGenericFirst(boolean infraGenericFirst) {
|
156
|
this.sortInfraGenericFirst = infraGenericFirst;
|
157
|
}
|
158
|
|
159
|
}
|