Project

General

Profile

Download (6.27 KB) Statistics
| Branch: | Tag: | Revision:
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.HashMap;
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
 * @date 13.09.2010
27
 *
28
 */
29
public class SimpleSchemaUpdaterStep extends SchemaUpdaterStepBase<SimpleSchemaUpdaterStep> 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 use  {@link #NewNonAuditedInstance(String, String)},
42
	 * {@link #NewAuditedInstance(String, String, boolean, String)},
43
	 * or {@link #NewExplicitAuditedInstance(String, String, String)} instead
44
	 */
45
	@Deprecated
46
	public static SimpleSchemaUpdaterStep NewInstance(String stepName, String defaultQuery, int adapt){
47
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, false, null, null);
48
	}
49

    
50
	public static SimpleSchemaUpdaterStep NewNonAuditedInstance(String stepName, String defaultQuery, int adapt){
51
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, false, null, null);
52
	}
53

    
54
	/**
55
	 * @param stepName Step name
56
	 * @param defaultQuery query
57
	 * @param nonAuditedTableName the name of the non audited table. E.g. TaxonNameBase
58
	 *     (while TaxonNameBase_AUD is the audited table
59
	 * @param adapt preliminary
60
	 * @return
61
	 */
62
	public static SimpleSchemaUpdaterStep NewAuditedInstance(String stepName, String defaultQuery, String nonAuditedTableName, int adapt){
63
		boolean audit = StringUtils.isNotBlank(nonAuditedTableName);
64
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, audit, nonAuditedTableName, null);
65
	}
66

    
67
	public static SimpleSchemaUpdaterStep NewExplicitAuditedInstance(String stepName, String defaultQuery, String defaultQueryForAuditedTables, int adapt){
68
		boolean audit = StringUtils.isNotBlank(defaultQueryForAuditedTables);
69
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, audit, null, defaultQueryForAuditedTables);
70
	}
71

    
72

    
73
//************************ CONSTRUCTOR ***********************************/
74
	private SimpleSchemaUpdaterStep(String stepName, String defaultQuery, boolean includeAudit, String tableName, String defaultQueryForAuditedTables){
75
		super(stepName);
76
		this.includeAudit = includeAudit;
77
		queryMap.put(null, defaultQuery);
78

    
79
		if (includeAudit){
80
			if (StringUtils.isNotBlank(defaultQueryForAuditedTables)){
81
				auditQueryMap.put(null, defaultQueryForAuditedTables);
82
			}else if (StringUtils.isNotBlank(tableName)){
83
				setDefaultAuditing(tableName);
84
			}
85
		}
86
	}
87

    
88
// *************************** INVOKE *****************************
89

    
90

    
91

    
92
	@Override
93
	public Integer invoke (ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType){
94
		boolean result = true;
95

    
96
		//non audit
97
		result &= invokeQueryMap(datasource, queryMap, caseType); ;
98
		//audit
99
		if (this.includeAudit){
100
			result &= invokeQueryMap(datasource, auditQueryMap, caseType);
101
		}else{
102
			logger.info("SimpleSchemaUpdaterStep non Audited");
103
		}
104

    
105
		return (result == true )? 0 : null;
106
	}
107

    
108
	private boolean invokeQueryMap(ICdmDataSource datasource, Map<DatabaseTypeEnum, String> queryMap, CaseType caseType) {
109
		boolean result = true;
110
		String query = queryMap.get(datasource.getDatabaseType());
111
		if (query == null){
112
			query = queryMap.get(null);
113
		}
114
		if (query != null){
115
			query = doReplacements(query, caseType, datasource);
116
			result = executeQuery(datasource, query);
117
		}else{
118
			//TODO exception ?
119
			logger.warn("No query found to execute");
120
		}
121
		return result;
122
	}
123

    
124
	private String doReplacements(String query, CaseType caseType, ICdmDataSource datasource) {
125
		query = caseType.replaceTableNames(query);
126
		query = query.replaceAll("@FALSE@", getBoolean(false, datasource));
127
		query = query.replaceAll("@TRUE@", getBoolean(true, datasource));
128
		return query;
129
	}
130

    
131
	private boolean executeQuery(ICdmDataSource datasource,  String replacedQuery) {
132
		try {
133
			datasource.executeUpdate(replacedQuery);
134
		} catch (SQLException e) {
135
			logger.error(e);
136
			return false;
137
		}
138
		return true;
139
	}
140

    
141
	private void makeAuditedQuery(DatabaseTypeEnum dbType, String tableName){
142
		String nonAuditQuery = this.queryMap.get(dbType);
143
		if (StringUtils.isBlank(nonAuditQuery)){
144
			throw new IllegalArgumentException("Non-audit query must not be blank");
145
		}
146
		String auditQuery = nonAuditQuery.replace("@@" + tableName + "@@", "@@" + tableName + "_AUD@@");
147
		//TODO warning if nothing changed
148
		this.auditQueryMap.put(dbType, auditQuery);
149
	}
150

    
151
//********************************* DELEGATES *********************************/
152

    
153
	/**
154
	 * For certain database types one may define special queries.<BR>
155
	 * Don't forget to put case-mask (@@) for table names
156
	 * @param dbType database type
157
	 * @param query query to use for the given database type.
158
	 * @return this schema updater step
159
	 */
160
	public SimpleSchemaUpdaterStep put(DatabaseTypeEnum dbType, String query) {
161
		queryMap.put(dbType, query);
162
		return this;
163
	}
164

    
165
	/**
166
	 * Defines the non audited table name for computing the audited query.
167
	 * @param nonAuditedTableName uncased table name that is to be audited
168
	 * @return the step
169
	 */
170
	public SimpleSchemaUpdaterStep setDefaultAuditing(String nonAuditedTableName){
171
		if (StringUtils.isBlank(nonAuditedTableName)){
172
			throw new IllegalArgumentException("TableName must not be blank");
173
		}
174
		this.includeAudit = true;
175
		makeAuditedQuery(null, nonAuditedTableName);
176
		return this;
177
	}
178

    
179
}
(21-21/34)