]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5727: Event listener now successfully acquires a list of related CollectionObj...
authorAron Roberts <aron@socrates.berkeley.edu>
Fri, 7 Dec 2012 02:41:46 +0000 (18:41 -0800)
committerAron Roberts <aron@socrates.berkeley.edu>
Fri, 7 Dec 2012 02:41:46 +0000 (18:41 -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 25f8c3d3073d92770004be4c141968176968cfe3..70166813467cf15dbadc37501996e0422ec6ff29 100644 (file)
@@ -6,20 +6,23 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.sql.Connection;
 import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.ArrayList;
+import java.util.List;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.collectionspace.services.client.workflow.WorkflowClient;
 import org.collectionspace.services.common.api.RefNameUtils;
 import org.collectionspace.services.common.api.Tools;
+import org.collectionspace.services.common.query.QueryContext;
 import org.collectionspace.services.common.storage.JDBCTools;
 import org.collectionspace.services.movement.nuxeo.MovementConstants;
 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
 import org.nuxeo.ecm.core.api.ClientException;
+import org.nuxeo.ecm.core.api.CoreSession;
 import org.nuxeo.ecm.core.api.DocumentModel;
+import org.nuxeo.ecm.core.api.DocumentModelList;
 import org.nuxeo.ecm.core.event.Event;
 import org.nuxeo.ecm.core.event.EventContext;
 import org.nuxeo.ecm.core.event.EventListener;
@@ -138,53 +141,88 @@ public class UpdateObjectLocationOnMove implements EventListener {
             // relations with Movement-as-subject and CollectionObject-as-object;
             // that may NOT always be the case.
 
-            // buildNXQLQuery(List<String> docTypes, QueryContext queryContext);
-
-            // 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:
+            // The following is boilerplate and may be incorrect:
+
+            CoreSession coreSession = docEventContext.getCoreSession();
+            final String RELATION_DOCTYPE = "Relation"; // FIXME: Get from external constant
+            final String COLLECTIONOBJECT_DOCTYPE = "CollectionObject"; // FIXME: Get from external constant
+            String query = String.format(
+                    "SELECT * FROM %1$s WHERE " // collectionspace_core:tenantId = 1 "
+                    + "(relations_common:subjectCsid = '%2$s' "
+                    + "AND relations_common:objectDocumentType = '%3$s') "
+                    + "AND (ecm:currentLifeCycleState <> 'deleted') "
+                    + "AND ecm:isProxy = 0 "
+                    + "AND ecm:isCheckedInVersion = 0", RELATION_DOCTYPE, movementCsid, COLLECTIONOBJECT_DOCTYPE);
+            DocumentModelList relatedDocModels = coreSession.query(query);
+            if (relatedDocModels == null || relatedDocModels.isEmpty()) {
+                return;
+            } else {
+                logger.debug("Found " + relatedDocModels.size() + " related documents.");
+            }
 
-            ArrayList<String> collectionObjectCsids = new ArrayList<String>();
-            collectionObjectCsids.add("5b4c617e-53a0-484b-804e"); // FIXME: Hard-coded for testing
+            // Iterate through the list of Relation records found and build
+            // a list of CollectionObject CSIDs, by extracting the object CSIDs
+            // from those Relation records.
+            String csid = "";
+            final String RELATIONS_COMMON_SCHEMA = "relations_common"; // FIXME: Get from external constant
+            final String OBJECT_CSID_PROPERTY = "objectCsid"; // FIXME: Get from external constant
+            List<String> collectionObjectCsids = new ArrayList<String>();
+            for (DocumentModel relatedDocModel : relatedDocModels) {
+                csid = (String) relatedDocModel.getProperty(RELATIONS_COMMON_SCHEMA, OBJECT_CSID_PROPERTY);
+                if (Tools.notBlank(csid)) {
+                    collectionObjectCsids.add(csid);
+                }
+            }
+            if (collectionObjectCsids == null || collectionObjectCsids.isEmpty()) {
+                return;
+            } else {
+                logger.debug("Found " + collectionObjectCsids.size() + " CollectionObject CSIDs.");
+            }
 
+            // Iterate through the list of CollectionObject CSIDs found.
             DocumentModel collectionObjectDocModel = null;
             String computedCurrentLocationRefName = "";
-            for (String csid : collectionObjectCsids) {
-
-                // Verify that the CollectionObject record is active (use isActiveDocument(), below).
+            for (String collectionObjectCsid : collectionObjectCsids) {
 
+                // Verify that the CollectionObject record is active.
+                // Code below untried; likely needs work.
                 /*
                  try {
                  collectionObjectDocModel = NuxeoUtils.getDocFromCsid(null, null, csid);
                  } catch (Exception e) {
                  logger.warn("Exception in getDocFromCsid: ", e);
                  }
+                 if (!isActiveDocument(collectionObjectDocModel)) {
+                 continue;
+                 }
+                 *
                  */
 
                 // Via a JDBC call, invoke the SQL function to obtain the computed
                 // current location of that CollectionObject.
-                computedCurrentLocationRefName = computeCurrentLocation(csid);
+                computedCurrentLocationRefName = computeCurrentLocation(collectionObjectCsid);
                 logger.debug("computedCurrentLocationRefName=" + computedCurrentLocationRefName);
 
-                // 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 and non-blank (need to verify this assumption; can a
+                // Check that the value returned from this SQL function, which
+                // is expected to be a reference (refName) to a storage location
+                // authority term, is, at a minimum:
+                // * Non-null and non-blank. (We need to verify this assumption; can a
                 //   CollectionObject's computed current location meaningfully be 'un-set'?)
-                // * Capable of being successfully parsed by an authority item parser,
-                //   returning a non-null parse result.
+                // * Capable of being successfully parsed by an authority item parser;
+                //   that is, returning a non-null parse result.
                 if ((Tools.notBlank(computedCurrentLocationRefName)
                         && (RefNameUtils.parseAuthorityTermInfo(computedCurrentLocationRefName) != null))) {
                     logger.debug("refName passes basic validation tests.");
-                }
 
-                // Compare that returned value to the value in the
-                // computedCurrentLocation field of that CollectionObject
+                    // If the value returned from the function passes validation,
+                    // compare that value to the value in the computedCurrentLocation
+                    // field of that CollectionObject
+                    //
+                    // If the two values differ, update the CollectionObject record,
+                    // setting the value of the computedCurrentLocation 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 computedCurrentLocation field of that
-                // CollectionObject record to the value returned from the SQL function.
+                }
 
             }
 
@@ -314,7 +352,7 @@ public class UpdateObjectLocationOnMove implements EventListener {
      * Cataloging) record.
      *
      * @param csid the CSID of a CollectionObject record.
-     * @return
+     * @return the computed current location of the CollectionObject record.
      */
     private String computeCurrentLocation(String csid) {
         String computedCurrentLocation = "";
@@ -403,13 +441,4 @@ public class UpdateObjectLocationOnMove implements EventListener {
         }
         return str;
     }
-
-    public void printResultSet(ResultSet rs) throws SQLException {
-        ResultSetMetaData metadata = rs.getMetaData();
-        int numberOfColumns = metadata.getColumnCount();
-        for (int i = 1; i <= numberOfColumns; i++) {
-            logger.debug(metadata.getColumnName(i));
-            logger.debug(metadata.getColumnType(i));
-        }
-    }
 }
\ No newline at end of file
index c8a622a6abd2184292ba9b47db1e7e45234a7a6b..4325afa116215db67a72c0e5cf1bb7d5ee8e73d6 100644 (file)
@@ -30,6 +30,7 @@ import javax.sql.DataSource;
 import java.sql.DatabaseMetaData;\r
 import java.sql.Connection;\r
 import java.sql.ResultSet;\r
+import java.sql.ResultSetMetaData;\r
 import java.sql.SQLException;\r
 import java.sql.Statement;\r
 import java.util.HashMap;\r
@@ -125,9 +126,14 @@ public class JDBCTools {
         \r
         return result;\r
     }\r
+    \r
+    // Regarding the method below, we might instead identify whether we can\r
+    // return a CachedRowSet or equivalent.\r
+    // http://docs.oracle.com/javase/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html\r
+    // -- ADR 2012-12-06\r
 \r
     /* THIS IS BROKEN - If you close the statement, it closes the ResultSet!!!\r
-    public static ResultSet executeQuery(String repoName, String sql) throws Exception {\r
+    public static CachedRowSet executeQuery(String repoName, String sql) throws Exception {\r
         Connection conn = null;\r
         Statement stmt = null;\r
         try {\r
@@ -302,5 +308,21 @@ public class JDBCTools {
             System.out.println("database url=" + metadata.getURL());\r
         }\r
     }\r
+    \r
+    /**\r
+     * Prints metadata related to a JDBC ResultSet, such as column names.\r
+     * This is a utility method for use during debugging.\r
+     * \r
+     * @param rs a ResultSet.\r
+     * @throws SQLException \r
+     */\r
+    public void printResultSetMetaData(ResultSet rs) throws SQLException {\r
+        ResultSetMetaData metadata = rs.getMetaData();\r
+        int numberOfColumns = metadata.getColumnCount();\r
+        for (int i = 1; i <= numberOfColumns; i++) {\r
+            logger.debug(metadata.getColumnName(i));\r
+            // Insert other debug statements to retrieve additional per-column metadata here ...\r
+        }\r
+    }\r
                \r
 }\r