]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b537c6a901418a7e81847306b15b8b46deab6e05
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener.botgarden;
2
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;
15
16 public class UpdateLocationListener extends AbstractCSEventListenerImpl {
17         final Logger logger = LoggerFactory.getLogger(UpdateLocationListener.class);
18
19         /*
20          * Set the currentLocation and previousLocation fields in a Current Location record
21          * to appropriate values.
22          *
23          * <ul>
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>
26          * </ui>
27          */
28         @Override
29         public void handleEvent(Event event) {
30                 EventContext ec = event.getContext();
31
32                 if (isRegistered(event) && ec instanceof DocumentEventContext) {
33                         DocumentEventContext context = (DocumentEventContext) ec;
34                         DocumentModel doc = context.getSourceDocument();
35
36                         if (doc.getType().startsWith(MovementConstants.NUXEO_DOCTYPE) &&
37                                         !doc.isVersion() &&
38                                         !doc.isProxy() &&
39                                         !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
40                                 String actionCode = (String) doc.getProperty(MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME,
41                                                 MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
42
43                                 logger.debug("actionCode=" + actionCode);
44
45                                 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
46                                         /*
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.
53                                          */
54                                         if (actionCode != null && actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE)) {
55                                                 context.getCoreSession().saveDocument(doc);
56
57                                                 /*
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.
60                                                  */
61                                                 ec.setProperty(CreateVersionListener.SKIP_PROPERTY, true);
62                                         }
63                                 }
64                                 else {
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);
67                                         }
68
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);
71
72                                         logger.debug("previousLocation=" + previousLocation);
73
74                                         doc.setProperty(MovementConstants.PREVIOUS_LOCATION_SCHEMA_NAME, MovementConstants.PREVIOUS_LOCATION_FIELD_NAME, previousLocation);
75                                 }
76                         }
77                 }
78         }
79 }