Project

General

Profile

Download (8.05 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
 * @date 13.09.2010
26
 *
27
 */
28
public class SimpleSchemaUpdaterStep extends SchemaUpdaterStepBase<SimpleSchemaUpdaterStep>
29
                implements ISchemaUpdaterStep, ITermUpdaterStep{
30
	private static final Logger logger = Logger.getLogger(SimpleSchemaUpdaterStep.class);
31

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

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

    
38
// *************************** FACTORY ********************************/
39

    
40
	/**
41
	 * Deprecated method
42
	 * @deprecated use  {@link #NewNonAuditedInstance(String, String)},
43
	 * {@link #NewAuditedInstance(String, String, boolean, String)},
44
	 * or {@link #NewExplicitAuditedInstance(String, String, String)} instead
45
	 */
46
	@Deprecated
47
	public static SimpleSchemaUpdaterStep NewInstance(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 only for non_AUD tables.
53
	 *
54
	 * @param stepName step name
55
	 * @param defaultQuery the query
56
	 * @param adapt preliminary
57
	 * @return
58
	 */
59
	public static SimpleSchemaUpdaterStep NewNonAuditedInstance(String stepName, String defaultQuery, int adapt){
60
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, false, null, null);
61
	}
62

    
63
	/**
64
	 * Simple schema updater with update query for AUD and non_AUD tables.
65
     *
66
     * @param stepName Step name
67
	 * @param defaultQuery query
68
	 * @param nonAuditedTableName the name of the non audited table. E.g. TaxonName
69
	 *     (while TaxonName_AUD is the audited table
70
	 * @param adapt preliminary
71
	 * @return
72
	 */
73
	public static SimpleSchemaUpdaterStep NewAuditedInstance(String stepName, String defaultQuery, String nonAuditedTableName, int adapt){
74
		boolean audit = StringUtils.isNotBlank(nonAuditedTableName);
75
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, audit, nonAuditedTableName, null);
76
	}
77

    
78
	/**
79
	 * Simple schema updater with an explicit query for AUD table.
80
	 * @param stepName step name
81
	 * @param defaultQuery the non_AUD update query
82
	 * @param defaultQueryForAuditedTables the AUD update query
83
	 * @param adapt preliminary
84
	 * @return
85
	 */
86
	public static SimpleSchemaUpdaterStep NewExplicitAuditedInstance(String stepName, String defaultQuery, String defaultQueryForAuditedTables, int adapt){
87
		boolean audit = StringUtils.isNotBlank(defaultQueryForAuditedTables);
88
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, audit, null, defaultQueryForAuditedTables);
89
	}
90

    
91

    
92
//************************ CONSTRUCTOR ***********************************/
93
	private SimpleSchemaUpdaterStep(String stepName, String defaultQuery, boolean includeAudit, String tableName, String defaultQueryForAuditedTables){
94
		super(stepName);
95
		this.includeAudit = includeAudit;
96
		queryMap.put(null, defaultQuery);
97

    
98
		if (includeAudit){
99
			if (StringUtils.isNotBlank(defaultQueryForAuditedTables)){
100
				auditQueryMap.put(null, defaultQueryForAuditedTables);
101
			}else if (StringUtils.isNotBlank(tableName)){
102
				setDefaultAuditing(tableName);
103
			}
104
		}
105
	}
106

    
107
// *************************** INVOKE *****************************
108

    
109

    
110

    
111
	@Override
112
	public Integer invoke (ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType){
113
		boolean result = true;
114

    
115
		//non audit
116
		result &= invokeQueryMap(datasource, queryMap, caseType); ;
117
		//audit
118
		if (this.includeAudit){
119
			result &= invokeQueryMap(datasource, auditQueryMap, caseType);
120
		}else{
121
			logger.info("SimpleSchemaUpdaterStep non Audited");
122
		}
123

    
124
		return (result == true )? 0 : null;
125
	}
126

    
127
	private boolean invokeQueryMap(ICdmDataSource datasource, Map<DatabaseTypeEnum, String> queryMap, CaseType caseType) {
128
		boolean result = true;
129
		String query = queryMap.get(datasource.getDatabaseType());
130
		if (query == null){
131
			query = queryMap.get(null);
132
		}
133
		if (query != null){
134
			query = doReplacements(query, caseType, datasource);
135
			result = executeQuery(datasource, query);
136
		}else{
137
			//TODO exception ?
138
			logger.warn("No query found to execute");
139
		}
140
		return result;
141
	}
142

    
143
	private String doReplacements(String query, CaseType caseType, ICdmDataSource datasource) {
144
		query = caseType.replaceTableNames(query);
145
		query = query.replaceAll("@FALSE@", getBoolean(false, datasource));
146
		query = query.replaceAll("@TRUE@", getBoolean(true, datasource));
147
		return query;
148
	}
149

    
150
	private boolean executeQuery(ICdmDataSource datasource,  String replacedQuery) {
151
		try {
152
			datasource.executeUpdate(replacedQuery);
153
		} catch (SQLException e) {
154
			logger.error(e);
155
			return false;
156
		}
157
		return true;
158
	}
159

    
160
	private void makeAuditedQuery(DatabaseTypeEnum dbType, String tableName, boolean addTable){
161
		String auditQuery = addTable? auditQueryMap.get(dbType) : queryMap.get(dbType);
162
		if (StringUtils.isBlank(auditQuery)){
163
			throw new IllegalArgumentException("Non-audit query must not be blank");
164
		}
165
	    auditQuery = auditQuery.replace("@@" + tableName + "@@", "@@" + tableName + "_AUD@@");
166
		//TODO warning if nothing changed
167
		this.auditQueryMap.put(dbType, auditQuery);
168
		this.includeAudit = true;
169
	}
170

    
171
//********************************* DELEGATES *********************************/
172

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

    
187
	/**
188
     * For certain database types one may define special queries.
189
     * This is for the AUD query.<BR>
190
     * Don't forget to put case-mask (@@) for table names
191
     * @param dbType database type
192
     * @param query query to use for the given database type.
193
     * @return this schema updater step
194
     * @see #put(DatabaseTypeEnum, String)
195
     */
196
    public SimpleSchemaUpdaterStep putAudited(DatabaseTypeEnum dbType, String query) {
197
        auditQueryMap.put(dbType, query);
198
        return this;
199
    }
200

    
201
	/**
202
	 * Defines the non audited table name for computing the audited query.
203
	 * @param nonAuditedTableName uncased table name that is to be audited
204
	 * @return the step
205
	 */
206
	public SimpleSchemaUpdaterStep setDefaultAuditing(String nonAuditedTableName){
207
		if (StringUtils.isBlank(nonAuditedTableName)){
208
			throw new IllegalArgumentException("TableName must not be blank");
209
		}
210
		makeAuditedQuery(null, nonAuditedTableName, false);
211
		return this;
212
	}
213

    
214
	 /**
215
     * Defines a further non audited table name for computing the audited query.
216
     * Requires at least one non audited table name to be defined already.
217
     * @param nonAuditedTableName non-cased table name that is to be audited
218
     * @return the step
219
     */
220
    public SimpleSchemaUpdaterStep addDefaultAuditing(String nonAuditedTableName){
221
        if (StringUtils.isBlank(nonAuditedTableName)){
222
            throw new IllegalArgumentException("TableName must not be blank");
223
        }
224
        makeAuditedQuery(null, nonAuditedTableName, true);
225
        return this;
226
    }
227

    
228
}
(23-23/36)