]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c07711e89f1b5a6c7bd9e0dd7dc532d6219467b2
[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.client.workflow.WorkflowClient;
6 import org.collectionspace.services.movement.nuxeo.MovementBotGardenConstants;
7 import org.collectionspace.services.movement.nuxeo.MovementConstants;
8 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
9 import org.nuxeo.ecm.core.api.DocumentModel;
10 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
11 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
12 import org.nuxeo.ecm.core.event.Event;
13 import org.nuxeo.ecm.core.event.EventContext;
14 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
15
16 public class UpdateLocationListener extends AbstractCSEventListenerImpl {
17         final Log logger = LogFactory.getLog(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         public void handleEvent(Event event) {
29                 EventContext ec = event.getContext();
30
31                 if (ec instanceof DocumentEventContext) {
32                         DocumentEventContext context = (DocumentEventContext) ec;
33                         DocumentModel doc = context.getSourceDocument();
34
35                         if (doc.getType().startsWith(MovementConstants.NUXEO_DOCTYPE) && 
36                                         !doc.isVersion() && 
37                                         !doc.isProxy() && 
38                                         !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
39                                 String actionCode = (String) doc.getProperty(MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME, 
40                                                 MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
41
42                                 logger.debug("actionCode=" + actionCode);
43
44                                 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
45                                         /*
46                                          * Special case for a document that is created with an action code of dead.
47                                          * In this case, we'll set the currentLocation to none, and the previousLocation to
48                                          * the current value of currentLocation, since there isn't a previous value. To do 
49                                          * this, we can simply save the document, which will cause the beforeDocumentModification
50                                          * event to fire, taking us into the other branch of this code, with the current document
51                                          * becoming the previous document.
52                                          */
53                                         if (actionCode != null && actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE)) {
54                                                 context.getCoreSession().saveDocument(doc);
55
56                                                 /*
57                                                  *  The saveDocument call will have caused the document to be versioned via documentModified,
58                                                  *  so we can skip the versioning that would normally happen on documentCreated.
59                                                  */
60                                                 ec.setProperty(CreateVersionListener.SKIP_PROPERTY, true);
61                                         }
62                                 }
63                                 else {                  
64                                         if (actionCode != null && actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE)) {
65                                                 doc.setProperty(MovementConstants.CURRENT_LOCATION_SCHEMA_NAME, MovementConstants.CURRENT_LOCATION_FIELD_NAME, MovementConstants.NONE_LOCATION);
66                                         }
67
68                                         DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);                    
69                                         String previousLocation = (String) previousDoc.getProperty(MovementConstants.CURRENT_LOCATION_SCHEMA_NAME, MovementConstants.CURRENT_LOCATION_FIELD_NAME);
70
71                                         logger.debug("previousLocation=" + previousLocation);
72
73                                         doc.setProperty(MovementConstants.PREVIOUS_LOCATION_SCHEMA_NAME, MovementConstants.PREVIOUS_LOCATION_FIELD_NAME, previousLocation);
74                                 }
75                         }
76                 }
77         }
78 }