Project

General

Profile

Download (5.37 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 java.util.List;
12

    
13
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
14

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

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

    
26
    private static final Logger logger = LogManager.getLogger(ClassChanger.class);
27

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

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

    
44

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

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

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

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

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

    
108

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

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

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

    
133
}
(6-6/41)