001 /** 002 * Copyright 2005-2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package mocks; 017 018 import java.util.ArrayList; 019 import java.util.Collection; 020 import java.util.HashMap; 021 import java.util.Iterator; 022 import java.util.List; 023 import java.util.Map; 024 025 import org.apache.commons.lang.StringUtils; 026 import org.apache.log4j.Logger; 027 import org.joda.time.DateTime; 028 import org.kuali.rice.core.api.config.property.ConfigContext; 029 import org.kuali.rice.core.mail.Mailer; 030 import org.kuali.rice.kew.api.action.ActionItem; 031 import org.kuali.rice.kew.mail.DailyEmailJob; 032 import org.kuali.rice.kew.mail.WeeklyEmailJob; 033 import org.kuali.rice.kew.mail.service.EmailContentService; 034 import org.kuali.rice.kew.api.KewApiConstants; 035 import org.kuali.rice.kim.api.identity.Person; 036 import org.kuali.rice.kim.api.identity.principal.Principal; 037 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 038 import org.quartz.CronTrigger; 039 import org.quartz.JobDetail; 040 041 042 public class MockEmailNotificationServiceImpl /*extends CustomizableActionListEmailServiceImpl*/ implements MockEmailNotificationService { 043 private static final Logger LOG = Logger.getLogger(MockEmailNotificationServiceImpl.class); 044 045 private static Map<String,List> immediateReminders = new HashMap<String,List>(); 046 private static Map<String,Integer> aggregateReminderCount = new HashMap<String,Integer>(); 047 private boolean sendDailyReminderCalled = false; 048 private boolean sendWeeklyReminderCalled = false; 049 050 private static final String DAILY_TRIGGER_NAME = "Daily Email Trigger"; 051 private static final String DAILY_JOB_NAME = "Daily Email"; 052 private static final String WEEKLY_TRIGGER_NAME = "Weekly Email Trigger"; 053 private static final String WEEKLY_JOB_NAME = "Weekly Email"; 054 055 private EmailContentService contentService; 056 private String deploymentEnvironment; 057 058 private Mailer mailer; 059 060 /** 061 * Resets the reminder counts 062 */ 063 public void resetReminderCounts() { 064 aggregateReminderCount.clear(); 065 immediateReminders.clear(); 066 } 067 068 /** 069 * This overridden method will perform the standard operations from org.kuali.rice.kew.mail.ActionListEmailServiceImpl but will also keep track of action 070 * items processed 071 */ 072 @Override 073 public void sendImmediateReminder(ActionItem actionItem, Boolean skipOnApprovals) { 074 if (skipOnApprovals != null && skipOnApprovals.booleanValue() 075 && actionItem.getActionRequestCd().equals(KewApiConstants.ACTION_REQUEST_APPROVE_REQ)) { 076 LOG.debug("As requested, skipping immediate reminder notification on action item approval for " + actionItem.getPrincipalId()); 077 return; 078 } 079 List actionItemsSentUser = (List)immediateReminders.get(actionItem.getPrincipalId()); 080 if (actionItemsSentUser == null) { 081 actionItemsSentUser = new ArrayList(); 082 immediateReminders.put(actionItem.getPrincipalId(), actionItemsSentUser); 083 } 084 actionItemsSentUser.add(actionItem); 085 } 086 087 /** 088 * This overridden method returns a value of true always 089 */ 090 //@Override 091 protected boolean sendActionListEmailNotification() { 092 093 return true; 094 } 095 096 @Override 097 public void sendDailyReminder() { 098 LOG.info("Sending daily reminder"); 099 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 }