1 package org.collectionspace.services.listener;
3 import java.util.ArrayList;
4 import java.util.IllegalFormatException;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
10 import org.collectionspace.services.client.workflow.WorkflowClient;
11 import org.collectionspace.services.common.document.DocumentException;
12 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
13 import org.collectionspace.services.nuxeo.client.java.CoreSessionWrapper;
14 import org.collectionspace.services.nuxeo.listener.AbstractCSEventSyncListenerImpl;
16 import org.nuxeo.ecm.core.api.DocumentModel;
17 import org.nuxeo.ecm.core.api.DocumentModelList;
18 import org.nuxeo.ecm.core.api.impl.LifeCycleFilter;
19 import org.nuxeo.ecm.core.event.Event;
20 import org.nuxeo.ecm.core.event.EventContext;
21 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
23 public class UpdateRelationsOnDelete extends AbstractCSEventSyncListenerImpl {
25 // FIXME: We might experiment here with using log4j instead of Apache Commons Logging;
26 // am using the latter to follow Ray's pattern for now
27 final Log logger = LogFactory.getLog(UpdateRelationsOnDelete.class);
29 // FIXME: Get these constant values from external sources rather than redeclaring here
30 final static String RELATION_DOCTYPE = "Relation";
31 final static String RELATIONS_COMMON_SUBJECT_CSID_FIELD = "relations_common:subjectCsid";
32 final static String RELATIONS_COMMON_OBJECT_CSID_FIELD = "relations_common:objectCsid";
35 public boolean shouldHandleEvent(Event event) {
36 EventContext eventContext = event.getContext();
38 // Event must be a soft-delete event
39 if (isDocumentSoftDeletedEvent(eventContext)) {
43 // Exclude soft deletion events involving Relation records themselves
44 // from handling by this event handler.
45 DocumentEventContext docContext = (DocumentEventContext) eventContext;
46 DocumentModel docModel = docContext.getSourceDocument();
47 if (docModel != null && docModel.getType().startsWith(RELATION_DOCTYPE)) {
55 public void handleCSEvent(Event event) {
56 EventContext eventContext = event.getContext();
57 DocumentEventContext docContext = (DocumentEventContext) eventContext;
58 DocumentModel docModel = docContext.getSourceDocument();
60 // Retrieve a list of relation records, where the soft deleted
61 // document provided in the context of the current event is
62 // either the subject or object of any relation
63 String csid = docModel.getName();
67 String.format("SELECT * FROM Relation WHERE ecm:isProxy = 0 AND (%1$s='%3$s' OR %2$s='%3$s')",
68 RELATIONS_COMMON_SUBJECT_CSID_FIELD, RELATIONS_COMMON_OBJECT_CSID_FIELD, csid);
69 logger.trace("Query string=" + queryString);
70 } catch (IllegalFormatException ife) {
71 logger.warn("Construction of formatted query string failed: ", ife);
72 logger.warn("Actions in this event listener will NOT be performed, as a result of a previous Exception.");
76 // Create a filter to exclude from the list results any records
77 // that have already been soft deleted or are locked
78 List<String> workflowStatesToFilter = new ArrayList<String>();
79 workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_DELETED);
80 workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_LOCKED);
81 workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_LOCKED_DELETED);
82 workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED);
84 LifeCycleFilter workflowStateFilter = new LifeCycleFilter(null, workflowStatesToFilter);
86 // Perform the filtered query
87 CoreSessionInterface session = new CoreSessionWrapper(docModel.getCoreSession());
88 DocumentModelList matchingDocuments;
90 matchingDocuments = session.query(queryString.toString(), workflowStateFilter);
91 } catch (DocumentException ce) {
92 logger.error("Error attempting to retrieve relation records where "
93 + "record of type '" + docModel.getType() + "' with CSID " + csid
94 + " is the subject or object of any relation: " + ce.getMessage());
98 // Cycle through the list results, soft deleting each matching relation record
99 logger.info("Attempting to soft delete " + matchingDocuments.size() + " relation records pertaining to a soft deleted record.");
100 for (DocumentModel doc : matchingDocuments) {
101 doc.followTransition(WorkflowClient.WORKFLOWTRANSITION_DELETE);
106 public Log getLogger() {