View Javadoc

1   /*
2    * Copyright 2006-2011 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  
17  package org.kuali.rice.kew.notification.service.impl;
18  
19  import java.util.List;
20  
21  import javax.xml.namespace.QName;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.rice.core.api.config.property.ConfigContext;
25  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
26  import org.kuali.rice.kcb.dto.MessageDTO;
27  import org.kuali.rice.kcb.service.KCBServiceNames;
28  import org.kuali.rice.kcb.service.MessagingService;
29  import org.kuali.rice.kcb.util.KCBConstants;
30  import org.kuali.rice.kew.actionitem.ActionItem;
31  import org.kuali.rice.kew.api.WorkflowRuntimeException;
32  import org.kuali.rice.kew.api.action.RecipientType;
33  import org.kuali.rice.kew.util.KEWConstants;
34  
35  
36  /**
37   * NotificationService implementation that delegates to KCB
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class KCBNotificationService extends DefaultNotificationService {
42      @Override
43      protected void sendNotification(ActionItem actionItem) {
44          super.sendNotification(actionItem);
45  
46          String enableKENNotificationValue = ConfigContext.getCurrentContextConfig().getProperty(KEWConstants.ENABLE_KEN_NOTIFICATION);
47          boolean enableKENNotification = Boolean.parseBoolean(enableKENNotificationValue);
48          // we only send per-user messages to KCB
49          if (!enableKENNotification || !RecipientType.PRINCIPAL.getCode().equals(actionItem.getRecipientTypeCode()))
50              return;
51  
52  
53          // send it off to KCB if available
54          MessagingService ms = (MessagingService) GlobalResourceLoader.getService(new QName(KCBConstants.SERVICE_NAMESPACE, KCBServiceNames.KCB_MESSAGING));
55          if (ms == null) {
56          	LOG.info("Could not locate KCB MessagingService.  Message will not be forwarded to the KCB.");
57          	return;
58          }
59          MessageDTO mvo = new MessageDTO();
60          mvo.setChannel("KEW");
61          mvo.setContent("i'm a kew notification");
62          mvo.setContentType("KEW notification");
63          mvo.setDeliveryType(actionItem.getActionRequestCd());
64          mvo.setProducer("kew@localhost");
65          mvo.setTitle(actionItem.getDocLabel() + " - " + actionItem.getDocName() + " - " + actionItem.getDocTitle());
66          if (StringUtils.isNotBlank(actionItem.getDocHandlerURL())) {
67          	mvo.setUrl(actionItem.getDocHandlerURL() + "?docId=" + actionItem.getDocumentId());
68          }
69          mvo.setOriginId(String.valueOf(actionItem.getActionItemId()));
70          try {
71              // just assume it's a user at this point...
72              mvo.setRecipient(actionItem.getPrincipalId());
73              LOG.debug("Sending message to KCB: " + mvo);
74              ms.deliver(mvo);
75          } catch (Exception e) {
76              throw new WorkflowRuntimeException("could not deliver message to KCB", e);
77          }
78      }
79  
80      @Override
81      public void removeNotification(List<ActionItem> actionItems) {
82      	String enableKENNotificationValue = ConfigContext.getCurrentContextConfig().getProperty(KEWConstants.ENABLE_KEN_NOTIFICATION);
83          boolean enableKENNotification = Boolean.parseBoolean(enableKENNotificationValue);
84          if (!enableKENNotification) {
85          	return;
86          }
87          MessagingService ms = (MessagingService) GlobalResourceLoader.getService(new QName(KCBConstants.SERVICE_NAMESPACE, KCBServiceNames.KCB_MESSAGING));
88  
89          for (ActionItem actionItem: actionItems) {
90          	LOG.debug("Removing KCB messages for action item: " + actionItem.getActionItemId() + " " + actionItem.getActionRequestCd() + " " + actionItem.getPerson());
91              try {
92                  // we don't have the action takens at this point...? :(
93                  ms.removeByOriginId(String.valueOf(actionItem.getActionItemId()), null, null);
94              } catch (Exception e) {
95                  throw new RuntimeException("could not remove message from KCB", e);
96              }
97          }
98      }
99  }