minor
[cdmlib.git] / 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 String newClassName;
28 private String[] oldClassNames;
29 private boolean isIdentifiable;
30 private boolean isAnnotatable;
31 private 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);
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;
53 }
54
55 @Override
56 protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) {
57 boolean result = true;
58 try {
59 if (true){
60 String updateQuery = getDtypeUpdateQueryString(tableName, datasource, monitor);
61 datasource.executeUpdate(updateQuery);
62 }
63
64 if (isAnnotatable){
65 updateAnnotatables(tableName, datasource, monitor);
66 }
67 if (isSourcable){
68 updateSourcable(tableName, datasource, monitor);
69 }
70
71 if (isIdentifiable){
72 updateIdentifiables(tableName, datasource, monitor);
73 }
74
75 return result;
76 } catch ( Exception e) {
77 monitor.warning(e.getMessage(), e);
78 logger.error(e);
79 return false;
80 }
81 }
82
83 private void updateSourcable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {
84 updateSingleExtension("OriginalSourceBase", "sourcedObj_type" , datasource, monitor);
85 }
86 private void updateIdentifiables(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {
87 updateSingleExtension("Extension", "extendedObj_type" , datasource, monitor);
88 }
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);
92 }
93 private void updateSingleExtension(String extensionClass, String typeAttr, ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException{
94 String sql = " UPDATE %s " +
95 " SET %s = '%s' " +
96 " WHERE %s = '%s'";
97
98 for (String oldClassPath : oldClassNames){
99 String query = String.format(sql, extensionClass,
100 typeAttr, newClassName,
101 typeAttr, oldClassPath);
102 datasource.executeUpdate(query);
103 }
104 }
105
106
107 public String getDtypeUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
108 String updateQuery;
109 updateQuery = " UPDATE @tableName t " +
110 " SET t.DTYPE = '@newTableName' " +
111 " WHERE (1=0 @dtypes)";
112
113 updateQuery = updateQuery.replace("@tableName", tableName);
114 updateQuery = updateQuery.replace("@newTableName", getSimpleName(newClassName));
115 String dtypes = "";
116 for (String oldClassName : oldClassNames){
117 dtypes += String.format(" OR t.DTYPE = '%s' ", getSimpleName(oldClassName)) ;
118 }
119 updateQuery = updateQuery.replace("@dtypes", dtypes);
120
121 return updateQuery;
122 }
123 private String getSimpleName(String className) {
124 String result = className;
125 while (result.contains(".")){
126 result = result.replaceAll(".*\\.", "");
127 }
128 return result;
129 }
130
131 }