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
.IProgressMonitor
;
18 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
19 import eu
.etaxonomy
.cdm
.model
.common
.CdmMetaData
;
26 public abstract class SchemaUpdaterBase
implements ISchemaUpdater
{
27 @SuppressWarnings("unused")
28 private static final Logger logger
= Logger
.getLogger(SchemaUpdaterBase
.class);
29 private String startSchemaVersion
;
30 private String targetSchemaVersion
;
33 protected static boolean INCLUDE_AUDIT
= true;
35 private List
<ISchemaUpdaterStep
> list
;
39 protected SchemaUpdaterBase(String startSchemaVersion
, String endSchemaVersion
){
40 this.startSchemaVersion
= startSchemaVersion
;
41 this.targetSchemaVersion
= endSchemaVersion
;
42 list
= getUpdaterList();
46 public int countSteps(ICdmDataSource datasource
){
48 //TODO test if previous updater is needed
49 if (getPreviousUpdater() != null){
50 result
+= getPreviousUpdater().countSteps(datasource
);
52 result
+= list
.size();
57 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#invoke()
60 public boolean invoke(ICdmDataSource datasource
, IProgressMonitor monitor
) throws Exception
{
61 String currentLibrarySchemaVersion
= CdmMetaData
.getCurrentSchemaVersion();
62 return invoke(currentLibrarySchemaVersion
, datasource
, monitor
);
66 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#invoke()
69 public boolean invoke(String targetVersion
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws Exception
{
70 boolean result
= true;
71 String datasourceSchemaVersion
;
73 datasourceSchemaVersion
= getCurrentVersion(datasource
, monitor
);
74 } catch (SQLException e1
) {
75 monitor
.warning("SQLException", e1
);
80 boolean isAfterMyStartVersion
= isAfterMyStartVersion(datasourceSchemaVersion
, monitor
);
81 boolean isBeforeMyStartVersion
= isBeforeMyStartVersion(datasourceSchemaVersion
, monitor
);
82 boolean isAfterMyTargetVersion
= isAfterMyTargetVersion(targetVersion
, monitor
);
83 boolean isBeforeMyTargetVersion
= isBeforeMyTargetVersion(targetVersion
, monitor
);
84 boolean isDatasourceBeforeMyTargetVersion
= isBeforeMyTargetVersion(datasourceSchemaVersion
, monitor
);
88 if (! isDatasourceBeforeMyTargetVersion
){
89 String warning
= "Target version ("+targetVersion
+") is not before updater target version ("+this.targetSchemaVersion
+"). Nothing to update.";
90 monitor
.warning(warning
);
94 if (isAfterMyStartVersion
&& isBeforeMyTargetVersion
){
95 String warning
= "Database version is higher than updater start version but lower than updater target version";
96 RuntimeException exeption
= new RuntimeException(warning
);
97 monitor
.warning(warning
, exeption
);
101 if (isBeforeMyStartVersion
){
102 if (getPreviousUpdater() == null){
103 String warning
= "Database version is before updater version but no previous version updater exists";
104 RuntimeException exeption
= new RuntimeException(warning
);
105 monitor
.warning(warning
, exeption
);
108 result
&= getPreviousUpdater().invoke(startSchemaVersion
, datasource
, monitor
);
113 if (isBeforeMyTargetVersion
){
114 String warning
= "Target version ("+targetVersion
+") is lower than updater target version ("+this.targetSchemaVersion
+")";
115 RuntimeException exeption
= new RuntimeException(warning
);
116 monitor
.warning(warning
, exeption
);
121 for (ISchemaUpdaterStep step
: list
){
123 monitor
.subTask(step
.getStepName());
124 Integer termId
= step
.invoke(datasource
, monitor
);
125 result
&= (termId
!= null);
127 } catch (Exception e
) {
128 // TODO Auto-generated catch block
129 monitor
.warning("Exception occurred while updating schema", e
);
133 updateSchemaVersion(datasource
, monitor
);
141 private void updateSchemaVersion(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
142 int intSchemaVersion
= 0;
143 String sqlUpdateSchemaVersion
= "UPDATE CdmMetaData SET value = '" + this.targetSchemaVersion
+ "' WHERE propertyname = " + intSchemaVersion
;
145 datasource
.executeUpdate(sqlUpdateSchemaVersion
);
146 } catch (Exception e
) {
147 monitor
.warning("Error when trying to set new schemaversion: ", e
);
148 throw new SQLException(e
);
153 protected abstract List
<ISchemaUpdaterStep
> getUpdaterList();
155 protected boolean isAfterMyStartVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
157 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, startSchemaVersion
, depth
, monitor
);
158 return compareResult
> 0;
161 protected boolean isBeforeMyStartVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
163 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, startSchemaVersion
, depth
, monitor
);
164 return compareResult
< 0;
166 protected boolean isAfterMyTargetVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
168 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, targetSchemaVersion
, depth
, monitor
);
169 return compareResult
> 0;
172 protected boolean isBeforeMyTargetVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
174 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, targetSchemaVersion
, depth
, monitor
);
175 return compareResult
< 0;
179 protected String
getCurrentVersion(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
180 int intSchemaVersion
= 0;
181 String sqlSchemaVersion
= "SELECT value FROM CdmMetaData WHERE propertyname = " + intSchemaVersion
;
183 String value
= (String
)datasource
.getSingleValue(sqlSchemaVersion
);
185 } catch (SQLException e
) {
186 monitor
.warning("Error when trying to receive schemaversion: ", e
);
193 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getNextUpdater()
196 public abstract ISchemaUpdater
getNextUpdater();
199 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getPreviousUpdater()
202 public abstract ISchemaUpdater
getPreviousUpdater();
205 public String
getTargetVersion() {
206 return this.targetSchemaVersion
;