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 mySchemaVersion;
|
30
|
protected static boolean INCLUDE_AUDIT = true;
|
31
|
|
32
|
private List<ISchemaUpdaterStep> list;
|
33
|
|
34
|
|
35
|
|
36
|
protected SchemaUpdaterBase(String mySchemaVersion){
|
37
|
this.mySchemaVersion = mySchemaVersion;
|
38
|
list = getUpdaterList();
|
39
|
}
|
40
|
|
41
|
@Override
|
42
|
public int countSteps(ICdmDataSource datasource){
|
43
|
int result = 0;
|
44
|
//TODO test if previous updater is needed
|
45
|
if (getPreviousUpdater() != null){
|
46
|
result += getPreviousUpdater().countSteps(datasource);
|
47
|
}
|
48
|
result += list.size();
|
49
|
return result;
|
50
|
}
|
51
|
|
52
|
|
53
|
/* (non-Javadoc)
|
54
|
* @see eu.etaxonomy.cdm.database.update.ICdmUpdater#invoke()
|
55
|
*/
|
56
|
@Override
|
57
|
public boolean invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws Exception{
|
58
|
boolean result = true;
|
59
|
|
60
|
String datasourceSchemaVersion;
|
61
|
try {
|
62
|
datasourceSchemaVersion = getCurrentVersion(datasource, monitor);
|
63
|
} catch (SQLException e1) {
|
64
|
monitor.warning("SQLException", e1);
|
65
|
return false;
|
66
|
}
|
67
|
|
68
|
boolean isAfterMyVersion = isAfterMyVersion(datasourceSchemaVersion, monitor);
|
69
|
if (isAfterMyVersion){
|
70
|
String warning = "Database version is higher than updater version";
|
71
|
RuntimeException exeption = new RuntimeException(warning);
|
72
|
monitor.warning(warning, exeption);
|
73
|
throw exeption;
|
74
|
}
|
75
|
|
76
|
boolean isBeforeMyVersion = isBeforeMyVersion(datasourceSchemaVersion, monitor);
|
77
|
if (isBeforeMyVersion){
|
78
|
if (getPreviousUpdater() == null){
|
79
|
String warning = "Database version is before updater version but no previous version updater exists";
|
80
|
RuntimeException exeption = new RuntimeException(warning);
|
81
|
monitor.warning(warning, exeption);
|
82
|
throw exeption;
|
83
|
}
|
84
|
result &= getPreviousUpdater().invoke(datasource, monitor);
|
85
|
}
|
86
|
|
87
|
|
88
|
for (ISchemaUpdaterStep step : list){
|
89
|
try {
|
90
|
monitor.subTask(step.getStepName());
|
91
|
Integer termId = step.invoke(datasource, monitor);
|
92
|
result &= (termId != null);
|
93
|
monitor.worked(1);
|
94
|
} catch (Exception e) {
|
95
|
// TODO Auto-generated catch block
|
96
|
monitor.warning("Exception occurred while updating schema", e);
|
97
|
throw e;
|
98
|
}
|
99
|
}
|
100
|
return result;
|
101
|
|
102
|
|
103
|
}
|
104
|
|
105
|
|
106
|
protected abstract List<ISchemaUpdaterStep> getUpdaterList();
|
107
|
|
108
|
protected boolean isAfterMyVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
|
109
|
int depth = 4;
|
110
|
int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, mySchemaVersion, depth, monitor);
|
111
|
return compareResult > 0;
|
112
|
}
|
113
|
|
114
|
protected boolean isBeforeMyVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
|
115
|
int depth = 4;
|
116
|
int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, mySchemaVersion, depth, monitor);
|
117
|
return compareResult > 0;
|
118
|
}
|
119
|
|
120
|
|
121
|
protected String getCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {
|
122
|
int intSchemaVersion = 0;
|
123
|
String sqlSchemaVersion = "SELECT value FROM CdmMetaData WHERE propertyname = " + intSchemaVersion;
|
124
|
try {
|
125
|
String value = (String)datasource.getSingleValue(sqlSchemaVersion);
|
126
|
return value;
|
127
|
} catch (SQLException e) {
|
128
|
monitor.warning("Error when trying to receive schemaversion: ", e);
|
129
|
throw e;
|
130
|
}
|
131
|
}
|
132
|
|
133
|
|
134
|
/* (non-Javadoc)
|
135
|
* @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getNextUpdater()
|
136
|
*/
|
137
|
@Override
|
138
|
public abstract ISchemaUpdater getNextUpdater();
|
139
|
|
140
|
/* (non-Javadoc)
|
141
|
* @see eu.etaxonomy.cdm.database.update.ICdmUpdater#getPreviousUpdater()
|
142
|
*/
|
143
|
@Override
|
144
|
public abstract ISchemaUpdater getPreviousUpdater();
|
145
|
}
|