]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5498: First pass at successfully soft deleting relation records pertaining...
authorAron Roberts <aron@socrates.berkeley.edu>
Wed, 21 Nov 2012 02:55:49 +0000 (18:55 -0800)
committerAron Roberts <aron@socrates.berkeley.edu>
Wed, 21 Nov 2012 02:55:49 +0000 (18:55 -0800)
3rdparty/nuxeo/nuxeo-platform-listener/relation/pom.xml
3rdparty/nuxeo/nuxeo-platform-listener/relation/src/main/java/org/collectionspace/services/nuxeo/listener/relation/RelationSubOrObjDeletionListener.java
services/IntegrationTests/src/test/resources/test-data/xmlreplay/relation/relation.xml

index 441487927db2dae9565149d1ef7b4af64099d982..8b17c62982cf20c3cb44fd854b5d0a2731966c27 100644 (file)
@@ -1,30 +1,35 @@
 <?xml version="1.0"?>
 <project
-       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
-       xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-       <modelVersion>4.0.0</modelVersion>
-       <parent>
-               <artifactId>org.collectionspace.services.3rdparty.nuxeo.listener</artifactId>
-               <groupId>org.collectionspace.services</groupId>
-               <version>3.2-SNAPSHOT</version>
-       </parent>
-       <artifactId>org.collectionspace.services.3rdparty.nuxeo.listener.relations</artifactId>
-       <name>org.collectionspace.services.3rdparty.nuxeo.listener.relations</name>
-       <url>http://maven.apache.org</url>
-       <properties>
-               <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-       </properties>
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <artifactId>org.collectionspace.services.3rdparty.nuxeo.listener</artifactId>
+        <groupId>org.collectionspace.services</groupId>
+        <version>3.2-SNAPSHOT</version>
+    </parent>
+    <artifactId>org.collectionspace.services.3rdparty.nuxeo.listener.relations</artifactId>
+    <name>org.collectionspace.services.3rdparty.nuxeo.listener.relations</name>
+    <url>http://maven.apache.org</url>
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
        
-       <dependencies>   
-       </dependencies>
+    <dependencies>
+        <dependency>
+            <groupId>org.collectionspace.services</groupId>
+            <artifactId>org.collectionspace.services.common</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
        
     <build>
-               <resources>
+        <resources>
             <resource>
                 <directory>src/main/resources</directory>
                 <filtering>true</filtering>
             </resource>
-               </resources>
+        </resources>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
index d457744363d082ad85d7d231773ec4f95bbbf16e..b609ce3bb1b65010677ce11b3670fc00409bcb50 100644 (file)
@@ -1,12 +1,16 @@
 package org.collectionspace.services.nuxeo.listener.relation;
 
-import java.io.Serializable;
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.collectionspace.services.common.document.DocumentFilter;
 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.DocumentRef;
+import org.nuxeo.ecm.core.api.DocumentModelList;
+import org.nuxeo.ecm.core.api.impl.LifeCycleFilter;
 import org.nuxeo.ecm.core.event.Event;
 import org.nuxeo.ecm.core.event.EventContext;
 import org.nuxeo.ecm.core.event.EventListener;
