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