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.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.LinkedHashMap;
024:
025: import org.apache.log4j.Logger;
026: import org.kuali.rice.core.Core;
027:
028: import edu.iu.uis.eden.EdenConstants;
029: import edu.iu.uis.eden.actionitem.ActionItem;
030: import edu.iu.uis.eden.clientapp.IDocHandler;
031: import edu.iu.uis.eden.doctype.DocumentType;
032: import edu.iu.uis.eden.feedback.web.FeedbackForm;
033: import edu.iu.uis.eden.plugin.attributes.CustomEmailAttribute;
034: import edu.iu.uis.eden.user.WorkflowUser;
035: import edu.iu.uis.eden.util.Utilities;
036:
037: /**
038: * EmailContentGenerator that reproduces the hardcoded messages generated by the
039: * current ActionListEmailServiceImpl. This is for testing purposes during the transition
040: * to the EmailContentService.
041: * @author Aaron Hamid (arh14 at cornell dot edu)
042: */
043: public class HardCodedEmailContentServiceImpl extends
044: BaseEmailContentServiceImpl {
045: private static final Logger LOG = Logger
046: .getLogger(HardCodedEmailContentServiceImpl.class);
047:
048: protected String defaultReminderSubject = "OneStart Action List Reminder";
049:
050: public void setDefaultReminderSubject(String defaultReminderSubject) {
051: this .defaultReminderSubject = defaultReminderSubject;
052: }
053:
054: // ---- EmailContentService interface implementation
055:
056: public EmailContent generateImmediateReminder(WorkflowUser user,
057: ActionItem actionItem, DocumentType documentType) {
058: String docHandlerUrl = actionItem.getRouteHeader()
059: .getDocumentType().getDocHandlerUrl();
060: if (docHandlerUrl.indexOf("?") == -1) {
061: docHandlerUrl += "?";
062: } else {
063: docHandlerUrl += "&";
064: }
065: docHandlerUrl += IDocHandler.ROUTEHEADER_ID_PARAMETER + "="
066: + actionItem.getRouteHeaderId();
067: docHandlerUrl += "&" + IDocHandler.COMMAND_PARAMETER + "="
068: + IDocHandler.ACTIONLIST_COMMAND;
069: StringBuffer emailBody = new StringBuffer();
070:
071: emailBody
072: .append("Your OneStart Action List has an eDoc(electronic document) that needs your attention: \n\n");
073: emailBody.append("Document ID:\t"
074: + actionItem.getRouteHeaderId() + "\n");
075: emailBody.append("Initiator:\t\t");
076: try {
077: emailBody.append(actionItem.getRouteHeader()
078: .getInitiatorUser().getDisplayName()
079: + "\n");
080: } catch (Exception e) {
081: LOG.error("Error retrieving initiator for action item "
082: + actionItem.getRouteHeaderId());
083: emailBody.append("\n");
084: }
085: emailBody.append("Type:\t\t"
086: + "Add/Modify "
087: + actionItem.getRouteHeader().getDocumentType()
088: .getName() + "\n");
089: emailBody
090: .append("Title:\t\t" + actionItem.getDocTitle() + "\n");
091: emailBody.append("\n\n");
092: emailBody.append("To respond to this eDoc: \n");
093: emailBody.append("\tGo to " + docHandlerUrl + "\n\n");
094: emailBody
095: .append("\tOr you may access the eDoc from your Action List: \n");
096: emailBody.append("\tGo to " + getActionListUrl()
097: + ", and then click on the numeric Document ID: "
098: + actionItem.getRouteHeaderId()
099: + " in the first column of the List. \n");
100: emailBody.append("\n\n\n");
101: emailBody
102: .append("To change how these email notifications are sent(daily, weekly or none): \n");
103: emailBody.append("\tGo to " + getPreferencesUrl() + "\n");
104: emailBody.append("\n\n\n");
105:
106: emailBody.append(getHelpLink(documentType) + "\n\n\n");
107:
108: // for debugging purposes on the immediate reminder only
109: if (!isProduction()) {
110: try {
111: emailBody.append("Action Item sent to "
112: + actionItem.getUser()
113: .getAuthenticationUserId()
114: .getAuthenticationId());
115: if (actionItem.getDelegationType() != null) {
116: emailBody.append(" for delegation type "
117: + actionItem.getDelegationType());
118: }
119: } catch (Exception e) {
120: throw new RuntimeException(e);
121: }
122: }
123:
124: StringBuffer emailSubject = new StringBuffer();
125: try {
126: CustomEmailAttribute customEmailAttribute = getCustomEmailAttribute(
127: user, actionItem);
128: if (customEmailAttribute != null) {
129: String customBody = customEmailAttribute
130: .getCustomEmailBody();
131: if (!Utilities.isEmpty(customBody)) {
132: emailBody.append(customBody);
133: }
134: String customEmailSubject = customEmailAttribute
135: .getCustomEmailSubject();
136: if (!Utilities.isEmpty(customEmailSubject)) {
137: emailSubject.append(customEmailSubject);
138: }
139: }
140: } catch (Exception e) {
141: LOG
142: .error(
143: "Error when checking for custom email body and subject.",
144: e);
145: }
146:
147: EmailContent content = new EmailContent();
148: content.setBody(emailBody.toString(), false);
149: content.setSubject(getEmailSubject(emailSubject.toString()));
150:
151: return content;
152:
153: }
154:
155: public EmailContent generateDailyReminder(WorkflowUser user,
156: Collection<ActionItem> actionItems) {
157: StringBuffer sf = new StringBuffer();
158: sf.append(getDailyWeeklyMessageBody(actionItems));
159: sf
160: .append("To change how these email notifications are sent (immediately, weekly or none): \n");
161: sf.append("\tGo to " + getPreferencesUrl() + "\n");
162: // sf.append("\tSend as soon as you get an eDoc\n\t" +
163: // getPreferencesUrl() + "\n\n");
164: // sf.append("\tSend weekly\n\t" + getPreferencesUrl() + "\n\n");
165: // sf.append("\tDo not send\n\t" + getPreferencesUrl() + "\n");
166: sf.append("\n\n\n");
167: sf.append(getHelpLink() + "\n\n\n");
168:
169: EmailContent content = new EmailContent();
170: content.setBody(sf.toString(), false);
171: content.setSubject(getEmailSubject());
172:
173: return content;
174: }
175:
176: public EmailContent generateWeeklyReminder(WorkflowUser user,
177: Collection<ActionItem> actionItems) {
178: StringBuffer sf = new StringBuffer();
179: sf.append(getDailyWeeklyMessageBody(actionItems));
180: sf
181: .append("To change how these email notifications are sent (immediately, daily or none): \n");
182: sf.append("\tGo to " + getPreferencesUrl() + "\n");
183: // sf.append("\tSend as soon as you get an eDoc\n\t" +
184: // getPreferencesUrl() + "\n\n");
185: // sf.append("\tSend daily\n\t" + getPreferencesUrl() + "\n\n");
186: // sf.append("\tDo not send\n\t" + getPreferencesUrl() + "\n");
187: sf.append("\n\n\n");
188: sf.append(getHelpLink() + "\n\n\n");
189:
190: EmailContent content = new EmailContent();
191: content.setBody(sf.toString(), false);
192: content.setSubject(getEmailSubject());
193:
194: return content;
195: }
196:
197: /**
198: * @see edu.iu.uis.eden.feedback.web.FeedbackAction
199: * @param form feedback form from action
200: * @return email content
201: */
202: public EmailContent generateFeedback(FeedbackForm form) {
203: EmailContent content = new EmailContent();
204: content.setBody(constructFeedbackEmailBody(form), false);
205: content.setSubject(constructFeedbackSubject(form));
206: return content;
207: }
208:
209: // ---- helper methods
210:
211: public String getDailyWeeklyMessageBody(Collection actionItems) {
212: StringBuffer sf = new StringBuffer();
213: HashMap docTypes = getActionListItemsStat(actionItems);
214:
215: sf
216: .append("Your OneStart Action List has "
217: + actionItems.size()
218: + " eDocs(electronic documents) that need your attention: \n\n");
219: Iterator iter = docTypes.keySet().iterator();
220: while (iter.hasNext()) {
221: String docTypeName = (String) iter.next();
222: sf.append("\t"
223: + ((Integer) docTypes.get(docTypeName)).toString()
224: + "\t" + docTypeName + "\n");
225: }
226: sf.append("\n\n");
227: sf.append("To respond to each of these eDocs: \n");
228: sf
229: .append("\tGo to "
230: + getActionListUrl()
231: + ", and then click on its numeric Document ID in the first column of the List.\n");
232: sf.append("\n\n\n");
233: return sf.toString();
234: }
235:
236: private HashMap getActionListItemsStat(Collection actionItems) {
237: HashMap docTypes = new LinkedHashMap();
238: Iterator iter = actionItems.iterator();
239:
240: while (iter.hasNext()) {
241: String docTypeName = ((ActionItem) iter.next())
242: .getRouteHeader().getDocumentType().getName();
243: if (docTypes.containsKey(docTypeName)) {
244: docTypes.put(docTypeName, new Integer(
245: ((Integer) docTypes.get(docTypeName))
246: .intValue() + 1));
247: } else {
248: docTypes.put(docTypeName, new Integer(1));
249: }
250: }
251: return docTypes;
252: }
253:
254: private String getHelpLink() {
255: return getHelpLink(null);
256: }
257:
258: private String getHelpLink(DocumentType documentType) {
259: return "For additional help, email " + "<mailto:"
260: + getDocumentTypeEmailAddress(documentType) + ">";
261: }
262:
263: private String getEmailSubject() {
264: return defaultReminderSubject;
265: }
266:
267: private String getEmailSubject(String customSubject) {
268: return defaultReminderSubject + " " + customSubject;
269: }
270:
271: /**
272: * @see edu.iu.uis.eden.feedback.web.FeedbackAction
273: * @param form feedback form from action
274: * @return email content
275: */
276: private String constructFeedbackSubject(FeedbackForm form) {
277: String subject = "Feedback from " + form.getNetworkId();
278: if (form.getRouteHeaderId() != null) {
279: subject += (" for document " + form.getRouteHeaderId());
280: }
281: return subject;
282: }
283:
284: /**
285: * @see edu.iu.uis.eden.feedback.web.FeedbackAction
286: * @param form feedback form from action
287: * @return email content
288: */
289: private String constructFeedbackEmailBody(FeedbackForm form) {
290: StringBuffer buffer = new StringBuffer();
291: buffer.append("\n");
292: buffer.append("Network ID: " + form.getNetworkId())
293: .append("\n");
294: buffer.append("Name: " + form.getUserName()).append("\n");
295: buffer.append("Email: " + form.getUserEmail()).append("\n");
296: buffer.append("Phone: " + form.getPhone()).append("\n");
297: buffer.append("Time: " + form.getTimeDate()).append("\n");
298: buffer.append(
299: "Environment: "
300: + Core.getCurrentContextConfig()
301: .getEnvironment()).append("\n\n");
302:
303: buffer.append("Document type: " + form.getDocumentType())
304: .append("\n");
305: buffer.append(
306: "Document id: "
307: + (form.getRouteHeaderId() != null ? form
308: .getRouteHeaderId() : "")).append(
309: "\n\n");
310:
311: buffer.append("Category: " + form.getEdenCategory()).append(
312: "\n");
313: buffer.append("Comments: \n" + form.getComments()).append(
314: "\n\n");
315:
316: buffer.append("Exception: \n" + form.getException());
317: return buffer.toString();
318: }
319:
320: private boolean isProduction() {
321: return EdenConstants.PROD_DEPLOYMENT_CODE
322: .equals(getDeploymentEnvironment());
323: }
324: }
|