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.
33 public abstract class UpdaterBase
<T
extends ISchemaUpdaterStep
, U
extends IUpdater
<U
>> implements IUpdater
<U
> {
34 private static final Logger logger
= Logger
.getLogger(TermUpdaterBase
.class);
36 protected List
<T
> list
;
37 protected String startVersion
;
38 protected String targetVersion
;
41 protected abstract boolean updateVersion(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) throws SQLException
;
43 protected abstract String
getCurrentVersion(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) throws SQLException
;
46 public int countSteps(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
){
48 //TODO test if previous updater is needed
49 if (isToBeInvoked(datasource
, monitor
, caseType
)){
51 result
++; //+= list.size();
52 result
+= step
.getInnerSteps().size();
54 if (getPreviousUpdater() != null){
55 result
+= getPreviousUpdater().countSteps(datasource
, monitor
, caseType
);
62 private boolean isToBeInvoked(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) {
63 boolean result
= true;
64 String datasourceVersion
;
66 datasourceVersion
= getCurrentVersion(datasource
, monitor
, caseType
);
67 } catch (SQLException e1
) {
68 monitor
.warning("SQLException", e1
);
72 boolean isAfterMyStartVersion
= isAfterMyStartVersion(datasourceVersion
, monitor
);
73 boolean isBeforeMyStartVersion
= isBeforeMyStartVersion(datasourceVersion
, monitor
);
74 // boolean isBeforeMyTargetVersion = isBeforeMyTargetVersion(targetVersion, monitor);
75 boolean isBeforeMyTargetVersion
= isBeforeMyTargetVersion(targetVersion
, monitor
);
76 boolean isDatasourceBeforeMyTargetVersion
= isBeforeMyTargetVersion(datasourceVersion
, monitor
);
78 result
&= isDatasourceBeforeMyTargetVersion
;
79 result
&= !(isAfterMyStartVersion
&& isBeforeMyTargetVersion
);
80 result
&= ! (isBeforeMyStartVersion
&& getPreviousUpdater() == null);
81 result
&= !isBeforeMyTargetVersion
;
87 public boolean invoke(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) throws Exception
{
88 String currentLibrarySchemaVersion
= CdmMetaData
.getDbSchemaVersion();
89 return invoke(currentLibrarySchemaVersion
, datasource
, monitor
, caseType
);
93 public boolean invoke(String targetVersion
, ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) throws Exception
{
94 boolean result
= true;
95 String datasourceVersion
;
98 datasourceVersion
= getCurrentVersion(datasource
, monitor
, caseType
);
99 } catch (SQLException e1
) {
100 monitor
.warning("SQLException", e1
);
105 boolean isAfterMyStartVersion
= isAfterMyStartVersion(datasourceVersion
, monitor
);
106 boolean isBeforeMyStartVersion
= isBeforeMyStartVersion(datasourceVersion
, monitor
);
107 // boolean isAfterMyTargetVersion = isAfterMyTargetVersion(targetVersion, monitor);
108 boolean isBeforeMyTargetVersion
= isBeforeMyTargetVersion(targetVersion
, monitor
);
109 boolean isDatasourceBeforeMyTargetVersion
= isBeforeMyTargetVersion(datasourceVersion
, monitor
);
113 if (! isDatasourceBeforeMyTargetVersion
){
114 String warning
= "Target version ("+targetVersion
+") is not before updater target version ("+this.targetVersion
+"). Nothing to update.";
115 monitor
.warning(warning
);
119 if (isAfterMyStartVersion
&& isBeforeMyTargetVersion
){
120 String warning
= "Database version is higher than updater start version but lower than updater target version";
121 RuntimeException exeption
= new RuntimeException(warning
);
122 monitor
.warning(warning
, exeption
);
126 if (isBeforeMyStartVersion
){
127 if (getPreviousUpdater() == null){
128 String warning
= "Database version is before updater version but no previous version updater exists";
129 RuntimeException exeption
= new RuntimeException(warning
);
130 monitor
.warning(warning
, exeption
);
133 result
&= getPreviousUpdater().invoke(startVersion
, datasource
, monitor
, caseType
);
138 if (isBeforeMyTargetVersion
){
139 String warning
= "Target version ("+targetVersion
+") is lower than updater target version ("+this.targetVersion
+")";
140 RuntimeException exeption
= new RuntimeException(warning
);
141 monitor
.warning(warning
, exeption
);
145 if (result
== false){
148 // datasource.startTransaction(); transaction already started by CdmUpdater
151 result
&= handleSingleStep(datasource
, monitor
, result
, step
, false, caseType
);
152 if (result
== false){
157 result
&= updateVersion(datasource
, monitor
, caseType
);
159 datasource
.rollback();
162 } catch (Exception e
) {
163 datasource
.rollback();
164 logger
.error("Error occurred while trying to run updater: " + this.getClass().getName());
171 // protected abstract boolean handleSingleStep(ICdmDataSource datasource, IProgressMonitor monitor, boolean result, ISchemaUpdaterStep step, boolean isInnerStep) throws Exception;
173 protected boolean handleSingleStep(ICdmDataSource datasource
, IProgressMonitor monitor
, boolean result
, ISchemaUpdaterStep step
, boolean isInnerStep
, CaseType caseType
)
176 monitor
.subTask(step
.getStepName());
177 Integer invokeResult
= step
.invoke(datasource
, monitor
, caseType
);
178 result
&= (invokeResult
!= null);
179 for (ISchemaUpdaterStep innerStep
: step
.getInnerSteps()){
180 result
&= handleSingleStep(datasource
, monitor
, result
, innerStep
, true, caseType
);
182 // if (! isInnerStep){
185 } catch (Exception e
) {
186 monitor
.warning("Monitor: Exception occurred while handling single schema updating step", e
);
187 datasource
.rollback();
193 protected boolean isAfterMyStartVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
195 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, startVersion
, depth
, monitor
);
196 return compareResult
> 0;
199 protected boolean isBeforeMyStartVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
201 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, startVersion
, depth
, monitor
);
202 return compareResult
< 0;
205 protected boolean isAfterMyTargetVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
207 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, targetVersion
, depth
, monitor
);
208 return compareResult
> 0;
211 protected boolean isBeforeMyTargetVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
213 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, targetVersion
, depth
, monitor
);
214 return compareResult
< 0;
218 * @see eu.etaxonomy.cdm.database.update.IUpdater#getNextUpdater()
221 public abstract U
getNextUpdater();
224 * @see eu.etaxonomy.cdm.database.update.IUpdater#getPreviousUpdater()
227 public abstract U
getPreviousUpdater();
233 public String
getTargetVersion() {
234 return this.targetVersion
;