From: Aron Roberts Date: Thu, 6 Jan 2011 00:04:18 +0000 (+0000) Subject: CSPACE-2496,CSPACE-3240: Added braindead example of init handler to change data type... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=65ca0542d5929193269ee94b39bdbb4f4afe8ec2;p=tmp%2Fjakarta-migration.git CSPACE-2496,CSPACE-3240: Added braindead example of init handler to change data type of comments field in CollectionObject. --- diff --git a/services/common/src/main/config/services/tenant-bindings.xml b/services/common/src/main/config/services/tenant-bindings.xml index 6736d569c..c02c1e89e 100644 --- a/services/common/src/main/config/services/tenant-bindings.xml +++ b/services/common/src/main/config/services/tenant-bindings.xml @@ -57,6 +57,17 @@ org.collectionspace.services.collectionobject.nuxeo.CollectionObjectValidatorHandler + + org.collectionspace.services.common.init.MakeLargeTextFields + + + nuxeo.collectionobjects_common_comments + item + TEXT + + + + objectNamePropertyobjectName objectNumberPropertyobjectNumber diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java b/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java index b763be671..d48ad84bd 100755 --- a/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java @@ -35,8 +35,7 @@ import java.util.List; public class AddIndices extends InitHandler implements IInitHandler { final Logger logger = LoggerFactory.getLogger(AddIndices.class); - - public void onRepositoryInitialized(ServiceBindingType sbt, List fields, List property) throws Exception { + public void onRepositoryInitialized(ServiceBindingType sbt, List fields, List properties) throws Exception { //todo: all post-init tasks for services, or delegate to services that override. System.out.println("\r\n\r\n~~~~~~~~~~~~~ in AddIndices.onRepositoryInitialized with ServiceBindingType: "+sbt); @@ -44,7 +43,7 @@ public class AddIndices extends InitHandler implements IInitHandler { ResultSet rs = null; try { String addIndex_SQL = "UPDATE TABLE ADD KEY `tablename`.`id`..."; - rs = openResultSet(addIndex_SQL); + rs = executeQuery(addIndex_SQL); if (rs != null){ // ..... } diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java b/services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java index 8786c5a76..ce2fa4ff9 100755 --- a/services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java @@ -15,7 +15,6 @@ * https://source.collectionspace.org/collection-space/LICENSE.txt */ - package org.collectionspace.services.common.init; import org.collectionspace.services.common.ServiceMain; @@ -41,29 +40,31 @@ public class InitHandler implements IInitHandler { public void onRepositoryInitialized(ServiceBindingType sbt, List fields, List properties) throws Exception { // see org.collectionspace.services.common.init.AddIndices for a real implementation example. - System.out.println("\r\n\r\n~~~~~~~~~~~~~ in InitHandler.onRepositoryInitialized with ServiceBindingType: "+sbt); - for (Field field : fields){ - System.out.println( "InitHandler.fields:" - +"\r\n col: "+field.getCol() - +" table: "+field.getTable() - +" type: "+field.getType() - +" param: "+field.getParam()); + System.out.println("\r\n\r\n~~~~~~~~~~~~~ in InitHandler.onRepositoryInitialized with ServiceBindingType: " + sbt); + for (Field field : fields) { + System.out.println("InitHandler.fields:" + + "\r\n col: " + field.getCol() + + " table: " + field.getTable() + + " type: " + field.getType() + + " param: " + field.getParam()); } - for (Property prop : properties){ - System.out.println( "InitHandler.properties:" - +"\r\n key: "+prop.getKey() - +" value: "+prop.getValue()); + for (Property prop : properties) { + System.out.println("InitHandler.properties:" + + "\r\n key: " + prop.getKey() + + " value: " + prop.getValue()); } } - public ResultSet openResultSet(String sql) throws Exception { - return JDBCTools.openResultSet(sql); + public ResultSet executeQuery(String sql) throws Exception { + return JDBCTools.executeQuery(sql); } public void closeResultSet(ResultSet rs) throws SQLException { rs.close(); } - + public int executeUpdate(String sql) throws Exception { + return JDBCTools.executeUpdate(sql); + } } diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/MakeLargeTextFields.java b/services/common/src/main/java/org/collectionspace/services/common/init/MakeLargeTextFields.java new file mode 100644 index 000000000..22591e0a0 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/init/MakeLargeTextFields.java @@ -0,0 +1,69 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 Regents of the University of California + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.collectionspace.services.common.init; + +import java.util.List; +import org.collectionspace.services.common.service.ServiceBindingType; +import org.collectionspace.services.common.service.InitHandler.Params.Field; +import org.collectionspace.services.common.service.InitHandler.Params.Property; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * MakeLargeTextFields, post-init action to configure text fields + * that must hold large amounts of text. + * + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public class MakeLargeTextFields extends InitHandler implements IInitHandler { + + final Logger logger = LoggerFactory.getLogger(MakeLargeTextFields.class); + + public void onRepositoryInitialized(ServiceBindingType sbt, List fields, List properties) throws Exception { + //todo: all post-init tasks for services, or delegate to services that override. + System.out.println("\r\n\r\n~~~~~~~~~~~~~ in MakeLargeTextFields.onRepositoryInitialized with ServiceBindingType: "+sbt); + + String tableName = "nuxeo.collectionobjects_common_comments"; + // String columnName = "item"; + String columnDataType = "TEXT"; + + int rows = 0; + try { + for (Field field : fields) { + // MySQL + String sql = "ALTER TABLE " + field.getTable() + " MODIFY COLUMN " + field.getCol() + " " + field.getType(); + // PostgreSQL + // String sql = "ALTER TABLE " + tableName + " ALTER COLUMN " + columnName + " TYPE " + columnDataType; + rows = executeUpdate(sql); + } + } catch (Exception e){ + throw e; + } + //call something like this: services.common.storage.DBUtils.addIndex(String tablename, String fields[]); + //for every field that has an authRef, do ... + // --> Connection conn = getConnection(); + //see parameter that you need for adding indices to SQL. + + } +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java index b6921a5f0..2fb7c16f4 100755 --- a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java @@ -70,7 +70,7 @@ public class JDBCTools { } } - public static ResultSet openResultSet(String sql) throws Exception { + public static ResultSet executeQuery(String sql) throws Exception { Connection conn = null; Statement stmt = null; try { @@ -103,7 +103,41 @@ public class JDBCTools { } + public static int executeUpdate(String sql) throws Exception { + Connection conn = null; + Statement stmt = null; + try { + conn = JDBCTools.getConnection(ServiceMain.DEFAULT_REPOSITORY_NAME); + stmt = conn.createStatement(); + int rows = stmt.executeUpdate(sql); + stmt.close(); + return rows; + } catch (RuntimeException rte) { + logger.debug("Exception in update: " + rte.getLocalizedMessage()); + logger.debug(rte.getStackTrace().toString()); + throw rte; + } catch (SQLException sqle) { + SQLException tempException = sqle; + while (null != tempException) { // SQLExceptions can be chained. Loop to log all. + logger.debug("SQL Exception: " + sqle.getLocalizedMessage()); + tempException = tempException.getNextException(); + } + logger.debug(sqle.getStackTrace().toString()); + throw new RuntimeException("SQL problem in update: ", sqle); + } finally { + try { + if (conn != null) { + conn.close(); + } + if (stmt != null) { + stmt.close(); + } + } catch (SQLException sqle) { + logger.debug("SQL Exception closing statement/connection in openResultSet: " + sqle.getLocalizedMessage()); + return -1; + } + } - + } }