Project

General

Profile

Download (4.36 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.api.service;
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.model.taxon.ITaxonNodeComparator;
19
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByNameComparator;
20
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
21

    
22
/**
23
 * @author k.luther/a.kohlbecker
24
 * @since 09.03.2018
25
 *
26
 */
27
public class TaxonNodeDtoByNameComparator extends AbstractStringComparator<TaxonNodeDto> implements Comparator<TaxonNodeDto>, ITaxonNodeComparator<TaxonNodeDto>{
28

    
29
    private static final String HYBRID_SIGN = UTF8.HYBRID.toString();
30

    
31
    private static final Logger logger = Logger.getLogger(TaxonNodeByNameComparator.class);
32

    
33
    private boolean ignoreHybridSign = true;
34
    private boolean sortInfraGenericFirst = true;
35

    
36
    @Override
37
    public int compare(TaxonNodeDto node1, TaxonNodeDto node2) {
38
        if (node1 == null && node2 == null) {
39
            return 0;
40
        }
41
        else if (node1 == null) {
42
            return 1;
43
        }
44
        else if (node2 == null) {
45
            return -1;
46
        }
47
        if (node1.equals(node2)){
48
            return 0;
49
        }
50
        boolean node1Excluded = node1.isExcluded();
51
        boolean node2Excluded = node2.isExcluded();
52
        boolean node1Unplaced = node1.isUnplaced();
53
        boolean node2Unplaced = node2.isUnplaced();
54

    
55
      //They should both be put to the end (first unplaced then excluded)
56
        if (node2Excluded && !node1Excluded){
57
            return -1;
58
        }
59
        if (node2Unplaced && !(node1Unplaced || node1Excluded)){
60
            return -1;
61
        }
62

    
63
        if (node1Excluded && !node2Excluded){
64
            return 1;
65
        }
66
        if (node1Unplaced && !(node2Unplaced || node2Excluded)){
67
            return 1;
68
        }
69

    
70
        if (node1Unplaced && node2Excluded){
71
            return -1;
72
        }
73
        if (node2Unplaced && node1Excluded){
74
            return 1;
75
        }
76

    
77
        String titleCache1 = createSortableTitleCache(node1);
78
        String titleCache2 = createSortableTitleCache(node2);
79

    
80
        if(isIgnoreHybridSign()) {
81
            if (logger.isTraceEnabled()){logger.trace("ignoring Hybrid Signs: " + HYBRID_SIGN);}
82
            titleCache1 = titleCache1.replace(HYBRID_SIGN, "");
83
            titleCache2 = titleCache2.replace(HYBRID_SIGN, "");
84
        }
85

    
86
        titleCache1 = applySubstitutionRules(titleCache1);
87
        titleCache2 = applySubstitutionRules(titleCache2);
88

    
89
        // 1
90
        StringTokenizer s2 = new StringTokenizer(titleCache1, "\"");
91
        if (s2.countTokens()>0){
92
            titleCache1 = "";
93
        }
94
        while(s2.hasMoreTokens()){
95
            titleCache1 += s2.nextToken();
96
        }
97

    
98
        // 2
99
        s2 = new StringTokenizer(titleCache2, "\"");
100
        if (s2.countTokens()>0){
101
            titleCache2 = "";
102
        }
103

    
104
        while(s2.hasMoreTokens()){
105
            titleCache2 += s2.nextToken();
106
        }
107

    
108
        int result = titleCache1.compareTo(titleCache2);
109
        if (result != 0){
110
            return result;
111
        }else{
112
            return node1.getUuid().compareTo(node2.getUuid());
113
        }
114
    }
115

    
116

    
117
    private String createSortableTitleCache(TaxonNodeDto taxonNode) {
118

    
119

    
120
        String nameTitleCache= taxonNode.getTitleCache();
121

    
122
        if (nameTitleCache == null){
123
            if (logger.isTraceEnabled()){logger.trace("titleCache still null, using taxonNode id");}
124
            nameTitleCache = String.valueOf(taxonNode.getId());
125
        }
126
        if (logger.isTraceEnabled()){logger.trace("SortableTitleCache: " + nameTitleCache);}
127
//            System.out.println(titleCache);
128
        return nameTitleCache;
129
    }
130

    
131

    
132
    @Override
133
    public boolean isIgnoreHybridSign() {
134
        return ignoreHybridSign;
135
    }
136

    
137
    @Override
138
    public void setIgnoreHybridSign(boolean ignore) {
139
        this.ignoreHybridSign = ignore;
140
    }
141

    
142
    public boolean isSortInfraGenericFirst() {
143
        return sortInfraGenericFirst;
144
    }
145

    
146
    public void setSortInfraGenericFirst(boolean infraGenericFirst) {
147
        this.sortInfraGenericFirst = infraGenericFirst;
148
    }
149

    
150
}
151

    
152

    
(95-95/105)