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.taxeditor.editor.descriptiveDataSet.matrix;
|
10
|
|
11
|
import java.util.Comparator;
|
12
|
|
13
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
14
|
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
|
15
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
16
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
17
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
18
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
19
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
20
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByNameComparator;
|
21
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByRankAndNameComparator;
|
22
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoNaturalComparator;
|
23
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
24
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
25
|
|
26
|
/**
|
27
|
* @author pplitzner
|
28
|
* @since Jan 25, 2018
|
29
|
*
|
30
|
*/
|
31
|
public class MatrixRowComparator implements Comparator<Object>{
|
32
|
|
33
|
private Comparator<TaxonNodeDto> comparator;
|
34
|
private ITaxonNodeService taxonNodeService;
|
35
|
|
36
|
public MatrixRowComparator() {
|
37
|
taxonNodeService = CdmStore.getService(ITaxonNodeService.class);
|
38
|
if (PreferencesUtil.isNodesSortedNaturally()){
|
39
|
comparator = new TaxonNodeDtoNaturalComparator();
|
40
|
} else if (PreferencesUtil.isNodesSortedByName()){
|
41
|
comparator = new TaxonNodeDtoByNameComparator();
|
42
|
}else {
|
43
|
comparator = new TaxonNodeDtoByRankAndNameComparator();
|
44
|
}
|
45
|
}
|
46
|
|
47
|
@Override
|
48
|
public int compare(Object o1, Object o2) {
|
49
|
if(o1 instanceof TaxonNodeDto){
|
50
|
if(o2 instanceof RowWrapperDTO){
|
51
|
return 1;
|
52
|
}
|
53
|
}
|
54
|
else{
|
55
|
if(o2 instanceof TaxonNodeDto){
|
56
|
return -1;
|
57
|
}
|
58
|
}
|
59
|
if(o1 instanceof TaxonNodeDto && o2 instanceof TaxonNodeDto){
|
60
|
return comparator.compare((TaxonNodeDto)o1, (TaxonNodeDto)o2);
|
61
|
}
|
62
|
if(o1 instanceof RowWrapperDTO && o2 instanceof RowWrapperDTO){
|
63
|
RowWrapperDTO rowWrapper1 = (RowWrapperDTO)o1;
|
64
|
RowWrapperDTO rowWrapper2 = (RowWrapperDTO)o2;
|
65
|
//check for equality
|
66
|
if(rowWrapper1.equals(rowWrapper2)){
|
67
|
return 0;
|
68
|
}
|
69
|
TaxonNodeDto node1 = rowWrapper1.getTaxonNode();
|
70
|
TaxonNodeDto node2 = rowWrapper2.getTaxonNode();
|
71
|
if(node1!=null && node2!=null){
|
72
|
//compare by taxon node
|
73
|
int compare = comparator.compare(node1, node2);
|
74
|
if(compare!=0){
|
75
|
return compare;
|
76
|
}
|
77
|
DescriptionBase description1 = rowWrapper1.getDescription();
|
78
|
DescriptionBase description2 = rowWrapper2.getDescription();
|
79
|
//compare by taxon description type
|
80
|
if(description1 instanceof TaxonDescription){
|
81
|
if(description2 instanceof SpecimenDescription){
|
82
|
return -1;
|
83
|
}
|
84
|
}
|
85
|
else{
|
86
|
if(description2 instanceof TaxonDescription){
|
87
|
return 1;
|
88
|
}
|
89
|
}
|
90
|
//both descriptions are of the same type
|
91
|
if(description1 instanceof SpecimenDescription){
|
92
|
//description2 has to also be a SpecimenDescription
|
93
|
SpecimenDescription specimenDescription1 = (SpecimenDescription)description1;
|
94
|
SpecimenDescription specimenDescription2 = (SpecimenDescription)description2;
|
95
|
int id1 = specimenDescription1.getDescribedSpecimenOrObservation().getId();
|
96
|
int id2 = specimenDescription2.getDescribedSpecimenOrObservation().getId();
|
97
|
return id1-id2;
|
98
|
}
|
99
|
else if(description1 instanceof TaxonDescription){
|
100
|
//description2 has to also be a TaxonDescription
|
101
|
TaxonDescription taxonDescription1 = (TaxonDescription)description1;
|
102
|
TaxonDescription taxonDescription2 = (TaxonDescription)description2;
|
103
|
boolean isComputed1 = taxonDescription1.getMarkers().stream()
|
104
|
.anyMatch(marker -> marker.getMarkerType().equals(MarkerType.COMPUTED()));
|
105
|
boolean isComputed2 = taxonDescription2.getMarkers().stream()
|
106
|
.anyMatch(marker -> marker.getMarkerType().equals(MarkerType.COMPUTED()));
|
107
|
if(isComputed1){
|
108
|
if(!isComputed2){
|
109
|
return -1;
|
110
|
}
|
111
|
}
|
112
|
if(isComputed2){
|
113
|
return 1;
|
114
|
}
|
115
|
//TODO: implement compare for different description types
|
116
|
}
|
117
|
}
|
118
|
}
|
119
|
return o1.hashCode()-o2.hashCode();
|
120
|
}
|
121
|
|
122
|
}
|