1 package org.collectionspace.services.listener;
3 import java.util.ArrayList;
4 import java.util.IllegalFormatException;
7 import org.collectionspace.services.client.workflow.WorkflowClient;
8 import org.collectionspace.services.common.document.DocumentException;
9 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
10 import org.collectionspace.services.nuxeo.client.java.CoreSessionWrapper;
11 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
12 import org.nuxeo.ecm.core.api.DocumentModel;
13 import org.nuxeo.ecm.core.api.DocumentModelList;
14 import org.nuxeo.ecm.core.api.impl.LifeCycleFilter;
15 import org.nuxeo.ecm.core.event.Event;
16 import org.nuxeo.ecm.core.event.EventContext;
17 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 public class UpdateRelationsOnDelete extends AbstractCSEventListenerImpl {
22 final Logger logger = LoggerFactory.getLogger(UpdateRelationsOnDelete.class);
24 // FIXME: Get these constant values from external sources rather than redeclaring here
25 final static String RELATION_DOCTYPE = "Relation";
26 final static String RELATIONS_COMMON_SUBJECT_CSID_FIELD = "relations_common:subjectCsid";
27 final static String RELATIONS_COMMON_OBJECT_CSID_FIELD = "relations_common:objectCsid";
30 public void handleEvent(Event event) {
31 logger.trace("In handleEvent in UpdateRelationsOnDelete ...");
33 EventContext eventContext = event.getContext();
35 if (isRegistered(event) && isDocumentSoftDeletedEvent(eventContext)) {
37 logger.trace("A soft deletion event was received by UpdateRelationsOnDelete ...");
39 DocumentEventContext docContext = (DocumentEventContext) eventContext;
40 DocumentModel docModel = docContext.getSourceDocument();
42 // Exclude soft deletion events involving Relation records themselves
43 // from handling by this event handler.
44 if (docModel != null && docModel.getType().startsWith(RELATION_DOCTYPE)) {
48 // Retrieve a list of relation records, where the soft deleted
49 // document provided in the context of the current event is
50 // either the subject or object of any relation
52 // Build a query string
53 String csid = docModel.getName();
58 String.format("SELECT * FROM Relation WHERE ecm:isProxy = 0 AND (%1$s='%3$s' OR %2$s='%3$s')",
59 RELATIONS_COMMON_SUBJECT_CSID_FIELD, RELATIONS_COMMON_OBJECT_CSID_FIELD, csid);
60 logger.trace("Query string=" + queryString);
61 } catch (IllegalFormatException ife) {
62 logger.warn("Construction of formatted query string failed: ", ife);
63 logger.warn("Actions in this event listener will NOT be performed, as a result of a previous Exception.");
67 // Create a filter to exclude from the list results any records
68 // that have already been soft deleted or are locked
69 List<String> workflowStatesToFilter = new ArrayList<String>();
70 workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_DELETED);
71 workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_LOCKED);
72 workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_LOCKED_DELETED);
73 workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED);
75 LifeCycleFilter workflowStateFilter = new LifeCycleFilter(null, workflowStatesToFilter);
77 // Perform the filtered query
78 CoreSessionInterface session = new CoreSessionWrapper(docModel.getCoreSession());
79 DocumentModelList matchingDocuments;
81 matchingDocuments = session.query(queryString.toString(), workflowStateFilter);
82 } catch (DocumentException ce) {
83 logger.error("Error attempting to retrieve relation records where "
84 + "record of type '" + docModel.getType() + "' with CSID " + csid
85 + " is the subject or object of any relation: " + ce.getMessage());
89 // Cycle through the list results, soft deleting each matching relation record
90 logger.info("Attempting to soft delete " + matchingDocuments.size() + " relation records pertaining to a soft deleted record.");
91 for (DocumentModel doc : matchingDocuments) {
92 doc.followTransition(WorkflowClient.WORKFLOWTRANSITION_DELETE);
99 // FIXME: Generic methods like the following might be split off
100 // into an event utilities class. - ADR 2012-12-05
103 * Identifies whether a supplied event concerns a document that has
104 * been transitioned to the 'deleted' workflow state.
106 * @param eventContext an event context
108 * @return true if this event concerns a document that has
109 * been transitioned to the 'deleted' workflow state.
111 private boolean isDocumentSoftDeletedEvent(EventContext eventContext) {
112 boolean isSoftDeletedEvent = false;
114 if (eventContext instanceof DocumentEventContext) {
115 if (eventContext.getProperties().containsKey(WorkflowClient.WORKFLOWTRANSITION_TO)
117 (eventContext.getProperties().get(WorkflowClient.WORKFLOWTRANSITION_TO).equals(WorkflowClient.WORKFLOWSTATE_DELETED)
119 eventContext.getProperties().get(WorkflowClient.WORKFLOWTRANSITION_TO).equals(WorkflowClient.WORKFLOWSTATE_LOCKED_DELETED))) {
120 isSoftDeletedEvent = true;
124 return isSoftDeletedEvent;