View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.api.WorkflowRuntimeException;
30  import org.kuali.rice.kew.api.action.DelegationType;
31  import org.kuali.rice.kew.doctype.bo.DocumentType;
32  import org.kuali.rice.kew.engine.RouteContext;
33  import org.kuali.rice.kew.mail.service.ActionListImmediateEmailReminderService;
34  import org.kuali.rice.kew.messaging.MessageServiceNames;
35  import org.kuali.rice.kew.notification.service.NotificationService;
36  import org.kuali.rice.kew.preferences.Preferences;
37  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
38  import org.kuali.rice.kew.service.KEWServiceLocator;
39  import org.kuali.rice.kew.util.KEWConstants;
40  
41  
42  /**
43   * The default implementation of the NotificationService.
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  public class DefaultNotificationService implements NotificationService {
48  
49  	protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
50  
51  	private static final Comparator notificationPriorityComparator = ComparatorUtils.reversedComparator(new ActionItemComparator());
52  
53  	/**
54  	 * Queues up immediate email processors for ActionItem notification.  Prioritizes the list of
55  	 * Action Items passed in and attempts to not send out multiple emails to the same user.
56  	 */
57  	public void notify(List<ActionItem> actionItems) {
58  		// sort the list of action items using the same comparator as the Action List
59  		Collections.sort(actionItems, notificationPriorityComparator);
60  		Set sentNotifications = new HashSet();
61  		for (Iterator iterator = actionItems.iterator(); iterator.hasNext();) {
62  			ActionItem actionItem = (ActionItem) iterator.next();
63  			if (!sentNotifications.contains(actionItem.getPrincipalId()) && shouldNotify(actionItem)) {
64  				sentNotifications.add(actionItem.getPrincipalId());
65  				sendNotification(actionItem);
66  			}
67  		}
68  	}
69  
70  	/**
71  	 * Sends a notification
72  	 * @param actionItem the action item
73  	 */
74  	protected void sendNotification(ActionItem actionItem) {
75          ActionListImmediateEmailReminderService immediateEmailService = MessageServiceNames.getImmediateEmailService();
76          immediateEmailService.sendReminder(actionItem, RouteContext.getCurrentRouteContext().isDoNotSendApproveNotificationEmails());
77  	}
78  
79  	protected boolean shouldNotify(ActionItem actionItem) {
80  		try {
81  
82  			Preferences preferences = KEWServiceLocator.getPreferencesService().getPreferences(actionItem.getPrincipalId());
83  			boolean sendEmail = false;
84  			if (KEWConstants.EMAIL_RMNDR_IMMEDIATE.equals(preferences.getEmailNotification())) {
85  				if (DelegationType.PRIMARY.getCode().equals(actionItem.getDelegationType())) {
86  					sendEmail = KEWConstants.PREFERENCES_YES_VAL.equals(preferences.getNotifyPrimaryDelegation());
87  				} else if (DelegationType.SECONDARY.getCode().equals(actionItem.getDelegationType())) {
88  					sendEmail = KEWConstants.PREFERENCES_YES_VAL.equals(preferences.getNotifySecondaryDelegation());
89  				} else {
90  					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  			if (sendEmail && isItemOriginatingFromSave(actionItem) && !shouldNotifyOnSave(actionItem)) {
95  				sendEmail = false;
96  			}
97  			return sendEmail;
98  		} catch (Exception e) {
99  			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 		return actionItem.getResponsibilityId() != null && actionItem.getResponsibilityId().equals(KEWConstants.SAVED_REQUEST_RESPONSIBILITY_ID);
109 	}
110 
111 	protected boolean shouldNotifyOnSave(ActionItem actionItem) {
112 		DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(actionItem.getDocumentId());
113 		DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findById(document.getDocumentTypeId());
114 		return documentType.getNotifyOnSavePolicy().getPolicyValue().booleanValue();
115 	}
116 
117     public void removeNotification(List<ActionItem> actionItems) {
118         // nothing
119     }
120 }