cdmlib 2.5
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / TermUpdaterBase.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 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
17
18 import eu.etaxonomy.cdm.common.IProgressMonitor;
19 import eu.etaxonomy.cdm.database.ICdmDataSource;
20 import eu.etaxonomy.cdm.model.common.CdmMetaData;
21
22 /**
23 * @author a.mueller
24 * @date 10.09.2010
25 *
26 */
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");
31
32 private List<SingleTermUpdater> list;
33 private String startTermVersion;
34 private String targetTermVersion;
35
36
37
38 protected TermUpdaterBase(String startTermVersion, String targetTermVersion){
39 this.startTermVersion = startTermVersion;
40 this.targetTermVersion = targetTermVersion;
41 list = getUpdaterList();
42 }
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 currentLibraryTermVersion = CdmMetaData.getCurrentTermsVersion();
62 return invoke(currentLibraryTermVersion, datasource, monitor);
63 }
64
65
66 /* (non-Javadoc)
67 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#invoke()
68 */
69 @Override
70 public boolean invoke(String targetVersion, ICdmDataSource datasource, IProgressMonitor monitor) throws Exception{
71 boolean result = true;
72
73 String datasourceSchemaVersion;
74 try {
75 datasourceSchemaVersion = getCurrentVersion(datasource, monitor);
76 } catch (SQLException e1) {
77 monitor.warning("SQLException", e1);
78 return false;
79 }
80
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);
86
87
88 if (! isDatasourceBeforeMyTargetVersion){
89 String warning = "Target version ("+targetVersion+") is not before updater target version ("+this.targetTermVersion+"). Nothing to update.";
90 monitor.warning(warning);
91 return true;
92 }
93
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);
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(startTermVersion, datasource, monitor);
109 }
110
111
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);
116 throw exeption;
117 }
118
119
120
121 for (SingleTermUpdater step : list){
122 try {
123 monitor.subTask(step.getStepName());
124 Integer stepResult = step.invoke(datasource, monitor);
125 result &= (stepResult != 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 result = false;
131 }
132 }
133 updateTermVersion(datasource, monitor);
134
135 return result;
136 }
137
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;
141 try {
142 datasource.executeUpdate(sqlUpdateSchemaVersion);
143 } catch (Exception e) {
144 monitor.warning("Error when trying to set new schemaversion: ", e);
145 throw new SQLException(e);
146 }
147
148 }
149
150 protected abstract List<SingleTermUpdater> getUpdaterList();
151
152 protected boolean isAfterMyStartVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
153 int depth = 4;
154 int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, startTermVersion, depth, monitor);
155 return compareResult > 0;
156 }
157
158 protected boolean isBeforeMyStartVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
159 int depth = 4;
160 int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, startTermVersion, depth, monitor);
161 return compareResult < 0;
162 }
163
164 protected boolean isAfterMyTargetVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
165 int depth = 4;
166 int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, targetTermVersion, depth, monitor);
167 return compareResult > 0;
168 }
169
170 protected boolean isBeforeMyTargetVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
171 int depth = 4;
172 int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, targetTermVersion, depth, monitor);
173 return compareResult < 0;
174 }
175
176
177 protected String getCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {
178 int intSchemaVersion = 1;
179 try {
180 String sqlCount = "SELECT count(*) FROM CdmMetaData WHERE propertyname = " + intSchemaVersion;
181 Long count = (Long)datasource.getSingleValue(sqlCount);
182 if (count == 0){
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;
189 }else{
190 String sqlSchemaVersion = "SELECT value FROM CdmMetaData WHERE propertyname = " + intSchemaVersion;
191 String value = (String)datasource.getSingleValue(sqlSchemaVersion);
192 return value;
193 }
194 } catch (SQLException e) {
195 monitor.warning("Error when trying to receive schemaversion: ", e);
196 throw e;
197 }
198 }
199
200
201 /* (non-Javadoc)
202 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getNextUpdater()
203 */
204 @Override
205 public abstract ITermUpdater getNextUpdater();
206
207 /* (non-Javadoc)
208 * @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getPreviousUpdater()
209 */
210 @Override
211 public abstract ITermUpdater getPreviousUpdater();
212
213
214 @Override
215 public String getTargetVersion() {
216 return this.targetTermVersion;
217 }
218 }