]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
751ab8e932a3672083b2a64e381ad62bae4ecc50
[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 import org.collectionspace.services.batch.nuxeo.UpdateDeadFlagBatchJob;
6 import org.collectionspace.services.client.workflow.WorkflowClient;
7 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectConstants;
8 import org.collectionspace.services.common.ResourceMap;
9 import org.collectionspace.services.common.invocable.InvocationResults;
10 import org.collectionspace.services.common.relation.nuxeo.RelationConstants;
11 import org.collectionspace.services.movement.nuxeo.MovementBotGardenConstants;
12 import org.collectionspace.services.movement.nuxeo.MovementConstants;
13 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
14 import org.jboss.resteasy.spi.ResteasyProviderFactory;
15 import org.nuxeo.ecm.core.api.DocumentModel;
16 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
17 import org.nuxeo.ecm.core.event.Event;
18 import org.nuxeo.ecm.core.event.EventContext;
19 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
20
21 public class UpdateDeadFlagListener extends AbstractCSEventListenerImpl {
22         final Log logger = LogFactory.getLog(UpdateDeadFlagListener.class);
23
24         /* 
25          * Set the dead flag and dead date on collectionobjects related to a new or modified movement record.
26          */
27         public void handleEvent(Event event) {
28                 EventContext ec = event.getContext();
29
30                 if (ec instanceof DocumentEventContext) {
31                         DocumentEventContext context = (DocumentEventContext) ec;
32                         DocumentModel doc = context.getSourceDocument();
33
34                         logger.debug("docType=" + doc.getType());
35
36                         if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
37                                 /*
38                                  * Handle the case where a new movement is created with action code revive, and then related
39                                  * to a collectionobject. The movement won't have any relations at the time it's created,
40                                  * so we need to capture the creation of the relation. 
41                                  */
42                                 if (doc.getType().equals(RelationConstants.NUXEO_DOCTYPE) &&
43                                                 !doc.isVersion() && 
44                                                 !doc.isProxy()) {
45                                         String subjectDocType = (String) doc.getProperty(RelationConstants.SUBJECT_DOCTYPE_SCHEMA_NAME, RelationConstants.SUBJECT_DOCTYPE_FIELD_NAME);
46                                         String objectDocType = (String) doc.getProperty(RelationConstants.OBJECT_DOCTYPE_SCHEMA_NAME, RelationConstants.OBJECT_DOCTYPE_FIELD_NAME);;
47
48                                         logger.debug("subjectDocType=" + subjectDocType + " objectDocType=" + objectDocType);
49
50                                         if (subjectDocType.equals(MovementConstants.NUXEO_DOCTYPE) && objectDocType.equals(CollectionObjectConstants.NUXEO_DOCTYPE)) {
51                                                 String movementCsid = (String) doc.getProperty(RelationConstants.SUBJECT_CSID_SCHEMA_NAME, RelationConstants.SUBJECT_CSID_FIELD_NAME);
52                                                 String collectionObjectCsid = (String) doc.getProperty(RelationConstants.OBJECT_CSID_SCHEMA_NAME, RelationConstants.OBJECT_CSID_FIELD_NAME);
53
54                                                 try {
55                                                         InvocationResults results = createUpdater().updateDeadFlag(collectionObjectCsid, movementCsid);
56
57                                                         logger.debug("updateDeadFlag complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
58                                                 } catch (Exception e) {
59                                                         logger.error(e.getMessage(), e);
60                                                 }                       
61                                         }
62                                 }
63                         }
64                         else {
65                                 /*
66                                  * Handle document modification. If the modified document was a movement record, and 
67                                  * its action code is dead or revived, update the dead flag. We don't actually have to
68                                  * check the action code here, since it will be checked inside UpdateDeadFlagBatchJob.updateRelatedDeadFlags,
69                                  * but it is an optimization.
70                                  */
71                                 if (doc.getType().startsWith(MovementConstants.NUXEO_DOCTYPE) &&
72                                                 !doc.isVersion() && 
73                                                 !doc.isProxy() && 
74                                                 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
75                                         String actionCode = (String) doc.getProperty(MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME, MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);            
76
77                                         logger.debug("actionCode=" + actionCode);
78
79                                         if (actionCode != null && (actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE) || actionCode.equals(MovementBotGardenConstants.REVIVED_ACTION_CODE))) {
80                                                 String movementCsid = doc.getName();
81
82                                                 try {
83                                                         InvocationResults results = createUpdater().updateRelatedDeadFlags(movementCsid);
84
85                                                         logger.debug("updateRelatedDeadFlags complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
86                                                 } catch (Exception e) {
87                                                         logger.error(e.getMessage(), e);
88                                                 }
89                                         }
90                                 }
91                         }
92                 }
93         }
94
95         private UpdateDeadFlagBatchJob createUpdater() {
96                 ResourceMap resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
97
98                 UpdateDeadFlagBatchJob updater = new UpdateDeadFlagBatchJob();
99                 updater.setResourceMap(resourceMap);
100
101                 return updater;
102         }
103 }