1 package org.collectionspace.services.listener.botgarden;
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.jboss.resteasy.spi.ResteasyProviderFactory;
7 import org.collectionspace.services.batch.nuxeo.FormatVoucherNameBatchJob;
8 import org.collectionspace.services.client.PoxPayloadIn;
9 import org.collectionspace.services.client.PoxPayloadOut;
10 import org.collectionspace.services.client.workflow.WorkflowClient;
11 import org.collectionspace.services.common.ResourceMap;
12 import org.collectionspace.services.loanout.nuxeo.LoanoutBotGardenConstants;
13 import org.collectionspace.services.loanout.nuxeo.LoanoutConstants;
14 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
15 import org.nuxeo.ecm.core.api.DocumentModel;
16 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
17 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
18 import org.nuxeo.ecm.core.event.Event;
19 import org.nuxeo.ecm.core.event.EventContext;
20 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
22 public class UpdateStyledNameListener extends AbstractCSEventListenerImpl {
23 public static final String RUN_AFTER_MODIFIED_PROPERTY = "UpdateStyledNameListener.RUN_AFTER_MODIFIED";
25 final Log logger = LogFactory.getLog(UpdateStyledNameListener.class);
27 public void handleEvent(Event event) {
28 EventContext ec = event.getContext();
30 if (ec instanceof DocumentEventContext) {
31 DocumentEventContext context = (DocumentEventContext) ec;
32 DocumentModel doc = context.getSourceDocument();
34 logger.debug("docType=" + doc.getType());
36 if (doc.getType().startsWith(LoanoutConstants.NUXEO_DOCTYPE) &&
39 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
41 if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
42 DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
44 String previousLabelRequested = (String) previousDoc.getProperty(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME,
45 LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME);
46 String labelRequested = (String) doc.getProperty(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME,
47 LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME);
49 logger.debug("previousLabelRequested=" + previousLabelRequested + " labelRequested=" + labelRequested);
51 if ((previousLabelRequested == null || previousLabelRequested.equals(LoanoutBotGardenConstants.LABEL_REQUESTED_NO_VALUE)) &&
52 labelRequested.equals(LoanoutBotGardenConstants.LABEL_REQUESTED_YES_VALUE)) {
53 // The label request is changing from no to yes, so we should update the styled name.
54 ec.setProperty(RUN_AFTER_MODIFIED_PROPERTY, true);
58 boolean doUpdate = false;
60 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
61 String labelRequested = (String) doc.getProperty(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME,
62 LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME);
64 doUpdate = (labelRequested != null && labelRequested.equals(LoanoutBotGardenConstants.LABEL_REQUESTED_YES_VALUE));
66 doUpdate = ec.hasProperty(RUN_AFTER_MODIFIED_PROPERTY) && ((Boolean) ec.getProperty(RUN_AFTER_MODIFIED_PROPERTY));
70 logger.debug("Updating styled name");
72 String voucherCsid = doc.getName();
75 createFormatter().formatVoucherName(voucherCsid);
76 } catch (Exception e) {
77 logger.error(e.getMessage(), e);
85 private FormatVoucherNameBatchJob createFormatter() {
86 ResourceMap<PoxPayloadIn, PoxPayloadOut> resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
88 FormatVoucherNameBatchJob formatter = new FormatVoucherNameBatchJob();
89 formatter.setResourceMap(resourceMap);