001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: // Created on Jan 18, 2007
018: package edu.iu.uis.eden.mail;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.log4j.Logger;
026:
027: import edu.iu.uis.eden.EdenConstants;
028: import edu.iu.uis.eden.KEWServiceLocator;
029: import edu.iu.uis.eden.actionitem.ActionItem;
030: import edu.iu.uis.eden.actionlist.ActionListService;
031: import edu.iu.uis.eden.doctype.DocumentType;
032: import edu.iu.uis.eden.preferences.Preferences;
033: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
034: import edu.iu.uis.eden.user.UserService;
035: import edu.iu.uis.eden.user.WorkflowUser;
036: import edu.iu.uis.eden.user.WorkflowUserId;
037: import edu.iu.uis.eden.useroptions.UserOptions;
038: import edu.iu.uis.eden.useroptions.UserOptionsService;
039: import edu.iu.uis.eden.util.Utilities;
040:
041: /**
042: * ActionListEmailService implementation whose content is configurable/parameterizable
043: * via a pluggable EmailContentService
044: * @author Aaron Hamid (arh14 at cornell dot edu)
045: */
046: public class CustomizableActionListEmailServiceImpl implements
047: ActionListEmailService {
048: private static final Logger LOG = Logger
049: .getLogger(CustomizableActionListEmailServiceImpl.class);
050:
051: private EmailContentService contentService;
052: private String deploymentEnvironment;
053:
054: // ---- Spring property
055:
056: public void setEmailContentGenerator(
057: EmailContentService contentService) {
058: this .contentService = contentService;
059: }
060:
061: public void sendImmediateReminder(WorkflowUser user,
062: ActionItem actionItem) {
063: if (!sendActionListEmailNotification()) {
064: LOG.debug("not sending immediate reminder");
065: return;
066: }
067: // since this is a message for a single document, we can customize the from
068: // line based on DocumentType
069: DocumentRouteHeaderValue document = KEWServiceLocator
070: .getRouteHeaderService().getRouteHeader(
071: actionItem.getRouteHeaderId());
072: EmailContent content = contentService
073: .generateImmediateReminder(user, actionItem, document
074: .getDocumentType());
075: sendEmail(user, new EmailSubject(content.getSubject()),
076: new EmailBody(content.getBody()), document
077: .getDocumentType());
078: }
079:
080: public void sendDailyReminder() {
081: if (!sendActionListEmailNotification()) {
082: LOG.debug("not sending daily reminder");
083: return;
084: }
085: Collection users = getUsersWithEmailSetting(EdenConstants.EMAIL_RMNDR_DAY_VAL);
086: for (Iterator userIter = users.iterator(); userIter.hasNext();) {
087: WorkflowUser user = (WorkflowUser) userIter.next();
088: try {
089: Collection actionItems = getActionListService()
090: .getActionList(user, null);
091: if (actionItems != null && actionItems.size() > 0) {
092: sendReminder(user, actionItems,
093: EdenConstants.EMAIL_RMNDR_DAY_VAL);
094: }
095: } catch (Exception e) {
096: LOG.error(
097: "Error sending daily action list reminder to user: "
098: + user.getEmailAddress(), e);
099: }
100: }
101: LOG.debug("Daily action list emails sent successful");
102: }
103:
104: public void sendWeeklyReminder() {
105: if (!sendActionListEmailNotification()) {
106: LOG.debug("not sending weekly reminder");
107: return;
108: }
109: Collection users = getUsersWithEmailSetting(EdenConstants.EMAIL_RMNDR_WEEK_VAL);
110: for (Iterator userIter = users.iterator(); userIter.hasNext();) {
111: WorkflowUser user = (WorkflowUser) userIter.next();
112: try {
113: Collection actionItems = getActionListService()
114: .getActionList(user, null);
115: if (actionItems != null && actionItems.size() > 0) {
116: sendReminder(user, actionItems,
117: EdenConstants.EMAIL_RMNDR_WEEK_VAL);
118: }
119: } catch (Exception e) {
120: LOG.error(
121: "Error sending weekly action list reminder to user: "
122: + user.getEmailAddress(), e);
123: }
124: }
125: LOG.debug("Weekly action list emails sent successful");
126: }
127:
128: // ----
129:
130: private void sendReminder(WorkflowUser user,
131: Collection<ActionItem> actionItems, String emailSetting) {
132: actionItems = filterActionItemsToNotify(user, actionItems);
133: // if there are no action items after being filtered, there's no
134: // reason to send the email
135: if (actionItems.isEmpty()) {
136: return;
137: }
138: EmailContent content;
139: if (EdenConstants.EMAIL_RMNDR_DAY_VAL.equals(emailSetting)) {
140: content = contentService.generateDailyReminder(user,
141: actionItems);
142: } else if (EdenConstants.EMAIL_RMNDR_WEEK_VAL
143: .equals(emailSetting)) {
144: content = contentService.generateWeeklyReminder(user,
145: actionItems);
146: } else {
147: // else...refactor this...
148: throw new RuntimeException(
149: "invalid email setting. this code needs refactoring");
150: }
151: sendEmail(user, new EmailSubject(content.getSubject()),
152: new EmailBody(content.getBody()));
153: }
154:
155: /**
156: * Returns a filtered Collection of {@link ActionItem}s which are
157: * filtered according to the user's preferences. If they have opted
158: * not to recieve secondary or primary delegation emails then they
159: * will not be included.
160: */
161: private Collection filterActionItemsToNotify(WorkflowUser user,
162: Collection<ActionItem> actionItems) {
163: List filteredItems = new ArrayList();
164: Preferences preferences = KEWServiceLocator
165: .getPreferencesService().getPreferences(user);
166: for (Iterator iterator = actionItems.iterator(); iterator
167: .hasNext();) {
168: ActionItem actionItem = (ActionItem) iterator.next();
169: if (!actionItem.getWorkflowId()
170: .equals(user.getWorkflowId())) {
171: LOG
172: .warn("Encountered an ActionItem with an incorrect workflow ID. Was "
173: + actionItem.getWorkflowId()
174: + " but expected "
175: + user.getWorkflowId());
176: continue;
177: }
178: boolean includeItem = true;
179: if (EdenConstants.DELEGATION_PRIMARY.equals(actionItem
180: .getDelegationType())) {
181: includeItem = EdenConstants.PREFERENCES_YES_VAL
182: .equals(preferences
183: .getNotifyPrimaryDelegation());
184: } else if (EdenConstants.DELEGATION_SECONDARY
185: .equals(actionItem.getDelegationType())) {
186: includeItem = EdenConstants.PREFERENCES_YES_VAL
187: .equals(preferences
188: .getNotifySecondaryDelegation());
189: }
190: if (includeItem) {
191: filteredItems.add(actionItem);
192: }
193: }
194: return filteredItems;
195: }
196:
197: private boolean sendActionListEmailNotification() {
198: return EdenConstants.ACTION_LIST_SEND_EMAIL_NOTIFICATION_VALUE
199: .equals(Utilities
200: .getApplicationConstant(EdenConstants.ACTION_LIST_SEND_EMAIL_NOTIFICATION_KEY));
201: }
202:
203: private void sendEmail(WorkflowUser user, EmailSubject subject,
204: EmailBody body) {
205: sendEmail(user, subject, body, null);
206: }
207:
208: private void sendEmail(WorkflowUser user, EmailSubject subject,
209: EmailBody body, DocumentType documentType) {
210: try {
211: if (isProduction()) {
212: KEWServiceLocator.getEmailService().sendEmail(
213: getEmailFrom(documentType),
214: new EmailTo(user.getEmailAddress()), subject,
215: body, false);
216: } else {
217: KEWServiceLocator
218: .getEmailService()
219: .sendEmail(
220: getEmailFrom(documentType),
221: new EmailTo(
222: Utilities
223: .getApplicationConstant(EdenConstants.ACTIONLIST_EMAIL_TEST_ADDRESS)),
224: subject, body, false);
225: }
226: } catch (Exception e) {
227: LOG.error("Error sending email.", e);
228: }
229: }
230:
231: private EmailFrom getEmailFrom(DocumentType documentType) {
232: return new EmailFrom(contentService
233: .getDocumentTypeEmailAddress(documentType));
234: }
235:
236: private List getUsersWithEmailSetting(String setting) {
237: List users = new ArrayList();
238: Collection userOptions = getUserOptionsService()
239: .findByOptionValue(EdenConstants.EMAIL_RMNDR_KEY,
240: setting);
241: for (Iterator iter = userOptions.iterator(); iter.hasNext();) {
242: String workflowId = ((UserOptions) iter.next())
243: .getWorkflowId();
244: try {
245: users.add(getUserService().getWorkflowUser(
246: new WorkflowUserId(workflowId)));
247: } catch (Exception e) {
248: LOG.error("error retrieving workflow user with ID: "
249: + workflowId);
250: }
251: }
252: return users;
253: }
254:
255: public UserService getUserService() {
256: return (UserService) KEWServiceLocator.getUserService();
257: }
258:
259: private UserOptionsService getUserOptionsService() {
260: return (UserOptionsService) KEWServiceLocator
261: .getUserOptionsService();
262: }
263:
264: private ActionListService getActionListService() {
265: return (ActionListService) KEWServiceLocator
266: .getActionListService();
267: }
268:
269: private boolean isProduction() {
270: return getDeploymentEnvironment().equals(
271: EdenConstants.PROD_DEPLOYMENT_CODE);
272: }
273:
274: public String getDeploymentEnvironment() {
275: return deploymentEnvironment;
276: }
277:
278: public void setDeploymentEnvironment(String deploymentEnvironment) {
279: this.deploymentEnvironment = deploymentEnvironment;
280: }
281: }
|