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.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  public class DefaultNotificationService implements NotificationService {
47  
48  	protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
49  
50  	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  		Collections.sort(actionItems, notificationPriorityComparator);
59  		Set sentNotifications = new HashSet();
60  		for (Iterator iterator = actionItems.iterator(); iterator.hasNext();) {
61  			ActionItem actionItem = (ActionItem) iterator.next();
62              if (!sentNotifications.contains(actionItem.getPrincipalId()) && shouldNotify(actionItem)) {
63                  sentNotifications.add(actionItem.getPrincipalId());
64  				sendNotification(actionItem);
65  			}
66  		}
67  	}
68  
69  	/**
70  	 * Sends a notification
71  	 * @param actionItem the action item
72  	 */
73  	protected void sendNotification(ActionItem actionItem) {
74          ImmediateEmailReminderQueue immediateEmailQueue = KewApiServiceLocator.getImmediateEmailReminderQueue();
75          immediateEmailQueue.sendReminder(actionItem, RouteContext.getCurrentRouteContext().isDoNotSendApproveNotificationEmails());
76          // TODO: JLR - replace with direct call to ActionListEmailService 
77  	}
78  
79  	protected boolean shouldNotify(ActionItem actionItem) {
80  		try {
81              boolean sendEmail = true;
82              // Removed preferences items since they will be checked before it sends
83              // the email in the action list email service
84  
85  			// don't send notification if this action item came from a SAVE action and the NOTIFY_ON_SAVE policy is not set
86  			if (sendEmail && isItemOriginatingFromSave(actionItem) && !shouldNotifyOnSave(actionItem)) {
87  				sendEmail = false;
88  			}
89  			return sendEmail;
90  		} catch (Exception e) {
91  			throw new WorkflowRuntimeException("Error loading user with workflow id " + actionItem.getPrincipalId() + " for notification.", e);
92  		}
93  	}
94  
95  	/**
96  	 * Returns true if the ActionItem doesn't represent a request generated from a "SAVE" action or, if it does,
97  	 * returns true if the document type policy
98  	 */
99  	protected boolean isItemOriginatingFromSave(ActionItem actionItem) {
100 		return actionItem.getResponsibilityId() != null && actionItem.getResponsibilityId().equals(KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID);
101 	}
102 
103 	protected boolean shouldNotifyOnSave(ActionItem actionItem) {
104 		DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(actionItem.getDocumentId());
105 		DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findById(document.getDocumentTypeId());
106 		return documentType.getNotifyOnSavePolicy().getPolicyValue().booleanValue();
107 	}
108 
109     public void removeNotification(List<ActionItem> actionItems) {
110         // nothing
111     }
112 }