Coverage Report - org.kuali.rice.kew.mail.service.impl.HardCodedEmailContentServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
HardCodedEmailContentServiceImpl
0%
0/129
0%
0/28
2.286
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.kuali.rice.kew.mail.service.impl;
 19  
 
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.apache.log4j.Logger;
 22  
 import org.kuali.rice.core.config.ConfigContext;
 23  
 import org.kuali.rice.kew.actionitem.ActionItem;
 24  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 25  
 import org.kuali.rice.kew.feedback.web.FeedbackForm;
 26  
 import org.kuali.rice.kew.mail.CustomEmailAttribute;
 27  
 import org.kuali.rice.kew.mail.EmailContent;
 28  
 import org.kuali.rice.kew.util.KEWConstants;
 29  
 import org.kuali.rice.kew.util.Utilities;
 30  
 import org.kuali.rice.kim.bo.Person;
 31  
 
 32  
 import java.util.Collection;
 33  
 import java.util.HashMap;
 34  
 import java.util.Iterator;
 35  
 import java.util.LinkedHashMap;
 36  
 
 37  
 
 38  
 /**
 39  
  * EmailContentGenerator that reproduces the hardcoded messages generated by the
 40  
  * current ActionListEmailServiceImpl. This is for testing purposes during the transition
 41  
  * to the EmailContentService.
 42  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 43  
  */
 44  0
 public class HardCodedEmailContentServiceImpl extends BaseEmailContentServiceImpl {
 45  0
     private static final Logger LOG = Logger.getLogger(HardCodedEmailContentServiceImpl.class);
 46  
 
 47  0
     protected String defaultReminderSubject = "Action List Reminder";
 48  
 
 49  
     public void setDefaultReminderSubject(String defaultReminderSubject) {
 50  0
         this.defaultReminderSubject = defaultReminderSubject;
 51  0
     }
 52  
 
 53  
     // ---- EmailContentService interface implementation
 54  
 
 55  
     public EmailContent generateImmediateReminder(Person person, ActionItem actionItem, DocumentType documentType) {
 56  0
         String docHandlerUrl = actionItem.getRouteHeader().getDocumentType().getDocHandlerUrl();
 57  0
         if (StringUtils.isNotBlank(docHandlerUrl)) {
 58  0
             if (!docHandlerUrl.contains("?")) {
 59  0
                 docHandlerUrl += "?";
 60  
             } else {
 61  0
                 docHandlerUrl += "&";
 62  
             }
 63  0
             docHandlerUrl += KEWConstants.ROUTEHEADER_ID_PARAMETER + "=" + actionItem.getRouteHeaderId();
 64  0
             docHandlerUrl += "&" + KEWConstants.COMMAND_PARAMETER + "=" + KEWConstants.ACTIONLIST_COMMAND;
 65  
         }
 66  0
         StringBuffer emailBody = new StringBuffer();
 67  
 
 68  0
         emailBody.append("Your Action List has an eDoc(electronic document) that needs your attention: \n\n");
 69  0
         emailBody.append("Document ID:\t" + actionItem.getRouteHeaderId() + "\n");
 70  0
         emailBody.append("Initiator:\t\t");
 71  
         try 
 72  
         {
 73  0
             String displayName = (person == null ? null : person.getName());
 74  0
             emailBody.append(displayName + "\n");
 75  0
         } catch (Exception e) {
 76  0
             LOG.error("Error retrieving initiator for action item " + actionItem.getRouteHeaderId());
 77  0
             emailBody.append("\n");
 78  0
         }
 79  0
         emailBody.append("Type:\t\t" + "Add/Modify " + actionItem.getRouteHeader().getDocumentType().getName() + "\n");
 80  0
         emailBody.append("Title:\t\t" + actionItem.getDocTitle() + "\n");
 81  0
         emailBody.append("\n\n");
 82  0
         if (StringUtils.isNotBlank(docHandlerUrl)) {
 83  0
             emailBody.append("To respond to this eDoc: \n");
 84  0
             emailBody.append("\tGo to " + docHandlerUrl + "\n\n");
 85  0
             emailBody.append("\tOr you may access the eDoc from your Action List: \n");
 86  0
             emailBody.append("\tGo to " + getActionListUrl() + ", and then click on the numeric Document ID: " + actionItem.getRouteHeaderId() + " in the first column of the List. \n");
 87  
         }
 88  
         else {
 89  0
             emailBody.append("To respond to this eDoc go to your Action List: \n");
 90  0
             emailBody.append("\tGo to " + getActionListUrl() + ", and then take actions related to Document ID: " + actionItem.getRouteHeaderId() + ". \n");
 91  
         }
 92  0
         emailBody.append("\n\n\n");
 93  0
         emailBody.append("To change how these email notifications are sent(daily, weekly or none): \n");
 94  0
         emailBody.append("\tGo to " + getPreferencesUrl() + "\n");
 95  0
         emailBody.append("\n\n\n");
 96  
 
 97  0
         emailBody.append(getHelpLink(documentType) + "\n\n\n");
 98  
 
 99  
         // for debugging purposes on the immediate reminder only
 100  0
         if (!isProduction()) {
 101  
             try {
 102  0
                 emailBody.append("Action Item sent to " + actionItem.getPerson().getPrincipalName());
 103  0
                 if (actionItem.getDelegationType() != null) {
 104  0
                     emailBody.append(" for delegation type "
 105  
                             + actionItem.getDelegationType());
 106  
                 }
 107  0
             } catch (Exception e) {
 108  0
                 throw new RuntimeException(e);
 109  0
             }
 110  
         }
 111  
         
 112  0
         StringBuffer emailSubject = new StringBuffer();
 113  
         try {
 114  0
             CustomEmailAttribute customEmailAttribute = getCustomEmailAttribute(person, actionItem);
 115  0
             if (customEmailAttribute != null) {
 116  0
                 String customBody = customEmailAttribute.getCustomEmailBody();
 117  0
                 if (!Utilities.isEmpty(customBody)) {
 118  0
                     emailBody.append(customBody);
 119  
                 }
 120  0
                 String customEmailSubject = customEmailAttribute.getCustomEmailSubject();
 121  0
                 if (!Utilities.isEmpty(customEmailSubject)) {
 122  0
                     emailSubject.append(customEmailSubject);
 123  
                 }
 124  
             }
 125  0
         } catch (Exception e) {
 126  0
             LOG.error("Error when checking for custom email body and subject.", e);
 127  0
         }
 128  
 
 129  0
         EmailContent content = new EmailContent();
 130  0
         content.setBody(emailBody.toString(), false);
 131  0
         content.setSubject(getEmailSubject(emailSubject.toString()));
 132  
 
 133  0
         return content;
 134  
 
 135  
     }
 136  
 
 137  
     /*
 138  
      * chb: 09Jan2009 Why are we passing in a Person object if it's not being used?
 139  
      */
 140  
     public EmailContent generateDailyReminder(Person person, Collection<ActionItem> actionItems) {
 141  0
         StringBuffer sf = new StringBuffer();
 142  0
         sf.append(getDailyWeeklyMessageBody(actionItems));
 143  0
         sf.append("To change how these email notifications are sent (immediately, weekly or none): \n");
 144  0
         sf.append("\tGo to " + getPreferencesUrl() + "\n");
 145  
         // sf.append("\tSend as soon as you get an eDoc\n\t" +
 146  
         // getPreferencesUrl() + "\n\n");
 147  
         // sf.append("\tSend weekly\n\t" + getPreferencesUrl() + "\n\n");
 148  
         // sf.append("\tDo not send\n\t" + getPreferencesUrl() + "\n");
 149  0
         sf.append("\n\n\n");
 150  0
         sf.append(getHelpLink() + "\n\n\n");
 151  
 
 152  0
         EmailContent content = new EmailContent();
 153  0
         content.setBody(sf.toString(), false);
 154  0
         content.setSubject(getEmailSubject());
 155  
 
 156  0
         return content;
 157  
     }
 158  
 
 159  
     public EmailContent generateWeeklyReminder(Person user, Collection<ActionItem> actionItems) {
 160  0
         StringBuffer sf = new StringBuffer();
 161  0
         sf.append(getDailyWeeklyMessageBody(actionItems));
 162  0
         sf
 163  
                 .append("To change how these email notifications are sent (immediately, daily or none): \n");
 164  0
         sf.append("\tGo to " + getPreferencesUrl() + "\n");
 165  
         // sf.append("\tSend as soon as you get an eDoc\n\t" +
 166  
         // getPreferencesUrl() + "\n\n");
 167  
         // sf.append("\tSend daily\n\t" + getPreferencesUrl() + "\n\n");
 168  
         // sf.append("\tDo not send\n\t" + getPreferencesUrl() + "\n");
 169  0
         sf.append("\n\n\n");
 170  0
         sf.append(getHelpLink() + "\n\n\n");
 171  
 
 172  0
         EmailContent content = new EmailContent();
 173  0
         content.setBody(sf.toString(), false);
 174  0
         content.setSubject(getEmailSubject());
 175  
 
 176  0
         return content;
 177  
     }
 178  
 
 179  
     /**
 180  
      * @see org.kuali.rice.kew.feedback.web.FeedbackAction
 181  
      * @param form feedback form from action
 182  
      * @return email content
 183  
      */
 184  
     public EmailContent generateFeedback(FeedbackForm form) {
 185  0
         EmailContent content = new EmailContent();
 186  0
         content.setBody(constructFeedbackEmailBody(form), false);
 187  0
         content.setSubject(constructFeedbackSubject(form));
 188  0
         return content;
 189  
     }
 190  
 
 191  
     // ---- helper methods
 192  
 
 193  
     public String getDailyWeeklyMessageBody(Collection actionItems) {
 194  0
         StringBuffer sf = new StringBuffer();
 195  0
         HashMap docTypes = getActionListItemsStat(actionItems);
 196  
 
 197  0
         sf
 198  
                 .append("Your Action List has "
 199  
                         + actionItems.size()
 200  
                         + " eDocs(electronic documents) that need your attention: \n\n");
 201  0
         Iterator iter = docTypes.keySet().iterator();
 202  0
         while (iter.hasNext()) {
 203  0
             String docTypeName = (String) iter.next();
 204  0
             sf.append("\t" + ((Integer) docTypes.get(docTypeName)).toString()
 205  
                     + "\t" + docTypeName + "\n");
 206  0
         }
 207  0
         sf.append("\n\n");
 208  0
         sf.append("To respond to each of these eDocs: \n");
 209  0
         sf
 210  
                 .append("\tGo to "
 211  
                         + getActionListUrl()
 212  
                         + ", and then click on its numeric Document ID in the first column of the List.\n");
 213  0
         sf.append("\n\n\n");
 214  0
         return sf.toString();
 215  
     }
 216  
 
 217  
     private HashMap getActionListItemsStat(Collection actionItems) {
 218  0
         HashMap docTypes = new LinkedHashMap();
 219  0
         Iterator iter = actionItems.iterator();
 220  
 
 221  0
         while (iter.hasNext()) {
 222  0
             String docTypeName = ((ActionItem) iter.next()).getRouteHeader()
 223  
                     .getDocumentType().getName();
 224  0
             if (docTypes.containsKey(docTypeName)) {
 225  0
                 docTypes.put(docTypeName, new Integer(((Integer) docTypes
 226  
                         .get(docTypeName)).intValue() + 1));
 227  
             } else {
 228  0
                 docTypes.put(docTypeName, new Integer(1));
 229  
             }
 230  0
         }
 231  0
         return docTypes;
 232  
     }
 233  
 
 234  
     private String getHelpLink() {
 235  0
         return getHelpLink(null);
 236  
     }
 237  
 
 238  
     private String getHelpLink(DocumentType documentType) {
 239  0
         return "For additional help, email " + "<mailto:" + getDocumentTypeEmailAddress(documentType) + ">";
 240  
     }
 241  
 
 242  
     private String getEmailSubject() {
 243  0
         return defaultReminderSubject;
 244  
     }
 245  
 
 246  
     private String getEmailSubject(String customSubject) {
 247  0
         return defaultReminderSubject + " " + customSubject;
 248  
     }
 249  
 
 250  
     /**
 251  
      * @see org.kuali.rice.kew.feedback.web.FeedbackAction
 252  
      * @param form feedback form from action
 253  
      * @return email content
 254  
      */
 255  
     private String constructFeedbackSubject(FeedbackForm form) {
 256  0
         String subject = "Feedback from " + form.getNetworkId();
 257  0
         if (form.getRouteHeaderId() != null) {
 258  0
             subject += (" for document " + form.getRouteHeaderId());
 259  
         }
 260  0
         return subject;
 261  
     }
 262  
 
 263  
     /**
 264  
      * @see org.kuali.rice.kew.feedback.web.FeedbackAction
 265  
      * @param form feedback form from action
 266  
      * @return email content
 267  
      */
 268  
     private String constructFeedbackEmailBody(FeedbackForm form) {
 269  0
         StringBuffer buffer = new StringBuffer();
 270  0
         buffer.append("\n");
 271  0
         buffer.append("Network ID: " + form.getNetworkId()).append("\n");
 272  0
         buffer.append("Name: " + form.getUserName()).append("\n");
 273  0
         buffer.append("Email: " + form.getUserEmail()).append("\n");
 274  0
         buffer.append("Phone: " + form.getPhone()).append("\n");
 275  0
         buffer.append("Time: " + form.getTimeDate()).append("\n");
 276  0
         buffer.append("Environment: " + ConfigContext.getCurrentContextConfig().getEnvironment()).append("\n\n");
 277  
 
 278  0
         buffer.append("Document type: " + form.getDocumentType()).append("\n");
 279  0
         buffer.append("Document id: " + (form.getRouteHeaderId() != null ? form.getRouteHeaderId() : "")).append("\n\n");
 280  
 
 281  0
         buffer.append("Category: " + form.getCategory()).append("\n");
 282  0
         buffer.append("Comments: \n" + form.getComments()).append("\n\n");
 283  
 
 284  0
         buffer.append("Exception: \n" + form.getException());
 285  0
         return buffer.toString();
 286  
     }
 287  
 
 288  
     private boolean isProduction() {
 289  0
         return ConfigContext.getCurrentContextConfig().getProperty(KEWConstants.PROD_DEPLOYMENT_CODE).equalsIgnoreCase(getDeploymentEnvironment());
 290  
     }
 291  
 }