]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b47f5ef4b03909d042c15df852d353ac5eff878c
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 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;
18
19 public class UpdateRelationsOnDelete implements EventListener {
20     
21     // FIXME: Get these constant values from external sources rather than redeclaring here
22     final static String RELATIONS_COMMON_SUBJECT_CSID_FIELD = "relations_common:subjectCsid";
23     final static String RELATIONS_COMMON_OBJECT_CSID_FIELD = "relations_common:objectCsid";
24
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);
28
29     @Override
30     public void handleEvent(Event event) throws ClientException {
31         logger.info("In handleEvent in UpdateRelationsOnDelete ...");
32         
33         EventContext eventContext = event.getContext();
34
35         if (isDocumentSoftDeletedEvent(eventContext)) {
36             
37             DocumentEventContext docContext = (DocumentEventContext) eventContext;
38             DocumentModel docModel = docContext.getSourceDocument();
39             
40             // Retrieve a list of relation records, where the soft deleted
41             // document provided in the context of the current event is
42             // either the subject or object of any relation
43             
44             // Build a query string
45             String csid = docModel.getName();
46             
47             String queryString;
48             try {
49                 queryString =
50                     String.format("SELECT * FROM Relation WHERE ecm:isProxy = 0 AND (%1$s='%3$s' OR %2$s='%3$s')",
51                     RELATIONS_COMMON_SUBJECT_CSID_FIELD, RELATIONS_COMMON_OBJECT_CSID_FIELD, csid);
52                 logger.trace("Query string=" + queryString);
53             } catch (IllegalFormatException ife) {
54                 logger.warn("Construction of formatted query string failed: ", ife);
55                 return;
56             }
57             
58             // Create a filter to exclude from the list results any records
59             // that have already been soft deleted or are locked
60             List<String> workflowStatesToFilter = new ArrayList<String>();
61             workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_DELETED);
62             workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_LOCKED);
63             LifeCycleFilter workflowStateFilter = new LifeCycleFilter(null, workflowStatesToFilter);
64             
65             // Perform the filtered query
66             CoreSession session = docModel.getCoreSession();
67             DocumentModelList matchingDocuments;
68             try {
69                 matchingDocuments = session.query(queryString.toString(), workflowStateFilter);
70             } catch (ClientException ce) {
71                 logger.warn("Error attempting to retrieve relation records where "
72                         + "record of type '" + docModel.getType() + "' with CSID " + csid
73                         + " is the subject or object of any relation: " + ce.getMessage());
74                 throw ce;
75             }
76
77             // Cycle through the list results, soft deleting each matching relation record
78             logger.trace("Attempting to soft delete " + matchingDocuments.size() + " relation records.");
79             for (DocumentModel doc : matchingDocuments) {
80                 doc.followTransition(WorkflowClient.WORKFLOWTRANSITION_DELETE);
81             }
82
83         }
84
85     }
86
87     /**
88      * Identifies whether a supplied event concerns a document that has
89      * been transitioned to the 'deleted' workflow state.
90      * 
91      * @param eventContext an event context
92      * 
93      * @return true if this event concerns a document that has
94      * been transitioned to the 'deleted' workflow state.
95      */
96     private boolean isDocumentSoftDeletedEvent(EventContext eventContext) {
97         boolean isSoftDeletedEvent = false;
98         if (eventContext instanceof DocumentEventContext) {
99             if (eventContext.getProperties().containsKey(WorkflowClient.WORKFLOWTRANSITION_TO)
100                     && eventContext.getProperties().get(WorkflowClient.WORKFLOWTRANSITION_TO).equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
101                 isSoftDeletedEvent = true;
102             }
103         }
104         return isSoftDeletedEvent;
105     }
106 }