Project

General

Profile

Download (7.57 KB) Statistics
| Branch: | Tag: | Revision:
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.Map;
14

    
15
import org.apache.commons.lang.StringUtils;
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
19
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
20
import eu.etaxonomy.cdm.database.ICdmDataSource;
21

    
22
/**
23
 * This class represents one step in a schema update.
24
 * @author a.mueller
25
 * @since 13.09.2010
26
 *
27
 */
28
public class SimpleSchemaUpdaterStep extends SchemaUpdaterStepBase {
29
	private static final Logger logger = Logger.getLogger(SimpleSchemaUpdaterStep.class);
30

    
31
	private final Map<DatabaseTypeEnum, String> queryMap = new HashMap<>();
32
	private final Map<DatabaseTypeEnum, String> auditQueryMap = new HashMap<>();
33

    
34
	private boolean includeAudit = false;
35
//	private String tableName;
36

    
37
// *************************** FACTORY ********************************/
38

    
39
	/**
40
     * Simple schema updater with update query only for non_AUD tables.
41
	 *
42
	 * @param stepName step name
43
	 * @param defaultQuery the query
44
	 * @param adapt preliminary
45
	 * @return
46
	 */
47
	public static SimpleSchemaUpdaterStep NewNonAuditedInstance(String stepName, String defaultQuery, int adapt){
48
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, false, null, null);
49
	}
50

    
51
	/**
52
	 * Simple schema updater with update query for AUD and non_AUD tables.
53
     *
54
     * @param stepName Step name
55
	 * @param defaultQuery query
56
	 * @param nonAuditedTableName the name of the non audited table. E.g. TaxonName
57
	 *     (while TaxonName_AUD is the audited table
58
	 * @param adapt preliminary
59
	 * @return
60
	 */
61
	public static SimpleSchemaUpdaterStep NewAuditedInstance(String stepName, String defaultQuery, String nonAuditedTableName, int adapt){
62
		boolean audit = StringUtils.isNotBlank(nonAuditedTableName);
63
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, audit, nonAuditedTableName, null);
64
	}
65

    
66
	/**
67
	 * Simple schema updater with an explicit query for AUD table.
68
	 * @param stepName step name
69
	 * @param defaultQuery the non_AUD update query
70
	 * @param defaultQueryForAuditedTables the AUD update query
71
	 * @param adapt preliminary
72
	 * @return
73
	 */
74
	public static SimpleSchemaUpdaterStep NewExplicitAuditedInstance(String stepName, String defaultQuery, String defaultQueryForAuditedTables, int adapt){
75
		boolean audit = StringUtils.isNotBlank(defaultQueryForAuditedTables);
76
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, audit, null, defaultQueryForAuditedTables);
77
	}
78

    
79

    
80
//************************ CONSTRUCTOR ***********************************/
81

    
82
	private SimpleSchemaUpdaterStep(String stepName, String defaultQuery, boolean includeAudit, String tableName, String defaultQueryForAuditedTables){
83
		super(stepName);
84
		this.includeAudit = includeAudit;
85
		queryMap.put(null, defaultQuery);
86

    
87
		if (includeAudit){
88
			if (StringUtils.isNotBlank(defaultQueryForAuditedTables)){
89
				auditQueryMap.put(null, defaultQueryForAuditedTables);
90
			}else if (StringUtils.isNotBlank(tableName)){
91
				setDefaultAuditing(tableName);
92
			}
93
		}
94
	}
95

    
96
// *************************** INVOKE *****************************
97

    
98

    
99

    
100
    @Override
101
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
102
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
103

    
104
		//non audit
105
		invokeQueryMap(datasource, queryMap, caseType, result);
106
		//audit
107
		if (this.includeAudit){
108
			invokeQueryMap(datasource, auditQueryMap, caseType, result);
109
		}else{
110
			logger.info("SimpleSchemaUpdaterStep non Audited");
111
		}
112

    
113
		return;
114
	}
