Project

General

Profile

Download (19.2 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.v24_30;
10

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

    
16
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
17

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

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

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

    
39
	public static final PolytomousKeyDataMover NewInstance(List<ISchemaUpdaterStep> stepList, String stepName, boolean includeAudTable){
40
		return new PolytomousKeyDataMover(stepList, stepName, includeAudTable);
41
	}
42

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

    
52

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

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

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

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

    
91

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

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

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

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

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

    
192

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

    
252
	}
253

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

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

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

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

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

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

    
315

    
316
	}
317

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

    
323
		//PolytomousKey node
324
		updateQuery = " INSERT INTO @polytomousKeyNodeTableName(id, created, uuid, updated, sortindex, createdby_id, updatedby_id, feature_id, parent_id, taxon_id, key_id @audit) " +
325
				" 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" +
326
				" FROM @featureNodeTableName n INNER JOIN @featureTreeTableName t ON n.featuretree_id = t.id" +
327
				" WHERE t.DTYPE = 'PolytomousKey'";
328
		updateQuery = updateQuery.replace("@polytomousKeyNodeTableName", polytomousKeyNodeTableName);
329
		updateQuery = updateQuery.replace("@featureNodeTableName", featureNodeTableName);
330
		updateQuery = updateQuery.replace("@featureTreeTableName", featureTreeTableName);
331

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

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

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

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

    
383
		}
384
		return;
385
	}
386

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

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