1 package org.collectionspace.services.listener.botgarden;
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
6 import org.jboss.resteasy.spi.ResteasyProviderFactory;
8 import org.collectionspace.services.batch.BatchResource;
9 import org.collectionspace.services.batch.nuxeo.FormatVoucherNameBatchJob;
10 import org.collectionspace.services.client.BatchClient;
11 import org.collectionspace.services.client.PoxPayloadIn;
12 import org.collectionspace.services.client.PoxPayloadOut;
13 import org.collectionspace.services.client.workflow.WorkflowClient;
14 import org.collectionspace.services.common.ResourceMap;
15 import org.collectionspace.services.common.context.ServiceContext;
16 import org.collectionspace.services.loanout.nuxeo.LoanoutBotGardenConstants;
17 import org.collectionspace.services.loanout.nuxeo.LoanoutConstants;
18 import org.collectionspace.services.nuxeo.client.java.CoreSessionWrapper;
19 import org.collectionspace.services.nuxeo.listener.AbstractCSEventSyncListenerImpl;
21 import org.nuxeo.ecm.core.api.DocumentModel;
22 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
23 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
24 import org.nuxeo.ecm.core.event.Event;
25 import org.nuxeo.ecm.core.event.EventContext;
26 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
28 public class UpdateStyledNameListener extends AbstractCSEventSyncListenerImpl {
29 public static final String RUN_AFTER_MODIFIED_PROPERTY = "UpdateStyledNameListener.RUN_AFTER_MODIFIED";
30 static final Log logger = LogFactory.getLog(UpdateStyledNameListener.class);
33 public boolean shouldHandleEvent(Event event) {
34 EventContext ec = event.getContext();
36 if (ec instanceof DocumentEventContext) {
37 DocumentEventContext context = (DocumentEventContext) ec;
38 DocumentModel doc = context.getSourceDocument();
40 logger.debug("docType=" + doc.getType());
42 if (doc.getType().startsWith(LoanoutConstants.NUXEO_DOCTYPE) &&
45 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
54 public void handleCSEvent(Event event) {
55 EventContext ec = event.getContext();
56 DocumentEventContext context = (DocumentEventContext) ec;
57 DocumentModel doc = context.getSourceDocument();
59 logger.debug("docType=" + doc.getType());
61 if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
62 DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
64 String previousLabelRequested = (String) previousDoc.getProperty(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME,
65 LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME);
66 String labelRequested = (String) doc.getProperty(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME,
67 LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME);
69 logger.debug("previousLabelRequested=" + previousLabelRequested + " labelRequested=" + labelRequested);
71 if ((previousLabelRequested == null || previousLabelRequested.equals(LoanoutBotGardenConstants.LABEL_REQUESTED_NO_VALUE)) &&
72 labelRequested.equals(LoanoutBotGardenConstants.LABEL_REQUESTED_YES_VALUE)) {
73 // The label request is changing from no to yes, so we should update the styled name.
74 ec.setProperty(RUN_AFTER_MODIFIED_PROPERTY, true);
78 boolean doUpdate = false;
80 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
81 String labelRequested = (String) doc.getProperty(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME,
82 LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME);
84 doUpdate = (labelRequested != null && labelRequested.equals(LoanoutBotGardenConstants.LABEL_REQUESTED_YES_VALUE));
86 doUpdate = ec.hasProperty(RUN_AFTER_MODIFIED_PROPERTY) && ((Boolean) ec.getProperty(RUN_AFTER_MODIFIED_PROPERTY));
90 logger.debug("Updating styled name");
92 String voucherCsid = doc.getName();
95 createFormatter(context).formatVoucherName(voucherCsid);
96 } catch (Exception e) {
97 logger.error(e.getMessage(), e);
103 private FormatVoucherNameBatchJob createFormatter(DocumentEventContext context) throws Exception {
104 ResourceMap resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
105 BatchResource batchResource = (BatchResource) resourceMap.get(BatchClient.SERVICE_NAME);
106 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext = batchResource.createServiceContext(batchResource.getServiceName());
108 serviceContext.setCurrentRepositorySession(new CoreSessionWrapper(context.getCoreSession()));
110 FormatVoucherNameBatchJob formatter = new FormatVoucherNameBatchJob();
111 formatter.setServiceContext(serviceContext);
112 formatter.setResourceMap(resourceMap);
118 public Log getLogger() {