include more auditing in schema update and refactor to better allow transaction suppo...
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / TreeIndexUpdater.java
index dbe9ab41dc209dfa6481f3bf6f6276d0d4d6b9f6..f1225d4d1a65831cc6a2890f7d9a6dfcb131176a 100644 (file)
@@ -9,8 +9,6 @@
 */\r
 package eu.etaxonomy.cdm.database.update;\r
 \r
-import java.sql.SQLException;\r
-\r
 import org.apache.log4j.Logger;\r
 \r
 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;\r
@@ -21,12 +19,10 @@ import eu.etaxonomy.cdm.database.ICdmDataSource;
  * @date 09.08.2013\r
  *\r
  */\r
-public class TreeIndexUpdater extends SchemaUpdaterStepBase<TreeIndexUpdater> implements ISchemaUpdaterStep {\r
+public class TreeIndexUpdater extends AuditedSchemaUpdaterStepBase<TreeIndexUpdater> implements ISchemaUpdaterStep {\r
        private static final Logger logger = Logger.getLogger(TreeIndexUpdater.class);\r
        \r
-       private String tableName;\r
        private String indexColumnName = "treeIndex";\r
-       private boolean includeAudTable;\r
        private String treeIdColumnName;\r
        private String parentIdColumnName = "parent_id";\r
        \r
@@ -48,64 +44,60 @@ public class TreeIndexUpdater extends SchemaUpdaterStepBase<TreeIndexUpdater> im
        }\r
 \r
        @Override\r
-       public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {\r
-               boolean result = true;\r
-               result &= indexTable(tableName, datasource, monitor);\r
-               if (includeAudTable){\r
-                       String aud = "_AUD";\r
-                       result &= indexTable(tableName + aud, datasource, monitor);\r
-               }\r
-               return (result == true )? 0 : null;\r
-       }\r
-\r
-       private boolean indexTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {\r
-               boolean result = true;                  \r
-               \r
-//             String charType = "CHAR";  //TODO may depend on database type\r
-               \r
-               //clean up  //this should not happen with correct "delete" implementation\r
-               String sql = String.format(" DELETE FROM %s WHERE %s IS NULL ", tableName, treeIdColumnName);\r
-               datasource.executeUpdate(sql);\r
-               \r
-               sql = String.format(" UPDATE %s SET %s = NULL", tableName, indexColumnName);\r
-               datasource.executeUpdate(sql);\r
-               \r
-               //start\r
-               sql = String.format(" UPDATE %s tn " +\r
-                               " SET tn.%s = CONCAT('#c', tn.%s, '#') " +\r
-                               " WHERE tn.%s IS NULL AND tn.%s IS NOT NULL ", \r
-                               tableName, indexColumnName, treeIdColumnName, parentIdColumnName, treeIdColumnName);\r
-               datasource.executeUpdate(sql);\r
-               \r
-               //width search index creation\r
-               String sqlCount = String.format(" SELECT count(*) as n " +\r
-                               " FROM %s child INNER JOIN %s parent ON child.%s = parent.id " +\r
-                               " WHERE parent.%s IS NOT NULL AND child.%s IS NULL ",\r
-                               tableName, tableName, parentIdColumnName, indexColumnName, indexColumnName);\r
-               \r
-               Long n;\r
-               do {\r
-               \r
-                       sql = String.format(" UPDATE %s child INNER JOIN %s parent ON child.%s = parent.id " +\r
-                                       " SET child.%s = CONCAT( parent.%s, child.id, '#') " +\r
-                                       " WHERE parent.%s IS NOT NULL AND child.%s IS NULL ", \r
-                                               tableName, tableName, parentIdColumnName, indexColumnName, indexColumnName,\r
-                                               indexColumnName, indexColumnName);\r
+       protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) {\r
+               try{\r
+                       boolean result = true;                  \r
+                       \r
+       //              String charType = "CHAR";  //TODO may depend on database type\r
+                       \r
+                       //clean up  //this should not happen with correct "delete" implementation\r
+                       String sql = String.format(" DELETE FROM %s WHERE %s IS NULL ", tableName, treeIdColumnName);\r
+                       datasource.executeUpdate(sql);\r
+                       \r
+                       sql = String.format(" UPDATE %s SET %s = NULL", tableName, indexColumnName);\r
+                       datasource.executeUpdate(sql);\r
+                       \r
+                       //start\r
+                       sql = String.format(" UPDATE %s tn " +\r
+                                       " SET tn.%s = CONCAT('#c', tn.%s, '#') " +\r
+                                       " WHERE tn.%s IS NULL AND tn.%s IS NOT NULL ", \r
+                                       tableName, indexColumnName, treeIdColumnName, parentIdColumnName, treeIdColumnName);\r
                        datasource.executeUpdate(sql);\r
                        \r
+                       //width search index creation\r
+                       String sqlCount = String.format(" SELECT count(*) as n " +\r
+                                       " FROM %s child INNER JOIN %s parent ON child.%s = parent.id " +\r
+                                       " WHERE parent.%s IS NOT NULL AND child.%s IS NULL ",\r
+                                       tableName, tableName, parentIdColumnName, indexColumnName, indexColumnName);\r
+                       \r
+                       Long n;\r
+                       do {\r
+                       \r
+                               sql = String.format(" UPDATE %s child INNER JOIN %s parent ON child.%s = parent.id " +\r
+                                               " SET child.%s = CONCAT( parent.%s, child.id, '#') " +\r
+                                               " WHERE parent.%s IS NOT NULL AND child.%s IS NULL ", \r
+                                                       tableName, tableName, parentIdColumnName, indexColumnName, indexColumnName,\r
+                                                       indexColumnName, indexColumnName);\r
+                               datasource.executeUpdate(sql);\r
+                               \r
+                               n = (Long)datasource.getSingleValue(sqlCount);\r
+                       }       while (n > 0) ;\r
+                       \r
+                       sqlCount = String.format(" SELECT count(*) as n " +\r
+                                       " FROM %s " +\r
+                                       " WHERE %s IS NULL ", tableName, indexColumnName);\r
                        n = (Long)datasource.getSingleValue(sqlCount);\r
-               }       while (n > 0) ;\r
-               \r
-               sqlCount = String.format(" SELECT count(*) as n " +\r
-                               " FROM %s " +\r
-                               " WHERE %s IS NULL ", tableName, indexColumnName);\r
-               n = (Long)datasource.getSingleValue(sqlCount);\r
-               if (n > 0){\r
-                       String message = "There are tree nodes with no tree index in %s. This indicates that there is a problem in the tree structure of 1 or more classifications.";\r
-                       logger.error(String.format(message, tableName));\r
+                       if (n > 0){\r
+                               String message = "There are tree nodes with no tree index in %s. This indicates that there is a problem in the tree structure of 1 or more classifications.";\r
+                               logger.error(String.format(message, tableName));\r
+                       }\r
+                       \r
+                       return result;\r
+               }catch(Exception e){\r
+                       monitor.warning(e.getMessage(), e);\r
+                       logger.error(e.getMessage());\r
+                       return false;\r
                }\r
-               \r
-               return result;\r
        }\r
 \r
 \r