1 package org.collectionspace.services.listener.botgarden;
3 import org.collectionspace.services.client.workflow.WorkflowClient;
4 import org.collectionspace.services.movement.nuxeo.MovementBotGardenConstants;
5 import org.collectionspace.services.movement.nuxeo.MovementConstants;
6 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
7 import org.nuxeo.ecm.core.api.DocumentModel;
8 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
9 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
10 import org.nuxeo.ecm.core.event.Event;
11 import org.nuxeo.ecm.core.event.EventContext;
12 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 public class UpdateLocationListener extends AbstractCSEventListenerImpl {
17 final Logger logger = LoggerFactory.getLogger(UpdateLocationListener.class);
20 * Set the currentLocation and previousLocation fields in a Current Location record
21 * to appropriate values.
24 * <li>If the plant is dead, set currentLocation to none</li>
25 * <li>Set the previousLocation field to the previous value of the currentLocation field</li>
29 public void handleEvent(Event event) {
30 EventContext ec = event.getContext();
32 if (isRegistered(event) && ec instanceof DocumentEventContext) {
33 DocumentEventContext context = (DocumentEventContext) ec;
34 DocumentModel doc = context.getSourceDocument();
36 if (doc.getType().startsWith(MovementConstants.NUXEO_DOCTYPE) &&
39 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
40 String actionCode = (String) doc.getProperty(MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME,
41 MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
43 logger.debug("actionCode=" + actionCode);
45 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
47 * Special case for a document that is created with an action code of dead.
48 * In this case, we'll set the currentLocation to none, and the previousLocation to
49 * the current value of currentLocation, since there isn't a previous value. To do
50 * this, we can simply save the document, which will cause the beforeDocumentModification
51 * event to fire, taking us into the other branch of this code, with the current document
52 * becoming the previous document.
54 if (actionCode != null && actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE)) {
55 context.getCoreSession().saveDocument(doc);
58 * The saveDocument call will have caused the document to be versioned via documentModified,
59 * so we can skip the versioning that would normally happen on documentCreated.
61 ec.setProperty(CreateVersionListener.SKIP_PROPERTY, true);
65 if (actionCode != null && actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE)) {
66 doc.setProperty(MovementConstants.CURRENT_LOCATION_SCHEMA_NAME, MovementConstants.CURRENT_LOCATION_FIELD_NAME, MovementConstants.NONE_LOCATION);
69 DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
70 String previousLocation = (String) previousDoc.getProperty(MovementConstants.CURRENT_LOCATION_SCHEMA_NAME, MovementConstants.CURRENT_LOCATION_FIELD_NAME);
72 logger.debug("previousLocation=" + previousLocation);
74 doc.setProperty(MovementConstants.PREVIOUS_LOCATION_SCHEMA_NAME, MovementConstants.PREVIOUS_LOCATION_FIELD_NAME, previousLocation);