From: Aron Roberts Date: Wed, 21 Nov 2012 02:55:49 +0000 (-0800) Subject: CSPACE-5498: First pass at successfully soft deleting relation records pertaining... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=88e311ae17ce4b1b56a9d6c140b0833b21449e35;p=tmp%2Fjakarta-migration.git CSPACE-5498: First pass at successfully soft deleting relation records pertaining to a subject or object document that has itself been soft deleted. --- diff --git a/3rdparty/nuxeo/nuxeo-platform-listener/relation/pom.xml b/3rdparty/nuxeo/nuxeo-platform-listener/relation/pom.xml index 441487927..8b17c6298 100644 --- a/3rdparty/nuxeo/nuxeo-platform-listener/relation/pom.xml +++ b/3rdparty/nuxeo/nuxeo-platform-listener/relation/pom.xml @@ -1,30 +1,35 @@ - 4.0.0 - - org.collectionspace.services.3rdparty.nuxeo.listener - org.collectionspace.services - 3.2-SNAPSHOT - - org.collectionspace.services.3rdparty.nuxeo.listener.relations - org.collectionspace.services.3rdparty.nuxeo.listener.relations - http://maven.apache.org - - UTF-8 - + 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"> + 4.0.0 + + org.collectionspace.services.3rdparty.nuxeo.listener + org.collectionspace.services + 3.2-SNAPSHOT + + org.collectionspace.services.3rdparty.nuxeo.listener.relations + org.collectionspace.services.3rdparty.nuxeo.listener.relations + http://maven.apache.org + + UTF-8 + - - + + + org.collectionspace.services + org.collectionspace.services.common + ${project.version} + + - + src/main/resources true - + org.apache.maven.plugins diff --git a/3rdparty/nuxeo/nuxeo-platform-listener/relation/src/main/java/org/collectionspace/services/nuxeo/listener/relation/RelationSubOrObjDeletionListener.java b/3rdparty/nuxeo/nuxeo-platform-listener/relation/src/main/java/org/collectionspace/services/nuxeo/listener/relation/RelationSubOrObjDeletionListener.java index d45774436..b609ce3bb 100644 --- a/3rdparty/nuxeo/nuxeo-platform-listener/relation/src/main/java/org/collectionspace/services/nuxeo/listener/relation/RelationSubOrObjDeletionListener.java +++ b/3rdparty/nuxeo/nuxeo-platform-listener/relation/src/main/java/org/collectionspace/services/nuxeo/listener/relation/RelationSubOrObjDeletionListener.java @@ -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 workflowStatesToFilter = new ArrayList(); + 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; } } diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/relation/relation.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/relation/relation.xml index f00037a74..76150204d 100644 --- a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/relation/relation.xml +++ b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/relation/relation.xml @@ -162,6 +162,9 @@ relation/res/workflowState.res.xml + + deleted + GET @@ -292,6 +295,9 @@ relation/res/workflowState.res.xml + + deleted + GET