@@ -14,9 +18,19 @@ import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
 
 public class RelationSubOrObjDeletionListener implements EventListener {
 
-    public static final String WORKFLOW_TRANSITION_TO = "to";
-    public static final String WORKFLOW_TRANSITION_DELETED = "deleted";
-    // FIXME We might experiment here with using log4j here instead of Apache Commons Logging
+    // FIXME: Consider adding the following constant to
+    // org.collectionspace.services.common.workflow.jaxb.WorkflowJAXBSchema
+    // and referencing it from there.
+    private static final String WORKFLOWTRANSITION_TO = "to";
+    // FIXME: Consider substituting existing constant WorkflowClient.WORKFLOWSTATE_DELETED
+    private static final String WORKFLOWSTATE_DELETED = "deleted";
+    // FIXME: Consider substituting existing constant WorkflowClient.WORKFLOWSTATE_LOCKED
+    private static final String WORKFLOWSTATE_LOCKED = "locked";
+    // FIXME: Consider substituting existing constant WorkflowClient.WORKFLOWTRANSITION_DELETE
+    private static final String WORKFLOWTRANSITION_DELETE = "delete";
+
+    // FIXME: We might experiment here with using log4j instead of Apache Commons Logging;
+    // am using the latter to follow Ray's pattern for now
     final Log logger = LogFactory.getLog(RelationSubOrObjDeletionListener.class);
 
     public void handleEvent(Event event) throws ClientException {
@@ -29,15 +43,56 @@ public class RelationSubOrObjDeletionListener implements EventListener {
             DocumentEventContext docContext = (DocumentEventContext) eventContext;
             DocumentModel docModel = docContext.getSourceDocument();
             
-            // FIXME: Temporary for debugging
-            logger.debug("docType=" + docModel.getType());
-            logger.debug("id=" + docModel.getSourceId());
-            logger.debug("transition to=" + docContext.getProperties().get(WORKFLOW_TRANSITION_TO));
+            // Retrieve a list of relation records, where the soft deleted
+            // document provided in the context of the current event is
+            // either the subject or object of any relation
             
-            // Get a list of relation records where the just-soft-deleted
-            // document is either the subject or object of the relation
+            // Build a query string
+            String csid = docModel.getName();
+            StringBuilder queryString = new StringBuilder("");
+            queryString.append("SELECT * FROM Relation WHERE ");
+            // FIXME: Obtain and add tenant ID to the query here
+            // queryString.append("collectionspace_core:tenantId = 1 ");
+            // queryString.append(" AND ");
+            // queryString.append("ecm:currentLifeCycleState <> 'deleted' ");
+            queryString.append("ecm:isProxy = 0 ");
+            queryString.append(" AND ");
+            queryString.append("(");
+            queryString.append("relations_common:subjectCsid = ");
+            queryString.append("'");
+            queryString.append(csid);
+            queryString.append("'");
+            queryString.append(" OR ");
+            queryString.append("relations_common:objectCsid = ");
+            queryString.append("'");
+            queryString.append(csid);
+            queryString.append("'");
+            queryString.append(")");
+                        
+            // Create a filter to exclude from the list results any records
+            // that have already been soft deleted or are locked
+            List<String> workflowStatesToFilter = new ArrayList<String>();
+            workflowStatesToFilter.add(WORKFLOWSTATE_DELETED);
+            workflowStatesToFilter.add(WORKFLOWSTATE_LOCKED);
+            LifeCycleFilter workflowStateFilter = new LifeCycleFilter(null, workflowStatesToFilter);
             
-            // Cycle through the list, soft deleting each of these relation records
+            // Perform the filtered query
+            CoreSession session = docModel.getCoreSession();
+            DocumentModelList matchingDocuments;
+            try {
+                matchingDocuments = session.query(queryString.toString(), workflowStateFilter);
+            } catch (ClientException ce) {
+                logger.warn("Error attempting to retrieve relation records where "
+                        + "record of type '" + docModel.getType() + "' with CSID " + csid
+                        + " is the subject or object of any relation: " + ce.getMessage());
+                throw ce;
+            }
+
+            // Cycle through the list results, soft deleting each matching relation record
+            logger.trace("Attempting to soft delete " + matchingDocuments.size() + " relation records.");
+            for (DocumentModel doc : matchingDocuments) {
+                doc.followTransition(WORKFLOWTRANSITION_DELETE);
+            }
 
         }
 
@@ -55,8 +110,8 @@ public class RelationSubOrObjDeletionListener implements EventListener {
     private boolean isDocumentSoftDeletedEvent(EventContext eventContext) {
         boolean isSoftDeletedEvent = false;
         if (eventContext instanceof DocumentEventContext) {
-            if (eventContext.getProperties().containsKey(WORKFLOW_TRANSITION_TO)
-                    && eventContext.getProperties().get(WORKFLOW_TRANSITION_TO).equals(WORKFLOW_TRANSITION_DELETED)) {
+            if (eventContext.getProperties().containsKey(WORKFLOWTRANSITION_TO)
+                    && eventContext.getProperties().get(WORKFLOWTRANSITION_TO).equals(WORKFLOWSTATE_DELETED)) {
                 isSoftDeletedEvent = true;
             }
         }
index f00037a74bfabe2f67788844f5a61e3f0565c85c..76150204d0e6c818406720138d19d4caf3223ecc 100644 (file)
              <!-- If not present, throws "java.io.FileNotFoundException: File '' does not exist" -->\r
              <!-- The contents of that file, sent in the PUT payload, will be ignored by the services. -->\r
              <filename>relation/res/workflowState.res.xml</filename>\r
+             <vars>\r
+                 <var ID="workflowState">deleted</var>\r
+             </vars>\r
          </test>\r
          <test ID="verifySubjectIsSoftDeleted">\r
              <method>GET</method>\r
              <!-- If not present, throws "java.io.FileNotFoundException: File '' does not exist" -->\r
              <!-- The contents of that file, sent in the PUT payload, will be ignored by the services. -->\r
              <filename>relation/res/workflowState.res.xml</filename>\r
+             <vars>\r
+                 <var ID="workflowState">deleted</var>\r
+             </vars>\r
          </test>\r
          <test ID="verifyObjectIsSoftDeleted">\r
              <method>GET</method>\r