Project

General

Profile

Download (4.31 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.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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
115

    
116
    private String createSortableTitleCache(TaxonNodeDto taxonNode) {
117

    
118

    
119
        String nameTitleCache= taxonNode.getTitleCache();
120

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

    
130

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

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

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

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

    
149
}
150

    
151

    
(10-10/16)