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.workflow.WorkflowClient;
9 import org.collectionspace.services.common.ResourceMap;
10 import org.collectionspace.services.loanout.nuxeo.LoanoutBotGardenConstants;
11 import org.collectionspace.services.loanout.nuxeo.LoanoutConstants;
12 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
13 import org.nuxeo.ecm.core.api.DocumentModel;
14 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
15 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
16 import org.nuxeo.ecm.core.event.Event;
17 import org.nuxeo.ecm.core.event.EventContext;
18 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
20 public class UpdateStyledNameListener extends AbstractCSEventListenerImpl {
21 public static final String RUN_AFTER_MODIFIED_PROPERTY = "UpdateStyledNameListener.RUN_AFTER_MODIFIED";
23 final Log logger = LogFactory.getLog(UpdateStyledNameListener.class);
26 public void handleEvent(Event event) {
27 EventContext ec = event.getContext();
29 if (isRegistered(event) && ec instanceof DocumentEventContext) {
30 DocumentEventContext context = (DocumentEventContext) ec;
31 DocumentModel doc = context.getSourceDocument();
33 logger.debug("docType=" + doc.getType());
35 if (doc.getType().startsWith(LoanoutConstants.NUXEO_DOCTYPE) &&
38 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
40 if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
41 DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
43 String previousLabelRequested = (String) previousDoc.getProperty(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME,
44 LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME);
45 String labelRequested = (String) doc.getProperty(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME,
46 LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME);
48 logger.debug("previousLabelRequested=" + previousLabelRequested + " labelRequested=" + labelRequested);
50 if ((previousLabelRequested == null || previousLabelRequested.equals(LoanoutBotGardenConstants.LABEL_REQUESTED_NO_VALUE)) &&
51 labelRequested.equals(LoanoutBotGardenConstants.LABEL_REQUESTED_YES_VALUE)) {
52 // The label request is changing from no to yes, so we should update the styled name.
53 ec.setProperty(RUN_AFTER_MODIFIED_PROPERTY, true);
57 boolean doUpdate = false;
59 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
60 String labelRequested = (String) doc.getProperty(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME,
61 LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME);
63 doUpdate = (labelRequested != null && labelRequested.equals(LoanoutBotGardenConstants.LABEL_REQUESTED_YES_VALUE));
65 doUpdate = ec.hasProperty(RUN_AFTER_MODIFIED_PROPERTY) && ((Boolean) ec.getProperty(RUN_AFTER_MODIFIED_PROPERTY));
69 logger.debug("Updating styled name");
71 String voucherCsid = doc.getName();
74 createFormatter().formatVoucherName(voucherCsid);
75 } catch (Exception e) {
76 logger.error(e.getMessage(), e);
84 private FormatVoucherNameBatchJob createFormatter() {
85 ResourceMap resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
87 FormatVoucherNameBatchJob formatter = new FormatVoucherNameBatchJob();
88 formatter.setResourceMap(resourceMap);