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.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;
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;
18 public class UpdateLocationListener extends AbstractCSEventSyncListenerImpl {
19 static final Log logger = LogFactory.getLog(UpdateLocationListener.class);
22 public boolean shouldHandleEvent(Event event) {
23 EventContext ec = event.getContext();
25 if (ec instanceof DocumentEventContext) {
26 DocumentEventContext context = (DocumentEventContext) ec;
27 DocumentModel doc = context.getSourceDocument();
29 if (doc.getType().startsWith(MovementConstants.NUXEO_DOCTYPE) &&
32 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
41 * Set the currentLocation and previousLocation fields in a Current Location record
42 * to appropriate values.
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>
50 public void handleCSEvent(Event event) {
51 EventContext ec = event.getContext();
52 DocumentEventContext context = (DocumentEventContext) ec;
53 DocumentModel doc = context.getSourceDocument();
55 String actionCode = (String) doc.getProperty(MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME,
56 MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
58 logger.debug("actionCode=" + actionCode);
60 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
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.
69 if (actionCode != null && actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE)) {
70 context.getCoreSession().saveDocument(doc);
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.
76 ec.setProperty(CreateVersionListener.SKIP_PROPERTY, true);
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);
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);
87 logger.debug("previousLocation=" + previousLocation);
89 doc.setProperty(MovementConstants.PREVIOUS_LOCATION_SCHEMA_NAME, MovementConstants.PREVIOUS_LOCATION_FIELD_NAME, previousLocation);
94 public Log getLogger() {