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
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
18 import eu
.etaxonomy
.cdm
.common
.IProgressMonitor
;
19 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
20 import eu
.etaxonomy
.cdm
.model
.common
.CdmMetaData
;
27 public abstract class TermUpdaterBase
implements ITermUpdater
{
28 @SuppressWarnings("unused")
29 private static final Logger logger
= Logger
.getLogger(TermUpdaterBase
.class);
30 protected static final UUID uuidFeatureVocabulary
= UUID
.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");
32 private List
<SingleTermUpdater
> list
;
33 private String startTermVersion
;
34 private String targetTermVersion
;
38 protected TermUpdaterBase(String startTermVersion
, String targetTermVersion
){
39 this.startTermVersion
= startTermVersion
;
40 this.targetTermVersion
= targetTermVersion
;
41 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 currentLibraryTermVersion
= CdmMetaData
.getCurrentTermsVersion();
62 return invoke(currentLibraryTermVersion
, datasource
, monitor
);
67 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#invoke()
70 public boolean invoke(String targetVersion
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws Exception
{
71 boolean result
= true;
73 String datasourceSchemaVersion
;
75 datasourceSchemaVersion
= getCurrentVersion(datasource
, monitor
);
76 } catch (SQLException e1
) {
77 monitor
.warning("SQLException", e1
);
81 boolean isAfterMyStartVersion
= isAfterMyStartVersion(datasourceSchemaVersion
, monitor
);
82 boolean isBeforeMyStartVersion
= isBeforeMyStartVersion(datasourceSchemaVersion
, monitor
);
83 boolean isAfterMyTargetVersion
= isAfterMyTargetVersion(targetVersion
, monitor
);
84 boolean isBeforeMyTargetVersion
= isBeforeMyTargetVersion(targetVersion
, monitor
);
85 boolean isDatasourceBeforeMyTargetVersion
= isBeforeMyTargetVersion(datasourceSchemaVersion
, monitor
);
88 if (! isDatasourceBeforeMyTargetVersion
){
89 String warning
= "Target version ("+targetVersion
+") is not before updater target version ("+this.targetTermVersion
+"). Nothing to update.";
90 monitor
.warning(warning
);
94 if (isAfterMyStartVersion
){
95 String warning
= "Database version is higher than updater start 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(startTermVersion
, datasource
, monitor
);
112 if (isBeforeMyTargetVersion
){
113 String warning
= "Target version ("+targetVersion
+") is lower than updater target version ("+this.targetTermVersion
+")";
114 RuntimeException exeption
= new RuntimeException(warning
);
115 monitor
.warning(warning
, exeption
);
121 for (SingleTermUpdater step
: list
){
123 monitor
.subTask(step
.getStepName());
124 Integer stepResult
= step
.invoke(datasource
, monitor
);
125 result
&= (stepResult
!= null);
127 } catch (Exception e
) {
128 // TODO Auto-generated catch block
129 monitor
.warning("Exception occurred while updating schema", e
);
133 updateTermVersion(datasource
, monitor
);
138 private void updateTermVersion(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
139 int intSchemaVersion
= 1;
140 String sqlUpdateSchemaVersion
= "UPDATE CdmMetaData SET value = '" + this.targetTermVersion
+ "' WHERE propertyname = " + intSchemaVersion
;
142 datasource
.executeUpdate(sqlUpdateSchemaVersion
);
143 } catch (Exception e
) {
144 monitor
.warning("Error when trying to set new schemaversion: ", e
);
145 throw new SQLException(e
);
150 protected abstract List
<SingleTermUpdater
> getUpdaterList();
152 protected boolean isAfterMyStartVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
154 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, startTermVersion
, depth
, monitor
);
155 return compareResult
> 0;
158 protected boolean isBeforeMyStartVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
160 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, startTermVersion
, depth
, monitor
);
161 return compareResult
< 0;
164 protected boolean isAfterMyTargetVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
166 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, targetTermVersion
, depth
, monitor
);
167 return compareResult
> 0;
170 protected boolean isBeforeMyTargetVersion(String dataSourceSchemaVersion
, IProgressMonitor monitor
) {
172 int compareResult
= CdmMetaData
.compareVersion(dataSourceSchemaVersion
, targetTermVersion
, depth
, monitor
);
173 return compareResult
< 0;
177 protected String
getCurrentVersion(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
178 int intSchemaVersion
= 1;
180 String sqlCount
= "SELECT count(*) FROM CdmMetaData WHERE propertyname = " + intSchemaVersion
;
181 Long count
= (Long
)datasource
.getSingleValue(sqlCount
);
183 String defaultVersion
= "2.4.2.2.201006011715";
184 String sqlMaxId
= "SELECT max(id) FROM CdmMetaData";
185 Integer maxId
= (Integer
)datasource
.getSingleValue(sqlMaxId
) + 1;
186 String sqlUpdate
= "INSERT INTO CdmMetaData (id, created, uuid, propertyname, value) VALUES (" + maxId
+ ", '2010-09-21 13:52:54', '"+UUID
.randomUUID()+"', 1, '" + defaultVersion
+ "')";
187 datasource
.executeUpdate(sqlUpdate
);
188 return defaultVersion
;
190 String sqlSchemaVersion
= "SELECT value FROM CdmMetaData WHERE propertyname = " + intSchemaVersion
;
191 String value
= (String
)datasource
.getSingleValue(sqlSchemaVersion
);
194 } catch (SQLException e
) {
195 monitor
.warning("Error when trying to receive schemaversion: ", e
);
202 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getNextUpdater()
205 public abstract ITermUpdater
getNextUpdater();
208 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getPreviousUpdater()
211 public abstract ITermUpdater
getPreviousUpdater();
215 public String
getTargetVersion() {
216 return this.targetTermVersion
;