001 /**
002 * Copyright 2005-2013 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.api.mail.Mailer;
030 import org.kuali.rice.kew.api.KewApiServiceLocator;
031 import org.kuali.rice.kew.api.action.ActionItem;
032 import org.kuali.rice.kew.api.preferences.Preferences;
033 import org.kuali.rice.kew.api.preferences.PreferencesService;
034 import org.kuali.rice.kew.mail.DailyEmailJob;
035 import org.kuali.rice.kew.mail.WeeklyEmailJob;
036 import org.kuali.rice.kew.mail.service.EmailContentService;
037 import org.kuali.rice.kew.api.KewApiConstants;
038 import org.kuali.rice.kew.mail.service.impl.CustomizableActionListEmailServiceImpl;
039 import org.kuali.rice.kim.api.identity.Person;
040 import org.kuali.rice.kim.api.identity.principal.Principal;
041 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
042 import org.quartz.CronTrigger;
043 import org.quartz.JobDetail;
044
045
046
047
048 public class MockEmailNotificationServiceImpl extends CustomizableActionListEmailServiceImpl implements MockEmailNotificationService {
049 private static final Logger LOG = Logger.getLogger(MockEmailNotificationServiceImpl.class);
050
051 private static Map<String,List> immediateReminders = new HashMap<String,List>();
052 private static Map<String,Integer> aggregateReminderCount = new HashMap<String,Integer>();
053 private boolean sendDailyReminderCalled = false;
054 private boolean sendWeeklyReminderCalled = false;
055
056 private static final String DAILY_TRIGGER_NAME = "Daily Email Trigger";
057 private static final String DAILY_JOB_NAME = "Daily Email";
058 private static final String WEEKLY_TRIGGER_NAME = "Weekly Email Trigger";
059 private static final String WEEKLY_JOB_NAME = "Weekly Email";
060
061 private EmailContentService contentService;
062 private String deploymentEnvironment;
063
064 private Mailer mailer;
065
066 /**
067 * Resets the reminder counts
068 */
069 public void resetReminderCounts() {
070 aggregateReminderCount.clear();
071 immediateReminders.clear();
072 }
073
074 /**
075 * This overridden method will perform the standard operations from org.kuali.rice.kew.mail.ActionListEmailServiceImpl but will also keep track of action
076 * items processed
077 */
078 @Override
079 public void sendImmediateReminder(ActionItem actionItem, Boolean skipOnApprovals) {
080 if (skipOnApprovals != null && skipOnApprovals.booleanValue()
081 && actionItem.getActionRequestCd().equals(KewApiConstants.ACTION_REQUEST_APPROVE_REQ)) {
082 LOG.debug("As requested, skipping immediate reminder notification on action item approval for " + actionItem.getPrincipalId());
083 return;
084 }
085 List actionItemsSentUser = (List)immediateReminders.get(actionItem.getPrincipalId());
086 Preferences preferences = getPreferencesService().getPreferences(actionItem.getPrincipalId());
087
088 boolean shouldNotify = checkEmailNotificationPreferences(actionItem, preferences, KewApiConstants.EMAIL_RMNDR_IMMEDIATE);
089 if(shouldNotify) {
090 if (actionItemsSentUser == null) {
091 actionItemsSentUser = new ArrayList();
092 immediateReminders.put(actionItem.getPrincipalId(), actionItemsSentUser);
093 }
094 actionItemsSentUser.add(actionItem);
095 }
096 }
097
098 /**
099 * This overridden method returns a value of true always
100 */
101 //@Override
102 protected boolean sendActionListEmailNotification() {
103
104 return true;
105 }
106
107 @Override
108 public void sendDailyReminder() {
109 LOG.info("Sending daily reminder");
110 try {
111 getEmailContentGenerator().generateWeeklyReminder(null, null);
112 }
113 catch (NullPointerException npe) {}
114
115 List<ActionItem> actionItems = new ArrayList<ActionItem>(1);
116 actionItems.add(ActionItem.Builder.create("ai1", "ai2", "ai3", new DateTime(), "ai4", "ai5", "ai6", "ai7", "ai8").build());
117 sendPeriodicReminder(null, actionItems, KewApiConstants.EMAIL_RMNDR_DAY_VAL);
118 //super.sendDailyReminder();
119 sendDailyReminderCalled = true;
120 }
121
122 @Override
123 public boolean wasDailyReminderSent(){
124 return this.sendDailyReminderCalled;
125 }
126
127 @Override
128 public void sendWeeklyReminder() {
129 LOG.info("Sending weekly reminder");
130 try {
131 getEmailContentGenerator().generateWeeklyReminder(null, null);
132 }
133 catch (NullPointerException npe) {}
134 List<ActionItem> actionItems = new ArrayList<ActionItem>(1);
135 actionItems.add(ActionItem.Builder.create("ai1", "ai2", "ai3", new DateTime(), "ai4", "ai5", "ai6", "ai7", "ai8").build());
136 sendPeriodicReminder(null, actionItems, KewApiConstants.EMAIL_RMNDR_WEEK_VAL);
137 //super.sendWeeklyReminder();
138 sendWeeklyReminderCalled = true;
139 }
140
141 @Override
142 public boolean wasWeeklyReminderSent(){
143 return this.sendWeeklyReminderCalled;
144 }
145
146 @Override
147 public void scheduleBatchEmailReminders() throws Exception {
148 sendDailyReminderCalled = false;
149 sendWeeklyReminderCalled = false;
150 LOG.info("Scheduling Batch Email Reminders.");
151 String emailBatchGroup = "Email Batch";
152 String dailyCron = ConfigContext.getCurrentContextConfig()
153 .getProperty(KewApiConstants.DAILY_EMAIL_CRON_EXPRESSION);
154 if (!StringUtils.isBlank(dailyCron)) {
155 LOG.info("Scheduling Daily Email batch with cron expression: " + dailyCron);
156 CronTrigger dailyTrigger = new CronTrigger(DAILY_TRIGGER_NAME, emailBatchGroup, dailyCron);
157 JobDetail dailyJobDetail = new JobDetail(DAILY_JOB_NAME, emailBatchGroup, DailyEmailJob.class);
158 dailyTrigger.setJobName(dailyJobDetail.getName());
159 dailyTrigger.setJobGroup(dailyJobDetail.getGroup());
160 sendDailyReminderCalled = true;
161 } else {
162 LOG.warn("No " + KewApiConstants.DAILY_EMAIL_CRON_EXPRESSION
163 + " parameter was configured. Daily Email batch was not scheduled!");
164 }
165
166 String weeklyCron = ConfigContext.getCurrentContextConfig().getProperty(
167 KewApiConstants.WEEKLY_EMAIL_CRON_EXPRESSION);
168 if (!StringUtils.isBlank(weeklyCron)) {
169 LOG.info("Scheduling Weekly Email batch with cron expression: " + weeklyCron);
170 CronTrigger weeklyTrigger = new CronTrigger(WEEKLY_TRIGGER_NAME, emailBatchGroup, weeklyCron);
171 JobDetail weeklyJobDetail = new JobDetail(WEEKLY_JOB_NAME, emailBatchGroup, WeeklyEmailJob.class);
172 weeklyTrigger.setJobName(weeklyJobDetail.getName());
173 weeklyTrigger.setJobGroup(weeklyJobDetail.getGroup());
174 sendWeeklyReminderCalled = true;
175 } else {
176 LOG.warn("No " + KewApiConstants.WEEKLY_EMAIL_CRON_EXPRESSION
177 + " parameter was configured. Weekly Email batch was not scheduled!");
178 }
179 }
180
181 //@Override
182 protected void sendPeriodicReminder(Person user, Collection<ActionItem> actionItems, String emailSetting) {
183 //super.sendPeriodicReminder(user, actionItems, emailSetting);
184 if (!aggregateReminderCount.containsKey(emailSetting)) {
185 aggregateReminderCount.put(emailSetting, actionItems.size());
186 } else {
187 aggregateReminderCount.put(emailSetting, aggregateReminderCount.get(emailSetting) + actionItems.size());
188 }
189 }
190
191 public Integer getTotalPeriodicRemindersSent(String emailReminderConstant) {
192 Integer returnVal = aggregateReminderCount.get(emailReminderConstant);
193 if (returnVal == null) {
194 returnVal = Integer.valueOf(0);
195 }
196 return returnVal;
197 }
198
199 public Integer getTotalPeriodicRemindersSent() {
200 int total = 0;
201 for (Map.Entry<String, Integer> mapEntry : aggregateReminderCount.entrySet()) {
202 Integer value = mapEntry.getValue();
203 total += (value == null) ? 0 : value.intValue();
204 }
205 return Integer.valueOf(total);
206 }
207
208 public boolean wasStyleServiceAccessed() {
209 return getEmailContentGenerator().wasServiceAccessed();
210 }
211
212 private void resetStyleService() {
213 getEmailContentGenerator().resetServiceAccessed();
214 }
215
216 public int immediateReminderEmailsSent(String networkId, String documentId, String actionRequestCd) {
217 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(networkId);
218 List actionItemsSentUser = immediateReminders.get(principal.getPrincipalId());
219 if (actionItemsSentUser == null) {
220 LOG.info("There are no immediate reminders for Principal " + networkId + " and Document ID " + documentId);
221 return 0;
222 }
223 else {
224 LOG.info("There are " + actionItemsSentUser.size() + " immediate reminders for Principal " + networkId + " and Document ID " + documentId);
225 }
226 int emailsSent = 0;
227 for (Iterator iter = actionItemsSentUser.iterator(); iter.hasNext();) {
228 ActionItem actionItem = (ActionItem) iter.next();
229 if (actionItem.getDocumentId().equals(documentId) && actionItem.getActionRequestCd().equals(actionRequestCd)) {
230 emailsSent++;
231 }
232 }
233 LOG.info(emailsSent + "No immediate e-mails were sent to Principal " + networkId + " and Document ID " + documentId);
234 return emailsSent;
235 }
236
237 public void setEmailContentGenerator(EmailContentService contentService) {
238 this.contentService = contentService;
239 }
240
241 protected MockStyleableEmailContentService getEmailContentGenerator() {
242 return (MockStyleableEmailContentService) contentService;
243 }
244
245 public void setMailer(Mailer mailer){
246 this.mailer = mailer;
247 }
248
249 public void setDeploymentEnvironment(String deploymentEnvironment) {
250 this.deploymentEnvironment = deploymentEnvironment;
251 }
252
253 private PreferencesService getPreferencesService() {
254 return KewApiServiceLocator.getPreferencesService();
255 }
256
257 }