* @date 10.09.2010\r
*\r
*/\r
-public abstract class TermUpdaterBase implements ITermUpdater {\r
+public abstract class TermUpdaterBase extends UpdaterBase<ITermUpdaterStep, ITermUpdater> implements ITermUpdater {\r
@SuppressWarnings("unused")\r
private static final Logger logger = Logger.getLogger(TermUpdaterBase.class);\r
protected static final UUID uuidFeatureVocabulary = UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");\r
\r
- private List<SingleTermUpdater> list;\r
- private String startTermVersion;\r
- private String targetTermVersion;\r
- \r
- \r
- \r
protected TermUpdaterBase(String startTermVersion, String targetTermVersion){\r
- this.startTermVersion = startTermVersion;\r
- this.targetTermVersion = targetTermVersion;\r
+ this.startVersion = startTermVersion;\r
+ this.targetVersion = targetTermVersion;\r
list = getUpdaterList();\r
}\r
\r
-\r
- @Override\r
- public int countSteps(ICdmDataSource datasource){\r
- int result = 0;\r
- //TODO test if previous updater is needed\r
- if (getPreviousUpdater() != null){\r
- result += getPreviousUpdater().countSteps(datasource);\r
- }\r
- result += list.size();\r
- return result;\r
- }\r
- \r
- /* (non-Javadoc)\r
- * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#invoke()\r
- */\r
- @Override\r
- public boolean invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws Exception{\r
- String currentLibraryTermVersion = CdmMetaData.getCurrentTermsVersion();\r
- return invoke(currentLibraryTermVersion, datasource, monitor);\r
- }\r
- \r
- \r
- /* (non-Javadoc)\r
- * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#invoke()\r
- */\r
@Override\r
- public boolean invoke(String targetVersion, ICdmDataSource datasource, IProgressMonitor monitor) throws Exception{\r
- boolean result = true;\r
- \r
- String datasourceSchemaVersion;\r
- try {\r
- datasourceSchemaVersion = getCurrentVersion(datasource, monitor);\r
- } catch (SQLException e1) {\r
- monitor.warning("SQLException", e1);\r
- return false;\r
- }\r
- \r
- boolean isAfterMyStartVersion = isAfterMyStartVersion(datasourceSchemaVersion, monitor);\r
- boolean isBeforeMyStartVersion = isBeforeMyStartVersion(datasourceSchemaVersion, monitor);\r
- boolean isAfterMyTargetVersion = isAfterMyTargetVersion(targetVersion, monitor);\r
- boolean isBeforeMyTargetVersion = isBeforeMyTargetVersion(targetVersion, monitor);\r
- boolean isDatasourceBeforeMyTargetVersion = isBeforeMyTargetVersion(datasourceSchemaVersion, monitor);\r
- \r
-\r
- if (! isDatasourceBeforeMyTargetVersion){\r
- String warning = "Target version ("+targetVersion+") is not before updater target version ("+this.targetTermVersion+"). Nothing to update.";\r
- monitor.warning(warning);\r
- return true;\r
- }\r
- \r
- if (isAfterMyStartVersion){\r
- String warning = "Database version is higher than updater start version";\r
- RuntimeException exeption = new RuntimeException(warning);\r
- monitor.warning(warning, exeption);\r
- throw exeption;\r
- }\r
- \r
- if (isBeforeMyStartVersion){\r
- if (getPreviousUpdater() == null){\r
- String warning = "Database version is before updater version but no previous version updater exists";\r
- RuntimeException exeption = new RuntimeException(warning);\r
- monitor.warning(warning, exeption);\r
- throw exeption;\r
- }\r
- result &= getPreviousUpdater().invoke(startTermVersion, datasource, monitor);\r
- }\r
-\r
- \r
- if (isBeforeMyTargetVersion){\r
- String warning = "Target version ("+targetVersion+") is lower than updater target version ("+this.targetTermVersion+")";\r
- RuntimeException exeption = new RuntimeException(warning);\r
- monitor.warning(warning, exeption);\r
- throw exeption;\r
- }\r
-\r
- \r
- \r
- for (SingleTermUpdater step : list){\r
- try {\r
- monitor.subTask(step.getStepName());\r
- Integer stepResult = step.invoke(datasource, monitor);\r
- result &= (stepResult != null);\r
- monitor.worked(1);\r
- } catch (Exception e) {\r
- // TODO Auto-generated catch block\r
- monitor.warning("Exception occurred while updating schema", e);\r
- result = false;\r
- }\r
- }\r
- updateTermVersion(datasource, monitor);\r
-\r
- return result;\r
- }\r
- \r
- private void updateTermVersion(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {\r
+ protected void updateVersion(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {\r
int intSchemaVersion = 1;\r
- String sqlUpdateSchemaVersion = "UPDATE CdmMetaData SET value = '" + this.targetTermVersion + "' WHERE propertyname = " + intSchemaVersion;\r
+ String sqlUpdateSchemaVersion = "UPDATE CdmMetaData SET value = '" + this.targetVersion + "' WHERE propertyname = " + intSchemaVersion;\r
try {\r
datasource.executeUpdate(sqlUpdateSchemaVersion);\r
} catch (Exception e) {\r
monitor.warning("Error when trying to set new schemaversion: ", e);\r
throw new SQLException(e);\r
}\r
- \r
-}\r
- \r
- protected abstract List<SingleTermUpdater> getUpdaterList();\r
-\r
- protected boolean isAfterMyStartVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {\r
- int depth = 4;\r
- int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, startTermVersion, depth, monitor);\r
- return compareResult > 0;\r
- }\r
-\r
- protected boolean isBeforeMyStartVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {\r
- int depth = 4;\r
- int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, startTermVersion, depth, monitor);\r
- return compareResult < 0;\r
- }\r
-\r
- protected boolean isAfterMyTargetVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {\r
- int depth = 4;\r
- int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, targetTermVersion, depth, monitor);\r
- return compareResult > 0;\r
}\r
+ \r
+ protected abstract List<ITermUpdaterStep> getUpdaterList();\r
\r
- protected boolean isBeforeMyTargetVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {\r
- int depth = 4;\r
- int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, targetTermVersion, depth, monitor);\r
- return compareResult < 0;\r
- }\r
\r
+ @Override\r
+ public boolean invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws Exception{\r
+ String currentLibrarySchemaVersion = CdmMetaData.getTermsVersion();\r
+ return invoke(currentLibrarySchemaVersion, datasource, monitor);\r
+ }\r
\r
+ @Override\r
protected String getCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {\r
int intSchemaVersion = 1;\r
try {\r
String defaultVersion = "2.4.2.2.201006011715";\r
String sqlMaxId = "SELECT max(id) FROM CdmMetaData";\r
Integer maxId = (Integer)datasource.getSingleValue(sqlMaxId) + 1;\r
- String sqlUpdate = "INSERT INTO CdmMetaData (id, created, uuid, propertyname, value) VALUES (" + maxId + ", '"+UUID.randomUUID()+"', '2010-09-21 13:52:54', 1, '" + defaultVersion + "')";\r
+ String sqlUpdate = "INSERT INTO CdmMetaData (id, created, uuid, propertyname, value) VALUES (" + maxId + ", '2010-09-21 13:52:54', '"+UUID.randomUUID()+"', 1, '" + defaultVersion + "')";\r
datasource.executeUpdate(sqlUpdate);\r
return defaultVersion;\r
}else{\r
}\r
}\r
\r
- \r
- /* (non-Javadoc)\r
- * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getNextUpdater()\r
- */\r
- @Override\r
- public abstract ITermUpdater getNextUpdater();\r
\r
- /* (non-Javadoc)\r
- * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getPreviousUpdater()\r
- */\r
- @Override\r
- public abstract ITermUpdater getPreviousUpdater();\r
\r
- \r
- @Override\r
- public String getTargetVersion() {\r
- return this.targetTermVersion;\r
- }\r
}\r