fix #5957 fix IndexOutOfBound exception and some other improvements for groupByHigher...
authorAndreas Müller <a.mueller@bgbm.org>
Mon, 12 Sep 2016 14:25:07 +0000 (16:25 +0200)
committerAndreas Müller <a.mueller@bgbm.org>
Mon, 12 Sep 2016 14:25:07 +0000 (16:25 +0200)
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/taxon/ITaxonNodeDao.java
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ClassificationServiceImpl.java
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TreeIndexComparator.java
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/dto/GroupedTaxonDTO.java
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TreeIndexComparatorTest.java [new file with mode: 0644]

index fd36c8eb2786dabd95747bcc944fa3728fdc89df..4908eca48be61c93a5186e871721bb0d5eb45e7c 100644 (file)
@@ -75,18 +75,24 @@ public interface ITaxonNodeDao extends IAnnotatableDao<TaxonNode> {
     long countTaxonNodeAgentRelations(UUID taxonUuid, UUID classificationUuid, UUID agentUuid, UUID rankUuid, UUID relTypeUuid);\r
 \r
     /**\r
-     * @param treeIndexClosure\r
-     * @param minRankOrderIndex\r
-     * @param maxRankOrderIndex\r
+     * Computes a map treeIndex->rank(sortIndex) for each given taxon node treeIndex. Required by #5957.\r
+     * If the taxon represented by the treeindex is not in the given rank range no record is returned for the given\r
+     * treeindex.\r
+     *\r
+     * @param treeIndex the list of treeIndexes\r
+     * @param minRankOrderIndex min rank\r
+     * @param maxRankOrderIndex max rank\r
      * @return\r
      */\r
-    Map<String, Integer> rankOrderIndexForTreeIndex(List<String> treeIndexClosure, Integer minRankOrderIndex,\r
+    Map<String, Integer> rankOrderIndexForTreeIndex(List<String> treeIndex, Integer minRankOrderIndex,\r
             Integer maxRankOrderIndex);\r
 \r
     /**\r
-     * @param keySet\r
-     * @return\r
+     * For a given set of taxon node tree indexes the uuid and title cache of the taxon represented\r
+     * by this treeindex is returned.\r
+     * @param treeIndexSet set of taxon node tree indexes\r
+     * @return map with treeindex and uuidAndTitleCache of the represented taxon\r
      */\r
-    Map<String, UuidAndTitleCache<?>> taxonUuidsForTreeIndexes(Set<String> keySet);\r
+    Map<String, UuidAndTitleCache<?>> taxonUuidsForTreeIndexes(Set<String> treeIndexSet);\r
 \r
 }\r
index 0283481887e8a849c92d82f244b12967d58d352a..52b02a93e8c23af86811517791c981ef047a90c9 100755 (executable)
@@ -625,7 +625,7 @@ public class ClassificationServiceImpl extends IdentifiableServiceBase<Classific
         //get treeindex for each taxonUUID
         Map<UUID, String> taxonIdTreeIndexMap = dao.treeIndexForTaxonUuids(classificationUuid, originalTaxonUuids);
 
-        //build treeindex tree or list
+        //build treeindex list (or tree)
         List<String> treeIndexClosure = new ArrayList<>();
         for (String treeIndex : taxonIdTreeIndexMap.values()){
             String[] splits = treeIndex.substring(1).split(ITreeNode.separator);
index 35c7be89a574c72e19a5744c052f14fff9dd40b8..3fa24c6aa19ee995adc3f4f89864476fc8aa0725 100644 (file)
@@ -17,6 +17,9 @@ import eu.etaxonomy.cdm.model.common.ITreeNode;
  * @author a.mueller
  * @date 05.07.2016
  *
+ * Comparator for treeindexes.
+ * Compares the tree indexes node by node, sorted by node number.
+ * If one index is shorter than the other one but
  */
 public class TreeIndexComparator implements Comparator<String>{
 
@@ -29,12 +32,16 @@ public class TreeIndexComparator implements Comparator<String>{
         }else if (treeIndex2 == null){
             return 1;
         }
+        if (treeIndex1.equals(treeIndex2)){
+            return 0;
+        }
+
         String[] splits1 = treeIndex1.split(ITreeNode.separator);
         String[] splits2 = treeIndex2.split(ITreeNode.separator);
 
 
-        for (int i=0; i<splits1.length; i++){
-            if (splits2.length < i){
+        for (int i=0; i < splits1.length; i++){
+            if (splits2.length <= i){
                 return 1;
             }
             int c = splits1[i].compareTo(splits2[i]);
index 83b9a4535acd2d283fed9322a4a217f09ff2a493..43536f51af445d587b985a8d7cfef388158bce19 100644 (file)
@@ -65,4 +65,15 @@ public class GroupedTaxonDTO {
     public void setGroupTaxonName(String groupTaxonName) {
         this.groupTaxonName = groupTaxonName;
     }
+
+//*********************** toString() ***************************/
+    @Override
+    public String toString() {
+        String result = "taxon:" + (taxonUuid == null? "-":taxonUuid.toString())
+                + "; group:" + (groupTaxonUuid == null? "-":groupTaxonUuid.toString())
+                + "; group name:" + (groupTaxonName == null? "-":groupTaxonName.toString());
+        return result;
+    }
+
+
 }
diff --git a/cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TreeIndexComparatorTest.java b/cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TreeIndexComparatorTest.java
new file mode 100644 (file)
index 0000000..ac057db
--- /dev/null
@@ -0,0 +1,52 @@
+// $Id$
+/**
+* Copyright (C) 2016 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.api.service;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+/**
+ * @author a.mueller
+ * @date 12.09.2016
+ *
+ */
+public class TreeIndexComparatorTest {
+
+    @Test
+    public void test() {
+        TreeIndexComparator comparator = new TreeIndexComparator();
+
+        //both null
+        Assert.assertTrue(0 == comparator.compare(null, null));
+        //one null
+        Assert.assertTrue(0 > comparator.compare(null, "#t10#10#"));
+        Assert.assertTrue(0 < comparator.compare("#t10#10#", null));
+        //equal
+        Assert.assertTrue(0 == comparator.compare("#t10#10#", "#t10#10#"));
+
+        //same start
+        Assert.assertTrue(0 > comparator.compare("#t10#10#", "#t10#10#20#"));
+        Assert.assertTrue(0 < comparator.compare("#t10#10#20#", "#t10#10#"));
+
+        //different ends
+        Assert.assertTrue(0 > comparator.compare("#t10#10#20#", "#t10#10#30#"));
+        Assert.assertTrue(0 < comparator.compare("#t10#10#30#", "#t10#10#20#"));
+
+        //different ends
+        Assert.assertTrue(0 > comparator.compare("#t10#10#20#", "#t10#10#30#"));
+        Assert.assertTrue(0 > comparator.compare("#t10#10#20#", "#t10#10#30#11"));
+
+        Assert.assertTrue(0 < comparator.compare("#t10#10#30#", "#t10#10#20#"));
+        Assert.assertTrue(0 < comparator.compare("#t10#10#30#11", "#t10#10#20#"));
+
+    }
+
+}