001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.notification.service.impl;
017    
018    import java.util.List;
019    
020    import javax.xml.namespace.QName;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.rice.core.api.config.property.ConfigContext;
024    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
025    import org.kuali.rice.kcb.api.message.MessageDTO;
026    import org.kuali.rice.kcb.api.service.KCBServiceNames;
027    import org.kuali.rice.kcb.api.service.MessagingService;
028    import org.kuali.rice.kcb.util.KCBConstants;
029    import org.kuali.rice.kew.api.WorkflowRuntimeException;
030    import org.kuali.rice.kew.api.action.ActionItem;
031    import org.kuali.rice.kew.api.KewApiConstants;
032    
033    
034    /**
035     * NotificationService implementation that delegates to KCB
036     *
037     * @author Kuali Rice Team (rice.collab@kuali.org)
038     */
039    public class KCBNotificationService extends DefaultNotificationService {
040        @Override
041        protected void sendNotification(ActionItem actionItem) {
042            super.sendNotification(actionItem);
043    
044            String enableKENNotificationValue = ConfigContext.getCurrentContextConfig().getProperty(KewApiConstants.ENABLE_KEN_NOTIFICATION);
045            boolean enableKENNotification = Boolean.parseBoolean(enableKENNotificationValue);
046            // we only send per-user messages to KCB
047            if (!enableKENNotification || actionItem.getPrincipalId() != null)
048                return;
049    
050    
051            // send it off to KCB if available
052            MessagingService ms = (MessagingService) GlobalResourceLoader.getService(new QName(KCBConstants.SERVICE_NAMESPACE, KCBServiceNames.KCB_MESSAGING));
053            if (ms == null) {
054                    LOG.info("Could not locate KCB MessagingService.  Message will not be forwarded to the KCB.");
055                    return;
056            }
057            MessageDTO mvo = new MessageDTO();
058            mvo.setChannel("KEW");
059            mvo.setContent("i'm a kew notification");
060            mvo.setContentType("KEW notification");
061            mvo.setDeliveryType(actionItem.getActionRequestCd());
062            mvo.setProducer("kew@localhost");
063            mvo.setTitle(actionItem.getDocLabel() + " - " + actionItem.getDocName() + " - " + actionItem.getDocTitle());
064            if (StringUtils.isNotBlank(actionItem.getDocHandlerURL())) {
065                    mvo.setUrl(actionItem.getDocHandlerURL() + "?docId=" + actionItem.getDocumentId());
066            }
067            mvo.setOriginId(String.valueOf(actionItem.getId()));
068            try {
069                // just assume it's a user at this point...
070                mvo.setRecipient(actionItem.getPrincipalId());
071                LOG.debug("Sending message to KCB: " + mvo);
072                ms.deliver(mvo);
073            } catch (Exception e) {
074                throw new WorkflowRuntimeException("could not deliver message to KCB", e);
075            }
076        }
077    
078        @Override
079        public void removeNotification(List<ActionItem> actionItems) {
080            String enableKENNotificationValue = ConfigContext.getCurrentContextConfig().getProperty(KewApiConstants.ENABLE_KEN_NOTIFICATION);
081            boolean enableKENNotification = Boolean.parseBoolean(enableKENNotificationValue);
082            if (!enableKENNotification) {
083                    return;
084            }
085            MessagingService ms = (MessagingService) GlobalResourceLoader.getService(new QName(KCBConstants.SERVICE_NAMESPACE, KCBServiceNames.KCB_MESSAGING));
086    
087            for (ActionItem actionItem: actionItems) {
088                    LOG.debug("Removing KCB messages for action item: " + actionItem.getId() + " " + actionItem.getActionRequestCd() + " " + actionItem.getPrincipalId());
089                try {
090                    // we don't have the action takens at this point...? :(
091                    ms.removeByOriginId(String.valueOf(actionItem.getId()), null, null);
092                } catch (Exception e) {
093                    throw new RuntimeException("could not remove message from KCB", e);
094                }
095            }
096        }
097    }