1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.api.mail.Mailer;
30 import org.kuali.rice.kew.api.KewApiServiceLocator;
31 import org.kuali.rice.kew.api.action.ActionItem;
32 import org.kuali.rice.kew.api.preferences.Preferences;
33 import org.kuali.rice.kew.api.preferences.PreferencesService;
34 import org.kuali.rice.kew.mail.DailyEmailJob;
35 import org.kuali.rice.kew.mail.WeeklyEmailJob;
36 import org.kuali.rice.kew.mail.service.EmailContentService;
37 import org.kuali.rice.kew.api.KewApiConstants;
38 import org.kuali.rice.kew.mail.service.impl.CustomizableActionListEmailServiceImpl;
39 import org.kuali.rice.kim.api.identity.Person;
40 import org.kuali.rice.kim.api.identity.principal.Principal;
41 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
42 import org.quartz.CronTrigger;
43 import org.quartz.JobDetail;
44
45
46
47
48 public class MockEmailNotificationServiceImpl extends CustomizableActionListEmailServiceImpl implements MockEmailNotificationService {
49 private static final Logger LOG = Logger.getLogger(MockEmailNotificationServiceImpl.class);
50
51 private static Map<String,List> immediateReminders = new HashMap<String,List>();
52 private static Map<String,Integer> aggregateReminderCount = new HashMap<String,Integer>();
53 private boolean sendDailyReminderCalled = false;
54 private boolean sendWeeklyReminderCalled = false;
55
56 private static final String DAILY_TRIGGER_NAME = "Daily Email Trigger";
57 private static final String DAILY_JOB_NAME = "Daily Email";
58 private static final String WEEKLY_TRIGGER_NAME = "Weekly Email Trigger";
59 private static final String WEEKLY_JOB_NAME = "Weekly Email";
60
61 private EmailContentService contentService;
62 private String deploymentEnvironment;
63
64 private Mailer mailer;
65
66
67
68
69 public void resetReminderCounts() {
70 aggregateReminderCount.clear();
71 immediateReminders.clear();
72 }
73
74
75
76
77
78 @Override
79 public void sendImmediateReminder(ActionItem actionItem, Boolean skipOnApprovals) {
80 if (skipOnApprovals != null && skipOnApprovals.booleanValue()
81 && actionItem.getActionRequestCd().equals(KewApiConstants.ACTION_REQUEST_APPROVE_REQ)) {
82 LOG.debug("As requested, skipping immediate reminder notification on action item approval for " + actionItem.getPrincipalId());
83 return;
84 }
85 List actionItemsSentUser = (List)immediateReminders.get(actionItem.getPrincipalId());
86 Preferences preferences = getPreferencesService().getPreferences(actionItem.getPrincipalId());
87
88 boolean shouldNotify = checkEmailNotificationPreferences(actionItem, preferences, KewApiConstants.EMAIL_RMNDR_IMMEDIATE);
89 if(shouldNotify) {
90 if (actionItemsSentUser == null) {
91 actionItemsSentUser = new ArrayList();
92 immediateReminders.put(actionItem.getPrincipalId(), actionItemsSentUser);
93 }
94 actionItemsSentUser.add(actionItem);
95 }
96 }
97
98
99
100
101
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
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
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
182 protected void sendPeriodicReminder(Person user, Collection<ActionItem> actionItems, String emailSetting) {
183
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 }