ref #8450 Fix order of taxon descriptions in matrix
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 6 Nov 2019 11:14:43 +0000 (12:14 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 6 Nov 2019 11:14:50 +0000 (12:14 +0100)
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/MatrixRowComparator.java

index 2f48f65fa3528552b306df8112b7d05b149cda76..fc3a5133c2bffff2cb5ad0bd11e58a274ec48429 100644 (file)
@@ -95,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));
+        boolean isComputed2 = taxonDescription2.getTypes().stream()
+                .anyMatch(type -> type.equals(DescriptionType.AGGREGATED));
+        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;
+    }
+
 }