|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
   package org.kuali.rice.kew.notification.service.impl;  | 
  |  18 |     | 
     | 
  |  19 |     | 
   import java.util.Collections;  | 
  |  20 |     | 
   import java.util.Comparator;  | 
  |  21 |     | 
   import java.util.HashSet;  | 
  |  22 |     | 
   import java.util.Iterator;  | 
  |  23 |     | 
   import java.util.List;  | 
  |  24 |     | 
   import java.util.Set;  | 
  |  25 |     | 
     | 
  |  26 |     | 
   import org.apache.commons.collections.ComparatorUtils;  | 
  |  27 |     | 
   import org.kuali.rice.kew.actionitem.ActionItem;  | 
  |  28 |     | 
   import org.kuali.rice.kew.actionitem.ActionItemComparator;  | 
  |  29 |     | 
   import org.kuali.rice.kew.doctype.bo.DocumentType;  | 
  |  30 |     | 
   import org.kuali.rice.kew.engine.RouteContext;  | 
  |  31 |     | 
   import org.kuali.rice.kew.exception.WorkflowRuntimeException;  | 
  |  32 |     | 
   import org.kuali.rice.kew.mail.service.ActionListImmediateEmailReminderService;  | 
  |  33 |     | 
   import org.kuali.rice.kew.messaging.MessageServiceNames;  | 
  |  34 |     | 
   import org.kuali.rice.kew.notification.service.NotificationService;  | 
  |  35 |     | 
   import org.kuali.rice.kew.preferences.Preferences;  | 
  |  36 |     | 
   import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;  | 
  |  37 |     | 
   import org.kuali.rice.kew.service.KEWServiceLocator;  | 
  |  38 |     | 
   import org.kuali.rice.kew.util.KEWConstants;  | 
  |  39 |     | 
     | 
  |  40 |     | 
     | 
  |  41 |     | 
     | 
  |  42 |     | 
     | 
  |  43 |     | 
     | 
  |  44 |     | 
     | 
  |  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 |     | 
     | 
  |  54 |     | 
     | 
  |  55 |     | 
     | 
  |  56 |     | 
           public void notify(List<ActionItem> actionItems) { | 
  |  57 |     | 
                     | 
  |  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 |     | 
     | 
  |  71 |     | 
     | 
  |  72 |     | 
     | 
  |  73 |     | 
           protected void sendNotification(ActionItem actionItem) { | 
  |  74 |    0 |            ActionListImmediateEmailReminderService immediateEmailService = MessageServiceNames.getImmediateEmailService();  | 
  |  75 |    0 |            immediateEmailService.sendReminder(actionItem, RouteContext.getCurrentRouteContext().isDoNotSendApproveNotificationEmails());  | 
  |  76 |    0 |            }  | 
  |  77 |     | 
     | 
  |  78 |     | 
           protected boolean shouldNotify(ActionItem actionItem) { | 
  |  79 |     | 
                   try { | 
  |  80 |     | 
     | 
  |  81 |    0 |                            Preferences preferences = KEWServiceLocator.getPreferencesService().getPreferences(actionItem.getPrincipalId());  | 
  |  82 |    0 |                            boolean sendEmail = false;  | 
  |  83 |    0 |                            if (KEWConstants.EMAIL_RMNDR_IMMEDIATE.equals(preferences.getEmailNotification())) { | 
  |  84 |    0 |                                    if (KEWConstants.DELEGATION_PRIMARY.equals(actionItem.getDelegationType())) { | 
  |  85 |    0 |                                            sendEmail = KEWConstants.PREFERENCES_YES_VAL.equals(preferences.getNotifyPrimaryDelegation());  | 
  |  86 |    0 |                                    } else if (KEWConstants.DELEGATION_SECONDARY.equals(actionItem.getDelegationType())) { | 
  |  87 |    0 |                                            sendEmail = KEWConstants.PREFERENCES_YES_VAL.equals(preferences.getNotifySecondaryDelegation());  | 
  |  88 |     | 
                                   } else { | 
  |  89 |    0 |                                            sendEmail = true;  | 
  |  90 |     | 
                                   }  | 
  |  91 |     | 
                           }  | 
  |  92 |     | 
                             | 
  |  93 |    0 |                            if (sendEmail && isItemOriginatingFromSave(actionItem) && !shouldNotifyOnSave(actionItem)) { | 
  |  94 |    0 |                                    sendEmail = false;  | 
  |  95 |     | 
                           }  | 
  |  96 |    0 |                            return sendEmail;  | 
  |  97 |    0 |                    } catch (Exception e) { | 
  |  98 |    0 |                            throw new WorkflowRuntimeException("Error loading user with workflow id " + actionItem.getPrincipalId() + " for notification.", e); | 
  |  99 |     | 
                   }  | 
  |  100 |     | 
           }  | 
  |  101 |     | 
     | 
  |  102 |     | 
             | 
  |  103 |     | 
     | 
  |  104 |     | 
     | 
  |  105 |     | 
     | 
  |  106 |     | 
           protected boolean isItemOriginatingFromSave(ActionItem actionItem) { | 
  |  107 |    0 |                    return actionItem.getResponsibilityId() != null && actionItem.getResponsibilityId().equals(KEWConstants.SAVED_REQUEST_RESPONSIBILITY_ID);  | 
  |  108 |     | 
           }  | 
  |  109 |     | 
     | 
  |  110 |     | 
           protected boolean shouldNotifyOnSave(ActionItem actionItem) { | 
  |  111 |    0 |                    DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(actionItem.getRouteHeaderId());  | 
  |  112 |    0 |                    DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findById(document.getDocumentTypeId());  | 
  |  113 |    0 |                    return documentType.getNotifyOnSavePolicy().getPolicyValue().booleanValue();  | 
  |  114 |     | 
           }  | 
  |  115 |     | 
     | 
  |  116 |     | 
       public void removeNotification(List<ActionItem> actionItems) { | 
  |  117 |     | 
             | 
  |  118 |    0 |        }  | 
  |  119 |     | 
   }  |