Project

General

Profile

Download (5.07 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

    
14
import org.apache.log4j.Logger;
15

    
16
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
17
import eu.etaxonomy.cdm.database.ICdmDataSource;
18

    
19
/**
20
 * @author a.mueller
21
 * @date 16.09.2010
22
 *
23
 */
24
public class ClassChanger extends AuditedSchemaUpdaterStepBase<ClassChanger> implements ISchemaUpdaterStep {
25
	private static final Logger logger = Logger.getLogger(ClassChanger.class);
26

    
27
	private final String newClassName;
28
	private final String[] oldClassNames;
29
	private final boolean isIdentifiable;
30
	private final boolean isAnnotatable;
31
	private final boolean isSourcable;
32

    
33
	public static final ClassChanger NewIdentifiableInstance(String stepName, String tableName, String newClassNamePath, String[] oldClassNames, boolean includeAudTable){
34
		return new ClassChanger(stepName, tableName, newClassNamePath, oldClassNames, includeAudTable, true, true, true);
35
	}
36
	public static final ClassChanger NewAnnotatableInstance(String stepName, String tableName, String newClassNamePath, String[] oldClassNames, boolean includeAudTable){
37
		return new ClassChanger(stepName, tableName, newClassNamePath, oldClassNames, includeAudTable, true, false, false);
38
	}
39
	public static final ClassChanger NewDescriptionElementInstance(String stepName, String tableName, String newClassNamePath, String[] oldClassNames, boolean includeAudTable){
40
		return new ClassChanger(stepName, tableName, newClassNamePath, oldClassNames, includeAudTable, true, true, false);
41
	}
42

    
43

    
44
	protected ClassChanger(String stepName, String tableName, String newClassName, String[] oldClassNames, boolean includeAudTable, boolean isAnnotatable, boolean isSourcable, boolean isIdentifiable) {
45
		super(stepName, tableName, includeAudTable);
46
		this.newClassName = newClassName;
47
		this.oldClassNames = oldClassNames;
48
		this.isIdentifiable = isIdentifiable;
49
		this.isAnnotatable = isAnnotatable;
50
		this.isSourcable = isSourcable;
51
	}
52

    
53
	@Override
54
	protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
55
		boolean result = true;
56
		try {
57
			if (true){
58
				String updateQuery = getDtypeUpdateQueryString(tableName, datasource, monitor);
59
				datasource.executeUpdate(updateQuery);
60
			}
61

    
62
			if (isAnnotatable){
63
				updateAnnotatables(tableName, datasource, monitor, caseType);
64
			}
65
			if (isSourcable){
66
				updateSourcable(tableName, datasource, monitor, caseType);
67
			}
68

    
69
			if (isIdentifiable){
70
				updateIdentifiables(tableName, datasource, monitor, caseType);
71
			}
72

    
73
			return result;
74
		} catch ( Exception e) {
75
			monitor.warning(e.getMessage(), e);
76
			logger.error(e);
77
			return false;
78
		}
79
	}
80

    
81
	private void updateSourcable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
82
		updateSingleExtension("OriginalSourceBase", "sourcedObj_type" , datasource, monitor, caseType);
83
	}
84
	private void updateIdentifiables(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
85
		updateSingleExtension("Extension", "extendedObj_type" , datasource, monitor, caseType);
86
	}
87
	private void updateAnnotatables(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
88
		updateSingleExtension("Marker", "markedObj_type" , datasource, monitor, caseType);
89
		updateSingleExtension("Annotation", "annotatedObj_type" , datasource, monitor, caseType);
90
	}
91
	private void updateSingleExtension(String extensionClass, String typeAttr, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException{
92
		String sql = " UPDATE %s " +
93
				" SET %s = '%s' " +
94
				" WHERE %s = '%s'";
95

    
96
		for (String oldClassPath : oldClassNames){
97
			String query = String.format(sql, caseType.transformTo(extensionClass),
98
					typeAttr, newClassName,
99
					typeAttr, oldClassPath);
100
			datasource.executeUpdate(query);
101
		}
102
	}
103

    
104

    
105
	public String getDtypeUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
106
		String updateQuery;
107
		updateQuery = " UPDATE @tableName " +
108
				" SET DTYPE = '@newTableName' " +
109
				" WHERE (1=0 @dtypes)";
110

    
111
		updateQuery = updateQuery.replace("@tableName", tableName);
112
		updateQuery = updateQuery.replace("@newTableName", getSimpleName(newClassName));
113
		String dtypes = "";
114
		for (String oldClassName : oldClassNames){
115
			dtypes += String.format(" OR DTYPE = '%s' ", getSimpleName(oldClassName)) ;
116
		}
117
		updateQuery = updateQuery.replace("@dtypes", dtypes);
118

    
119
		return updateQuery;
120
	}
121
	private String getSimpleName(String className) {
122
		String result = className;
123
		while (result.contains(".")){
124
			result = result.replaceAll(".*\\.", "");
125
		}
126
		return result;
127
	}
128

    
129
}
(5-5/34)