]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bc54278a2672e3c2417a328bee07258437eadf28
[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.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;
20
21 public class UpdateRelationsOnDelete extends AbstractCSEventListenerImpl {
22     final Logger logger = LoggerFactory.getLogger(UpdateRelationsOnDelete.class);
23
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";
28
29     @Override
30     public void handleEvent(Event event) {
31         logger.trace("In handleEvent in UpdateRelationsOnDelete ...");
32
33         EventContext eventContext = event.getContext();
34
35         if (isRegistered(event) && isDocumentSoftDeletedEvent(eventContext)) {
36
37             logger.trace("A soft deletion event was received by UpdateRelationsOnDelete ...");
38
39             DocumentEventContext docContext = (DocumentEventContext) eventContext;
40             DocumentModel docModel = docContext.getSourceDocument();
41
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)) {
45                 return;
46             }
47
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
51
52             // Build a query string
53             String csid = docModel.getName();
54
55             String queryString;
56             try {
57                 queryString =
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.");
64                 return;
65             }
66
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);
74
75             LifeCycleFilter workflowStateFilter = new LifeCycleFilter(null, workflowStatesToFilter);
76
77             // Perform the filtered query
78             CoreSessionInterface session = new CoreSessionWrapper(docModel.getCoreSession());
79             DocumentModelList matchingDocuments;
80             try {
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());
86                 return;
87             }
88
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);
93             }
94
95         }
96
97     }
98
99     // FIXME: Generic methods like the following might be split off
100     // into an event utilities class. - ADR 2012-12-05
101
102     /**
103      * Identifies whether a supplied event concerns a document that has
104      * been transitioned to the 'deleted' workflow state.
105      *
106      * @param eventContext an event context
107      *
108      * @return true if this event concerns a document that has
109      * been transitioned to the 'deleted' workflow state.
110      */
111     private boolean isDocumentSoftDeletedEvent(EventContext eventContext) {
112         boolean isSoftDeletedEvent = false;
113
114         if (eventContext instanceof DocumentEventContext) {
115             if (eventContext.getProperties().containsKey(WorkflowClient.WORKFLOWTRANSITION_TO)
116                     &&
117                 (eventContext.getProperties().get(WorkflowClient.WORKFLOWTRANSITION_TO).equals(WorkflowClient.WORKFLOWSTATE_DELETED)
118                                 ||
119                 eventContext.getProperties().get(WorkflowClient.WORKFLOWTRANSITION_TO).equals(WorkflowClient.WORKFLOWSTATE_LOCKED_DELETED))) {
120                 isSoftDeletedEvent = true;
121             }
122         }
123
124         return isSoftDeletedEvent;
125     }
126 }