Coverage Report - org.kuali.rice.kew.notification.service.impl.DefaultNotificationService
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultNotificationService
0%
0/33
0%
0/22
3.167
 
 1  
 /**
 2  
  * Copyright 2005-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  
 package org.kuali.rice.kew.notification.service.impl;
 17  
 
 18  
 import java.util.Collections;
 19  
 import java.util.Comparator;
 20  
 import java.util.HashSet;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 import java.util.Set;
 24  
 
 25  
 import org.apache.commons.collections.ComparatorUtils;
 26  
 import org.kuali.rice.core.api.delegation.DelegationType;
 27  
 import org.kuali.rice.kew.actionitem.ActionItemComparator;
 28  
 import org.kuali.rice.kew.api.KewApiServiceLocator;
 29  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 30  
 import org.kuali.rice.kew.api.action.ActionItem;
 31  
 import org.kuali.rice.kew.api.mail.ImmediateEmailReminderQueue;
 32  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 33  
 import org.kuali.rice.kew.engine.RouteContext;
 34  
 import org.kuali.rice.kew.notification.service.NotificationService;
 35  
 import org.kuali.rice.kew.api.preferences.Preferences;
 36  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 37  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 38  
 import org.kuali.rice.kew.api.KewApiConstants;
 39  
 
 40  
 
 41  
 /**
 42  
  * The default implementation of the NotificationService.
 43  
  *
 44  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 45  
  */
 46  0
 public class DefaultNotificationService implements NotificationService {
 47  
 
 48  0
         protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
 49  
 
 50  0
         private static final Comparator notificationPriorityComparator = ComparatorUtils.reversedComparator(new ActionItemComparator());
 51  
 
 52  
         /**
 53  
          * Queues up immediate email processors for ActionItem notification.  Prioritizes the list of
 54  
          * Action Items passed in and attempts to not send out multiple emails to the same user.
 55  
          */
 56  
         public void notify(List<ActionItem> actionItems) {
 57  
                 // sort the list of action items using the same comparator as the Action List
 58  0
                 Collections.sort(actionItems, notificationPriorityComparator);
 59  0
                 Set sentNotifications = new HashSet();
 60  0
                 for (Iterator iterator = actionItems.iterator(); iterator.hasNext();) {
 61  0
                         ActionItem actionItem = (ActionItem) iterator.next();
 62  0
                         if (!sentNotifications.contains(actionItem.getPrincipalId()) && shouldNotify(actionItem)) {
 63  0
                                 sentNotifications.add(actionItem.getPrincipalId());
 64  0
                                 sendNotification(actionItem);
 65  
                         }
 66  0
                 }
 67  0
         }
 68  
 
 69  
         /**
 70  
          * Sends a notification
 71  
          * @param actionItem the action item
 72  
          */
 73  
         protected void sendNotification(ActionItem actionItem) {
 74  0
         ImmediateEmailReminderQueue immediateEmailQueue = KewApiServiceLocator.getImmediateEmailReminderQueue();
 75  0
         immediateEmailQueue.sendReminder(actionItem, RouteContext.getCurrentRouteContext().isDoNotSendApproveNotificationEmails());
 76  
         // TODO: JLR - replace with direct call to ActionListEmailService 
 77  0
         }
 78  
 
 79  
         protected boolean shouldNotify(ActionItem actionItem) {
 80  
                 try {
 81  
 
 82  0
                         Preferences preferences = KewApiServiceLocator.getPreferencesService().getPreferences(actionItem.getPrincipalId());
 83  0
                         boolean sendEmail = false;
 84  0
                         if (KewApiConstants.EMAIL_RMNDR_IMMEDIATE.equals(preferences.getEmailNotification())) {
 85  0
                                 if (DelegationType.PRIMARY.equals(actionItem.getDelegationType())) {
 86  0
                                         sendEmail = KewApiConstants.PREFERENCES_YES_VAL.equals(preferences.getNotifyPrimaryDelegation());
 87  0
                                 } else if (DelegationType.SECONDARY.equals(actionItem.getDelegationType())) {
 88  0
                                         sendEmail = KewApiConstants.PREFERENCES_YES_VAL.equals(preferences.getNotifySecondaryDelegation());
 89  
                                 } else {
 90  0
                                         sendEmail = true;
 91  
                                 }
 92  
                         }
 93  
                         // don't send notification if this action item came from a SAVE action and the NOTIFY_ON_SAVE policy is not set
 94  0
                         if (sendEmail && isItemOriginatingFromSave(actionItem) && !shouldNotifyOnSave(actionItem)) {
 95  0
                                 sendEmail = false;
 96  
                         }
 97  0
                         return sendEmail;
 98  0
                 } catch (Exception e) {
 99  0
                         throw new WorkflowRuntimeException("Error loading user with workflow id " + actionItem.getPrincipalId() + " for notification.", e);
 100  
                 }
 101  
         }
 102  
 
 103  
         /**
 104  
          * Returns true if the ActionItem doesn't represent a request generated from a "SAVE" action or, if it does,
 105  
          * returns true if the document type policy
 106  
          */
 107  
         protected boolean isItemOriginatingFromSave(ActionItem actionItem) {
 108  0
                 return actionItem.getResponsibilityId() != null && actionItem.getResponsibilityId().equals(KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID);
 109  
         }
 110  
 
 111  
         protected boolean shouldNotifyOnSave(ActionItem actionItem) {
 112  0
                 DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(actionItem.getDocumentId());
 113  0
                 DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findById(document.getDocumentTypeId());
 114  0
                 return documentType.getNotifyOnSavePolicy().getPolicyValue().booleanValue();
 115  
         }
 116  
 
 117  
     public void removeNotification(List<ActionItem> actionItems) {
 118  
         // nothing
 119  0
     }
 120  
 }