3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.database
.update
;
12 import java
.sql
.SQLException
;
14 import org
.apache
.log4j
.Logger
;
16 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
17 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
24 public class ClassChanger
extends AuditedSchemaUpdaterStepBase
<ClassChanger
> implements ISchemaUpdaterStep
{
25 private static final Logger logger
= Logger
.getLogger(ClassChanger
.class);
27 private String newClassName
;
28 private String
[] oldClassNames
;
29 private boolean isIdentifiable
;
30 private boolean isAnnotatable
;
31 private boolean isSourcable
;
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);
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);
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);
44 protected ClassChanger(String stepName
, String tableName
, String newClassName
, String
[] oldClassNames
, boolean includeAudTable
, boolean isAnnotatable
, boolean isSourcable
, boolean isIdentifiable
) {
46 this.tableName
= tableName
;
47 this.newClassName
= newClassName
;
48 this.oldClassNames
= oldClassNames
;
49 this.includeAudTable
= includeAudTable
;
50 this.isIdentifiable
= isIdentifiable
;
51 this.isAnnotatable
= isAnnotatable
;
52 this.isSourcable
= isSourcable
;
56 protected boolean invokeOnTable(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) {
57 boolean result
= true;
60 String updateQuery
= getDtypeUpdateQueryString(tableName
, datasource
, monitor
);
61 datasource
.executeUpdate(updateQuery
);
65 updateAnnotatables(tableName
, datasource
, monitor
);
68 updateSourcable(tableName
, datasource
, monitor
);
72 updateIdentifiables(tableName
, datasource
, monitor
);
76 } catch ( Exception e
) {
77 monitor
.warning(e
.getMessage(), e
);
83 private void updateSourcable(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
84 updateSingleExtension("OriginalSourceBase", "sourcedObj_type" , datasource
, monitor
);
86 private void updateIdentifiables(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
87 updateSingleExtension("Extension", "extendedObj_type" , datasource
, monitor
);
89 private void updateAnnotatables(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
90 updateSingleExtension("Marker", "markedObj_type" , datasource
, monitor
);
91 updateSingleExtension("Annotation", "annotatedObj_type" , datasource
, monitor
);
93 private void updateSingleExtension(String extensionClass
, String typeAttr
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
94 String sql
= " UPDATE %s " +
98 for (String oldClassPath
: oldClassNames
){
99 String query
= String
.format(sql
, extensionClass
,
100 typeAttr
, newClassName
,
101 typeAttr
, oldClassPath
);
102 datasource
.executeUpdate(query
);
107 public String
getDtypeUpdateQueryString(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws DatabaseTypeNotSupportedException
{
109 updateQuery
= " UPDATE @tableName t " +
110 " SET t.DTYPE = '@newTableName' " +
111 " WHERE (1=0 @dtypes)";
113 updateQuery
= updateQuery
.replace("@tableName", tableName
);
114 updateQuery
= updateQuery
.replace("@newTableName", getSimpleName(newClassName
));
116 for (String oldClassName
: oldClassNames
){
117 dtypes
+= String
.format(" OR t.DTYPE = '%s' ", getSimpleName(oldClassName
)) ;
119 updateQuery
= updateQuery
.replace("@dtypes", dtypes
);
123 private String
getSimpleName(String className
) {
124 String result
= className
;
125 while (result
.contains(".")){
126 result
= result
.replaceAll(".*\\.", "");