View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.notification.service.impl;
17  
18  import java.util.List;
19  
20  import javax.xml.namespace.QName;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.core.api.config.property.ConfigContext;
24  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
25  import org.kuali.rice.kcb.api.message.MessageDTO;
26  import org.kuali.rice.kcb.api.service.KCBServiceNames;
27  import org.kuali.rice.kcb.api.service.MessagingService;
28  import org.kuali.rice.kcb.util.KCBConstants;
29  import org.kuali.rice.kew.api.WorkflowRuntimeException;
30  import org.kuali.rice.kew.api.action.ActionItem;
31  import org.kuali.rice.kew.api.KewApiConstants;
32  
33  
34  /**
35   * NotificationService implementation that delegates to KCB
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public class KCBNotificationService extends DefaultNotificationService {
40      @Override
41      protected void sendNotification(ActionItem actionItem) {
42          super.sendNotification(actionItem);
43  
44          String enableKENNotificationValue = ConfigContext.getCurrentContextConfig().getProperty(KewApiConstants.ENABLE_KEN_NOTIFICATION);
45          boolean enableKENNotification = Boolean.parseBoolean(enableKENNotificationValue);
46          // we only send per-user messages to KCB
47          if (!enableKENNotification || actionItem.getPrincipalId() != null)
48              return;
49  
50  
51          // send it off to KCB if available
52          MessagingService ms = (MessagingService) GlobalResourceLoader.getService(new QName(KCBConstants.SERVICE_NAMESPACE, KCBServiceNames.KCB_MESSAGING));
53          if (ms == null) {
54          	LOG.info("Could not locate KCB MessagingService.  Message will not be forwarded to the KCB.");
55          	return;
56          }
57          MessageDTO mvo = new MessageDTO();
58          mvo.setChannel("KEW");
59          mvo.setContent("i'm a kew notification");
60          mvo.setContentType("KEW notification");
61          mvo.setDeliveryType(actionItem.getActionRequestCd());
62          mvo.setProducer("kew@localhost");
63          mvo.setTitle(actionItem.getDocLabel() + " - " + actionItem.getDocName() + " - " + actionItem.getDocTitle());
64          if (StringUtils.isNotBlank(actionItem.getDocHandlerURL())) {
65          	mvo.setUrl(actionItem.getDocHandlerURL() + "?docId=" + actionItem.getDocumentId());
66          }
67          mvo.setOriginId(String.valueOf(actionItem.getId()));
68          try {
69              // just assume it's a user at this point...
70              mvo.setRecipient(actionItem.getPrincipalId());
71              LOG.debug("Sending message to KCB: " + mvo);
72              ms.deliver(mvo);
73          } catch (Exception e) {
74              throw new WorkflowRuntimeException("could not deliver message to KCB", e);
75          }
76      }
77  
78      @Override
79      public void removeNotification(List<ActionItem> actionItems) {
80      	String enableKENNotificationValue = ConfigContext.getCurrentContextConfig().getProperty(KewApiConstants.ENABLE_KEN_NOTIFICATION);
81          boolean enableKENNotification = Boolean.parseBoolean(enableKENNotificationValue);
82          if (!enableKENNotification) {
83          	return;
84          }
85          MessagingService ms = (MessagingService) GlobalResourceLoader.getService(new QName(KCBConstants.SERVICE_NAMESPACE, KCBServiceNames.KCB_MESSAGING));
86  
87          for (ActionItem actionItem: actionItems) {
88          	LOG.debug("Removing KCB messages for action item: " + actionItem.getId() + " " + actionItem.getActionRequestCd() + " " + actionItem.getPrincipalId());
89              try {
90                  // we don't have the action takens at this point...? :(
91                  ms.removeByOriginId(String.valueOf(actionItem.getId()), null, null);
92              } catch (Exception e) {
93                  throw new RuntimeException("could not remove message from KCB", e);
94              }
95          }
96      }
97  }