]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5727: Stored function in the database is now successfully created programmatic...
authorAron Roberts <aron@socrates.berkeley.edu>
Thu, 6 Dec 2012 20:48:07 +0000 (12:48 -0800)
committerAron Roberts <aron@socrates.berkeley.edu>
Thu, 6 Dec 2012 20:48:07 +0000 (12:48 -0800)
3rdparty/nuxeo/nuxeo-platform-listener/updateobjectlocationonmove/src/main/java/org/collectionspace/services/listener/UpdateObjectLocationOnMove.java
services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java

index 62a8e33bfc5197b5e40a3af0e27f2bf94a480418..d151bb50decda9eb5a6cef87be85a907985b5361 100644 (file)
@@ -30,6 +30,11 @@ public class UpdateObjectLocationOnMove implements EventListener {
     // FIXME: Currently hard-coded; get this from JDBC utilities or equivalent
     private final String DATABASE_SYSTEM_NAME = "postgresql";
     private final String STORED_FUNCTION_NAME = "computecurrentlocation";
+    private final String SQL_FILENAME_EXTENSION = ".sql";
+    private final String SQL_RESOURCE_PATH =
+            DATABASE_RESOURCE_DIRECTORY_NAME + "/"
+            + DATABASE_SYSTEM_NAME + "/"
+            + STORED_FUNCTION_NAME + SQL_FILENAME_EXTENSION;
 
     // ####################################################################
     // FIXME: Per Rick, what happens if a relation record is updated,
@@ -61,84 +66,95 @@ public class UpdateObjectLocationOnMove implements EventListener {
 
             // Test whether a SQL function exists to supply the computed
             // current location of a CollectionObject.
-            if (storedFunctionExists(STORED_FUNCTION_NAME)) {
-                logger.debug("Stored function " + STORED_FUNCTION_NAME + "exists.");
-            } else {
-                logger.debug("Stored function " + STORED_FUNCTION_NAME + "does NOT exist.");
-
-                // If the function does not exist in the database, load the
-                // SQL command to create that function from a resource
-                // available to this class, and run a JDBC command to create
-                // that function in the database.
-                //
-                // For now, assume this function will be created in the
-                // 'nuxeo' database.
-                //
-                // FIXME: Future work to create per-tenant repositories will
-                // likely require that our JDBC statements connect to the
-                // appropriate tenant-specific database.
-                //
-                // It doesn't appear we can reliably create this function via
-                // 'ant create_nuxeo db' during the build process, because
-                // there's a substantial likelihood at that point that
-                // tables referred to by the function (e.g. movements_common
-                // and collectionobjects_common) will not yet exist.
-                // (PostgreSQL will not permit the function to be created if
-                // any of its referred-to tables do not exist.)
-                String sqlResourcePath =
-                        DATABASE_RESOURCE_DIRECTORY_NAME + "/"
-                        + DATABASE_SYSTEM_NAME + "/"
-                        + STORED_FUNCTION_NAME + ".sql";
-                String sql = getStringFromResource(sqlResourcePath);
+            //
+            // If the function does not exist in the database, load the
+            // SQL command to create that function from a resource
+            // available to this class, and run a JDBC command to create
+            // that function in the database.
+            //
+            // For now, assume this function will be created in the
+            // 'nuxeo' database.
+            //
+            // FIXME: Future work to create per-tenant repositories will
+            // likely require that our JDBC statements connect to the
+            // appropriate tenant-specific database.
+            //
+            // It doesn't appear we can reliably create this function via
+            // 'ant create_nuxeo db' during the build process, because
+            // there's a substantial likelihood at that point that
+            // tables referred to by the function (e.g. movements_common
+            // and collectionobjects_common) will not yet exist.
+            // (PostgreSQL will not permit the function to be created if
+            // any of its referred-to tables do not exist.)
+            if (!storedFunctionExists(STORED_FUNCTION_NAME)) {
+                logger.debug("Stored function " + STORED_FUNCTION_NAME + " does NOT exist.");
+                String sql = getStringFromResource(SQL_RESOURCE_PATH);
                 if (Tools.isBlank(sql)) {
-                    logger.warn("Could not create stored function to update computed current location.");
+                    logger.warn("Could not obtain SQL command to create stored function.");
                     logger.warn("Actions in this event listener will NOT be performed, as a result of a previous error.");
                     return;
                 }
 
+                // FIXME: Remove these temporary log statements after debugging
                 logger.debug("After reading stored function command from resource path.");
                 logger.debug("sql=" + sql);
 
-                // FIXME: Execute SQL command here to create stored function.
+                int result;
+                try {
+                    result = JDBCTools.executeUpdate(JDBCTools.getDataSource(JDBCTools.NUXEO_REPOSITORY_NAME), sql);
+                    logger.debug("Result of executeUpdate=" + result);
+                    if (result < 0) {
+                        logger.warn("Could not create stored function.");
+                        logger.warn("Actions in this event listener will NOT be performed, as a result of a previous error.");
+                        return;
+                    } else {
+                        logger.debug("Stored function " + STORED_FUNCTION_NAME + " was successfully created.");
+                    }
+                } catch (Exception e) {
+                    logger.warn("Could not create stored function: ", e);
+                    logger.warn("Actions in this event listener will NOT be performed, as a result of a previous Exception.");
+                    return;
+                }
+            } else {
+                logger.debug("Stored function " + STORED_FUNCTION_NAME + " exists.");
+            }
 
-                // Pseudocode:
+            // Pseudocode:
 
-                // Get this Movement record's CSID via the document model.
+            // Get this Movement record's CSID via the document model.
 
-                // Find every CollectionObject record related to this Movement record:
-                //
-                // Via an NXQL query, get a list of (non-deleted) relation records where:
-                // * This movement record's CSID is the subject CSID of the relation.
-                // * The object document type is a CollectionObject doctype.
+            // Find every CollectionObject record related to this Movement record:
+            //
+            // Via an NXQL query, get a list of (non-deleted) relation records where:
+            // * This movement record's CSID is the subject CSID of the relation.
+            // * The object document type is a CollectionObject doctype.
 
-                // Iterate through that list of Relation records and build a list of
-                // CollectionObject CSIDs, by extracting the object CSIDs of those records.
+            // Iterate through that list of Relation records and build a list of
+            // CollectionObject CSIDs, by extracting the object CSIDs of those records.
 
-                // For each such CollectionObject:
+            // For each such CollectionObject:
 
-                // Verify that the CollectionObject record is active (use isActiveDocument(), below).
+            // Verify that the CollectionObject record is active (use isActiveDocument(), below).
 
-                // Via a JDBC call, invoke the SQL function to supply the last
-                // identified location of that CollectionObject, giving it the CSID
-                // of the CollectionObject record as an argument.
+            // Via a JDBC call, invoke the SQL function to supply the last
+            // identified location of that CollectionObject, giving it the CSID
+            // of the CollectionObject record as an argument.
 
-                // Check that the SQL function's returned value, which is expected
-                // to be a reference (refName) to a storage location authority term,
-                // is at a minimum:
-                // * Non-null
-                // * Capable of being successfully parsed by an authority item parser,
-                //   returning a non-null parse result.
+            // Check that the SQL function's returned value, which is expected
+            // to be a reference (refName) to a storage location authority term,
+            // is at a minimum:
+            // * Non-null
+            // * Capable of being successfully parsed by an authority item parser,
+            //   returning a non-null parse result.
 
-                // Compare that returned value to the value in the
-                // lastIdentifiedLocation field of that CollectionObject
+            // Compare that returned value to the value in the
+            // lastIdentifiedLocation field of that CollectionObject
 
-                // If the two values differ, update the CollectionObject record,
-                // setting the value of the lastIdentifiedLocation field of that
-                // CollectionObject record to the value returned from the SQL function.
+            // If the two values differ, update the CollectionObject record,
+            // setting the value of the lastIdentifiedLocation field of that
+            // CollectionObject record to the value returned from the SQL function.
 
-            }
         }
-
     }
 
     /**
index ab5909642c41e1d3bf83bcdf24c5659a2d3cb736..c8a622a6abd2184292ba9b47db1e7e45234a7a6b 100644 (file)
@@ -40,7 +40,7 @@ import java.util.HashMap;
  * $LastChangedDate:  $\r
  */\r
 public class JDBCTools {\r
-       public static HashMap<String, DataSource> cachedDataSources = new HashMap<String, DataSource>();\r
+    public static HashMap<String, DataSource> cachedDataSources = new HashMap<String, DataSource>();\r
     public static String CSPACE_REPOSITORY_NAME = "CspaceDS";\r
     public static String NUXEO_REPOSITORY_NAME = "NuxeoDS";\r
     //\r