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.monitor.IProgressMonitor;
|
19
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
20
|
import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
|
21
|
|
22
|
/**
|
23
|
* @author a.mueller
|
24
|
* @date 10.09.2010
|
25
|
*
|
26
|
*/
|
27
|
public abstract class TermUpdaterBase extends UpdaterBase<ITermUpdaterStep, ITermUpdater> 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
|
protected TermUpdaterBase(String startTermVersion, String targetTermVersion){
|
33
|
this.startVersion = startTermVersion;
|
34
|
this.targetVersion = targetTermVersion;
|
35
|
list = getUpdaterList();
|
36
|
}
|
37
|
|
38
|
@Override
|
39
|
protected boolean updateVersion(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
|
40
|
int intSchemaVersion = 1;
|
41
|
String sqlUpdateSchemaVersion = "UPDATE %s SET value = '%s' WHERE propertyname = " + intSchemaVersion;
|
42
|
sqlUpdateSchemaVersion = String.format(sqlUpdateSchemaVersion, caseType.transformTo("CdmMetaData"), this.targetVersion);
|
43
|
try {
|
44
|
int n = datasource.executeUpdate(sqlUpdateSchemaVersion);
|
45
|
return n > 0;
|
46
|
} catch (Exception e) {
|
47
|
monitor.warning("Error when trying to set new schemaversion: ", e);
|
48
|
throw new SQLException(e);
|
49
|
}
|
50
|
}
|
51
|
|
52
|
protected abstract List<ITermUpdaterStep> getUpdaterList();
|
53
|
|
54
|
|
55
|
@Override
|
56
|
public boolean invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws Exception{
|
57
|
String currentLibrarySchemaVersion = CdmMetaData.getTermsVersion();
|
58
|
return invoke(currentLibrarySchemaVersion, datasource, monitor, caseType);
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
protected String getCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
|
63
|
int intSchemaVersion = 1;
|
64
|
try {
|
65
|
String sqlCount = "SELECT count(*) FROM @@CdmMetaData@@ WHERE propertyname = " + intSchemaVersion;
|
66
|
Long count = (Long)datasource.getSingleValue(caseType.replaceTableNames(sqlCount));
|
67
|
if (count == 0){
|
68
|
String defaultVersion = "2.4.2.2.201006011715";
|
69
|
String sqlMaxId = caseType.replaceTableNames("SELECT max(id) FROM @@CdmMetaData@@");
|
70
|
Integer maxId = (Integer)datasource.getSingleValue(sqlMaxId) + 1;
|
71
|
String sqlUpdate = "INSERT INTO @@CdmMetaData@@ (id, created, uuid, propertyname, value) VALUES (" + maxId + ", '2010-09-21 13:52:54', '"+UUID.randomUUID()+"', 1, '" + defaultVersion + "')";
|
72
|
datasource.executeUpdate(caseType.replaceTableNames(sqlUpdate));
|
73
|
return defaultVersion;
|
74
|
}else{
|
75
|
String sqlSchemaVersion = "SELECT value FROM @@CdmMetaData@@ WHERE propertyname = " + intSchemaVersion;
|
76
|
String value = (String)datasource.getSingleValue(caseType.replaceTableNames(sqlSchemaVersion));
|
77
|
return value;
|
78
|
}
|
79
|
} catch (SQLException e) {
|
80
|
monitor.warning("Error when trying to receive schemaversion: ", e);
|
81
|
throw e;
|
82
|
}
|
83
|
}
|
84
|
|
85
|
|
86
|
|
87
|
}
|