3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.database
.update
;
12 import java
.sql
.SQLException
;
13 import java
.util
.List
;
15 import org
.apache
.log4j
.Logger
;
17 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
18 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
19 import eu
.etaxonomy
.cdm
.model
.common
.CdmMetaData
;
23 * Common updater base class for updating schema or terms.
28 public abstract class UpdaterBase
<T
extends ISchemaUpdaterStep
, U
extends IUpdater
<U
>> implements IUpdater
<U
> {
29 private static final Logger logger
= Logger
.getLogger(TermUpdaterBase
.class);
31 protected List
<T
> list
;
32 protected String startVersion
;
33 protected String targetVersion
;
35 protected abstract boolean updateVersion(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
;
37 protected abstract String
getCurrentVersion(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
;
40 public int countSteps(ICdmDataSource datasource
, IProgressMonitor monitor
){
42 //TODO test if previous updater is needed
43 if (isToBeInvoked(/*targetVerison, */datasource
, monitor
)){
45 result
++; //+= list.size();
46 result
+= step
.getInnerSteps().size();
48 if (getPreviousUpdater() != null){
49 result
+= getPreviousUpdater().countSteps(/*targetVerison, */datasource
, monitor
);
56 private boolean isToBeInvoked(ICdmDataSource datasource
, IProgressMonitor monitor
) {
57 boolean result
= true;
58 String datasourceVersion
;
60 datasourceVersion
= getCurrentVersion(datasource
, monitor
);
61 } catch (SQLException e1
) {
62 monitor
.warning("SQLException", e1
);
66 boolean isAfterMyStartVersion
= isAfterMyStartVersion(datasourceVersion
, monitor
);
67 boolean isBeforeMyStartVersion
= isBeforeMyStartVersion(datasourceVersion
, monitor
);
68 // boolean isBeforeMyTargetVersion = isBeforeMyTargetVersion(targetVersion, monitor);
69 boolean isBeforeMyTargetVersion
= isBeforeMyTargetVersion(targetVersion
, monitor
);
70 boolean isDatasourceBeforeMyTargetVersion
= isBeforeMyTargetVersion(datasourceVersion
, monitor
);
72 result
&= isDatasourceBeforeMyTargetVersion
;
73 result
&= !(isAfterMyStartVersion
&& isBeforeMyTargetVersion
);
74 result
&= ! (isBeforeMyStartVersion
&& getPreviousUpdater() == null);
75 result
&= !isBeforeMyTargetVersion
;
81 public boolean invoke(ICdmDataSource datasource
, IProgressMonitor monitor
) throws Exception
{
82 String currentLibrarySchemaVersion
= CdmMetaData
.getDbSchemaVersion();
83 return invoke(currentLibrarySchemaVersion
, datasource
, monitor
);
87 * @see eu.etaxonomy.cdm.database.update.IUpdater#invoke(java.lang.String, eu.etaxonomy.cdm.database.ICdmDataSource, eu.etaxonomy.cdm.common.IProgressMonitor)
90 public boolean invoke(String targetVersion
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws Exception
{
91 boolean result
= true;
92 String datasourceVersion
;
94 datasourceVersion
= getCurrentVersion(datasource
, monitor
);
95 } catch (SQLException e1
) {
96 monitor
.warning("SQLException", e1
);
101 boolean isAfterMyStartVersion
= isAfterMyStartVersion(datasourceVersion
, monitor
);
102 boolean isBeforeMyStartVersion
= isBeforeMyStartVersion(datasourceVersion
, monitor
);
103 // boolean isAfterMyTargetVersion = isAfterMyTargetVersion(targetVersion, monitor);
104 boolean isBeforeMyTargetVersion
= isBeforeMyTargetVersion(targetVersion
, monitor
);
105 boolean isDatasourceBeforeMyTargetVersion
= isBeforeMyTargetVersion(datasourceVersion
, monitor
);
109 if (! isDatasourceBeforeMyTargetVersion
){
110 String warning
= "Target version ("+targetVersion
+") is not before updater target version ("+this.targetVersion
+"). Nothing to update.";
111 monitor
.warning(warning
);
115 if (isAfterMyStartVersion
&& isBeforeMyTargetVersion
){
116 String warning
= "Database version is higher than updater start version but lower than updater target version";
117 RuntimeException exeption
= new RuntimeException(warning
);
118 monitor
.warning(warning
, exeption
);
122 if (isBeforeMyStartVersion
){
123 if (getPreviousUpdater() == null){
124 String warning
= "Database version is before updater version but no previous version updater exists";
125 RuntimeException exeption
= new RuntimeException(warning
);
126 monitor
.warning(warning
, exeption
);
129 result
&= getPreviousUpdater().invoke(startVersion
, datasource
, monitor
);
134 if (isBeforeMyTargetVersion
){
135 String warning
= "Target version ("+targetVersion
+") is lower than updater target version ("+this.targetVersion
+")";
136 RuntimeException exeption
= new RuntimeException(warning
);
137 monitor
.warning(warning
, exeption
);
141 if (result
== false){
144 // datasource.startTransaction(); transaction already started by CdmUpdater
147 result
&= handleSingleStep(datasource
, monitor
, result
, step
, false);
148 if (result
== false){
153 result
&= updateVersion(datasource
, monitor
);
155 datasource
.rollback();
158 } catch (Exception e
) {
159 datasource
.rollback();
160 logger
.error("Error occurred while trying to run updater: " + this.getClass().getName());
167 // protected abstract boolean handleSingleStep(ICdmDataSource datasource, IProgressMonitor monitor, boolean result, ISchemaUpdaterStep step, boolean isInnerStep) throws Exception;
169 protected boolean handleSingleStep(ICdmDataSource datasource
, IProgressMonitor monitor
, boolean result
, ISchemaUpdaterStep step
, boolean isInnerStep
)
172 monitor
.subTask(step
.getStepName());
173 Integer invokeResult
= step
.invoke(datasource
, monitor
);
174 result
&= (invokeResult
!= null);
175 for (ISchemaUpdaterStep innerStep
: step
.getInnerSteps()){
176 result
&= handleSingleStep(datasource
, monitor
, result
, innerStep
, true);
178 // if (! isInnerStep){
181 } catch (Exception e
) {
182 monitor
.warning("Monitor: Exception occurred while handling single schema updating step", e
);
183 datasource
.rollback();
189 protected boolean isAfterMyStartVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
191 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, startVersion
, depth
, monitor
);
192 return compareResult
> 0;
195 protected boolean isBeforeMyStartVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
197 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, startVersion
, depth
, monitor
);
198 return compareResult
< 0;
201 protected boolean isAfterMyTargetVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
203 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, targetVersion
, depth
, monitor
);
204 return compareResult
> 0;
207 protected boolean isBeforeMyTargetVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
209 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, targetVersion
, depth
, monitor
);
210 return compareResult
< 0;
214 * @see eu.etaxonomy.cdm.database.update.IUpdater#getNextUpdater()
217 public abstract U
getNextUpdater();
220 * @see eu.etaxonomy.cdm.database.update.IUpdater#getPreviousUpdater()
223 public abstract U
getPreviousUpdater();
229 public String
getTargetVersion() {
230 return this.targetVersion
;