115

    
116
	private void invokeQueryMap(ICdmDataSource datasource, Map<DatabaseTypeEnum, String> queryMap, CaseType caseType, SchemaUpdateResult result) {
117
		String query = queryMap.get(datasource.getDatabaseType());
118
		if (query == null){
119
			query = queryMap.get(null);
120
		}
121
		if (query != null){
122
			query = doReplacements(query, caseType, datasource);
123
			executeQuery(datasource, query, result);
124
		}else{
125
		    result.addError("No query found to execute " + getStepName());
126
		}
127
		return;
128
	}
129

    
130
	private String doReplacements(String query, CaseType caseType, ICdmDataSource datasource) {
131
		query = caseType.replaceTableNames(query);
132
		query = query.replaceAll("@FALSE@", getBoolean(false, datasource));
133
		query = query.replaceAll("@TRUE@", getBoolean(true, datasource));
134
		return query;
135
	}
136

    
137
	private boolean executeQuery(ICdmDataSource datasource,  String replacedQuery, SchemaUpdateResult result) {
138
		try {
139
			datasource.executeUpdate(replacedQuery);
140
		} catch (SQLException e) {
141
			logger.error(e);
142
			result.addException(e, "Unexpected SQL Exception", getStepName());
143
			return false;
144
		}
145
		return true;
146
	}
147

    
148
	private void makeAuditedQuery(DatabaseTypeEnum dbType, String tableName, boolean addTable){
149
		String auditQuery = addTable? auditQueryMap.get(dbType) : queryMap.get(dbType);
150
		if (StringUtils.isBlank(auditQuery)){
151
			throw new IllegalArgumentException("Non-audit query must not be blank");
152
		}
153
	    auditQuery = auditQuery.replace("@@" + tableName + "@@", "@@" + tableName + "_AUD@@");
154
		//TODO warning if nothing changed
155
		this.auditQueryMap.put(dbType, auditQuery);
156
		this.includeAudit = true;
157
	}
158

    
159
//********************************* DELEGATES *********************************/
160

    
161
	/**
162
	 * For certain database types one may define special queries.<BR>
163
	 * Don't forget to put case-mask (@@) for table names and also
164
	 * add AUD query if required.
165
	 * @param dbType database type
166
	 * @param query query to use for the given database type.
167
	 * @return this schema updater step
168
     * @see #putAudited(DatabaseTypeEnum, String)
169
	 */
170
	public SimpleSchemaUpdaterStep put(DatabaseTypeEnum dbType, String query) {
171
		queryMap.put(dbType, query);
172
		return this;
173
	}
174

    
175
	/**
176
     * For certain database types one may define special queries.
177
     * This is for the AUD query.<BR>
178
     * Don't forget to put case-mask (@@) for table names
179
     * @param dbType database type
180
     * @param query query to use for the given database type.
181
     * @return this schema updater step
182
     * @see #put(DatabaseTypeEnum, String)
183
     */
184
    public SimpleSchemaUpdaterStep putAudited(DatabaseTypeEnum dbType, String query) {
185
        auditQueryMap.put(dbType, query);
186
        return this;
187
    }
188

    
189
	/**
190
	 * Defines the non audited table name for computing the audited query.
191
	 * @param nonAuditedTableName uncased table name that is to be audited
192
	 * @return the step
193
	 */
194
	public SimpleSchemaUpdaterStep setDefaultAuditing(String nonAuditedTableName){
195
		if (StringUtils.isBlank(nonAuditedTableName)){
196
			throw new IllegalArgumentException("TableName must not be blank");
197
		}
198
		makeAuditedQuery(null, nonAuditedTableName, false);
199
		return this;
200
	}
201

    
202
	 /**
203
     * Defines a further non audited table name for computing the audited query.
204
     * Requires at least one non audited table name to be defined already.
205
     * @param nonAuditedTableName non-cased table name that is to be audited
206
     * @return the step
207
     */
208
    public SimpleSchemaUpdaterStep addDefaultAuditing(String nonAuditedTableName){
209
        if (StringUtils.isBlank(nonAuditedTableName)){
210
            throw new IllegalArgumentException("TableName must not be blank");
211
        }
212
        makeAuditedQuery(null, nonAuditedTableName, true);
213
        return this;
214
    }
215

    
216
}
(23-23/35)