Project

General

Profile

Download (4.42 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.persistence.dto;
10

    
11
import java.io.Serializable;
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.common.UTF8;
19
import eu.etaxonomy.cdm.model.taxon.ITaxonNodeComparator;
20
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByNameComparator;
21

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

    
29
    private static final long serialVersionUID = -5939529760454590279L;
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(TaxonNodeDto node1, TaxonNodeDto node2) {
40
        if (node1 == null && node2 == null) {
41
            return 0;
42
        }
43
        else if (node1 == null) {
44
            return 1;
45
        }
46
        else if (node2 == null) {
47
            return -1;
48
        }
49
        if (node1.equals(node2)){
50
            return 0;
51
        }
52
        boolean node1Excluded = node1.isExcluded();
53
        boolean node2Excluded = node2.isExcluded();
54
        boolean node1Unplaced = node1.isUnplaced();
55
        boolean node2Unplaced = node2.isUnplaced();
56

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

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

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

    
79
        String titleCache1 = createSortableTitleCache(node1);
80
        String titleCache2 = createSortableTitleCache(node2);
81

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

    
88
        titleCache1 = applySubstitutionRules(titleCache1);
89
        titleCache2 = applySubstitutionRules(titleCache2);
90

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

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

    
106
        while(s2.hasMoreTokens()){
107
            titleCache2 += s2.nextToken();
108
        }
109

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

    
118

    
119
    private String createSortableTitleCache(TaxonNodeDto taxonNode) {
120

    
121

    
122
        String nameTitleCache= taxonNode.getTitleCache();
123

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

    
133

    
134
    @Override
135
    public boolean isIgnoreHybridSign() {
136
        return ignoreHybridSign;
137
    }
138

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

    
144
    public boolean isSortInfraGenericFirst() {
145
        return sortInfraGenericFirst;
146
    }
147

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

    
152
}
153

    
154

    
(15-15/24)