001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.notification;
018:
019: import java.util.Collections;
020: import java.util.Comparator;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025:
026: import org.apache.commons.collections.ComparatorUtils;
027:
028: import edu.iu.uis.eden.EdenConstants;
029: import edu.iu.uis.eden.KEWServiceLocator;
030: import edu.iu.uis.eden.actionitem.ActionItem;
031: import edu.iu.uis.eden.actionlist.dao.ActionItemComparator;
032: import edu.iu.uis.eden.doctype.DocumentType;
033: import edu.iu.uis.eden.engine.RouteContext;
034: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
035: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
036: import edu.iu.uis.eden.mail.ActionListImmediateEmailReminderService;
037: import edu.iu.uis.eden.messaging.MessageServiceNames;
038: import edu.iu.uis.eden.preferences.Preferences;
039: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
040: import edu.iu.uis.eden.user.WorkflowUser;
041: import edu.iu.uis.eden.user.WorkflowUserId;
042:
043: /**
044: * The default implementation of the NotificationService.
045: *
046: * @author ewestfal
047: */
048: public class DefaultNotificationService implements NotificationService {
049:
050: protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
051: .getLogger(getClass());
052:
053: private static final Comparator notificationPriorityComparator = ComparatorUtils
054: .reversedComparator(new ActionItemComparator());
055:
056: /**
057: * Queues up immediate email processors for ActionItem notification. Prioritizes the list of
058: * Action Items passed in and attempts to not send out multiple emails to the same user.
059: */
060: public void notify(List actionItems) {
061: // sort the list of action items using the same comparator as the Action List
062: Collections.sort(actionItems, notificationPriorityComparator);
063: Set sentNotifications = new HashSet();
064: for (Iterator iterator = actionItems.iterator(); iterator
065: .hasNext();) {
066: ActionItem actionItem = (ActionItem) iterator.next();
067: if (!sentNotifications.contains(actionItem.getWorkflowId())
068: && shouldNotify(actionItem)) {
069: sentNotifications.add(actionItem.getWorkflowId());
070: ActionListImmediateEmailReminderService immediateEmailService = MessageServiceNames
071: .getImmediateEmailService();
072: immediateEmailService
073: .sendReminder(actionItem, RouteContext
074: .getCurrentRouteContext()
075: .isDoNotSendApproveNotificationEmails());
076: }
077: }
078: }
079:
080: protected boolean shouldNotify(ActionItem actionItem) {
081: try {
082: WorkflowUser user = KEWServiceLocator.getUserService()
083: .getWorkflowUser(
084: new WorkflowUserId(actionItem
085: .getWorkflowId()));
086: Preferences preferences = KEWServiceLocator
087: .getPreferencesService().getPreferences(user);
088: boolean sendEmail = false;
089: if (EdenConstants.EMAIL_RMNDR_IMMEDIATE.equals(preferences
090: .getEmailNotification())) {
091: if (EdenConstants.DELEGATION_PRIMARY.equals(actionItem
092: .getDelegationType())) {
093: sendEmail = EdenConstants.PREFERENCES_YES_VAL
094: .equals(preferences
095: .getNotifyPrimaryDelegation());
096: } else if (EdenConstants.DELEGATION_SECONDARY
097: .equals(actionItem.getDelegationType())) {
098: sendEmail = EdenConstants.PREFERENCES_YES_VAL
099: .equals(preferences
100: .getNotifySecondaryDelegation());
101: } else {
102: sendEmail = true;
103: }
104: }
105: // don't send notification if this action item came from a SAVE action and the NOTIFY_ON_SAVE policy is not set
106: if (sendEmail && isItemOriginatingFromSave(actionItem)
107: && !shouldNotifyOnSave(actionItem)) {
108: sendEmail = false;
109: }
110: return sendEmail;
111: } catch (EdenUserNotFoundException e) {
112: throw new WorkflowRuntimeException(
113: "Error loading user with workflow id "
114: + actionItem.getWorkflowId()
115: + " for notification.", e);
116: }
117: }
118:
119: /**
120: * Returns true if the ActionItem doesn't represent a request generated from a "SAVE" action or, if it does,
121: * returns true if the document type policy
122: */
123: protected boolean isItemOriginatingFromSave(ActionItem actionItem) {
124: return actionItem.getResponsibilityId() != null
125: && actionItem.getResponsibilityId().equals(
126: EdenConstants.SAVED_REQUEST_RESPONSIBILITY_ID);
127: }
128:
129: protected boolean shouldNotifyOnSave(ActionItem actionItem) {
130: DocumentRouteHeaderValue document = KEWServiceLocator
131: .getRouteHeaderService().getRouteHeader(
132: actionItem.getRouteHeaderId());
133: DocumentType documentType = KEWServiceLocator
134: .getDocumentTypeService().findById(
135: document.getDocumentTypeId());
136: return documentType.getNotifyOnSavePolicy().getPolicyValue()
137: .booleanValue();
138: }
139:
140: }
|