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.commons.lang.StringUtils;
26  import org.apache.log4j.Logger;
27  import org.joda.time.DateTime;
28  import org.kuali.rice.core.api.config.property.ConfigContext;
29  import org.kuali.rice.core.mail.Mailer;
30  import org.kuali.rice.kew.api.action.ActionItem;
31  import org.kuali.rice.kew.mail.DailyEmailJob;
32  import org.kuali.rice.kew.mail.WeeklyEmailJob;
33  import org.kuali.rice.kew.mail.service.EmailContentService;
34  import org.kuali.rice.kew.api.KewApiConstants;
35  import org.kuali.rice.kim.api.identity.Person;
36  import org.kuali.rice.kim.api.identity.principal.Principal;
37  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
38  import org.quartz.CronTrigger;
39  import org.quartz.JobDetail;
40  
41  
42  public class MockEmailNotificationServiceImpl /*extends CustomizableActionListEmailServiceImpl*/ implements MockEmailNotificationService {
43      private static final Logger LOG = Logger.getLogger(MockEmailNotificationServiceImpl.class);
44  
45      private static Map<String,List> immediateReminders = new HashMap<String,List>();
46      private static Map<String,Integer> aggregateReminderCount = new HashMap<String,Integer>();
47      private boolean sendDailyReminderCalled = false;
48      private boolean sendWeeklyReminderCalled = false;
49      
50      private static final String DAILY_TRIGGER_NAME = "Daily Email Trigger";
51      private static final String DAILY_JOB_NAME = "Daily Email";
52      private static final String WEEKLY_TRIGGER_NAME = "Weekly Email Trigger";
53      private static final String WEEKLY_JOB_NAME = "Weekly Email";
54  
55      private EmailContentService contentService;
56      private String deploymentEnvironment;
57  
58  	private Mailer mailer;
59  
60      /**
61       * Resets the reminder counts
62       */
63      public void resetReminderCounts() {
64          aggregateReminderCount.clear();
65          immediateReminders.clear();
66      }
67  
68      /**
69       * This overridden method will perform the standard operations from org.kuali.rice.kew.mail.ActionListEmailServiceImpl but will also keep track of action
70       * items processed
71       */
72      @Override
73      public void sendImmediateReminder(ActionItem actionItem, Boolean skipOnApprovals) {
74          if (skipOnApprovals != null && skipOnApprovals.booleanValue()
75                  && actionItem.getActionRequestCd().equals(KewApiConstants.ACTION_REQUEST_APPROVE_REQ)) {
76              LOG.debug("As requested, skipping immediate reminder notification on action item approval for " + actionItem.getPrincipalId());
77              return;
78          }
79          List actionItemsSentUser = (List)immediateReminders.get(actionItem.getPrincipalId());
80          if (actionItemsSentUser == null) {
81              actionItemsSentUser = new ArrayList();
82              immediateReminders.put(actionItem.getPrincipalId(), actionItemsSentUser);
83          }
84          actionItemsSentUser.add(actionItem);
85      }
86  
87      /**
88       * This overridden method returns a value of true always
89       */
90      //@Override
91      protected boolean sendActionListEmailNotification() {
92  
93          return true;
94      }
95  
96      @Override
97  	public void sendDailyReminder() {
98          LOG.info("Sending daily reminder");
99          try {
100             getEmailContentGenerator().generateWeeklyReminder(null, null);
101         }
102         catch (NullPointerException npe) {}
103 
104         List<ActionItem> actionItems = new ArrayList<ActionItem>(1);
105         actionItems.add(ActionItem.Builder.create("ai1", "ai2", "ai3", new DateTime(), "ai4", "ai5", "ai6", "ai7", "ai8").build());
106         sendPeriodicReminder(null, actionItems, KewApiConstants.EMAIL_RMNDR_DAY_VAL);
107         //super.sendDailyReminder();
108         sendDailyReminderCalled = true;
109     }
110 
111     @Override
112     public boolean wasDailyReminderSent(){
113         return this.sendDailyReminderCalled;
114     }
115     
116     @Override
117     public void sendWeeklyReminder() {
118         LOG.info("Sending weekly reminder");
119         try {
120             getEmailContentGenerator().generateWeeklyReminder(null, null);
121         }
122         catch (NullPointerException npe) {}
123         List<ActionItem> actionItems = new ArrayList<ActionItem>(1);
124         actionItems.add(ActionItem.Builder.create("ai1", "ai2", "ai3", new DateTime(), "ai4", "ai5", "ai6", "ai7", "ai8").build());
125         sendPeriodicReminder(null, actionItems, KewApiConstants.EMAIL_RMNDR_WEEK_VAL);
126         //super.sendWeeklyReminder();
127         sendWeeklyReminderCalled = true;
128     }
129     
130     @Override
131     public boolean wasWeeklyReminderSent(){
132         return this.sendWeeklyReminderCalled;
133     }
134 
135     @Override
136     public void scheduleBatchEmailReminders() throws Exception {
137         sendDailyReminderCalled = false;
138         sendWeeklyReminderCalled = false;
139         LOG.info("Scheduling Batch Email Reminders.");
140         String emailBatchGroup = "Email Batch";
141         String dailyCron = ConfigContext.getCurrentContextConfig()
142                 .getProperty(KewApiConstants.DAILY_EMAIL_CRON_EXPRESSION);
143         if (!StringUtils.isBlank(dailyCron)) {
144             LOG.info("Scheduling Daily Email batch with cron expression: " + dailyCron);
145             CronTrigger dailyTrigger = new CronTrigger(DAILY_TRIGGER_NAME, emailBatchGroup, dailyCron);
146             JobDetail dailyJobDetail = new JobDetail(DAILY_JOB_NAME, emailBatchGroup, DailyEmailJob.class);
147             dailyTrigger.setJobName(dailyJobDetail.getName());
148             dailyTrigger.setJobGroup(dailyJobDetail.getGroup());
149             sendDailyReminderCalled = true;
150         } else {
151             LOG.warn("No " + KewApiConstants.DAILY_EMAIL_CRON_EXPRESSION
152                     + " parameter was configured.  Daily Email batch was not scheduled!");
153         }
154 
155         String weeklyCron = ConfigContext.getCurrentContextConfig().getProperty(
156                 KewApiConstants.WEEKLY_EMAIL_CRON_EXPRESSION);
157         if (!StringUtils.isBlank(weeklyCron)) {
158             LOG.info("Scheduling Weekly Email batch with cron expression: " + weeklyCron);
159             CronTrigger weeklyTrigger = new CronTrigger(WEEKLY_TRIGGER_NAME, emailBatchGroup, weeklyCron);
160             JobDetail weeklyJobDetail = new JobDetail(WEEKLY_JOB_NAME, emailBatchGroup, WeeklyEmailJob.class);
161             weeklyTrigger.setJobName(weeklyJobDetail.getName());
162             weeklyTrigger.setJobGroup(weeklyJobDetail.getGroup());
163             sendWeeklyReminderCalled = true;
164         } else {
165             LOG.warn("No " + KewApiConstants.WEEKLY_EMAIL_CRON_EXPRESSION
166                     + " parameter was configured.  Weekly Email batch was not scheduled!");
167         }
168     }
169 
170     //@Override
171     protected void sendPeriodicReminder(Person user, Collection<ActionItem> actionItems, String emailSetting) {
172         //super.sendPeriodicReminder(user, actionItems, emailSetting);
173         if (!aggregateReminderCount.containsKey(emailSetting)) {
174             aggregateReminderCount.put(emailSetting, actionItems.size());
175         } else {
176             aggregateReminderCount.put(emailSetting, aggregateReminderCount.get(emailSetting) + actionItems.size());
177         }
178     }
179 
180     public Integer getTotalPeriodicRemindersSent(String emailReminderConstant) {
181         Integer returnVal = aggregateReminderCount.get(emailReminderConstant);
182         if (returnVal == null) {
183             returnVal = Integer.valueOf(0);
184         }
185         return returnVal;
186     }
187 
188     public Integer getTotalPeriodicRemindersSent() {
189         int total = 0;
190         for (Map.Entry<String, Integer> mapEntry : aggregateReminderCount.entrySet()) {
191             Integer value = mapEntry.getValue();
192             total += (value == null) ? 0 : value.intValue();
193         }
194         return Integer.valueOf(total);
195     }
196 
197     public boolean wasStyleServiceAccessed() {
198         return getEmailContentGenerator().wasServiceAccessed();
199     }
200 
201     private void resetStyleService() {
202         getEmailContentGenerator().resetServiceAccessed();
203     }
204 
205     public int immediateReminderEmailsSent(String networkId, String documentId, String actionRequestCd) {
206         Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(networkId);
207         List actionItemsSentUser = immediateReminders.get(principal.getPrincipalId());
208         if (actionItemsSentUser == null) {
209             LOG.info("There are no immediate reminders for Principal " + networkId + " and Document ID " + documentId);
210             return 0;
211         }
212         else {
213             LOG.info("There are " + actionItemsSentUser.size() + " immediate reminders for Principal " + networkId + " and Document ID " + documentId);
214         }
215         int emailsSent = 0;
216         for (Iterator iter = actionItemsSentUser.iterator(); iter.hasNext();) {
217             ActionItem actionItem = (ActionItem) iter.next();
218             if (actionItem.getDocumentId().equals(documentId) && actionItem.getActionRequestCd().equals(actionRequestCd)) {
219                 emailsSent++;
220             }
221         }
222         LOG.info(emailsSent + "No immediate e-mails were sent to Principal " + networkId + " and Document ID " + documentId);
223         return emailsSent;
224     }
225 
226     public void setEmailContentGenerator(EmailContentService contentService) {
227         this.contentService = contentService;
228     }
229 
230     protected MockStyleableEmailContentService getEmailContentGenerator() {
231         return (MockStyleableEmailContentService) contentService;
232     }
233 
234 	public void setMailer(Mailer mailer){
235 		this.mailer = mailer;
236 	}
237 
238 	public void setDeploymentEnvironment(String deploymentEnvironment) {
239 		this.deploymentEnvironment = deploymentEnvironment;
240 	}
241 }