]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3569452ce1c5396c9ffdc409b8c02cc9b7b97f62
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener.botgarden;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
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;
21
22 import org.jboss.resteasy.spi.ResteasyProviderFactory;
23
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;
29
30 public class UpdateDeadFlagListener extends AbstractCSEventSyncListenerImpl {
31         static final Log logger = LogFactory.getLog(UpdateDeadFlagListener.class);
32
33     @Override
34         public boolean shouldHandleEvent(Event event) {
35         return event.getContext() instanceof DocumentEventContext;
36     }
37     
38         /*
39          * Set the dead flag and dead date on collectionobjects related to a new or modified movement record.
40          */
41         @Override
42         public void handleCSEvent(Event event) {
43                 EventContext ec = event.getContext();
44                 DocumentEventContext context = (DocumentEventContext) ec;
45                 DocumentModel doc = context.getSourceDocument();
46
47                 logger.debug("docType=" + doc.getType());
48
49                 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
50                         /*
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.
54                          */
55                         if (doc.getType().equals(RelationConstants.NUXEO_DOCTYPE) &&
56                                         !doc.isVersion() &&
57                                         !doc.isProxy()) {
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);;
60
61                                 logger.debug("subjectDocType=" + subjectDocType + " objectDocType=" + objectDocType);
62
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);
66
67                                         try {
68                                                 InvocationResults results = createUpdater(context).updateDeadFlag(collectionObjectCsid, movementCsid);
69
70                                                 logger.debug("updateDeadFlag complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
71                                         } catch (Exception e) {
72                                                 logger.error(e.getMessage(), e);
73                                         }
74                                 }
75                         }
76                 }
77                 else {
78                         /*
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.
83                          */
84                         if (doc.getType().startsWith(MovementConstants.NUXEO_DOCTYPE) &&
85                                         !doc.isVersion() &&
86                                         !doc.isProxy() &&
87                                         !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
88                                 String actionCode = (String) doc.getProperty(MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME, MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
89
90                                 logger.debug("actionCode=" + actionCode);
91
92                                 if (actionCode != null && (actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE) || actionCode.equals(MovementBotGardenConstants.REVIVED_ACTION_CODE))) {
93                                         String movementCsid = doc.getName();
94
95                                         try {
96                                                 InvocationResults results = createUpdater(context).updateRelatedDeadFlags(movementCsid);
97
98                                                 logger.debug("updateRelatedDeadFlags complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
99                                         } catch (Exception e) {
100                                                 logger.error(e.getMessage(), e);
101                                         }
102                                 }
103                         }
104                 }
105                 }
106
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());
111
112                 serviceContext.setCurrentRepositorySession(new CoreSessionWrapper(context.getCoreSession()));
113
114                 UpdateDeadFlagBatchJob updater = new UpdateDeadFlagBatchJob();
115                 updater.setServiceContext(serviceContext);
116                 updater.setResourceMap(resourceMap);
117
118                 return updater;
119         }
120         
121         @Override
122         public Log getLogger() {
123                 return logger;
124         }
125 }