Project

General

Profile

Download (19.4 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.v25_30;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.util.UUID;
14

    
15
import org.apache.log4j.Logger;
16

    
17
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
18
import eu.etaxonomy.cdm.database.ICdmDataSource;
19
import eu.etaxonomy.cdm.database.update.CaseType;
20
import eu.etaxonomy.cdm.database.update.ISchemaUpdaterStep;
21
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
22
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
23

    
24
/**
25
 * @author a.mueller
26
 \* @since 16.09.2010
27
 *
28
 */
29
public class PolytomousKeyDataMover extends SchemaUpdaterStepBase implements ISchemaUpdaterStep {
30
	private static final Logger logger = Logger.getLogger(PolytomousKeyDataMover.class);
31

    
32
	private String featureTreeTableName;
33
	private String featureNodeTableName;
34
	private String polytomousKeyTableName;
35
	private String polytomousKeyNodeTableName;
36
	private boolean includeAudTable;
37

    
38
	public static final PolytomousKeyDataMover NewInstance(String stepName, boolean includeAudTable){
39
		return new PolytomousKeyDataMover(stepName, includeAudTable);
40
	}
41

    
42
	protected PolytomousKeyDataMover(String stepName,  boolean includeAudTable) {
43
		super(stepName);
44
		this.featureTreeTableName = "FeatureTree";
45
		this.featureNodeTableName = "FeatureNode";
46
		this.polytomousKeyTableName = "PolytomousKey";
47
		this.polytomousKeyNodeTableName = "PolytomousKeyNode";
48
		this.includeAudTable = includeAudTable;
49
	}
50

    
51

    
52
    @Override
53
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
54
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
55
        try {
56
			movePolytomousKeys(caseType.transformTo(featureTreeTableName),
57
			        caseType.transformTo(featureNodeTableName),
58
			        caseType.transformTo(polytomousKeyTableName),
59
			        caseType.transformTo(polytomousKeyNodeTableName),
60
			        datasource, monitor, false, caseType, result);
61
			if (includeAudTable){
62
				String aud = "_AUD";
63
				movePolytomousKeys(caseType.transformTo(featureTreeTableName + aud),
64
				        caseType.transformTo(featureNodeTableName + aud),
65
				        caseType.transformTo(polytomousKeyTableName + aud),
66
				        caseType.transformTo(polytomousKeyNodeTableName + aud),
67
				        datasource, monitor, true, caseType, result);
68
			}
69
			return;
70
		} catch (Exception e) {
71
			monitor.warning(e.getMessage(), e);
72
			logger.error(e.getMessage());
73
			result.addException(e, e.getMessage(), "PolytomousKeyDataMover.invoke");
74
		}
75
	}
76

    
77
	private void movePolytomousKeys(String featureTreeTableName, String featureNodeTableName, String polytomousKeyTableName,
78
	        String polytomousKeyNodeTableName, ICdmDataSource datasource, IProgressMonitor monitor,
79
	        boolean isAudit, CaseType caseType, SchemaUpdateResult result) throws SQLException {
80
		movePolytomousKey(featureTreeTableName, polytomousKeyTableName, datasource, isAudit, result);
81
		movePolytomousKeyMns(featureTreeTableName, polytomousKeyTableName, datasource, isAudit, caseType, result);
82
		movePolytomousKeyNodes(featureTreeTableName, featureNodeTableName, polytomousKeyNodeTableName, datasource, isAudit, result);
83

    
84
		moveQuestions(featureNodeTableName, polytomousKeyNodeTableName, datasource, isAudit, caseType, result);
85

    
86
		deleteOldData(datasource, isAudit, caseType, result);
87
		return;
88
	}
89

    
90

    
91
	private void moveQuestions(String featureNodeTableName, String polytomousKeyNodeTableName, ICdmDataSource datasource, boolean isAudit,
92
	        CaseType caseType, SchemaUpdateResult result) throws SQLException {
93
		String aud = "";
94
		String audValue = "";
95
		String audParam = "";
96
		if (isAudit){
97
			aud = "_AUD";
98
			audValue = ",@REV, @revtype";
99
			audParam = ", REV, revtype";
100
		}
101
		//For each Question
102
		String questionSql = " SELECT * " +
103
			" FROM @@FeatureNode_Representation@_aud@@ mn INNER JOIN Representation@_aud r ON mn.questions_id = r.id ";
104
		questionSql = questionSql.replace("@_aud", aud);
105
		ResultSet rs = datasource.executeQuery(caseType.replaceTableNames(questionSql));
106
		while (rs.next()){
107

    
108
			//Created KeyStatement
109
			String updateQuery = " INSERT INTO @@KeyStatement@_aud@@ (id, uuid, created, updated, createdby_id, updatedby_id @audParam)" +
110
				" VALUES (@id, @uuid, @createdWhen, @updatedWhen, @createdby_id, @updatedby_id @audValue)";
111
			updateQuery = updateQuery.replace("@audValue", audValue);
112
			updateQuery = updateQuery.replace("@id", rs.getObject("FeatureNode_id").toString()); //use feature node id for key statement id
113
			updateQuery = updateQuery.replace("@uuid", UUID.randomUUID().toString()); //use random uuid
114
			updateQuery = updateQuery.replace("@createdWhen", nullSafeString(rs.getString("created")));
115
			updateQuery = updateQuery.replace("@updatedWhen", nullSafeString((rs.getString("updated"))));
116
			updateQuery = updateQuery.replace("@createdby_id", nullSafe(rs.getObject("createdby_id")));
117
			updateQuery = updateQuery.replace("@updatedby_id", nullSafe(rs.getObject("updatedby_id")));
118
			if (isAudit){
119
				updateQuery = updateQuery.replace("@REV", nullSafe(rs.getObject("r.REV")));
120
				updateQuery = updateQuery.replace("@revtype", nullSafe(rs.getObject("r.revtype")));
121
			}
122
			updateQuery = updateQuery.replace("@_aud", aud);
123
			updateQuery = updateQuery.replace("@audParam", audParam);
124
			try {
125
				datasource.executeUpdate(caseType.replaceTableNames(updateQuery));
126
			} catch (SQLException e) {
127
				logger.error(e);
128
				result.addException(e);;
129
			}
130

    
131
			//create entry in Language String
132
			updateQuery = " INSERT INTO @@LanguageString@_aud@@ (id, created, uuid, updated, text, createdby_id, updatedby_id, language_id @audParam) " +
133
				" VALUES (@id, @createdWhen, @uuid, @updatedWhen, @text, @createdby_id, @updatedby_id, @language_id @audValue)";
134
			updateQuery = updateQuery.replace("@audValue", audValue);
135
			updateQuery = updateQuery.replace("@id", rs.getObject("id").toString());
136
			updateQuery = updateQuery.replace("@createdWhen", nullSafeString(rs.getString("created")));
137
			updateQuery = updateQuery.replace("@updatedWhen", nullSafeString(rs.getString("updated")));
138
			updateQuery = updateQuery.replace("@createdby_id", nullSafe(rs.getObject("createdby_id")));
139
			updateQuery = updateQuery.replace("@updatedby_id", nullSafe(rs.getObject("updatedby_id")));
140
			updateQuery = updateQuery.replace("@uuid", nullSafeString(rs.getString("uuid")));
141
			updateQuery = updateQuery.replace("@text", nullSafeString(rs.getString("text")));
142
			updateQuery = updateQuery.replace("@language_id", nullSafe(rs.getObject("language_id")));
143
			if (isAudit){
144
				updateQuery = updateQuery.replace("@REV", nullSafe(rs.getObject("r.REV")));
145
				updateQuery = updateQuery.replace("@revtype", nullSafe(rs.getObject("r.revtype")));
146
			}
147
			updateQuery = updateQuery.replace("@_aud", aud);
148
			updateQuery = updateQuery.replace("@audParam", audParam);
149
			try {
150
				datasource.executeUpdate(caseType.replaceTableNames(updateQuery));
151
			} catch (SQLException e) {
152
				logger.error(e);
153
				result.addException(e);;
154
			}
155

    
156
			//create entry in KeyStatement_LanguageString
157
			updateQuery = " INSERT INTO @@KeyStatement_LanguageString@_aud@@ (KeyStatement_id, label_id, label_mapkey_id @audParam) " +
158
				" VALUES (@keystatement_id, @languagestring_id, @language_id @audValue) ";
159
			updateQuery = updateQuery.replace("@audValue", audValue);
160
			updateQuery = updateQuery.replace("@keystatement_id", nullSafe(rs.getObject("FeatureNode_id")));
161
			updateQuery = updateQuery.replace("@languagestring_id", nullSafe(rs.getObject("id")));
162
			updateQuery = updateQuery.replace("@language_id", nullSafe(rs.getObject("language_id")));
163
			if (isAudit){
164
				updateQuery = updateQuery.replace("@REV", nullSafe(rs.getObject("r.REV")));
165
				updateQuery = updateQuery.replace("@revtype", nullSafe(rs.getObject("r.revtype")));
166
			}
167
			updateQuery = updateQuery.replace("@_aud", aud);
168
			updateQuery = updateQuery.replace("@audParam", audParam);
169
			try {
170
				datasource.executeUpdate(caseType.replaceTableNames(updateQuery));
171
			} catch (SQLException e) {
172
				logger.error(e);
173
				result.addException(e);
174
			}
175

    
176
			//link polytomouskeynode statement to KeyStatement
177
			updateQuery = " UPDATE @@PolytomousKeyNode@_aud@@ " +
178
					" SET statement_id = id " +
179
					" WHERE id = @id ";
180
			updateQuery = updateQuery.replace("@id", nullSafe(rs.getObject("FeatureNode_id")));
181
			updateQuery = updateQuery.replace("@_aud", aud);
182
			try {
183
				datasource.executeUpdate(caseType.replaceTableNames(updateQuery));
184
			} catch (SQLException e) {
185
				logger.error(e);
186
				result.addException(e);
187
			}
188
		}
189
		return;
190

    
191

    
192
//		// move representations
193
//		String languageStringTable = "LanguageString" +  (isAudit ? "_AUD" : "");
194
//		String representationTable = "Representation" + (isAudit ? "_AUD" : "");
195
//		String oldMnTable = "featuretree_representation" + (isAudit ? "_AUD" : "");
196
//		String newMnTable = "KeyStatement_LanguageString" + (isAudit ? "_AUD" : "");
197
//		String keyStatementTable = "KeyStatement" + (isAudit ? "_AUD" : "");
198
//		String audit = "";
199
//		String rAudit = "";
200
//		if (isAudit){
201
//			audit = ", REV, revtype";
202
//			rAudit = ", r.REV, r.revtype";
203
//		}
204
//
205
//
206
//		String updateQuery = "INSERT INTO @languageStringTable (id, created, uuid, updated, text, createdby_id, updatedby_id, language_id @audit) " +
207
//				" SELECT id, created, uuid, updated, text, createdby_id, updatedby_id, language_id @rAudit " +
208
//				" FROM @representationTable r INNER JOIN @oldMnTable fr ON fr.representations_id = r.id " +
209
//				" WHERE (1=1) ";
210
//		updateQuery = updateQuery.replace("@languageStringTable", languageStringTable);
211
//		updateQuery = updateQuery.replace("@representationTable", representationTable);
212
//		updateQuery = updateQuery.replace("@oldMnTable", oldMnTable);
213
//		updateQuery = updateQuery.replace("@audit", audit);
214
//		updateQuery = updateQuery.replace("@nAudit", rAudit);
215
//		System.out.println(updateQuery);
216
//		datasource.executeUpdate(updateQuery);
217
//
218
//		//key statement
219
//		audit = "";
220
//		rAudit = "";
221
//		updateQuery = "INSERT INTO @keyStatementTable (id, created, uuid, updated, createdby_id, updatedby_id @audit) " +
222
//			" SELECT r.id, r.created, r.uuid, r.updated, r.createdby_id, r.updatedby_id @rAudit " +
223
//			" FROM @oldMnTable mn INNER JOIN representationTable r ";
224
//		updateQuery = updateQuery.replace("@keyStatementTable", keyStatementTable);
225
//		updateQuery = updateQuery.replace("@oldMnTable", oldMnTable);
226
//		updateQuery = updateQuery.replace("@audit", audit);
227
//		updateQuery = updateQuery.replace("@rAudit", rAudit);
228
//
229
//		System.out.println(updateQuery);
230
//		datasource.executeUpdate(updateQuery);
231
//
232
//
233
//		//move relation
234
//		audit = "";
235
//		updateQuery = "INSERT INTO @newMnTable (KeyStatement_id, label_id, label_mapkey_id @audit) " +
236
//			" SELECT FeatureNode_id, questions_id @audit, language_id " +
237
//			" FROM @oldMnTable fr INNER JOIN @representationTable r " +
238
//			" WHERE (1=1) ";
239
//		updateQuery = updateQuery.replace("@audit", audit);
240
//		updateQuery = updateQuery.replace("@representationTable", representationTable);
241
//		updateQuery = updateQuery.replace("@oldMnTable", oldMnTable);
242
//		updateQuery = updateQuery.replace("@newMnTable", newMnTable);
243
//		System.out.println(updateQuery);
244
//		datasource.executeUpdate(updateQuery);
245
//
246
//		//link polytoumous key node statement to keyStatement
247
//		updateQuery = "UPDATE @polytomousKeyNodeTable pkn" +
248
//			" SET statement_id = (SELECT r.id FROM @representationTable r WHERE r.id = pkn.statement_id) " +
249
//			" WHERE pkn.id ";
250

    
251
	}
252

    
253
	private String nullSafeString(Object object){
254
		if (object == null){
255
			return "NULL";
256
		}else{
257
			String result = object.toString().replace("'", "''");
258
			return "'" + result + "'";
259
		}
260
	}
261

    
262
	private String nullSafe(Object object) {
263
		if (object == null){
264
			return "NULL";
265
		}else{
266
			return object.toString();
267
		}
268
	}
269

    
270
	private void deleteOldData(ICdmDataSource datasource, boolean isAudit,
271
	        CaseType caseType, SchemaUpdateResult result) {
272
		String updateQuery;
273
		String featureNodeTable = caseType.transformTo("FeatureNode" +  (isAudit ? "_AUD" : ""));
274
		String featureTreeTable = caseType.transformTo("FeatureTree" + (isAudit ? "_AUD" : ""));
275
		String representationTable = caseType.transformTo("Representation" + (isAudit ? "_AUD" : ""));
276
		String oldMnTable = caseType.transformTo("FeatureNode_Representation" + (isAudit ? "_AUD" : ""));
277

    
278
//		statements
279
		updateQuery = " DELETE FROM @representationTable WHERE id IN (SELECT questions_id FROM @oldMnTable)";
280
		updateQuery = updateQuery.replace("@representationTable", representationTable);
281
		updateQuery = updateQuery.replace("@oldMnTable", oldMnTable);
282
		logger.debug(updateQuery);
283
		try {
284
			datasource.executeUpdate(updateQuery);
285
		} catch (SQLException e) {
286
			logger.error(e);
287
			result.addException(e);
288
		}
289

    
290
//		feature nodes
291
		updateQuery = " DELETE FROM @featureNodeTable WHERE featuretree_id IN (SELECT t.id FROM @featureTreeTable t WHERE t.DTYPE = 'PolytomousKey' )";
292
		updateQuery = updateQuery.replace("@featureNodeTable", featureNodeTable);
293
		updateQuery = updateQuery.replace("@featureTreeTable", featureTreeTable);
294
		logger.debug(updateQuery);
295
		try {
296
			datasource.executeUpdate(updateQuery);
297
		} catch (SQLException e) {
298
			logger.error(e);
299
            result.addException(e);
300
		}
301

    
302
		//trees
303
		updateQuery = " DELETE FROM @featureTreeTable WHERE DTYPE = 'PolytomousKey' " ;
304
		updateQuery = updateQuery.replace("@featureTreeTable", featureTreeTable);
305
		logger.debug(updateQuery);
306
		try {
307
			datasource.executeUpdate(updateQuery);
308
		} catch (SQLException e) {
309
			logger.error(e);
310
            result.addException(e);
311
		}
312
		return;
313

    
314

    
315
	}
316

    
317
	private void movePolytomousKeyNodes(String featureTreeTableName, String featureNodeTableName,
318
			String polytomousKeyNodeTableName, ICdmDataSource datasource, boolean isAudit,
319
			SchemaUpdateResult result) {
320
		String updateQuery;
321

    
322
		//PolytomousKey node
323
		updateQuery = " INSERT INTO @polytomousKeyNodeTableName(id, created, uuid, updated, sortindex, createdby_id, updatedby_id, feature_id, parent_id, taxon_id, key_id @audit) " +
324
				" SELECT n.id, n.created, n.uuid, n.updated, n.sortindex, n.createdby_id, n.updatedby_id, n.feature_id, n.parent_fk, n.taxon_id, n.featuretree_id @nAudit" +
325
				" FROM @featureNodeTableName n INNER JOIN @featureTreeTableName t ON n.featuretree_id = t.id" +
326
				" WHERE t.DTYPE = 'PolytomousKey'";
327
		updateQuery = updateQuery.replace("@polytomousKeyNodeTableName", polytomousKeyNodeTableName);
328
		updateQuery = updateQuery.replace("@featureNodeTableName", featureNodeTableName);
329
		updateQuery = updateQuery.replace("@featureTreeTableName", featureTreeTableName);
330

    
331
		String audit = "";
332
		String nAudit = "";
333
		if (isAudit){
334
			audit = ", REV, revtype";
335
			nAudit = ", n.REV, n.revtype";
336
		}
337
		updateQuery = updateQuery.replace("@audit", audit);
338
		updateQuery = updateQuery.replace("@nAudit", nAudit);
339
		logger.debug(updateQuery);
340
		try {
341
			datasource.executeUpdate(updateQuery);
342
		} catch (SQLException e) {
343
			logger.error(e);
344
			result.addException(e);
345
		}
346
		return;
347
	}
348

    
349
	private void  movePolytomousKeyMns(String featureTreeTableName, String polytomousKeyTableName,
350
	        ICdmDataSource datasource, boolean isAudit, CaseType caseType, SchemaUpdateResult result) {
351
		//PolytomousKey MN update
352
		updateMnTables(featureTreeTableName, polytomousKeyTableName, "Annotation", null, datasource, isAudit, false, caseType, result);
353
		updateMnTables(featureTreeTableName, polytomousKeyTableName, "Credit", null, datasource, isAudit, true, caseType, result);
354
		updateMnTables(featureTreeTableName, polytomousKeyTableName, "Extension", null, datasource, isAudit, false, caseType, result);
355
		updateMnTables(featureTreeTableName, polytomousKeyTableName, "Marker", null, datasource, isAudit, false, caseType, result);
356
		updateMnTables(featureTreeTableName, polytomousKeyTableName, "OriginalSourceBase", "Sources", datasource, isAudit, false, caseType, result);
357
		updateMnTables(featureTreeTableName, polytomousKeyTableName, "Rights", "Rights", datasource, isAudit, false, caseType, result);
358
		return;
359
	}
360

    
361
	private void movePolytomousKey(String featureTreeTableName, String polytomousKeyTableName,
362
			ICdmDataSource datasource, boolean isAudit, SchemaUpdateResult result) {
363
		//PolytomousKey
364
		//TODO monitor polytomous keys with uri for data loss
365

    
366
		String updateQuery = " INSERT INTO @polytomousKeyTableName(id, created, uuid, updated, lsid_authority, lsid_lsid, lsid_namespace, lsid_object, lsid_revision, protectedtitlecache, titleCache, createdby_id, updatedby_id, root_id @audit) " +
367
					" SELECT id, created, uuid, updated, lsid_authority, lsid_lsid, lsid_namespace, lsid_object, lsid_revision, protectedtitlecache, titleCache, createdby_id, updatedby_id, root_id @audit" +
368
					" FROM @featureTreeTableName WHERE DTYPE = 'PolytomousKey'";
369
		updateQuery = updateQuery.replace("@polytomousKeyTableName", polytomousKeyTableName);
370
		updateQuery = updateQuery.replace("@featureTreeTableName", featureTreeTableName);
371
		String audit = "";
372
		if (isAudit){
373
			audit = ", REV, revtype";
374
		}
375
		updateQuery = updateQuery.replace("@audit", audit);
376
		try {
377
			datasource.executeUpdate(updateQuery);
378
		} catch (SQLException e) {
379
			logger.error(e);
380
			result.addException(e, e.getMessage(), "PolytomousKeyDataMover.movePolytomousKey");
381

    
382
		}
383
		return;
384
	}
385

    
386
	private void updateMnTables(String featureTreeTableName, String polytomousKeyTableName,
387
	        String attributeName, String attributePluralString, ICdmDataSource datasource,
388
	        boolean isAudit, boolean hasSortIndex, CaseType caseType, SchemaUpdateResult result) {
389

    
390
	    String updateQuery;
391
		String audit;
392
		if (isAudit){
393
			featureTreeTableName = featureTreeTableName.replace(caseType.transformTo("_AUD"), "");
394
			polytomousKeyTableName = polytomousKeyTableName.replace(caseType.transformTo("_AUD"), "");
395
		}
396
		String newMnTable = caseType.transformTo(polytomousKeyTableName + "_" + attributeName + (isAudit? "_AUD" : ""));
397
		String oldMnTable = caseType.transformTo(featureTreeTableName + "_" + attributeName + (isAudit? "_AUD" : ""));
398
		String pluralIdAttribute = ((attributePluralString == null) ? attributeName + "s" : attributePluralString)  + "_id";
399
		String sortIndex = (hasSortIndex ? ", sortIndex" : "");
400
		updateQuery = " INSERT INTO @newMnTable(PolytomousKey_id, @pluralIdAttribute @sortIndex @audit) " +
401
					" SELECT FeatureTree_id, @pluralIdAttribute @sortIndex @audit" +
402
					" FROM @oldMnTable mn INNER JOIN @featureTreeTableName pk ON mn.FeatureTree_id = pk.id " +
403
					" WHERE pk.DTYPE = 'PolytomousKey'";
404
		updateQuery = updateQuery.replace("@polytomousKeyTableName", polytomousKeyTableName);
405
		updateQuery = updateQuery.replace("@featureTreeTableName", featureTreeTableName);
406
		updateQuery = updateQuery.replace("@newMnTable", newMnTable);
407
		updateQuery = updateQuery.replace("@oldMnTable", oldMnTable);
408
		updateQuery = updateQuery.replace("@pluralIdAttribute", pluralIdAttribute);
409
		updateQuery = updateQuery.replace("@sortIndex", sortIndex);
410
		audit = "";
411
		if (isAudit){
412
			audit = ", REV, revtype";
413
		}
414
		updateQuery = updateQuery.replace("@audit", audit);
415
		logger.debug(updateQuery);
416
		try {
417
			datasource.executeUpdate(updateQuery);
418
		} catch (SQLException e) {
419
			logger.error(e);
420
			result.addException(e);
421
		}
422
		return;
423
	}
424
}
(2-2/4)