Merge branch 'release/5.11.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiveDataSet / matrix / MatrixRowComparator.java
index 0a5ebcaed3e29b1acecd16399c0512468fdc7cfb..46e589e9e7f9aa52857b67c71cc355d0f707bc83 100644 (file)
@@ -10,7 +10,6 @@ package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
 
 import java.util.Comparator;
 
-import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
 import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
 import eu.etaxonomy.cdm.model.description.DescriptionType;
@@ -21,7 +20,6 @@ import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByNameComparator;
 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByRankAndNameComparator;
 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoNaturalComparator;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
-import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
  * @author pplitzner
@@ -31,10 +29,8 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
 public class MatrixRowComparator implements Comparator<Object>{
 
     private Comparator<TaxonNodeDto> comparator;
-    private ITaxonNodeService taxonNodeService;
 
     public MatrixRowComparator() {
-        taxonNodeService = CdmStore.getService(ITaxonNodeService.class);
         if (PreferencesUtil.isNodesSortedNaturally()){
             comparator = new TaxonNodeDtoNaturalComparator();
         } else if (PreferencesUtil.isNodesSortedByName()){
@@ -99,33 +95,51 @@ public class MatrixRowComparator implements Comparator<Object>{
                 //both descriptions are of the same type
                 if(description1 instanceof SpecimenDescription){
                     //description2 has to also be a SpecimenDescription
-                    SpecimenDescription specimenDescription1 = (SpecimenDescription)description1;
-                    SpecimenDescription specimenDescription2 = (SpecimenDescription)description2;
-                    int id1 = specimenDescription1.getDescribedSpecimenOrObservation().getId();
-                    int id2 = specimenDescription2.getDescribedSpecimenOrObservation().getId();
-                    return id1-id2;
+                    return compareSpecimenDescriptions((SpecimenDescription)description1, (SpecimenDescription)description2);
                 }
                 else if(description1 instanceof TaxonDescription){
                     //description2 has to also be a TaxonDescription
-                    TaxonDescription taxonDescription1 = (TaxonDescription)description1;
-                    TaxonDescription taxonDescription2 = (TaxonDescription)description2;
-                    boolean isComputed1 = taxonDescription1.getTypes().stream()
-                            .anyMatch(type -> type.equals(DescriptionType.AGGREGATED));
-                    boolean isComputed2 = taxonDescription2.getTypes().stream()
-                            .anyMatch(type -> type.equals(DescriptionType.AGGREGATED));
-                    if(isComputed1){
-                        if(!isComputed2){
-                            return -1;
-                        }
-                    }
-                    if(isComputed2){
-                        return 1;
-                    }
-                    //TODO: implement compare for different description types
+                    return compareTaxonDescriptions((TaxonDescription)description1, (TaxonDescription)description2);
                 }
             }
         }
         return o1.hashCode()-o2.hashCode();
     }
 
+    private int compareTaxonDescriptions(TaxonDescription taxonDescription1, TaxonDescription taxonDescription2) {
+        boolean isComputed1 = taxonDescription1.getTypes().stream()
+                .anyMatch(type -> type.equals(DescriptionType.AGGREGATED_STRUC_DESC));
+        boolean isComputed2 = taxonDescription2.getTypes().stream()
+                .anyMatch(type -> type.equals(DescriptionType.AGGREGATED_STRUC_DESC));
+        if(isComputed1){
+            if(!isComputed2){
+                return -1;
+            }
+        }
+        if(isComputed2){
+            return 1;
+        }
+        // no aggregation description found
+        boolean isLiterature1 = taxonDescription1.getTypes().stream()
+                .anyMatch(type -> type.equals(DescriptionType.SECONDARY_DATA));
+        boolean isLiterature2 = taxonDescription2.getTypes().stream()
+                .anyMatch(type -> type.equals(DescriptionType.SECONDARY_DATA));
+        if(isLiterature1){
+            if(!isLiterature2){
+                return -1;
+            }
+        }
+        if(isLiterature2){
+            return 1;
+        }
+        return taxonDescription1.hashCode()-taxonDescription2.hashCode();
+    }
+
+    private int compareSpecimenDescriptions(SpecimenDescription specimenDescription1,
+            SpecimenDescription specimenDescription2) {
+        int id1 = specimenDescription1.getDescribedSpecimenOrObservation().getId();
+        int id2 = specimenDescription2.getDescribedSpecimenOrObservation().getId();
+        return id1-id2;
+    }
+
 }