added column name changer
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / TermUpdaterBase.java
index b3eae2770cde9bcc9910d6d2d63773e37f610046..9a75ab0280e877e3a4bd75a5da9c74a31b9c289f 100644 (file)
@@ -30,12 +30,14 @@ public abstract class TermUpdaterBase implements ITermUpdater {
        protected static final UUID uuidFeatureVocabulary = UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");\r
        \r
        private List<SingleTermUpdater> list;\r
-       private String mySchemaVersion;\r
+       private String startTermVersion;\r
+       private String targetTermVersion;\r
        \r
        \r
        \r
-       protected TermUpdaterBase(String mySchemaVersion){\r
-               this.mySchemaVersion = mySchemaVersion;\r
+       protected TermUpdaterBase(String startTermVersion, String targetTermVersion){\r
+               this.startTermVersion = startTermVersion;\r
+               this.targetTermVersion = targetTermVersion;\r
                list = getUpdaterList();\r
        }\r
        \r
@@ -55,7 +57,17 @@ public abstract class TermUpdaterBase implements ITermUpdater {
         * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#invoke()\r
         */\r
        @Override\r
-       public boolean invoke(ICdmDataSource datasource, IProgressMonitor monitor){\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
@@ -66,24 +78,44 @@ public abstract class TermUpdaterBase implements ITermUpdater {
                        return false;\r
                }\r
                \r
-               boolean isAfterMyVersion = isAfterMyVersion(datasourceSchemaVersion, monitor);\r
-               if (isAfterMyVersion){\r
-                       String warning = "Database version is higher than updater version";\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
-               boolean isBeforeMyVersion = isBeforeMyVersion(datasourceSchemaVersion, monitor);\r
-               if (isBeforeMyVersion){\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(datasource, monitor);\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
@@ -98,26 +130,52 @@ public abstract class TermUpdaterBase implements ITermUpdater {
                                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
+               int intSchemaVersion = 1;\r
+               String sqlUpdateSchemaVersion = "UPDATE CdmMetaData SET value = '" + this.targetTermVersion + "' 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 isAfterMyVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {\r
+       protected boolean isAfterMyStartVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {\r
                int depth = 4;\r
-               int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, mySchemaVersion, depth, monitor);\r
+               int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, startTermVersion, depth, monitor);\r
                return compareResult > 0;\r
        }\r
 \r
-       protected boolean isBeforeMyVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {\r
+       protected boolean isBeforeMyStartVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {\r
                int depth = 4;\r
-               int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, mySchemaVersion, depth, monitor);\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 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
 \r
        protected String getCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {\r
-               int intSchemaVersion = 0;\r
+               int intSchemaVersion = 1;\r
                String sqlSchemaVersion = "SELECT value FROM CdmMetaData WHERE propertyname = " +  intSchemaVersion;\r
                try {\r
                        String value = (String)datasource.getSingleValue(sqlSchemaVersion);\r
@@ -141,5 +199,9 @@ public abstract class TermUpdaterBase implements ITermUpdater {
        @Override\r
        public abstract ITermUpdater getPreviousUpdater();\r
 \r
-\r
+       \r
+       @Override\r
+       public String getTargetVersion() {\r
+               return this.targetTermVersion;\r
+       }\r
 }\r