Project

General

Profile

Download (5.92 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.model.taxon;
11

    
12
import java.util.Comparator;
13
import java.util.StringTokenizer;
14

    
15
import org.apache.log4j.Logger;
16

    
17
import eu.etaxonomy.cdm.common.AbstractStringComparator;
18
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
19
import eu.etaxonomy.cdm.model.name.NonViralName;
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
//@Component
29
public class TaxonNodeByNameComparator extends AbstractStringComparator<TaxonNode> implements Comparator<TaxonNode>, ITaxonNodeComparator<TaxonNode> {
30

    
31
    private static final String HYBRID_SIGN = "\u00D7";
32

    
33
    private static final Logger logger = Logger.getLogger(TaxonNodeByNameComparator.class);
34

    
35
    private boolean ignoreHybridSign = true;
36
    private boolean sortInfraGenericFirst = true;
37

    
38
    /* (non-Javadoc)
39
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
40
     */
41
    @Override
42
    public int compare(TaxonNode o1, TaxonNode o2) {
43

    
44
        if (o1 == null && o2 == null) {
45
            return 0;
46
        }
47
        else if (o1 == null) {
48
            return 1;
49
        }
50
        else if (o2 == null) {
51
            return -1;
52
        }
53
        if (o1.equals(o2)){
54
        	return 0;
55
        }
56

    
57
        String titleCache1 = createSortableTitleCache(o1);
58
        String titleCache2 = createSortableTitleCache(o2);
59

    
60
        if(isIgnoreHybridSign()) {
61
            if (logger.isTraceEnabled()){logger.trace("ignoring Hybrid Signs: " + HYBRID_SIGN);}
62
            titleCache1 = titleCache1.replace(HYBRID_SIGN, "");
63
            titleCache2 = titleCache2.replace(HYBRID_SIGN, "");
64
        }
65

    
66
        titleCache1 = applySubstitutionRules(titleCache1);
67
        titleCache2 = applySubstitutionRules(titleCache2);
68

    
69
        // 1
70
        StringTokenizer s2 = new StringTokenizer(titleCache1, "\"");
71
        if (s2.countTokens()>0){
72
            titleCache1 = "";
73
        }
74
        while(s2.hasMoreTokens()){
75
            titleCache1 += s2.nextToken();
76
        }
77

    
78
        // 2
79
        s2 = new StringTokenizer(titleCache2, "\"");
80
        if (s2.countTokens()>0){
81
            titleCache2 = "";
82
        }
83

    
84
        while(s2.hasMoreTokens()){
85
            titleCache2 += s2.nextToken();
86
        }
87

    
88
        int result = titleCache1.compareTo(titleCache2);
89
        if (result != 0){
90
        	return result;
91
        }else{
92
        	return o1.getUuid().compareTo(o2.getUuid());
93
        }
94
    }
95

    
96

    
97
    private String createSortableTitleCache(TaxonNode taxonNode) {
98

    
99
        String titleCache = null;
100
        if(taxonNode.getTaxon() != null && taxonNode.getTaxon().getName() != null ){
101
            TaxonNameBase<?,?> name = HibernateProxyHelper.deproxy(taxonNode.getTaxon().getName(), TaxonNameBase.class);
102

    
103
            if (name instanceof NonViralName){
104
                if (logger.isTraceEnabled()){logger.trace(name + " isNonViralName");}
105
                NonViralName<?> nonViralName = (NonViralName<?>)name;
106
                if (nonViralName.getGenusOrUninomial() != null){
107
                    titleCache = nonViralName.getGenusOrUninomial();
108
                    if (name.isSpecies() && nonViralName.getSpecificEpithet() != null){
109
                        titleCache = titleCache + " " + nonViralName.getSpecificEpithet();
110
                    }
111
                	if (name.isInfraSpecific() && nonViralName.getSpecificEpithet() != null
112
                			&& nonViralName.getInfraSpecificEpithet() != null){
113
                		if (logger.isTraceEnabled()){logger.trace(name + " isInfraSpecific");}
114
                		titleCache = titleCache + " " + nonViralName.getInfraSpecificEpithet();
115
                		if (nonViralName.getSpecificEpithet().equals(nonViralName.getInfraSpecificEpithet())){
116
                			titleCache = nonViralName.getNameCache() + " "+nonViralName.getAuthorshipCache();
117
                		}
118
                	}
119
                	if (name.isInfraGeneric() && nonViralName.getInfraGenericEpithet() != null){
120
                		if (logger.isTraceEnabled()){logger.trace(name + " isInfraGeneric");}
121
                		titleCache = titleCache + " " + nonViralName.getInfraGenericEpithet();
122
                	}
123
                	if (nonViralName.isSpeciesAggregate() && nonViralName.getSpecificEpithet() != null){
124
                		if (logger.isTraceEnabled()){logger.trace(name + " isSpeciesAggregate");}
125
                		titleCache = nonViralName.getGenusOrUninomial() + " " + nonViralName.getSpecificEpithet();
126
                	}
127
                }
128

    
129
            }
130
            if (titleCache == null){
131
                if (logger.isTraceEnabled()){logger.trace("titleCache still null, using name.getTitleCache()");}
132
                titleCache = name.getTitleCache();
133
            }
134
        }
135
        if (titleCache == null){
136
            if (logger.isTraceEnabled()){logger.trace("titleCache still null, using taxonNode id");}
137
            titleCache = String.valueOf(taxonNode.getId());
138
        }
139
        if (logger.isTraceEnabled()){logger.trace("SortableTitleCache: " + titleCache);}
140
//        System.out.println(titleCache);
141
        return titleCache;
142
    }
143

    
144

    
145
    @Override
146
    public boolean isIgnoreHybridSign() {
147
        return ignoreHybridSign;
148
    }
149

    
150
    @Override
151
    public void setIgnoreHybridSign(boolean ignore) {
152
        this.ignoreHybridSign = ignore;
153
    }
154

    
155
    public boolean isSortInfraGenericFirst() {
156
        return sortInfraGenericFirst;
157
    }
158

    
159
    public void setSortInfraGenericFirst(boolean infraGenericFirst) {
160
        this.sortInfraGenericFirst = infraGenericFirst;
161
    }
162

    
163
}
(15-15/20)