1 package org.collectionspace.services.listener;
3 import java.util.ArrayList;
4 import java.util.IllegalFormatException;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.collectionspace.services.client.workflow.WorkflowClient;
9 import org.nuxeo.ecm.core.api.ClientException;
10 import org.nuxeo.ecm.core.api.CoreSession;
11 import org.nuxeo.ecm.core.api.DocumentModel;
12 import org.nuxeo.ecm.core.api.DocumentModelList;
13 import org.nuxeo.ecm.core.api.impl.LifeCycleFilter;
14 import org.nuxeo.ecm.core.event.Event;
15 import org.nuxeo.ecm.core.event.EventContext;
16 import org.nuxeo.ecm.core.event.EventListener;
17 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
19 public class UpdateRelationsOnDelete implements EventListener {
21 // FIXME: We might experiment here with using log4j instead of Apache Commons Logging;
22 // am using the latter to follow Ray's pattern for now
23 final Log logger = LogFactory.getLog(UpdateRelationsOnDelete.class);
25 // FIXME: Get these constant values from external sources rather than redeclaring here
26 final static String RELATION_DOCTYPE = "Relation";
27 final static String RELATIONS_COMMON_SUBJECT_CSID_FIELD = "relations_common:subjectCsid";
28 final static String RELATIONS_COMMON_OBJECT_CSID_FIELD = "relations_common:objectCsid";
31 public void handleEvent(Event event) throws ClientException {
32 logger.trace("In handleEvent in UpdateRelationsOnDelete ...");
34 EventContext eventContext = event.getContext();
36 if (isDocumentSoftDeletedEvent(eventContext)) {
38 logger.trace("A soft deletion event was received by UpdateRelationsOnDelete ...");
40 DocumentEventContext docContext = (DocumentEventContext) eventContext;
41 DocumentModel docModel = docContext.getSourceDocument();
43 // Exclude soft deletion events involving Relation records themselves
44 // from handling by this event handler.
45 if (docModel != null && docModel.getType().startsWith(RELATION_DOCTYPE)) {
49 // Retrieve a list of relation records, where the soft deleted
50 // document provided in the context of the current event is
51 // either the subject or object of any relation
53 // Build a query string
54 String csid = docModel.getName();
59 String.format("SELECT * FROM Relation WHERE ecm:isProxy = 0 AND (%1$s='%3$s' OR %2$s='%3$s')",
60 RELATIONS_COMMON_SUBJECT_CSID_FIELD, RELATIONS_COMMON_OBJECT_CSID_FIELD, csid);
61 logger.trace("Query string=" + queryString);
62 } catch (IllegalFormatException ife) {
63 logger.warn("Construction of formatted query string failed: ", ife);
64 logger.warn("Actions in this event listener will NOT be performed, as a result of a previous Exception.");
68 // Create a filter to exclude from the list results any records
69 // that have already been soft deleted or are locked
70 List<String> workflowStatesToFilter = new ArrayList<String>();
71 workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_DELETED);
72 workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_LOCKED);
73 LifeCycleFilter workflowStateFilter = new LifeCycleFilter(null, workflowStatesToFilter);
75 // Perform the filtered query
76 CoreSession session = docModel.getCoreSession();
77 DocumentModelList matchingDocuments;
79 matchingDocuments = session.query(queryString.toString(), workflowStateFilter);
80 } catch (ClientException ce) {
81 logger.warn("Error attempting to retrieve relation records where "
82 + "record of type '" + docModel.getType() + "' with CSID " + csid
83 + " is the subject or object of any relation: " + ce.getMessage());
87 // Cycle through the list results, soft deleting each matching relation record
88 logger.info("Attempting to soft delete " + matchingDocuments.size() + " relation records pertaining to a soft deleted record.");
89 for (DocumentModel doc : matchingDocuments) {
90 doc.followTransition(WorkflowClient.WORKFLOWTRANSITION_DELETE);
97 // FIXME: Generic methods like the following might be split off
98 // into an event utilities class. - ADR 2012-12-05
101 * Identifies whether a supplied event concerns a document that has
102 * been transitioned to the 'deleted' workflow state.
104 * @param eventContext an event context
106 * @return true if this event concerns a document that has
107 * been transitioned to the 'deleted' workflow state.
109 private boolean isDocumentSoftDeletedEvent(EventContext eventContext) {
110 boolean isSoftDeletedEvent = false;
111 if (eventContext instanceof DocumentEventContext) {
112 if (eventContext.getProperties().containsKey(WorkflowClient.WORKFLOWTRANSITION_TO)
113 && eventContext.getProperties().get(WorkflowClient.WORKFLOWTRANSITION_TO).equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
114 isSoftDeletedEvent = true;
117 return isSoftDeletedEvent;