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