Project

General

Profile

Download (5.02 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 org.apache.log4j.Logger;
13

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

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

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

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

    
41

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

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

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

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

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

    
103

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

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

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

    
128
}
(5-5/34)