Project

General

Profile

Download (5.11 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;
10

    
11
import org.apache.log4j.Logger;
12

    
13
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
14
import eu.etaxonomy.cdm.database.ICdmDataSource;
15

    
16
/**
17
 * @author a.mueller
18
 * @since 16.09.2010
19
 *
20
 */
21
public class ClassChanger
22
       extends AuditedSchemaUpdaterStepBase{
23

    
24
    private static final Logger logger = Logger.getLogger(ClassChanger.class);
25

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

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

    
42

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

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

    
61
//			if (isAnnotatable){
62
//				updateAnnotatables(tableName, datasource, monitor, caseType);
63
//			}
64
//			if (isSourcable){
65
//				updateSourcable(tableName, datasource, monitor, caseType);
66
//			}
67
//
68
//			if (isIdentifiable){
69
//				updateIdentifiables(tableName, datasource, monitor, caseType);
70
//			}
71

    
72
			return;
73
		} catch ( Exception e) {
74
		    String message = "Unhandled exception " + e.getMessage() + " in invokeOnTable";
75
			monitor.warning(message, e);
76
			logger.error(e);
77
			result.addException(e, message, "ClassChanger.invokeOnTable");
78
			return;
79
		}
80
	}
81

    
82
// not required anymore since #5743
83
//	private void updateSourcable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
84
//		updateSingleExtension("OriginalSourceBase", "sourcedObj_type" , datasource, monitor, caseType);
85
//	}
86
//	private void updateIdentifiables(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
87
//		updateSingleExtension("Extension", "extendedObj_type" , datasource, monitor, caseType);
88
//	}
89
//	private void updateAnnotatables(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
90
//		updateSingleExtension("Marker", "markedObj_type" , datasource, monitor, caseType);
91
//		updateSingleExtension("Annotation", "annotatedObj_type" , datasource, monitor, caseType);
92
//	}
93
//	private void updateSingleExtension(String extensionClass, String typeAttr, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException{
94
//		String sql = " UPDATE %s " +
95
//				" SET %s = '%s' " +
96
//				" WHERE %s = '%s'";
97
//
98
//		for (String oldClassPath : oldClassNames){
99
//			String query = String.format(sql, caseType.transformTo(extensionClass),
100
//					typeAttr, newClassName,
101
//					typeAttr, oldClassPath);
102
//			datasource.executeUpdate(query);
103
//		}
104
//	}
105

    
106

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

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

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

    
131
}
(5-5/35)