View Javadoc

1   /**
2    * Copyright 2005-2011 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 mocks;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.log4j.Logger;
26  import org.joda.time.DateTime;
27  import org.kuali.rice.core.mail.Mailer;
28  import org.kuali.rice.kew.api.action.ActionItem;
29  import org.kuali.rice.kew.api.action.ActionRequestType;
30  import org.kuali.rice.kew.mail.service.EmailContentService;
31  import org.kuali.rice.kew.api.KewApiConstants;
32  import org.kuali.rice.kim.api.identity.Person;
33  import org.kuali.rice.kim.api.identity.principal.Principal;
34  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35  
36  
37  public class MockEmailNotificationServiceImpl /*extends CustomizableActionListEmailServiceImpl*/ implements MockEmailNotificationService {
38      private static final Logger LOG = Logger.getLogger(MockEmailNotificationServiceImpl.class);
39  
40      private static Map<String,List> immediateReminders = new HashMap<String,List>();
41      private static Map<String,Integer> aggregateReminderCount = new HashMap<String,Integer>();
42      public static boolean SEND_DAILY_REMINDER_CALLED = false;
43      public static boolean SEND_WEEKLY_REMINDER_CALLED = false;
44  
45      private EmailContentService contentService;
46      private String deploymentEnvironment;
47  
48  	private Mailer mailer;
49  
50      /**
51       * Resets the reminder counts
52       */
53      public void resetReminderCounts() {
54          aggregateReminderCount.clear();
55          immediateReminders.clear();
56      }
57  
58      /**
59       * This overridden method will perform the standard operations from org.kuali.rice.kew.mail.ActionListEmailServiceImpl but will also keep track of action
60       * items processed
61       */
62      @Override
63      public void sendImmediateReminder(ActionItem actionItem, Boolean skipOnApprovals) {
64          if (skipOnApprovals != null && skipOnApprovals.booleanValue()
65                  && actionItem.getActionRequestCd().equals(KewApiConstants.ACTION_REQUEST_APPROVE_REQ)) {
66              LOG.debug("As requested, skipping immediate reminder notification on action item approval for " + actionItem.getPrincipalId());
67              return;
68          }
69          List actionItemsSentUser = (List)immediateReminders.get(actionItem.getPrincipalId());
70          if (actionItemsSentUser == null) {
71              actionItemsSentUser = new ArrayList();
72              immediateReminders.put(actionItem.getPrincipalId(), actionItemsSentUser);
73          }
74          actionItemsSentUser.add(actionItem);
75      }
76  
77      /**
78       * This overridden method returns a value of true always
79       */
80      //@Override
81      protected boolean sendActionListEmailNotification() {
82  
83          return true;
84      }
85  
86      @Override
87  	public void sendDailyReminder() {
88          try {
89              getEmailContentGenerator().generateWeeklyReminder(null, null);
90          }
91          catch (NullPointerException npe) {}
92  
93          List<ActionItem> actionItems = new ArrayList<ActionItem>(1);
94          actionItems.add(ActionItem.Builder.create("ai1", "ai2", "ai3", new DateTime(), "ai4", "ai5", "ai6", "ai7", "ai8").build());
95          sendPeriodicReminder(null, actionItems, KewApiConstants.EMAIL_RMNDR_DAY_VAL);
96          //super.sendDailyReminder();
97  		SEND_DAILY_REMINDER_CALLED = true;
98      }
99  
100     @Override
101     public void sendWeeklyReminder() {
102         try {
103             getEmailContentGenerator().generateWeeklyReminder(null, null);
104         }
105         catch (NullPointerException npe) {}
106         List<ActionItem> actionItems = new ArrayList<ActionItem>(1);
107         actionItems.add(ActionItem.Builder.create("ai1", "ai2", "ai3", new DateTime(), "ai4", "ai5", "ai6", "ai7", "ai8").build());
108         sendPeriodicReminder(null, actionItems, KewApiConstants.EMAIL_RMNDR_WEEK_VAL);
109         //super.sendWeeklyReminder();
110     	SEND_WEEKLY_REMINDER_CALLED = true;
111     }
112 
113     @Override
114     public void scheduleBatchEmailReminders() throws Exception {
115         //do nothing
116     }
117 
118     //@Override
119     protected void sendPeriodicReminder(Person user, Collection<ActionItem> actionItems, String emailSetting) {
120         //super.sendPeriodicReminder(user, actionItems, emailSetting);
121         if (!aggregateReminderCount.containsKey(emailSetting)) {
122             aggregateReminderCount.put(emailSetting, actionItems.size());
123         } else {
124             aggregateReminderCount.put(emailSetting, aggregateReminderCount.get(emailSetting) + actionItems.size());
125         }
126     }
127 
128     public Integer getTotalPeriodicRemindersSent(String emailReminderConstant) {
129         Integer returnVal = aggregateReminderCount.get(emailReminderConstant);
130         if (returnVal == null) {
131             returnVal = Integer.valueOf(0);
132         }
133         return returnVal;
134     }
135 
136     public Integer getTotalPeriodicRemindersSent() {
137         int total = 0;
138         for (Map.Entry<String, Integer> mapEntry : aggregateReminderCount.entrySet()) {
139             Integer value = mapEntry.getValue();
140             total += (value == null) ? 0 : value.intValue();
141         }
142         return Integer.valueOf(total);
143     }
144 
145     public boolean wasStyleServiceAccessed() {
146         return getEmailContentGenerator().wasServiceAccessed();
147     }
148 
149     private void resetStyleService() {
150         getEmailContentGenerator().resetServiceAccessed();
151     }
152 
153     public int immediateReminderEmailsSent(String networkId, String documentId, String actionRequestCd) {
154         Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(networkId);
155         List actionItemsSentUser = immediateReminders.get(principal.getPrincipalId());
156         if (actionItemsSentUser == null) {
157             return 0;
158         }
159         int emailsSent = 0;
160         for (Iterator iter = actionItemsSentUser.iterator(); iter.hasNext();) {
161             ActionItem actionItem = (ActionItem) iter.next();
162             if (actionItem.getDocumentId().equals(documentId) && actionItem.getActionRequestCd().equals(actionRequestCd)) {
163                 emailsSent++;
164             }
165         }
166         return emailsSent;
167     }
168 
169     public void setEmailContentGenerator(EmailContentService contentService) {
170         this.contentService = contentService;
171     }
172 
173     protected MockStyleableEmailContentService getEmailContentGenerator() {
174         return (MockStyleableEmailContentService) contentService;
175     }
176 
177 	public void setMailer(Mailer mailer){
178 		this.mailer = mailer;
179 	}
180 
181 	public void setDeploymentEnvironment(String deploymentEnvironment) {
182 		this.deploymentEnvironment = deploymentEnvironment;
183 	}
184 }