]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c9656ea9f2c25a9787b0b1c4f47fd0ddecd405c6
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener.botgarden;
2
3 import org.jboss.resteasy.spi.ResteasyProviderFactory;
4
5 import org.collectionspace.services.batch.nuxeo.FormatVoucherNameBatchJob;
6 import org.collectionspace.services.client.workflow.WorkflowClient;
7 import org.collectionspace.services.common.ResourceMap;
8 import org.collectionspace.services.loanout.nuxeo.LoanoutBotGardenConstants;
9 import org.collectionspace.services.loanout.nuxeo.LoanoutConstants;
10 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
11 import org.nuxeo.ecm.core.api.DocumentModel;
12 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
13 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
14 import org.nuxeo.ecm.core.event.Event;
15 import org.nuxeo.ecm.core.event.EventContext;
16 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class UpdateStyledNameListener extends AbstractCSEventListenerImpl {
21         public static final String RUN_AFTER_MODIFIED_PROPERTY = "UpdateStyledNameListener.RUN_AFTER_MODIFIED";
22
23         final Logger logger = LoggerFactory.getLogger(UpdateStyledNameListener.class);
24
25         @Override
26         public void handleEvent(Event event) {
27                 EventContext ec = event.getContext();
28
29                 if (isRegistered(event) && ec instanceof DocumentEventContext) {
30                         DocumentEventContext context = (DocumentEventContext) ec;
31                         DocumentModel doc = context.getSourceDocument();
32
33                         logger.debug("docType=" + doc.getType());
34
35                         if (doc.getType().startsWith(LoanoutConstants.NUXEO_DOCTYPE) &&
36                                         !doc.isVersion() &&
37                                         !doc.isProxy() &&
38                                         !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
39
40                                 if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
41                                         DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
42
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);
47
48                                         logger.debug("previousLabelRequested=" + previousLabelRequested + " labelRequested=" + labelRequested);
49
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);
54                                         }
55                                 }
56                                 else {
57                                         boolean doUpdate = false;
58
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);
62
63                                                 doUpdate = (labelRequested != null && labelRequested.equals(LoanoutBotGardenConstants.LABEL_REQUESTED_YES_VALUE));
64                                         } else {
65                                                 doUpdate = ec.hasProperty(RUN_AFTER_MODIFIED_PROPERTY) && ((Boolean) ec.getProperty(RUN_AFTER_MODIFIED_PROPERTY));
66                                         }
67
68                                         if (doUpdate) {
69                                                 logger.debug("Updating styled name");
70
71                                                 String voucherCsid = doc.getName();
72
73                                                 try {
74                                                         createFormatter().formatVoucherName(voucherCsid);
75                                                 } catch (Exception e) {
76                                                         logger.error(e.getMessage(), e);
77                                                 }
78                                         }
79                                 }
80                         }
81                 }
82         }
83
84         private FormatVoucherNameBatchJob createFormatter() {
85                 ResourceMap resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
86
87                 FormatVoucherNameBatchJob formatter = new FormatVoucherNameBatchJob();
88                 formatter.setResourceMap(resourceMap);
89
90                 return formatter;
91         }
92 }