added column name changer
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / SchemaUpdaterBase.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.cdm.database.update;
11
12 import java.sql.SQLException;
13 import java.util.List;
14
15 import org.apache.log4j.Logger;
16
17 import eu.etaxonomy.cdm.common.IProgressMonitor;
18 import eu.etaxonomy.cdm.database.ICdmDataSource;
19 import eu.etaxonomy.cdm.model.common.CdmMetaData;
20
21 /**
22 * @author a.mueller
23 * @date 10.09.2010
24 *
25 */
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;
31
32
33 protected static boolean INCLUDE_AUDIT = true;
34
35 private List<ISchemaUpdaterStep> list;
36
37
38
39 protected SchemaUpdaterBase(String startSchemaVersion, String endSchemaVersion){
40 this.startSchemaVersion = startSchemaVersion;
41 this.targetSchemaVersion = endSchemaVersion;
42 list = getUpdaterList();
43 }
44
45 @Override
46 public int countSteps(ICdmDataSource datasource){
47 int result = 0;
48 //TODO test if previous updater is needed
49 if (getPreviousUpdater() != null){
50 result += getPreviousUpdater().countSteps(datasource);
51 }
52 result += list.size();
53 return result;
54 }
55
56 /* (non-Javadoc)
57 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#invoke()
58 */
59 @Override
60 public boolean invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws Exception{
61 String currentLibrarySchemaVersion = CdmMetaData.getCurrentSchemaVersion();
62 return invoke(currentLibrarySchemaVersion, datasource, monitor);
63 }
64
65 /* (non-Javadoc)
66 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#invoke()
67 */
68 @Override
69 public boolean invoke(String targetVersion, ICdmDataSource datasource, IProgressMonitor monitor) throws Exception{
70 boolean result = true;
71 String datasourceSchemaVersion;
72 try {
73 datasourceSchemaVersion = getCurrentVersion(datasource, monitor);
74 } catch (SQLException e1) {
75 monitor.warning("SQLException", e1);
76 return false;
77 }
78
79
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);
85
86
87
88 if (! isDatasourceBeforeMyTargetVersion){
89 String warning = "Target version ("+targetVersion+") is not before updater target version ("+this.targetSchemaVersion+"). Nothing to update.";
90 monitor.warning(warning);
91 return true;
92 }
93
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);
98 throw exeption;
99 }
100
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);
106 throw exeption;
107 }
108 result &= getPreviousUpdater().invoke(startSchemaVersion, datasource, monitor);
109 }
110
111
112
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);
117 throw exeption;
118 }
119
120
121 for (ISchemaUpdaterStep step : list){
122 try {
123 monitor.subTask(step.getStepName());
124 Integer termId = step.invoke(datasource, monitor);
125 result &= (termId != null);
126 monitor.worked(1);
127 } catch (Exception e) {
128 // TODO Auto-generated catch block
129 monitor.warning("Exception occurred while updating schema", e);
130 throw e;
131 }
132 }
133 updateSchemaVersion(datasource, monitor);
134
135 return result;
136
137
138 }
139
140
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;
144 try {
145 datasource.executeUpdate(sqlUpdateSchemaVersion);
146 } catch (Exception e) {
147 monitor.warning("Error when trying to set new schemaversion: ", e);
148 throw new SQLException(e);
149 }
150
151 }
152
153 protected abstract List<ISchemaUpdaterStep> getUpdaterList();
154
155 protected boolean isAfterMyStartVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
156 int depth = 4;
157 int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, startSchemaVersion, depth, monitor);
158 return compareResult > 0;
159 }
160
161 protected boolean isBeforeMyStartVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
162 int depth = 4;
163 int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, startSchemaVersion, depth, monitor);
164 return compareResult < 0;
165 }
166 protected boolean isAfterMyTargetVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
167 int depth = 4;
168 int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, targetSchemaVersion, depth, monitor);
169 return compareResult > 0;
170 }
171
172 protected boolean isBeforeMyTargetVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
173 int depth = 4;
174 int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, targetSchemaVersion, depth, monitor);
175 return compareResult < 0;
176 }
177
178
179 protected String getCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {
180 int intSchemaVersion = 0;
181 String sqlSchemaVersion = "SELECT value FROM CdmMetaData WHERE propertyname = " + intSchemaVersion;
182 try {
183 String value = (String)datasource.getSingleValue(sqlSchemaVersion);
184 return value;
185 } catch (SQLException e) {
186 monitor.warning("Error when trying to receive schemaversion: ", e);
187 throw e;
188 }
189 }
190
191
192 /* (non-Javadoc)
193 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getNextUpdater()
194 */
195 @Override
196 public abstract ISchemaUpdater getNextUpdater();
197
198 /* (non-Javadoc)
199 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getPreviousUpdater()
200 */
201 @Override
202 public abstract ISchemaUpdater getPreviousUpdater();
203
204 @Override
205 public String getTargetVersion() {
206 return this.targetSchemaVersion;
207 }
208
209 }