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.SQLException;
|
12
|
import java.util.HashMap;
|
13
|
import java.util.List;
|
14
|
import java.util.Map;
|
15
|
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.apache.log4j.Logger;
|
18
|
|
19
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
20
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
21
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
22
|
|
23
|
/**
|
24
|
* This class represents one step in a schema update.
|
25
|
* @author a.mueller
|
26
|
* @since 13.09.2010
|
27
|
*
|
28
|
*/
|
29
|
public class SimpleSchemaUpdaterStep extends SchemaUpdaterStepBase {
|
30
|
private static final Logger logger = Logger.getLogger(SimpleSchemaUpdaterStep.class);
|
31
|
|
32
|
private final Map<DatabaseTypeEnum, String> queryMap = new HashMap<>();
|
33
|
private final Map<DatabaseTypeEnum, String> auditQueryMap = new HashMap<>();
|
34
|
|
35
|
private boolean includeAudit = false;
|
36
|
// private String tableName;
|
37
|
|
38
|
// *************************** FACTORY ********************************/
|
39
|
|
40
|
/**
|
41
|
* Simple schema updater with update query only for non_AUD tables.
|
42
|
*
|
43
|
* @param stepName step name
|
44
|
* @param defaultQuery the query
|
45
|
* @param adapt preliminary
|
46
|
* @return
|
47
|
*/
|
48
|
public static SimpleSchemaUpdaterStep NewNonAuditedInstance(List<ISchemaUpdaterStep> stepList, String stepName, String defaultQuery, int adapt){
|
49
|
return new SimpleSchemaUpdaterStep(stepList, stepName, defaultQuery, false, null, null);
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* Simple schema updater with update query for AUD and non_AUD tables.
|
54
|
*
|
55
|
* @param stepName Step name
|
56
|
* @param defaultQuery query
|
57
|
* @param nonAuditedTableName the name of the non audited table. E.g. TaxonName
|
58
|
* (while TaxonName_AUD is the audited table
|
59
|
* @param adapt preliminary
|
60
|
* @return
|
61
|
*/
|
62
|
public static SimpleSchemaUpdaterStep NewAuditedInstance(List<ISchemaUpdaterStep> stepList, String stepName, String defaultQuery, String nonAuditedTableName, int adapt){
|
63
|
boolean audit = StringUtils.isNotBlank(nonAuditedTableName);
|
64
|
return new SimpleSchemaUpdaterStep(stepList, stepName, defaultQuery, audit, nonAuditedTableName, null);
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Simple schema updater with an explicit query for AUD table.
|
69
|
*
|
70
|
* @param stepName step name
|
71
|
* @param defaultQuery the non_AUD update query
|
72
|
* @param defaultQueryForAuditedTables the AUD update query
|
73
|
* @param adapt preliminary
|
74
|
* @return
|
75
|
*/
|
76
|
public static SimpleSchemaUpdaterStep NewExplicitAuditedInstance(List<ISchemaUpdaterStep> stepList, String stepName,
|
77
|
String defaultQuery, String defaultQueryForAuditedTables, int adapt){
|
78
|
boolean audit = StringUtils.isNotBlank(defaultQueryForAuditedTables);
|
79
|
return new SimpleSchemaUpdaterStep(stepList, stepName, defaultQuery, audit, null, defaultQueryForAuditedTables);
|
80
|
}
|
81
|
|
82
|
|
83
|
//************************ CONSTRUCTOR ***********************************/
|
84
|
|
85
|
private SimpleSchemaUpdaterStep(List<ISchemaUpdaterStep> stepList, String stepName, String defaultQuery,
|
86
|
boolean includeAudit, String tableName, String defaultQueryForAuditedTables){
|
87
|
|
88
|
super(stepList, stepName);
|
89
|
this.includeAudit = includeAudit;
|
90
|
queryMap.put(null, defaultQuery);
|
91
|
|
92
|
if (includeAudit){
|
93
|
if (StringUtils.isNotBlank(defaultQueryForAuditedTables)){
|
94
|
auditQueryMap.put(null, defaultQueryForAuditedTables);
|
95
|
}else if (isNotBlank(tableName)){
|
96
|
setDefaultAuditing(tableName);
|
97
|
}
|
98
|
}
|
99
|
}
|
100
|
|
101
|
// *************************** INVOKE *****************************
|
102
|
|
103
|
@Override
|
104
|
public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
|
105
|
CaseType caseType, SchemaUpdateResult result) throws SQLException {
|
106
|
|
107
|
//non audit
|
108
|
invokeQueryMap(datasource, queryMap, caseType, result);
|
109
|
//audit
|
110
|
if (this.includeAudit){
|
111
|
invokeQueryMap(datasource, auditQueryMap, caseType, result);
|
112
|
}else{
|
113
|
logger.info("SimpleSchemaUpdaterStep non Audited");
|
114
|
}
|
115
|
|
116
|
return;
|
117
|
}
|
118
|
|
119
|
private void invokeQueryMap(ICdmDataSource datasource, Map<DatabaseTypeEnum, String> queryMap, CaseType caseType, SchemaUpdateResult result) {
|
120
|
String query = queryMap.get(datasource.getDatabaseType());
|
121
|
if (query == null){
|
122
|
query = queryMap.get(null);
|
123
|
}
|
124
|
if (query != null){
|
125
|
query = doReplacements(query, caseType, datasource);
|
126
|
executeQuery(datasource, query, result);
|
127
|
}else{
|
128
|
result.addError("No query found to execute " + getStepName());
|
129
|
}
|
130
|
return;
|
131
|
}
|
132
|
|
133
|
private String doReplacements(String query, CaseType caseType, ICdmDataSource datasource) {
|
134
|
query = caseType.replaceTableNames(query);
|
135
|
query = query.replaceAll("@FALSE@", getBoolean(false, datasource));
|
136
|
query = query.replaceAll("@TRUE@", getBoolean(true, datasource));
|
137
|
return query;
|
138
|
}
|
139
|
|
140
|
private boolean executeQuery(ICdmDataSource datasource, String replacedQuery, SchemaUpdateResult result) {
|
141
|
try {
|
142
|
datasource.executeUpdate(replacedQuery);
|
143
|
} catch (SQLException e) {
|
144
|
logger.error(e);
|
145
|
result.addException(e, "Unexpected SQL Exception", getStepName());
|
146
|
return false;
|
147
|
}
|
148
|
return true;
|
149
|
}
|
150
|
|
151
|
private void makeAuditedQuery(DatabaseTypeEnum dbType, String tableName, boolean addTable){
|
152
|
String auditQuery = addTable? auditQueryMap.get(dbType) : queryMap.get(dbType);
|
153
|
if (isBlank(auditQuery)){
|
154
|
throw new IllegalArgumentException("Non-audit query must not be blank");
|
155
|
}
|
156
|
auditQuery = auditQuery.replace("@@" + tableName + "@@", "@@" + tableName + "_AUD@@");
|
157
|
//TODO warning if nothing changed
|
158
|
this.auditQueryMap.put(dbType, auditQuery);
|
159
|
this.includeAudit = true;
|
160
|
}
|
161
|
|
162
|
//********************************* DELEGATES *********************************/
|
163
|
|
164
|
/**
|
165
|
* For certain database types one may define special queries.<BR>
|
166
|
* Don't forget to put case-mask (@@) for table names and also
|
167
|
* add AUD query if required.
|
168
|
* @param dbType database type
|
169
|
* @param query query to use for the given database type.
|
170
|
* @return this schema updater step
|
171
|
* @see #putAudited(DatabaseTypeEnum, String)
|
172
|
*/
|
173
|
public SimpleSchemaUpdaterStep put(DatabaseTypeEnum dbType, String query) {
|
174
|
queryMap.put(dbType, query);
|
175
|
return this;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* For certain database types one may define special queries.
|
180
|
* This is for the AUD query.<BR>
|
181
|
* Don't forget to put case-mask (@@) for table names
|
182
|
* @param dbType database type
|
183
|
* @param query query to use for the given database type.
|
184
|
* @return this schema updater step
|
185
|
* @see #put(DatabaseTypeEnum, String)
|
186
|
*/
|
187
|
public SimpleSchemaUpdaterStep putAudited(DatabaseTypeEnum dbType, String query) {
|
188
|
auditQueryMap.put(dbType, query);
|
189
|
return this;
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* Defines the non audited table name for computing the audited query.
|
194
|
* @param nonAuditedTableName uncased table name that is to be audited
|
195
|
* @return the step
|
196
|
*/
|
197
|
public SimpleSchemaUpdaterStep setDefaultAuditing(String nonAuditedTableName){
|
198
|
if (StringUtils.isBlank(nonAuditedTableName)){
|
199
|
throw new IllegalArgumentException("TableName must not be blank");
|
200
|
}
|
201
|
makeAuditedQuery(null, nonAuditedTableName, false);
|
202
|
return this;
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* Defines a further non audited table name for computing the audited query.
|
207
|
* Requires at least one non audited table name to be defined already.
|
208
|
* @param nonAuditedTableName non-cased table name that is to be audited
|
209
|
* @return the step
|
210
|
*/
|
211
|
public SimpleSchemaUpdaterStep addDefaultAuditing(String nonAuditedTableName){
|
212
|
if (StringUtils.isBlank(nonAuditedTableName)){
|
213
|
throw new IllegalArgumentException("TableName must not be blank");
|
214
|
}
|
215
|
makeAuditedQuery(null, nonAuditedTableName, true);
|
216
|
return this;
|
217
|
}
|
218
|
|
219
|
}
|