1 package org.collectionspace.services.listener.botgarden;
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
6 import org.collectionspace.services.batch.BatchResource;
7 import org.collectionspace.services.batch.nuxeo.UpdateDeadFlagBatchJob;
8 import org.collectionspace.services.client.BatchClient;
9 import org.collectionspace.services.client.PoxPayloadIn;
10 import org.collectionspace.services.client.PoxPayloadOut;
11 import org.collectionspace.services.client.workflow.WorkflowClient;
12 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectConstants;
13 import org.collectionspace.services.common.ResourceMap;
14 import org.collectionspace.services.common.context.ServiceContext;
15 import org.collectionspace.services.common.invocable.InvocationResults;
16 import org.collectionspace.services.common.relation.nuxeo.RelationConstants;
17 import org.collectionspace.services.movement.nuxeo.MovementBotGardenConstants;
18 import org.collectionspace.services.movement.nuxeo.MovementConstants;
19 import org.collectionspace.services.nuxeo.client.java.CoreSessionWrapper;
20 import org.collectionspace.services.nuxeo.listener.AbstractCSEventSyncListenerImpl;
22 import org.jboss.resteasy.spi.ResteasyProviderFactory;
24 import org.nuxeo.ecm.core.api.DocumentModel;
25 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
26 import org.nuxeo.ecm.core.event.Event;
27 import org.nuxeo.ecm.core.event.EventContext;
28 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
30 public class UpdateDeadFlagListener extends AbstractCSEventSyncListenerImpl {
31 static final Log logger = LogFactory.getLog(UpdateDeadFlagListener.class);
34 public boolean shouldHandleEvent(Event event) {
35 return event.getContext() instanceof DocumentEventContext;
39 * Set the dead flag and dead date on collectionobjects related to a new or modified movement record.
42 public void handleCSEvent(Event event) {
43 EventContext ec = event.getContext();
44 DocumentEventContext context = (DocumentEventContext) ec;
45 DocumentModel doc = context.getSourceDocument();
47 logger.debug("docType=" + doc.getType());
49 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
51 * Handle the case where a new movement is created with action code revive, and then related
52 * to a collectionobject. The movement won't have any relations at the time it's created,
53 * so we need to capture the creation of the relation.
55 if (doc.getType().equals(RelationConstants.NUXEO_DOCTYPE) &&
58 String subjectDocType = (String) doc.getProperty(RelationConstants.SUBJECT_DOCTYPE_SCHEMA_NAME, RelationConstants.SUBJECT_DOCTYPE_FIELD_NAME);
59 String objectDocType = (String) doc.getProperty(RelationConstants.OBJECT_DOCTYPE_SCHEMA_NAME, RelationConstants.OBJECT_DOCTYPE_FIELD_NAME);;
61 logger.debug("subjectDocType=" + subjectDocType + " objectDocType=" + objectDocType);
63 if (subjectDocType.equals(MovementConstants.NUXEO_DOCTYPE) && objectDocType.equals(CollectionObjectConstants.NUXEO_DOCTYPE)) {
64 String movementCsid = (String) doc.getProperty(RelationConstants.SUBJECT_CSID_SCHEMA_NAME, RelationConstants.SUBJECT_CSID_FIELD_NAME);
65 String collectionObjectCsid = (String) doc.getProperty(RelationConstants.OBJECT_CSID_SCHEMA_NAME, RelationConstants.OBJECT_CSID_FIELD_NAME);
68 InvocationResults results = createUpdater(context).updateDeadFlag(collectionObjectCsid, movementCsid);
70 logger.debug("updateDeadFlag complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
71 } catch (Exception e) {
72 logger.error(e.getMessage(), e);
79 * Handle document modification. If the modified document was a movement record, and
80 * its action code is dead or revived, update the dead flag. We don't actually have to
81 * check the action code here, since it will be checked inside UpdateDeadFlagBatchJob.updateRelatedDeadFlags,
82 * but it is an optimization.
84 if (doc.getType().startsWith(MovementConstants.NUXEO_DOCTYPE) &&
87 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
88 String actionCode = (String) doc.getProperty(MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME, MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
90 logger.debug("actionCode=" + actionCode);
92 if (actionCode != null && (actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE) || actionCode.equals(MovementBotGardenConstants.REVIVED_ACTION_CODE))) {
93 String movementCsid = doc.getName();
96 InvocationResults results = createUpdater(context).updateRelatedDeadFlags(movementCsid);
98 logger.debug("updateRelatedDeadFlags complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
99 } catch (Exception e) {
100 logger.error(e.getMessage(), e);
107 private UpdateDeadFlagBatchJob createUpdater(DocumentEventContext context) throws Exception {
108 ResourceMap resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
109 BatchResource batchResource = (BatchResource) resourceMap.get(BatchClient.SERVICE_NAME);
110 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext = batchResource.createServiceContext(batchResource.getServiceName());
112 serviceContext.setCurrentRepositorySession(new CoreSessionWrapper(context.getCoreSession()));
114 UpdateDeadFlagBatchJob updater = new UpdateDeadFlagBatchJob();
115 updater.setServiceContext(serviceContext);
116 updater.setResourceMap(resourceMap);
122 public Log getLogger() {