Project

General

Profile

« Previous | Next » 

Revision 1312cd94

Added by Katja Luther over 5 years ago

Dto classes for distribution editor

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dto/TaxonNodeDtoByNameComparator.java
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

  
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dto/TaxonNodeDtoByRankAndNameComparator.java
1
/**
2
* Copyright (C) 2007 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.UUID;
14

  
15
import eu.etaxonomy.cdm.model.common.OrderIndexComparator;
16
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
17
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
18

  
19
/**
20
 * @author k.luther
21
 * @since 18.03.2010
22
 *
23
 */
24
public class TaxonNodeDtoByRankAndNameComparator implements Serializable, Comparator<TaxonNodeDto> {
25
	private static final long serialVersionUID = 2596641007876609704L;
26

  
27
	@Override
28
    public int compare(TaxonNodeDto node1, TaxonNodeDto node2) {
29

  
30
	    boolean node1Excluded = node1.isExcluded();
31
	    boolean node2Excluded = node2.isExcluded();
32
	    boolean node1Unplaced = node1.isUnplaced();
33
	    boolean node2Unplaced = node2.isUnplaced();
34

  
35

  
36
		if (node1.getUuid().equals(node2.getUuid())){
37
			return 0;
38
		}
39
		//They should both be put to the end (first unplaced then excluded)
40
		if (node2Excluded && !node1Excluded){
41
		    return -1;
42
		}
43
		if (node2Unplaced && !(node1Unplaced || node1Excluded)){
44
		    return -1;
45
		}
46

  
47
		if (node1Excluded && !node2Excluded){
48
            return 1;
49
        }
50
        if (node1Unplaced && !(node2Unplaced || node2Excluded)){
51
            return 1;
52
        }
53

  
54
        if (node1Unplaced && node2Excluded){
55
            return -1;
56
        }
57
        if (node2Unplaced && node1Excluded){
58
            return 1;
59
        }
60

  
61

  
62

  
63

  
64
		Integer rankTax1 = node1.getRankOrderIndex();
65
		Integer rankTax2 = node2.getRankOrderIndex();
66

  
67
		//first compare ranks, if ranks are equal (or both null) compare names or taxon title cache if names are null
68
		int rankOrder = OrderIndexComparator.instance().compare(rankTax1, rankTax2);
69

  
70
		if (rankOrder == 0) {
71
			if (node1.getTitleCache() != null && node2.getTitleCache() != null){
72
				//same rank, order by name
73
				int result = node1.getNameTitleCache().compareTo(node2.getNameTitleCache());
74
				if (result == 0){
75
					return node1.getTaxonUuid().compareTo(node2.getTaxonUuid());
76
				}else{
77
					return result;
78
				}
79
			}else {
80
				//this is maybe not 100% correct, we need to compare name cases, but it is a very rare case
81
				return node1.getTaxonTitleCache().compareTo(node2.getTaxonTitleCache());
82
			}
83
		}else{
84
			//rankTax2.isHigher(rankTax1)
85
			return rankOrder;
86
		}
87
	}
88

  
89
    /**
90
     * @param taxon1
91
     * @return
92
     */
93
    public String getTaxonTitle(TaxonBase<?> taxon, TaxonNode node) {
94
        return (taxon == null) ? node.getUuid().toString(): taxon.getTitleCache();
95
    }
96

  
97
    /**
98
     * @param taxon
99
     * @param node
100
     * @return
101
     */
102
    private UUID getTaxonUuid(TaxonBase<?> taxon, TaxonNode node) {
103
        return (taxon == null) ? node.getUuid(): taxon.getUuid();
104
    }
105

  
106

  
107

  
108
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dto/TaxonNodeDtoNaturalComparator.java
1
/**
2
* Copyright (C) 2007 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

  
13
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
14

  
15
/**
16
 * Comparator to compare {@link TaxonNode taxon nodes} by its user defined ordering
17
 *
18
 * @author k.luther
19
 */
20
public class TaxonNodeDtoNaturalComparator implements Comparator<TaxonNodeDto> {
21

  
22
    public TaxonNodeDtoNaturalComparator(){
23
		super();
24

  
25
	}
26

  
27
	@SuppressWarnings("null")
28
    @Override
29
	public int compare(TaxonNodeDto node1, TaxonNodeDto node2) {
30
	   // System.out.println("compare node 1: "+ node1.getTaxon().getTitleCache() + " - node 2: " + node2.getTaxon().getTitleCache());
31
	    if (node1.equals(node2)) {
32
	        return 0;
33
        }
34
	    // if we do not check for null for the treeIndex we always return 1 if one of the nodes have no treeIndex
35
	    if (node1.getTreeIndex() == null){
36
	        return 1;
37
	    }
38
	    if (node2.getTreeIndex() == null){
39
            return -1;
40
        }
41

  
42
	    if (node1.getParentUUID().equals(node2.getParentUUID())){
43
            return node1.getSortIndex().compareTo(node2.getSortIndex());
44
        }
45

  
46

  
47
	    if (node2.getTreeIndex().startsWith(node1.getTreeIndex())) {
48
            return -1;
49
        }
50
		if (node1.getTreeIndex().startsWith(node2.getTreeIndex())) {
51
            return 1;
52
        }
53

  
54

  
55
//		String[] splitNode1 = node1.getTreeIndex().split("#");
56
//		String[] splitNode2 = node2.getTreeIndex().split("#");
57

  
58

  
59

  
60
//		String lastEqualAncestorTreeIndex = "";
61
//		List<TaxonNodeDto> ancestorAndNode= new ArrayList<>();
62
//		ancestorAndNode.add(node1);
63
//		ancestorAndNode.addAll(node1.getAncestors());
64
//		java.util.Collections.sort(ancestorAndNode, new TreeIndexComparator());
65
//
66
//
67
//		List<TaxonNode> ancestorAndNode2= new ArrayList<>();
68
//        ancestorAndNode2.add(node2);
69
//        ancestorAndNode2.addAll(node2.getAncestors());
70
//        java.util.Collections.sort(ancestorAndNode2, new TreeIndexComparator());
71
//
72
//		for (int i = 0; i < splitNode1.length; i++){
73
//			if (!splitNode1[i].equals(splitNode2[i])){
74
//				// take the last equal ancestor and compare the sortindex
75
//				if (lastEqualAncestorTreeIndex != null){
76
//					TaxonNode lastEqualTreeIndexAncestorNode1 = null;
77
//					TaxonNode lastEqualTreeIndexAncestorNode2 = null;
78
//					for (TaxonNode next1 :ancestorAndNode){
79
//
80
//						if (next1.treeIndex().equals(lastEqualAncestorTreeIndex+"#"+splitNode1[i]+ "#") ){
81
//						    lastEqualTreeIndexAncestorNode1 = next1;
82
//						}
83
//					}
84
//					for (TaxonNode next2 :ancestorAndNode2){
85
//
86
//						if (next2.treeIndex().equals(lastEqualAncestorTreeIndex+"#"+splitNode2[i]+ "#")){
87
//						    lastEqualTreeIndexAncestorNode2 = next2;
88
//						}
89
//					}
90
//					return lastEqualTreeIndexAncestorNode1.getSortIndex().compareTo(lastEqualTreeIndexAncestorNode2.getSortIndex());
91
//				}
92
//			}
93
//			if (!splitNode1[i].equals("")){
94
//			    lastEqualAncestorTreeIndex = lastEqualAncestorTreeIndex+"#"+splitNode1[i];
95
//			}
96
//		}
97
		return 0;
98
	}
99

  
100
	private final class TreeIndexComparator implements Comparator<TaxonNodeDto> {
101
	    @Override
102
	    public int compare(TaxonNodeDto node1,TaxonNodeDto node2){
103
	        return node1.getTreeIndex().compareTo(node2.getTreeIndex());
104
	    }
105
	}
106
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/dto/TaxonDescriptionDTO.java
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.dto;
10

  
11
import java.io.Serializable;
12
import java.util.HashSet;
13
import java.util.Set;
14
import java.util.UUID;
15

  
16
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
17
import eu.etaxonomy.cdm.model.description.Distribution;
18
import eu.etaxonomy.cdm.model.description.TaxonDescription;
19
import eu.etaxonomy.cdm.model.taxon.Taxon;
20

  
21
/**
22
 * @author k.luther
23
 * @since 30.11.2018
24
 *
25
 */
26
public class TaxonDescriptionDTO implements Serializable{
27

  
28
    private static final long serialVersionUID = -4440059497898077690L;
29
    UUID taxonUuid;
30
    Set<TaxonDescription> descriptions = new HashSet();
31

  
32
    public TaxonDescriptionDTO(Taxon taxon){
33
        this.taxonUuid = taxon.getUuid();
34
        for (TaxonDescription desc: taxon.getDescriptions()){
35
            for (DescriptionElementBase element: desc.getElements()){
36
                if (element instanceof Distribution){
37
                    descriptions.add(desc);
38
                    break;
39
                }
40
            }
41
        }
42
    }
43

  
44
    /**
45
     * @return the taxonUuid
46
     */
47
    public UUID getTaxonUuid() {
48
        return taxonUuid;
49
    }
50

  
51
    /**
52
     * @param taxonUuid the taxonUuid to set
53
     */
54
    public void setTaxonUuid(UUID taxonUuid) {
55
        this.taxonUuid = taxonUuid;
56
    }
57

  
58
    /**
59
     * @return the descriptions
60
     */
61
    public Set<TaxonDescription> getDescriptions() {
62
        return descriptions;
63
    }
64

  
65
    /**
66
     * @param descriptions the descriptions to set
67
     */
68
    public void setDescriptions(Set<TaxonDescription> descriptions) {
69
        this.descriptions = descriptions;
70
    }
71

  
72
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/dto/TaxonDistributionDTO.java
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.dto;
10

  
11
import java.io.Serializable;
12
import java.util.HashMap;
13
import java.util.HashSet;
14
import java.util.Map;
15
import java.util.Set;
16
import java.util.UUID;
17

  
18
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
19
import eu.etaxonomy.cdm.model.description.Distribution;
20
import eu.etaxonomy.cdm.model.description.TaxonDescription;
21
import eu.etaxonomy.cdm.model.location.NamedArea;
22
import eu.etaxonomy.cdm.model.name.Rank;
23
import eu.etaxonomy.cdm.model.taxon.Taxon;
24

  
25
/**
26
 * @author k.luther
27
 * @since 27.11.2018
28
 *
29
 */
30
public class TaxonDistributionDTO implements Serializable{
31

  
32
    private static final long serialVersionUID = -6565463192135410612L;
33
    UUID taxonUuid;
34
    String nameCache;
35
    Rank rank;
36
    TaxonDescriptionDTO descriptionsWrapper;
37
    Map<NamedArea,Set<DescriptionElementBase>> distributionMap = new HashMap();
38

  
39

  
40

  
41
    public TaxonDistributionDTO(Taxon taxon){
42
        this.taxonUuid = taxon.getUuid();
43
        this.nameCache = taxon.getName().getNameCache();
44
        this.descriptionsWrapper = new TaxonDescriptionDTO(taxon);
45
        for (TaxonDescription desc: descriptionsWrapper.descriptions){
46
            for (DescriptionElementBase descElement: desc.getElements()){
47
                if (descElement instanceof Distribution){
48
                    Set<DescriptionElementBase> distributions = distributionMap.get(((Distribution)descElement).getArea());
49
                    if (distributions == null){
50
                        distributions = new HashSet();
51
                        distributionMap.put(((Distribution)descElement).getArea(), distributions);
52
                    }
53
                    distributions.add(descElement);
54

  
55
                }
56
            }
57
        }
58

  
59
    }
60

  
61
   /* ------ Getter / Setter -----------*/
62

  
63
    /**
64
     * @return the taxonUuid
65
     */
66
    public UUID getTaxonUuid() {
67
        return taxonUuid;
68
    }
69

  
70
    /**
71
     * @param taxonUuid the taxonUuid to set
72
     */
73
    public void setTaxonUuid(UUID taxonUuid) {
74
        this.taxonUuid = taxonUuid;
75
    }
76

  
77
    /**
78
     * @return the nameCache
79
     */
80
    public String getNameCache() {
81
        return nameCache;
82
    }
83

  
84
    /**
85
     * @param nameCache the nameCache to set
86
     */
87
    public void setNameCache(String nameCache) {
88
        this.nameCache = nameCache;
89
    }
90

  
91
    /**
92
     * @return the rankString
93
     */
94
    public String getRankString() {
95
        return rank.getLabel();
96
    }
97

  
98
    /**
99
     * @return the rank
100
     */
101
    public Rank getRank() {
102
        return rank;
103
    }
104

  
105
    /**
106
     * @param rank the rank to set
107
     */
108
    public void setRank(Rank rank) {
109
        this.rank = rank;
110
    }
111

  
112

  
113
    /**
114
     * @return the descriptionsWrapper
115
     */
116
    public TaxonDescriptionDTO getDescriptionsWrapper() {
117
        return descriptionsWrapper;
118
    }
119

  
120
    /**
121
     * @param descriptionsWrapper the descriptionsWrapper to set
122
     */
123
    public void setDescriptionsWrapper(TaxonDescriptionDTO descriptionsWrapper) {
124
        this.descriptionsWrapper = descriptionsWrapper;
125
    }
126

  
127
    /**
128
     * @return the distributionMap
129
     */
130
    public Map<NamedArea, Set<DescriptionElementBase>> getDistributionMap() {
131
        return distributionMap;
132
    }
133

  
134
    /**
135
     * @param distributionMap the distributionMap to set
136
     */
137
    public void setDistributionMap(Map<NamedArea, Set<DescriptionElementBase>> distributionMap) {
138
        this.distributionMap = distributionMap;
139
    }
140

  
141
}

Also available in: Unified diff