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 mocks;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.log4j.Logger;
27  import org.kuali.rice.kew.actionitem.ActionItem;
28  import org.kuali.rice.kew.mail.service.impl.CustomizableActionListEmailServiceImpl;
29  import org.kuali.rice.kim.bo.Person;
30  import org.kuali.rice.kim.bo.entity.KimPrincipal;
31  import org.kuali.rice.kim.service.KIMServiceLocator;
32  
33  
34  public class MockEmailNotificationServiceImpl extends CustomizableActionListEmailServiceImpl implements MockEmailNotificationService {
35      private static final Logger LOG = Logger.getLogger(MockEmailNotificationServiceImpl.class);
36  
37      private static Map<String,List> immediateReminders = new HashMap<String,List>();
38      private static Map<String,Integer> aggregateReminderCount = new HashMap<String,Integer>();
39      public static boolean SEND_DAILY_REMINDER_CALLED = false;
40      public static boolean SEND_WEEKLY_REMINDER_CALLED = false;
41  
42      /**
43       * Resets the reminder counts
44       */
45      public void resetReminderCounts() {
46          aggregateReminderCount.clear();
47          immediateReminders.clear();
48      }
49  
50      /**
51       * This overridden method will perform the standard operations from org.kuali.rice.kew.mail.ActionListEmailServiceImpl but will also keep track of action
52       * items processed
53       */
54      @Override
55      public void sendImmediateReminder(Person user, ActionItem actionItem) {
56          super.sendImmediateReminder(user, actionItem);
57          List actionItemsSentUser = (List)immediateReminders.get(user.getPrincipalId());
58          if (actionItemsSentUser == null) {
59              actionItemsSentUser = new ArrayList();
60              immediateReminders.put(user.getPrincipalId(), actionItemsSentUser);
61          }
62          actionItemsSentUser.add(actionItem);
63      }
64  
65      /**
66       * This overridden method returns a value of true always
67       */
68      @Override
69      protected boolean sendActionListEmailNotification() {
70  
71          return true;
72      }
73  
74      @Override
75  	public void sendDailyReminder() {
76          resetStyleService();
77  	    super.sendDailyReminder();
78  		SEND_DAILY_REMINDER_CALLED = true;
79      }
80  
81      @Override
82      public void sendWeeklyReminder() {
83          resetStyleService();
84          super.sendWeeklyReminder();
85      	SEND_WEEKLY_REMINDER_CALLED = true;
86      }
87  
88      @Override
89      protected void sendPeriodicReminder(Person user, Collection<ActionItem> actionItems, String emailSetting) {
90          super.sendPeriodicReminder(user, actionItems, emailSetting);
91          if (!aggregateReminderCount.containsKey(emailSetting)) {
92              aggregateReminderCount.put(emailSetting, actionItems.size());
93          } else {
94              aggregateReminderCount.put(emailSetting, aggregateReminderCount.get(emailSetting) + actionItems.size());
95          }
96      }
97  
98      public Integer getTotalPeriodicRemindersSent(String emailReminderConstant) {
99          Integer returnVal = aggregateReminderCount.get(emailReminderConstant);
100         if (returnVal == null) {
101             returnVal = Integer.valueOf(0);
102         }
103         return returnVal;
104     }
105 
106     public Integer getTotalPeriodicRemindersSent() {
107         int total = 0;
108         for (Map.Entry<String, Integer> mapEntry : aggregateReminderCount.entrySet()) {
109             Integer value = mapEntry.getValue();
110             total += (value == null) ? 0 : value.intValue();
111         }
112         return Integer.valueOf(total);
113     }
114 
115     public boolean wasStyleServiceAccessed() {
116         return getEmailContentGenerator().wasServiceAccessed();
117     }
118 
119     private void resetStyleService() {
120         getEmailContentGenerator().resetServiceAccessed();
121     }
122 
123     public int immediateReminderEmailsSent(String networkId, Long documentId, String actionRequestCd) {
124     	KimPrincipal principal = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(networkId);
125         List actionItemsSentUser = immediateReminders.get(principal.getPrincipalId());
126         if (actionItemsSentUser == null) {
127             return 0;
128         }
129         int emailsSent = 0;
130         for (Iterator iter = actionItemsSentUser.iterator(); iter.hasNext();) {
131             ActionItem actionItem = (ActionItem) iter.next();
132             if (actionItem.getRouteHeaderId().equals(documentId) && actionItem.getActionRequestCd().equals(actionRequestCd)) {
133                 emailsSent++;
134             }
135         }
136         return emailsSent;
137     }
138 
139     @Override
140     protected MockStyleableEmailContentService getEmailContentGenerator() {
141         return (MockStyleableEmailContentService) super.getEmailContentGenerator();
142     }
143 }