]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
81bc6b154b759c5bdbfa976b190d401539d81bc8
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener;
2
3 import java.util.ArrayList;
4 import java.util.IllegalFormatException;
5 import java.util.List;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
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;
15
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;
22
23 public class UpdateRelationsOnDelete extends AbstractCSEventSyncListenerImpl {
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     // 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";
33
34     @Override
35         public boolean shouldHandleEvent(Event event) {
36         EventContext eventContext = event.getContext();
37
38         // Event must be a soft-delete event
39         if (isDocumentSoftDeletedEvent(eventContext)) {
40                 return true;
41         }
42         
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)) {
48             return false;
49         }
50         
51         return false;
52     }
53     
54     @Override
55     public void handleCSEvent(Event event) {        
56         EventContext eventContext = event.getContext();
57         DocumentEventContext docContext = (DocumentEventContext) eventContext;
58         DocumentModel docModel = docContext.getSourceDocument();
59         
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();
64         String queryString;
65         try {
66             queryString =
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.");
73             return;
74         }
75         
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);
83         
84         LifeCycleFilter workflowStateFilter = new LifeCycleFilter(null, workflowStatesToFilter);
85         
86         // Perform the filtered query
87         CoreSessionInterface session = new CoreSessionWrapper(docModel.getCoreSession());
88         DocumentModelList matchingDocuments;
89         try {
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());
95             return;
96         }
97
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);
102         }
103     }
104
105     @Override
106     public Log getLogger() {
107         return this.logger;
108     }
109 }