Coverage Report - org.kuali.rice.kcb.service.impl.EmailServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
EmailServiceImpl
0%
0/42
0%
0/14
2.75
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  * 
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kcb.service.impl;
 17  
 
 18  
 import javax.mail.internet.AddressException;
 19  
 import javax.mail.internet.InternetAddress;
 20  
 
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 import org.apache.log4j.Logger;
 23  
 import org.kuali.rice.kcb.bo.Message;
 24  
 import org.kuali.rice.kcb.bo.MessageDelivery;
 25  
 import org.kuali.rice.kcb.service.EmailService;
 26  
 import org.kuali.rice.ken.util.NotificationConstants;
 27  
 import org.kuali.rice.kew.mail.EmailBody;
 28  
 import org.kuali.rice.kew.mail.EmailFrom;
 29  
 import org.kuali.rice.kew.mail.EmailSubject;
 30  
 import org.kuali.rice.kew.mail.EmailTo;
 31  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 32  
 import org.springframework.beans.factory.annotation.Required;
 33  
 
 34  
 /**
 35  
  * This class is responsible for implementing the service that sends emails to individuals.
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  * @auther Aaron Godert (ag266 at cornell dot edu)
 38  
  */
 39  0
 public class EmailServiceImpl implements EmailService {
 40  
 
 41  0
         private static Logger LOG = Logger.getLogger(EmailServiceImpl.class);
 42  
 
 43  
         private static final String FORMAT_TEXT_HTML = "text/html";
 44  
         private static final String FORMAT_TEXT_PLAIN = "text/plain";
 45  
 
 46  
     // values injected into these from Spring
 47  
     private String weburl;
 48  0
     private String defaultSender = "kcb@localhost";
 49  
 
 50  0
     private final String DETAILACTION = "DetailView.form";
 51  
 
 52  
     /**
 53  
      * Sets the weburl attribute value (injected from Spring).
 54  
      * @param weburl
 55  
      */
 56  
     @Required
 57  
     public void setWeburl(String weburl) {
 58  0
         this.weburl = weburl;
 59  0
     }
 60  
 
 61  
     /**
 62  
      * Sets the default sender address to use if no valid producer email address
 63  
      * was specified in the message
 64  
      * @param defaultSender the default sender address to use if no valid producer email address was specified in the message
 65  
      */
 66  
     public void setDefaultSender(String defaultSender) {
 67  0
         this.defaultSender = defaultSender;
 68  0
     }
 69  
 
 70  
     /**
 71  
      * First constructs the appropriately formatted mail message then sends it off.
 72  
      * @see org.kuali.rice.kcb.service.EmailService#sendNotificationEmail(org.kuali.rice.kcb.bo.MessageDelivery, java.lang.String, java.lang.String)
 73  
      */
 74  
     public Long sendEmail(MessageDelivery messageDelivery, String recipientEmailAddress, String emailFormat) throws Exception {
 75  
         // reconcile the need for custom rendering depending on message producer
 76  
         // or can we just have a single URL that redirects to original dochandler?
 77  
 
 78  0
         Message message = messageDelivery.getMessage();
 79  0
         String channelName = message.getChannel();
 80  
 
 81  0
         String producer = message.getProducer();
 82  0
         String sender = defaultSender;
 83  0
         if (producer != null) {
 84  
             try {
 85  0
                 InternetAddress[] addresses = InternetAddress.parse(producer, false);
 86  0
                 if (addresses.length > 0) {
 87  0
                     sender = addresses[0].toString();
 88  
                 }
 89  0
             } catch (AddressException ae) {
 90  
                 // not a valid email address
 91  0
             }
 92  
         }
 93  
 
 94  0
         String title = messageDelivery.getMessage().getTitle();
 95  0
         String subject = (channelName == null ? "" : channelName + " ") + (!StringUtils.isBlank(title) ? " - " + title : "");
 96  
 
 97  0
         String format = FORMAT_TEXT_PLAIN;
 98  0
         String linebreak = "\n\n";
 99  
 
 100  
         // NOTE: we don't set the docId parameter in the link
 101  
         // This forces the detail view to not render an acknowledge
 102  
         // button
 103  0
         String link = weburl +"/"+ DETAILACTION +"?" 
 104  
         + NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID +"="+ messageDelivery.getId();
 105  
 
 106  0
         if (emailFormat == null || emailFormat.equals("text")) {
 107  
                 // defaults values are good for text
 108  
         } else {  // html format
 109  0
             format = FORMAT_TEXT_HTML;
 110  0
             link = "<a href='"+ link +"'>Notification Detail</a>";
 111  0
             linebreak = "<br /><br />";
 112  
         }
 113  
 
 114  0
         LOG.debug("link: "+link);
 115  
 
 116  
         // construct the message
 117  0
         StringBuffer sb = new StringBuffer();
 118  0
         sb.append("You have a new notification. Click the link below to review its details.");
 119  0
         sb.append(linebreak);
 120  0
         sb.append(linebreak);
 121  0
         sb.append(link);
 122  0
         String content = sb.toString();
 123  
 
 124  0
         LOG.debug("subject: "+subject);
 125  0
         LOG.debug("sender: "+sender);
 126  0
         LOG.debug("recipient: "+recipientEmailAddress);
 127  0
         LOG.debug("content: "+content);
 128  
 
 129  
         // actually do the send
 130  0
         sendEmail(content, subject, sender, recipientEmailAddress, format);
 131  
 
 132  0
         return null;
 133  
     }
 134  
 
 135  
     /**
 136  
      * do the actual sending.  Uses the KEW's configured EmailService by default. 
 137  
      * @param message message content
 138  
      * @param subject subject of message
 139  
      * @param from sender of message
 140  
      * @param sendTo recipient of message
 141  
      * @param format text or html 
 142  
      */
 143  
     protected void sendEmail(String message, String subject, String from, String sendTo, String format) {
 144  
             
 145  0
             KEWServiceLocator.getEmailService().sendEmail(
 146  
                             new EmailFrom(from), 
 147  
                             new EmailTo(sendTo), 
 148  
                             new EmailSubject(subject), 
 149  
                             new EmailBody(message), 
 150  
                             !FORMAT_TEXT_PLAIN.equals(format));
 151  
 
 152  0
     }
 153  
 }