Revision 0ecfd682
Added by Andreas Müller almost 8 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/ClassChanger.java | ||
---|---|---|
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 java.sql.SQLException; |
|
13 |
|
|
14 |
import org.apache.log4j.Logger; |
|
15 |
|
|
16 |
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor; |
|
17 |
import eu.etaxonomy.cdm.database.ICdmDataSource; |
|
18 |
|
|
19 |
/** |
|
20 |
* @author a.mueller |
|
21 |
* @date 16.09.2010 |
|
22 |
* |
|
23 |
*/ |
|
24 |
public class ClassChanger extends AuditedSchemaUpdaterStepBase<ClassChanger> implements ISchemaUpdaterStep { |
|
25 |
private static final Logger logger = Logger.getLogger(ClassChanger.class); |
|
26 |
|
|
27 |
private final String newClassName; |
|
28 |
private final String[] oldClassNames; |
|
29 |
private final boolean isIdentifiable; |
|
30 |
private final boolean isAnnotatable; |
|
31 |
private final boolean isSourcable; |
|
32 |
|
|
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); |
|
35 |
} |
|
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); |
|
38 |
} |
|
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); |
|
41 |
} |
|
42 |
|
|
43 |
|
|
44 |
protected ClassChanger(String stepName, String tableName, String newClassName, String[] oldClassNames, boolean includeAudTable, boolean isAnnotatable, boolean isSourcable, boolean isIdentifiable) { |
|
45 |
super(stepName, tableName, includeAudTable); |
|
46 |
this.newClassName = newClassName; |
|
47 |
this.oldClassNames = oldClassNames; |
|
48 |
this.isIdentifiable = isIdentifiable; |
|
49 |
this.isAnnotatable = isAnnotatable; |
|
50 |
this.isSourcable = isSourcable; |
|
51 |
} |
|
52 |
|
|
53 |
@Override |
|
54 |
protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) { |
|
55 |
boolean result = true; |
|
56 |
try { |
|
57 |
if (true){ |
|
58 |
String updateQuery = getDtypeUpdateQueryString(tableName, datasource, monitor); |
|
59 |
datasource.executeUpdate(updateQuery); |
|
60 |
} |
|
61 |
|
|
62 |
if (isAnnotatable){ |
|
63 |
updateAnnotatables(tableName, datasource, monitor, caseType); |
|
64 |
} |
|
65 |
if (isSourcable){ |
|
66 |
updateSourcable(tableName, datasource, monitor, caseType); |
|
67 |
} |
|
68 |
|
|
69 |
if (isIdentifiable){ |
|
70 |
updateIdentifiables(tableName, datasource, monitor, caseType); |
|
71 |
} |
|
72 |
|
|
73 |
return result; |
|
74 |
} catch ( Exception e) { |
|
75 |
monitor.warning(e.getMessage(), e); |
|
76 |
logger.error(e); |
|
77 |
return false; |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
private void updateSourcable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException { |
|
82 |
updateSingleExtension("OriginalSourceBase", "sourcedObj_type" , datasource, monitor, caseType); |
|
83 |
} |
|
84 |
private void updateIdentifiables(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException { |
|
85 |
updateSingleExtension("Extension", "extendedObj_type" , datasource, monitor, caseType); |
|
86 |
} |
|
87 |
private void updateAnnotatables(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException { |
|
88 |
updateSingleExtension("Marker", "markedObj_type" , datasource, monitor, caseType); |
|
89 |
updateSingleExtension("Annotation", "annotatedObj_type" , datasource, monitor, caseType); |
|
90 |
} |
|
91 |
private void updateSingleExtension(String extensionClass, String typeAttr, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException{ |
|
92 |
String sql = " UPDATE %s " + |
|
93 |
" SET %s = '%s' " + |
|
94 |
" WHERE %s = '%s'"; |
|
95 |
|
|
96 |
for (String oldClassPath : oldClassNames){ |
|
97 |
String query = String.format(sql, caseType.transformTo(extensionClass), |
|
98 |
typeAttr, newClassName, |
|
99 |
typeAttr, oldClassPath); |
|
100 |
datasource.executeUpdate(query); |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
|
|
105 |
public String getDtypeUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException { |
|
106 |
String updateQuery; |
|
107 |
updateQuery = " UPDATE @tableName " + |
|
108 |
" SET DTYPE = '@newTableName' " + |
|
109 |
" WHERE (1=0 @dtypes)"; |
|
110 |
|
|
111 |
updateQuery = updateQuery.replace("@tableName", tableName); |
|
112 |
updateQuery = updateQuery.replace("@newTableName", getSimpleName(newClassName)); |
|
113 |
String dtypes = ""; |
|
114 |
for (String oldClassName : oldClassNames){ |
|
115 |
dtypes += String.format(" OR DTYPE = '%s' ", getSimpleName(oldClassName)) ; |
|
116 |
} |
|
117 |
updateQuery = updateQuery.replace("@dtypes", dtypes); |
|
118 |
|
|
119 |
return updateQuery; |
|
120 |
} |
|
121 |
private String getSimpleName(String className) { |
|
122 |
String result = className; |
|
123 |
while (result.contains(".")){ |
|
124 |
result = result.replaceAll(".*\\.", ""); |
|
125 |
} |
|
126 |
return result; |
|
127 |
} |
|
128 |
|
|
129 |
} |
|
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 |
} |
Also available in: Unified diff
Remove bidirectionality for supplemental data #5743