2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.database
.update
;
11 import java
.util
.List
;
13 import org
.apache
.log4j
.Logger
;
15 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
16 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
23 public class ClassChanger
24 extends AuditedSchemaUpdaterStepBase
{
26 private static final Logger logger
= Logger
.getLogger(ClassChanger
.class);
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
;
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);
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);
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);
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
;
55 protected void invokeOnTable(String tableName
, ICdmDataSource datasource
,
56 IProgressMonitor monitor
, CaseType caseType
, SchemaUpdateResult result
) {
59 String updateQuery
= getDtypeUpdateQueryString(tableName
, datasource
, monitor
);
60 datasource
.executeUpdate(updateQuery
);
63 // if (isAnnotatable){
64 // updateAnnotatables(tableName, datasource, monitor, caseType);
67 // updateSourcable(tableName, datasource, monitor, caseType);
70 // if (isIdentifiable){
71 // updateIdentifiables(tableName, datasource, monitor, caseType);
75 } catch ( Exception e
) {
76 String message
= "Unhandled exception " + e
.getMessage() + " in invokeOnTable";
77 monitor
.warning(message
, e
);
79 result
.addException(e
, message
, "ClassChanger.invokeOnTable");
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);
88 // private void updateIdentifiables(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
89 // updateSingleExtension("Extension", "extendedObj_type" , datasource, monitor, caseType);
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);
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'";
100 // for (String oldClassPath : oldClassNames){
101 // String query = String.format(sql, caseType.transformTo(extensionClass),
102 // typeAttr, newClassName,
103 // typeAttr, oldClassPath);
104 // datasource.executeUpdate(query);
109 public String
getDtypeUpdateQueryString(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws DatabaseTypeNotSupportedException
{
111 updateQuery
= " UPDATE @tableName " +
112 " SET DTYPE = '@newTableName' " +
113 " WHERE (1=0 @dtypes)";
115 updateQuery
= updateQuery
.replace("@tableName", tableName
);
116 updateQuery
= updateQuery
.replace("@newTableName", getSimpleName(newClassName
));
118 for (String oldClassName
: oldClassNames
){
119 dtypes
+= String
.format(" OR DTYPE = '%s' ", getSimpleName(oldClassName
)) ;
121 updateQuery
= updateQuery
.replace("@dtypes", dtypes
);
125 private String
getSimpleName(String className
) {
126 String result
= className
;
127 while (result
.contains(".")){
128 result
= result
.replaceAll(".*\\.", "");