Project

General

Profile

Download (4.24 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.v30_40;
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
 * Creates empty root nodes for each classification. Replacing MN tables for classification root nodes.
27
 * For single use in {@link SchemaUpdater_33_331}
28
 * @author a.mueller
29
 * @since 15.12.2013
30
 */
31
public class ClassificationRootNodeUpdater extends SchemaUpdaterStepBase {
32
	private static final Logger logger = LogManager.getLogger(ClassificationRootNodeUpdater.class);
33

    
34
	private static final String stepName = "Update Classification Root Nodes";
35

    
36
// **************************** STATIC METHODS ********************************/
37

    
38
	public static final ClassificationRootNodeUpdater NewInstance(List<ISchemaUpdaterStep> stepList){
39
		return new ClassificationRootNodeUpdater(stepList, stepName);
40
	}
41

    
42
	protected ClassificationRootNodeUpdater(List<ISchemaUpdaterStep> stepList, String stepName) {
43
		super(stepList, stepName);
44
	}
45

    
46
	@Override
47
	public void invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType,
48
	        SchemaUpdateResult result) throws SQLException {
49

    
50
		try {
51

    
52
			// for each classification
53
			String sql = " SELECT id FROM " + caseType.transformTo("Classification") + " ORDER BY id ";
54
			ResultSet rs = datasource.executeQuery(sql);
55
			while (rs.next()){
56
				Number classificationId = rs.getInt("id");
57

    
58
				//getMaxId in TaxonNode
59
				sql = " SELECT max(id) FROM " + caseType.transformTo("TaxonNode");
60
				Number maxId = ((Number)datasource.getSingleValue(sql));
61
				maxId = maxId == null ? 1 : ((Integer)maxId + 1);
62

    
63
				//count children
64
				sql = " SELECT count(*) as n " +
65
						" FROM @@Classification_TaxonNode@@ MN " +
66
						" WHERE MN.Classification_id = " + classificationId;
67
				Number countChildren = (Number)datasource.getSingleValue(caseType.replaceTableNames(sql));
68

    
69
				//create root node
70
				sql = " INSERT INTO @@TaxonNode@@ (id, created, createdby_id , uuid, countchildren, classification_id, parent_id, taxon_id, treeIndex, sortIndex ) "+
71
						" VALUES (%d, '%s', null, '%s', %d, %d, NULL, NULL, '#c%d#%d', NULL) ";
72
				sql = String.format(sql,
73
						maxId, this.getNowString(), UUID.randomUUID(), countChildren, classificationId, classificationId, maxId);
74
				datasource.executeUpdate(caseType.replaceTableNames(sql));
75

    
76
				//create fks to new root node
77
				sql = " UPDATE @@Classification@@ " +
78
						" SET rootnode_id = " +  maxId +
79
						" WHERE id = " + classificationId;
80
				datasource.executeUpdate(caseType.replaceTableNames(sql));
81

    
82
//				//update current root nodes (parent)
83
//				sql = " UPDATE @@TaxonNode@@ " +
84
//						" SET parent_id = " +  maxId +
85
//						" WHERE c.id = " + classificationId + " AND parent_id IS NULL";
86
//				datasource.executeUpdate(caseType.replaceTableNames(sql));
87

    
88
				//update sort index and parent_id
89
				sql = " UPDATE @@TaxonNode@@ " +
90
						" SET sortIndex = (SELECT sortIndex FROM @@Classification_TaxonNode@@ MN WHERE MN.rootnodes_id = TaxonNode.id AND MN.Classification_id = @@TaxonNode@@.classification_id)," +
91
							" parent_id = " + maxId +
92
						" WHERE EXISTS (SELECT * FROM @@Classification_TaxonNode@@ MN2 WHERE MN2.Classification_id = %d AND MN2.rootnodes_id = @@TaxonNode@@.id )";
93
				sql = String.format(sql, classificationId);
94
				datasource.executeUpdate(caseType.replaceTableNames(sql));
95

    
96
			}
97

    
98
			//run treeindex creator
99
			// remove old MN table
100
			//done in Schema updater
101

    
102
			return;
103

    
104
		} catch (Exception e) {
105
		    String message = e.getMessage();
106
			monitor.warning(message, e);
107
			logger.warn(message);
108
			result.addException(e, message, this, "invoke");
109
			return;
110
		}
111
	}
112

    
113
}
(1-1/16)