]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0d3496a77d6d97cbb191ace55c31446e7564b5ed
[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.client.workflow.WorkflowClient;
7 import org.collectionspace.services.movement.nuxeo.MovementBotGardenConstants;
8 import org.collectionspace.services.movement.nuxeo.MovementConstants;
9 import org.collectionspace.services.nuxeo.listener.AbstractCSEventSyncListenerImpl;
10
11 import org.nuxeo.ecm.core.api.DocumentModel;
12 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
13 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
14 import org.nuxeo.ecm.core.event.Event;
15 import org.nuxeo.ecm.core.event.EventContext;
16 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
17
18 public class UpdateLocationListener extends AbstractCSEventSyncListenerImpl {
19         static final Log logger = LogFactory.getLog(UpdateLocationListener.class);
20
21     @Override
22         public boolean shouldHandleEvent(Event event) {
23                 EventContext ec = event.getContext();
24
25         if (ec instanceof DocumentEventContext) {
26                 DocumentEventContext context = (DocumentEventContext) ec;
27                 DocumentModel doc = context.getSourceDocument();
28
29                 if (doc.getType().startsWith(MovementConstants.NUXEO_DOCTYPE) &&
30                                 !doc.isVersion() &&
31                                 !doc.isProxy() &&
32                                 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
33                         return true;
34                 }
35         }
36
37         return false;
38     }
39     
40         /*
41          * Set the currentLocation and previousLocation fields in a Current Location record
42          * to appropriate values.
43          *
44          * <ul>
45          * <li>If the plant is dead, set currentLocation to none</li>
46          * <li>Set the previousLocation field to the previous value of the currentLocation field</li>
47          * </ui>
48          */
49         @Override
50         public void handleCSEvent(Event event) {
51                 EventContext ec = event.getContext();
52                 DocumentEventContext context = (DocumentEventContext) ec;
53                 DocumentModel doc = context.getSourceDocument();
54
55                 String actionCode = (String) doc.getProperty(MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME,
56                                 MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
57
58                 logger.debug("actionCode=" + actionCode);
59
60                 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
61                         /*
62                          * Special case for a document that is created with an action code of dead.
63                          * In this case, we'll set the currentLocation to none, and the previousLocation to
64                          * the current value of currentLocation, since there isn't a previous value. To do
65                          * this, we can simply save the document, which will cause the beforeDocumentModification
66                          * event to fire, taking us into the other branch of this code, with the current document
67                          * becoming the previous document.
68                          */
69                         if (actionCode != null && actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE)) {
70                                 context.getCoreSession().saveDocument(doc);
71
72                                 /*
73                                  *  The saveDocument call will have caused the document to be versioned via documentModified,
74                                  *  so we can skip the versioning that would normally happen on documentCreated.
75                                  */
76                                 ec.setProperty(CreateVersionListener.SKIP_PROPERTY, true);
77                         }
78                 }
79                 else {
80                         if (actionCode != null && actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE)) {
81                                 doc.setProperty(MovementConstants.CURRENT_LOCATION_SCHEMA_NAME, MovementConstants.CURRENT_LOCATION_FIELD_NAME, MovementConstants.NONE_LOCATION);
82                         }
83
84                         DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
85                         String previousLocation = (String) previousDoc.getProperty(MovementConstants.CURRENT_LOCATION_SCHEMA_NAME, MovementConstants.CURRENT_LOCATION_FIELD_NAME);
86
87                         logger.debug("previousLocation=" + previousLocation);
88
89                         doc.setProperty(MovementConstants.PREVIOUS_LOCATION_SCHEMA_NAME, MovementConstants.PREVIOUS_LOCATION_FIELD_NAME, previousLocation);
90                 }
91         }
92         
93         @Override
94         public Log getLogger() {
95                 return logger;
96         }
97 }