]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-2496,CSPACE-3240: Added braindead example of init handler to change data type...
authorAron Roberts <aron@socrates.berkeley.edu>
Thu, 6 Jan 2011 00:04:18 +0000 (00:04 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Thu, 6 Jan 2011 00:04:18 +0000 (00:04 +0000)
services/common/src/main/config/services/tenant-bindings.xml
services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java
services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java
services/common/src/main/java/org/collectionspace/services/common/init/MakeLargeTextFields.java [new file with mode: 0644]
services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java

index 6736d569c2a50ac36ee4f9c4aabfe13a7f6ea5cc..c02c1e89e2096ed26e30b95e9658c51ea05eb7c1 100644 (file)
             <service:validatorHandler xmlns:service='http://collectionspace.org/services/common/service'>
                 org.collectionspace.services.collectionobject.nuxeo.CollectionObjectValidatorHandler
             </service:validatorHandler>
+            <service:initHandler xmlns:service='http://collectionspace.org/services/common/service'>
+                <service:classname>org.collectionspace.services.common.init.MakeLargeTextFields</service:classname>
+                <service:params>
+                    <service:field>
+                        <service:table>nuxeo.collectionobjects_common_comments</service:table>
+                        <service:col>item</service:col>
+                        <service:type>TEXT</service:type>
+                        <service:param></service:param>
+                    </service:field>
+                </service:params>
+            </service:initHandler>
             <service:properties xmlns:service='http://collectionspace.org/services/common/service'>
                 <types:item><types:key>objectNameProperty</types:key><types:value>objectName</types:value></types:item>
                 <types:item><types:key>objectNumberProperty</types:key><types:value>objectNumber</types:value></types:item>
index b763be67122d856e103ff5e515297c1dc1ec7e6b..d48ad84bdda71efbfb67315a069882c01cb1d1ce 100755 (executable)
@@ -35,8 +35,7 @@ import java.util.List;
 public class AddIndices extends InitHandler implements IInitHandler {\r
 \r
     final Logger logger = LoggerFactory.getLogger(AddIndices.class);\r
-\r
-    public void onRepositoryInitialized(ServiceBindingType sbt, List<org.collectionspace.services.common.service.InitHandler.Params.Field> fields, List<Property> property) throws Exception {\r
+    public void onRepositoryInitialized(ServiceBindingType sbt, List<Field> fields, List<Property> properties) throws Exception {\r
           //todo: all post-init tasks for services, or delegate to services that override.\r
           System.out.println("\r\n\r\n~~~~~~~~~~~~~ in AddIndices.onRepositoryInitialized with ServiceBindingType: "+sbt);\r
 \r
@@ -44,7 +43,7 @@ public class AddIndices extends InitHandler implements IInitHandler {
         ResultSet rs = null;\r
         try {\r
             String addIndex_SQL = "UPDATE TABLE ADD KEY `tablename`.`id`...";\r
-            rs = openResultSet(addIndex_SQL);\r
+            rs = executeQuery(addIndex_SQL);\r
             if (rs != null){\r
                 // .....\r
             }\r
index 8786c5a768d8a5d2442bda27d4f756f8a7393925..ce2fa4ff94b923122f6d40854eff4be20a387aba 100755 (executable)
@@ -15,7 +15,6 @@
 \r
  *  https://source.collectionspace.org/collection-space/LICENSE.txt\r
  */\r
-\r
 package org.collectionspace.services.common.init;\r
 \r
 import org.collectionspace.services.common.ServiceMain;\r
@@ -41,29 +40,31 @@ public class InitHandler implements IInitHandler {
 \r
     public void onRepositoryInitialized(ServiceBindingType sbt, List<Field> fields, List<Property> properties) throws Exception {\r
         // see org.collectionspace.services.common.init.AddIndices for a real implementation example.\r
-        System.out.println("\r\n\r\n~~~~~~~~~~~~~ in InitHandler.onRepositoryInitialized with ServiceBindingType: "+sbt);\r
-        for (Field field : fields){\r
-            System.out.println( "InitHandler.fields:"\r
-                               +"\r\n    col: "+field.getCol()\r
-                               +"   table: "+field.getTable()\r
-                               +"   type: "+field.getType()\r
-                               +"   param: "+field.getParam());\r
+        System.out.println("\r\n\r\n~~~~~~~~~~~~~ in InitHandler.onRepositoryInitialized with ServiceBindingType: " + sbt);\r
+        for (Field field : fields) {\r
+            System.out.println("InitHandler.fields:"\r
+                    + "\r\n    col: " + field.getCol()\r
+                    + "   table: " + field.getTable()\r
+                    + "   type: " + field.getType()\r
+                    + "   param: " + field.getParam());\r
         }\r
-        for (Property prop : properties){\r
-            System.out.println( "InitHandler.properties:"\r
-                               +"\r\n    key: "+prop.getKey()\r
-                               +"   value: "+prop.getValue());\r
+        for (Property prop : properties) {\r
+            System.out.println("InitHandler.properties:"\r
+                    + "\r\n    key: " + prop.getKey()\r
+                    + "   value: " + prop.getValue());\r
 \r
         }\r
     }\r
 \r
-    public ResultSet openResultSet(String sql) throws Exception {\r
-        return JDBCTools.openResultSet(sql);\r
+    public ResultSet executeQuery(String sql) throws Exception {\r
+        return JDBCTools.executeQuery(sql);\r
     }\r
 \r
     public void closeResultSet(ResultSet rs) throws SQLException {\r
         rs.close();\r
     }\r
 \r
-\r
+    public int executeUpdate(String sql) throws Exception {\r
+        return JDBCTools.executeUpdate(sql);\r
+    }\r
 }\r
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 (file)
index 0000000..22591e0
--- /dev/null
@@ -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<Field> fields, List<Property> 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.
+
+    }
+}
index b6921a5f0cd5aca3a34a4e12088fc52db91dc1e7..2fb7c16f496ff99c89872e6fb88dab2e5d019a8d 100755 (executable)
@@ -70,7 +70,7 @@ public class JDBCTools {
         }\r
     }\r
 \r
-    public static ResultSet openResultSet(String sql) throws Exception {\r
+    public static ResultSet executeQuery(String sql) throws Exception {\r
         Connection conn = null;\r
        Statement stmt = null;\r
         try {\r
@@ -103,7 +103,41 @@ public class JDBCTools {
 \r
     }\r
 \r
+    public static int executeUpdate(String sql) throws Exception {\r
+        Connection conn = null;\r
+        Statement stmt = null;\r
+        try {\r
+            conn = JDBCTools.getConnection(ServiceMain.DEFAULT_REPOSITORY_NAME);\r
+            stmt = conn.createStatement();\r
+            int rows = stmt.executeUpdate(sql);\r
+            stmt.close();\r
+            return rows;\r
+        } catch (RuntimeException rte) {\r
+            logger.debug("Exception in update: " + rte.getLocalizedMessage());\r
+            logger.debug(rte.getStackTrace().toString());\r
+            throw rte;\r
+        } catch (SQLException sqle) {\r
+            SQLException tempException = sqle;\r
+            while (null != tempException) {       // SQLExceptions can be chained. Loop to log all.\r
+                logger.debug("SQL Exception: " + sqle.getLocalizedMessage());\r
+                tempException = tempException.getNextException();\r
+            }\r
+            logger.debug(sqle.getStackTrace().toString());\r
+            throw new RuntimeException("SQL problem in update: ", sqle);\r
+        } finally {\r
+            try {\r
+                if (conn != null) {\r
+                    conn.close();\r
+                }\r
+                if (stmt != null) {\r
+                    stmt.close();\r
+                }\r
+            } catch (SQLException sqle) {\r
+                logger.debug("SQL Exception closing statement/connection in openResultSet: " + sqle.getLocalizedMessage());\r
+                return -1;\r
+            }\r
+        }\r
 \r
-\r
+    }\r
 \r
 }\r