fix #6274: add an update for childrenCount cache if count of children not the same...
authorKatja Luther <k.luther@bgbm.org>
Mon, 12 Dec 2016 13:26:55 +0000 (14:26 +0100)
committerKatja Luther <k.luther@bgbm.org>
Mon, 12 Dec 2016 13:27:44 +0000 (14:27 +0100)
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/common/SortIndexUpdaterWrapper.java
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/SortIndexUpdater.java

index 7c4ef2145ee1978e89f07a96a0c732ac517700b7..8afc452bb8aa25526a7704d59f29985b56887b01 100644 (file)
@@ -102,9 +102,30 @@ public class SortIndexUpdaterWrapper extends CdmImportBase<SortIndexUpdaterConfi
                 query = updater.createUpdateIndicesQuery(null,entry.getKey(), idSet);
                 sqlQuery = getAgentService().getSession().createSQLQuery(query);
                 int resultInt = sqlQuery.executeUpdate();
-                System.out.println("update all indice with index "+entry.getKey()+ " - " + resultInt);
+                logger.debug("update all indice with index "+entry.getKey()+ " - " + resultInt);
             }
-            commitTransaction(tx);
+            //Update childrenCount
+            query = updater.getChildrenCountQuery();
+            sqlQuery = getAgentService().getSession().createSQLQuery(query);
+            data = sqlQuery.list();
+            int realCount;
+            int countChildren;
+            for(Object object : data)
+            {
+               Object[] row = (Object[])object;
+               realCount =  ((Number) row[0]).intValue();
+               countChildren = ((Number) row[1]).intValue();
+               id = ((Number) row[2]).intValue();
+
+               if (realCount != countChildren){
+                   query = updater.getUpdateChildrenCount(realCount, id);
+                   sqlQuery = getAgentService().getSession().createSQLQuery(query);
+                   int resultInt = sqlQuery.executeUpdate();
+                   logger.debug("update all childrenCount "+ resultInt);
+               }
+             }
+
+              commitTransaction(tx);
         } catch (SQLException e) {
 
             monitor.warning("Stopped sortIndex updater");
index 331b920ef2b3595c30aae84763e2c400a0d7211e..fe026e5747e464ef5fe2bd62e2c36ae3b48a3910 100644 (file)
@@ -210,4 +210,45 @@ public class SortIndexUpdater extends SchemaUpdaterStepBase<SortIndexUpdater> {
                set.add(id);\r
        }\r
 \r
+\r
+       public String getChildrenCountQuery(){\r
+\r
+        String countSelect = "SELECT COUNT(child.id) as realCount, parent.countChildren as countChildren, parent.id as parentID FROM @tableName child RIGHT JOIN @tableName parent ON child.parent_id = parent.id GROUP BY parent.id";\r
+        countSelect = countSelect.replace("@tableName", tableName);\r
+        return countSelect;\r
+\r
+       }\r
+\r
+       public String getUpdateChildrenCount(int count, int id){\r
+\r
+        String countUpdate =  "UPDATE @tableName SET countChildren = " + count+" WHERE id = " + id;\r
+        countUpdate = countUpdate.replace("@tableName", tableName);\r
+\r
+\r
+        return countUpdate;\r
+\r
+    }\r
+\r
+       public void updateChildrenCount(String select, ICdmDataSource datasource) throws SQLException{\r
+           ResultSet rs;\r
+\r
+        rs = datasource.executeQuery(select);\r
+\r
+        List<Integer[]> result = new ArrayList<Integer[]>();\r
+        int count ;\r
+        int countChildren;\r
+        int parentId;\r
+\r
+        while (rs.next()){\r
+            count = rs.getInt("realCount");\r
+            countChildren = rs.getInt("countChildren");\r
+            parentId = rs.getInt("parentID");\r
+            if (count != countChildren){\r
+                String updateQuery = getUpdateChildrenCount(count, parentId);\r
+                datasource.executeUpdate(updateQuery);\r
+            }\r
+        }\r
+\r
+       }\r
+\r
 }\r