Project

General

Profile

Download (4.13 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.v31_33;
11

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

    
16
import org.apache.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.ITermUpdaterStep;
22
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
23

    
24
/**
25
 * Creates empty root nodes for each classification. Replacing MN tables for classification root nodes.
26
 * For single use in {@link SchemaUpdater_33_331}
27
 * @author a.mueller
28
 * @date 15.12.2013
29
 */
30
public class ClassificationRootNodeUpdater extends SchemaUpdaterStepBase<ClassificationRootNodeUpdater> implements ITermUpdaterStep{
31
	private static final Logger logger = Logger.getLogger(ClassificationRootNodeUpdater.class);
32

    
33
	private static final String stepName = "Update Classification Root Nodes";
34
	
35
// **************************** STATIC METHODS ********************************/
36

    
37
	public static final ClassificationRootNodeUpdater NewInstance(){
38
		return new ClassificationRootNodeUpdater(stepName);	
39
	}
40

    
41
	protected ClassificationRootNodeUpdater(String stepName) {
42
		super(stepName);
43
	}
44

    
45
	@Override
46
	public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
47
		
48
		try {
49
			
50
			// for each classification
51
			String sql = " SELECT id FROM " + caseType.transformTo("Classification") + " ORDER BY id ";
52
			ResultSet rs = datasource.executeQuery(sql);
53
			while (rs.next()){
54
				Number classificationId = rs.getInt("id");
55
				
56
				//getMaxId in TaxonNode
57
				sql = " SELECT max(id) FROM " + caseType.transformTo("TaxonNode");
58
				Number maxId = ((Number)datasource.getSingleValue(sql));
59
				maxId = maxId == null ? 1 : ((Integer)maxId + 1);
60
				
61
				//count children
62
				sql = " SELECT count(*) as n " +
63
						" FROM @@Classification_TaxonNode@@ MN " +
64
						" WHERE MN.Classification_id = " + classificationId; 
65
				Number countChildren = (Number)datasource.getSingleValue(caseType.replaceTableNames(sql));
66
				
67
				//create root node
68
				sql = " INSERT INTO @@TaxonNode@@ (id, created, createdby_id , uuid, countchildren, classification_id, parent_id, taxon_id, treeIndex, sortIndex ) "+ 
69
						" VALUES (%d, '%s', null, '%s', %d, %d, NULL, NULL, '#c%d#%d', NULL) ";
70
				sql = String.format(sql, 
71
						maxId, this.getNowString(), UUID.randomUUID(), countChildren, classificationId, classificationId, maxId);
72
				datasource.executeUpdate(caseType.replaceTableNames(sql));
73
				
74
				//create fks to new root node
75
				sql = " UPDATE @@Classification@@ " +
76
						" SET rootnode_id = " +  maxId +
77
						" WHERE id = " + classificationId;
78
				datasource.executeUpdate(caseType.replaceTableNames(sql));
79
				
80
//				//update current root nodes (parent) 
81
//				sql = " UPDATE @@TaxonNode@@ " +
82
//						" SET parent_id = " +  maxId +
83
//						" WHERE c.id = " + classificationId + " AND parent_id IS NULL";
84
//				datasource.executeUpdate(caseType.replaceTableNames(sql));
85
				
86
				//update sort index and parent_id 
87
				sql = " UPDATE @@TaxonNode@@ " +
88
						" SET sortIndex = (SELECT sortIndex FROM @@Classification_TaxonNode@@ MN WHERE MN.rootnodes_id = TaxonNode.id AND MN.Classification_id = @@TaxonNode@@.classification_id)," +
89
							" parent_id = " + maxId +
90
						" WHERE EXISTS (SELECT * FROM @@Classification_TaxonNode@@ MN2 WHERE MN2.Classification_id = %d AND MN2.rootnodes_id = @@TaxonNode@@.id )";
91
				sql = String.format(sql, classificationId);
92
				datasource.executeUpdate(caseType.replaceTableNames(sql));
93
				
94
			}
95
			
96
			//run treeindex creator
97
			// remove old MN table
98
			//done in Schema updater
99
			
100
			return 0;
101

    
102
		} catch (Exception e) {
103
			monitor.warning(e.getMessage(), e);
104
			logger.warn(e.getMessage());
105
			return null;
106
		}
107
	}
108

    
109
}
(1-1/7)