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
.metadata
.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
;
36 protected abstract boolean updateVersion(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) throws SQLException
;
38 protected abstract String
getCurrentVersion(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) throws SQLException
;
41 public int countSteps(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
){
43 //TODO test if previous updater is needed
44 if (isToBeInvoked(datasource
, monitor
, caseType
)){
46 result
++; //+= list.size();
47 result
+= step
.getInnerSteps().size();
49 if (getPreviousUpdater() != null){
50 result
+= getPreviousUpdater().countSteps(datasource
, monitor
, caseType
);
57 private boolean isToBeInvoked(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) {
58 boolean result
= true;
59 String datasourceVersion
;
61 datasourceVersion
= getCurrentVersion(datasource
, monitor
, caseType
);
62 } catch (SQLException e1
) {
63 monitor
.warning("SQLException", e1
);
67 boolean isAfterMyStartVersion
= isAfterMyStartVersion(datasourceVersion
, monitor
);
68 boolean isBeforeMyStartVersion
= isBeforeMyStartVersion(datasourceVersion
, monitor
);
69 // boolean isBeforeMyTargetVersion = isBeforeMyTargetVersion(targetVersion, monitor);
70 boolean isBeforeMyTargetVersion
= isBeforeMyTargetVersion(targetVersion
, monitor
);
71 boolean isDatasourceBeforeMyTargetVersion
= isBeforeMyTargetVersion(datasourceVersion
, monitor
);
73 result
&= isDatasourceBeforeMyTargetVersion
;
74 result
&= !(isAfterMyStartVersion
&& isBeforeMyTargetVersion
);
75 result
&= ! (isBeforeMyStartVersion
&& getPreviousUpdater() == null);
76 result
&= !isBeforeMyTargetVersion
;
82 public boolean invoke(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) throws Exception
{
83 String currentLibrarySchemaVersion
= CdmMetaData
.getDbSchemaVersion();
84 return invoke(currentLibrarySchemaVersion
, datasource
, monitor
, caseType
);
88 public boolean invoke(String targetVersion
, ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) throws Exception
{
89 boolean result
= true;
90 String datasourceVersion
;
93 datasourceVersion
= getCurrentVersion(datasource
, monitor
, caseType
);
94 } catch (SQLException e1
) {
95 monitor
.warning("SQLException", e1
);
100 boolean isAfterMyStartVersion
= isAfterMyStartVersion(datasourceVersion
, monitor
);
101 boolean isBeforeMyStartVersion
= isBeforeMyStartVersion(datasourceVersion
, monitor
);
102 // boolean isAfterMyTargetVersion = isAfterMyTargetVersion(targetVersion, monitor);
103 boolean isBeforeMyTargetVersion
= isBeforeMyTargetVersion(targetVersion
, monitor
);
104 boolean isDatasourceBeforeMyTargetVersion
= isBeforeMyTargetVersion(datasourceVersion
, monitor
);
108 if (! isDatasourceBeforeMyTargetVersion
){
109 String warning
= "Target version ("+targetVersion
+") is not before updater target version ("+this.targetVersion
+"). Nothing to update.";
110 monitor
.warning(warning
);
114 if (isAfterMyStartVersion
&& isBeforeMyTargetVersion
){
115 String warning
= "Database version is higher than updater start version but lower than updater target version";
116 RuntimeException exeption
= new RuntimeException(warning
);
117 monitor
.warning(warning
, exeption
);
121 if (isBeforeMyStartVersion
){
122 if (getPreviousUpdater() == null){
123 String warning
= "Database version is before updater version but no previous version updater exists";
124 RuntimeException exeption
= new RuntimeException(warning
);
125 monitor
.warning(warning
, exeption
);
128 result
&= getPreviousUpdater().invoke(startVersion
, datasource
, monitor
, caseType
);
133 if (isBeforeMyTargetVersion
){
134 String warning
= "Target version ("+targetVersion
+") is lower than updater target version ("+this.targetVersion
+")";
135 RuntimeException exeption
= new RuntimeException(warning
);
136 monitor
.warning(warning
, exeption
);
140 if (result
== false){
143 // datasource.startTransaction(); transaction already started by CdmUpdater
146 result
&= handleSingleStep(datasource
, monitor
, result
, step
, false, caseType
);
147 if (result
== false){
152 result
&= updateVersion(datasource
, monitor
, caseType
);
154 datasource
.rollback();
157 } catch (Exception e
) {
158 datasource
.rollback();
159 logger
.error("Error occurred while trying to run updater: " + this.getClass().getName());
166 // protected abstract boolean handleSingleStep(ICdmDataSource datasource, IProgressMonitor monitor, boolean result, ISchemaUpdaterStep step, boolean isInnerStep) throws Exception;
168 protected boolean handleSingleStep(ICdmDataSource datasource
, IProgressMonitor monitor
, boolean result
, ISchemaUpdaterStep step
, boolean isInnerStep
, CaseType caseType
)
171 monitor
.subTask(step
.getStepName());
172 Integer invokeResult
= step
.invoke(datasource
, monitor
, caseType
);
173 result
&= (invokeResult
!= null);
174 for (ISchemaUpdaterStep innerStep
: step
.getInnerSteps()){
175 result
&= handleSingleStep(datasource
, monitor
, result
, innerStep
, true, caseType
);
177 // if (! isInnerStep){
180 } catch (Exception e
) {
181 monitor
.warning("Monitor: Exception occurred while handling single schema updating step", e
);
182 datasource
.rollback();
188 protected boolean isAfterMyStartVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
190 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, startVersion
, depth
, monitor
);
191 return compareResult
> 0;
194 protected boolean isBeforeMyStartVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
196 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, startVersion
, depth
, monitor
);
197 return compareResult
< 0;
200 protected boolean isAfterMyTargetVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
202 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, targetVersion
, depth
, monitor
);
203 return compareResult
> 0;
206 protected boolean isBeforeMyTargetVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
208 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, targetVersion
, depth
, monitor
);
209 return compareResult
< 0;
213 * @see eu.etaxonomy.cdm.database.update.IUpdater#getNextUpdater()
216 public abstract U
getNextUpdater();
219 * @see eu.etaxonomy.cdm.database.update.IUpdater#getPreviousUpdater()
222 public abstract U
getPreviousUpdater();
228 public String
getTargetVersion() {
229 return this.targetVersion
;