*/\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
* @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
}\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