1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.database.update;
|
10
|
|
11
|
import java.sql.ResultSet;
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.apache.commons.lang.StringUtils;
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.joda.time.DateTime;
|
20
|
|
21
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
22
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
23
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
24
|
import eu.etaxonomy.cdm.model.common.Language;
|
25
|
|
26
|
/**
|
27
|
* @author a.mueller
|
28
|
* @since 13.09.2010
|
29
|
*
|
30
|
*/
|
31
|
public abstract class SchemaUpdaterStepBase implements ISchemaUpdaterStep {
|
32
|
private static final Logger logger = Logger.getLogger(SchemaUpdaterStepBase.class);
|
33
|
|
34
|
protected String stepName;
|
35
|
|
36
|
private boolean ignoreErrors;
|
37
|
|
38
|
|
39
|
//************************ CONSTRUCTOR ***********************************/
|
40
|
|
41
|
protected SchemaUpdaterStepBase(String stepName){
|
42
|
this.setStepName(stepName);
|
43
|
}
|
44
|
|
45
|
|
46
|
@Override
|
47
|
public abstract void invoke (ICdmDataSource datasource, IProgressMonitor monitor,
|
48
|
CaseType caseType, SchemaUpdateResult result) throws SQLException;
|
49
|
|
50
|
|
51
|
@Override
|
52
|
public void setStepName(String stepName) {
|
53
|
this.stepName = stepName;
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
public String getStepName() {
|
58
|
return stepName;
|
59
|
}
|
60
|
|
61
|
protected String getBoolean(boolean value, ICdmDataSource datasource) {
|
62
|
|
63
|
String result;
|
64
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
65
|
//TODO use
|
66
|
// type.getHibernateDialect().toBooleanValueString(bool);
|
67
|
int intValue = value == true? 1 : 0;
|
68
|
if (type.equals(DatabaseTypeEnum.MySQL)){
|
69
|
result = "b'"+intValue+"'";
|
70
|
}else if (type.equals(DatabaseTypeEnum.PostgreSQL)){
|
71
|
result = "'"+intValue+"'";
|
72
|
}else if (type.equals(DatabaseTypeEnum.H2)){
|
73
|
result = value == true ? "TRUE" : "FALSE";
|
74
|
}else if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
75
|
logger.warn("SQLServer boolean not tested yet");
|
76
|
result = "b'"+intValue+"'";
|
77
|
}else{
|
78
|
throw new RuntimeException("Database type not supported for boolean" + type.getName());
|
79
|
}
|
80
|
return result;
|
81
|
}
|
82
|
|
83
|
protected Integer getEnglishLanguageId(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
|
84
|
return getLanguageId(Language.uuidEnglish, datasource, monitor, caseType);
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* @param uuidLanguage
|
89
|
* @param datasource
|
90
|
* @param monitor
|
91
|
* @return
|
92
|
* @throws SQLException
|
93
|
*/
|
94
|
protected Integer getLanguageId(UUID uuidLanguage, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
|
95
|
|
96
|
ResultSet rs;
|
97
|
Integer langId = null;
|
98
|
String sqlLangId = " SELECT id FROM %s WHERE uuid = '%s'";
|
99
|
sqlLangId = String.format(sqlLangId, caseType.transformTo("DefinedTermBase"), uuidLanguage.toString() );
|
100
|
rs = datasource.executeQuery(sqlLangId);
|
101
|
if (rs.next()){
|
102
|
langId = rs.getInt("id");
|
103
|
}else{
|
104
|
String warning = "Term for language (" + uuidLanguage + ") does not exist!";
|
105
|
monitor.warning(warning);
|
106
|
}
|
107
|
return langId;
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* {@inheritDoc}
|
112
|
*/
|
113
|
@Override
|
114
|
public List<ISchemaUpdaterStep> getInnerSteps(){
|
115
|
return new ArrayList<ISchemaUpdaterStep>();
|
116
|
}
|
117
|
|
118
|
@Override
|
119
|
public boolean isIgnoreErrors() {
|
120
|
return ignoreErrors;
|
121
|
}
|
122
|
|
123
|
|
124
|
@Override
|
125
|
public void setIgnoreErrors(boolean ignoreErrors) {
|
126
|
this.ignoreErrors = ignoreErrors;
|
127
|
}
|
128
|
|
129
|
|
130
|
/**
|
131
|
* Returns a time string with date and time (without millis) that
|
132
|
* can be used as a time string for database insert and update
|
133
|
* @return
|
134
|
*/
|
135
|
protected String getNowString() {
|
136
|
return DateTime.now().toString("YYYY-MM-dd HH:mm:ss");
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
public String toString(){
|
141
|
if (StringUtils.isNotBlank(stepName)){
|
142
|
return stepName;
|
143
|
}else{
|
144
|
return super.toString();
|
145
|
}
|
146
|
}
|
147
|
|
148
|
}
|