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");
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