]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5498: Use String formatter to construct query string. Use established workflow...
authorAron Roberts <aron@socrates.berkeley.edu>
Wed, 5 Dec 2012 22:10:02 +0000 (14:10 -0800)
committerAron Roberts <aron@socrates.berkeley.edu>
Wed, 5 Dec 2012 22:10:02 +0000 (14:10 -0800)
3rdparty/nuxeo/nuxeo-platform-listener/updaterelationsondelete/pom.xml
3rdparty/nuxeo/nuxeo-platform-listener/updaterelationsondelete/src/main/java/org/collectionspace/services/listener/UpdateRelationsOnDelete.java
services/client/src/main/java/org/collectionspace/services/client/workflow/WorkflowClient.java

index a4294aea9eb16f0f52c81486caa1898da3155f22..34b2d77c7af7e773bc06e824f4611ede1cae4b8a 100644 (file)
             <artifactId>org.collectionspace.services.common</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.collectionspace.services</groupId>
+            <artifactId>org.collectionspace.services.client</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
        
     <build>
index 8efc9c12084c62fa9146e81998e76883661b55db..b47f5ef4b03909d042c15df852d353ac5eff878c 100644 (file)
@@ -2,8 +2,10 @@ package org.collectionspace.services.listener;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.IllegalFormatException;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.collectionspace.services.client.workflow.WorkflowClient;
 import org.nuxeo.ecm.core.api.ClientException;
 import org.nuxeo.ecm.core.api.CoreSession;
 import org.nuxeo.ecm.core.api.DocumentModel;
@@ -15,17 +17,10 @@ import org.nuxeo.ecm.core.event.EventListener;
 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
 
 public class UpdateRelationsOnDelete implements EventListener {
-
-    // 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: Get these constant values from external sources rather than redeclaring here
+    final static String RELATIONS_COMMON_SUBJECT_CSID_FIELD = "relations_common:subjectCsid";
+    final static String RELATIONS_COMMON_OBJECT_CSID_FIELD = "relations_common:objectCsid";
 
     // FIXME: We might experiment here with using log4j instead of Apache Commons Logging;
     // am using the latter to follow Ray's pattern for now
@@ -34,7 +29,7 @@ public class UpdateRelationsOnDelete implements EventListener {
     @Override
     public void handleEvent(Event event) throws ClientException {
         logger.info("In handleEvent in UpdateRelationsOnDelete ...");
-
+        
         EventContext eventContext = event.getContext();
 
         if (isDocumentSoftDeletedEvent(eventContext)) {
@@ -48,31 +43,23 @@ public class UpdateRelationsOnDelete implements EventListener {
             
             // 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(")");
-                        
+            
+            String queryString;
+            try {
+                queryString =
+                    String.format("SELECT * FROM Relation WHERE ecm:isProxy = 0 AND (%1$s='%3$s' OR %2$s='%3$s')",
+                    RELATIONS_COMMON_SUBJECT_CSID_FIELD, RELATIONS_COMMON_OBJECT_CSID_FIELD, csid);
+                logger.trace("Query string=" + queryString);
+            } catch (IllegalFormatException ife) {
+                logger.warn("Construction of formatted query string failed: ", ife);
+                return;
+            }
+            
             // 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);
+            workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_DELETED);
+            workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_LOCKED);
             LifeCycleFilter workflowStateFilter = new LifeCycleFilter(null, workflowStatesToFilter);
             
             // Perform the filtered query
@@ -90,7 +77,7 @@ public class UpdateRelationsOnDelete implements EventListener {
             // 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);
+                doc.followTransition(WorkflowClient.WORKFLOWTRANSITION_DELETE);
             }
 
         }
@@ -109,8 +96,8 @@ public class UpdateRelationsOnDelete implements EventListener {
     private boolean isDocumentSoftDeletedEvent(EventContext eventContext) {
         boolean isSoftDeletedEvent = false;
         if (eventContext instanceof DocumentEventContext) {
-            if (eventContext.getProperties().containsKey(WORKFLOWTRANSITION_TO)
-                    && eventContext.getProperties().get(WORKFLOWTRANSITION_TO).equals(WORKFLOWSTATE_DELETED)) {
+            if (eventContext.getProperties().containsKey(WorkflowClient.WORKFLOWTRANSITION_TO)
+                    && eventContext.getProperties().get(WorkflowClient.WORKFLOWTRANSITION_TO).equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
                 isSoftDeletedEvent = true;
             }
         }
index abe65e4b316272ffc918e9dbf44f7a41bef23b94..1d72d0fb5a8f991a5a39a7e9cce43419f7a04a99 100644 (file)
@@ -47,6 +47,8 @@ public class WorkflowClient extends AbstractCommonListPoxServiceClientImpl<Workf
        
        public static final String WORKFLOWTRANSITION_LOCK = "lock";
        public static final String WORKFLOWSTATE_LOCKED = "locked";
+        
+        public static final String WORKFLOWTRANSITION_TO = "to";
        //
        // DocumentHandler passed properties
        //