View Javadoc

1   /**
2    * Copyright 2005-2011 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.core.mail.EmailBody;
24  import org.kuali.rice.core.mail.EmailFrom;
25  import org.kuali.rice.core.mail.EmailSubject;
26  import org.kuali.rice.core.mail.EmailTo;
27  import org.kuali.rice.core.mail.Mailer;
28  import org.kuali.rice.kcb.bo.Message;
29  import org.kuali.rice.kcb.bo.MessageDelivery;
30  import org.kuali.rice.kcb.service.EmailService;
31  import org.kuali.rice.ken.util.NotificationConstants;
32  import org.kuali.rice.kew.service.KEWServiceLocator;
33  import org.springframework.beans.factory.annotation.Required;
34  
35  /**
36   * This class is responsible for implementing the service that sends emails to individuals.
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   * @auther Aaron Godert (ag266 at cornell dot edu)
39   */
40  public class EmailServiceImpl implements EmailService {
41  
42  	private static Logger LOG = Logger.getLogger(EmailServiceImpl.class);
43  
44  	private static final String FORMAT_TEXT_HTML = "text/html";
45  	private static final String FORMAT_TEXT_PLAIN = "text/plain";
46  
47      // values injected into these from Spring
48      private String weburl;
49      private String defaultSender = "kcb@localhost";
50  
51      private final String DETAILACTION = "DetailView.form";
52      
53      private Mailer mailer;
54      
55      public void setMailer(Mailer mailer){
56      	this.mailer = mailer;
57      }
58  
59      /**
60       * Sets the weburl attribute value (injected from Spring).
61       * @param weburl
62       */
63      @Required
64      public void setWeburl(String weburl) {
65          this.weburl = weburl;
66      }
67  
68      /**
69       * Sets the default sender address to use if no valid producer email address
70       * was specified in the message
71       * @param defaultSender the default sender address to use if no valid producer email address was specified in the message
72       */
73      public void setDefaultSender(String defaultSender) {
74          this.defaultSender = defaultSender;
75      }
76  
77      /**
78       * First constructs the appropriately formatted mail message then sends it off.
79       * @see org.kuali.rice.kcb.service.EmailService#sendNotificationEmail(org.kuali.rice.kcb.bo.MessageDelivery, java.lang.String, java.lang.String)
80       */
81      public Long sendEmail(MessageDelivery messageDelivery, String recipientEmailAddress, String emailFormat) throws Exception {
82          // reconcile the need for custom rendering depending on message producer
83          // or can we just have a single URL that redirects to original dochandler?
84  
85          Message message = messageDelivery.getMessage();
86          String channelName = message.getChannel();
87  
88          String producer = message.getProducer();
89          String sender = defaultSender;
90          if (producer != null) {
91              try {
92                  InternetAddress[] addresses = InternetAddress.parse(producer, false);
93                  if (addresses.length > 0) {
94                      sender = addresses[0].toString();
95                  }
96              } catch (AddressException ae) {
97                  // not a valid email address
98              }
99          }
100 
101         String title = messageDelivery.getMessage().getTitle();
102         String subject = (channelName == null ? "" : channelName + " ") + (!StringUtils.isBlank(title) ? " - " + title : "");
103 
104         String format = FORMAT_TEXT_PLAIN;
105         String linebreak = "\n\n";
106 
107         // NOTE: we don't set the docId parameter in the link
108         // This forces the detail view to not render an acknowledge
109         // button
110         String link = weburl +"/"+ DETAILACTION +"?" 
111         + NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID +"="+ messageDelivery.getId();
112 
113         if (emailFormat == null || emailFormat.equals("text")) {
114         	// defaults values are good for text
115         } else {  // html format
116             format = FORMAT_TEXT_HTML;
117             link = "<a href='"+ link +"'>Notification Detail</a>";
118             linebreak = "<br /><br />";
119         }
120 
121         LOG.debug("link: "+link);
122 
123         // construct the message
124         StringBuffer sb = new StringBuffer();
125         sb.append("You have a new notification. Click the link below to review its details.");
126         sb.append(linebreak);
127         sb.append(linebreak);
128         sb.append(link);
129         String content = sb.toString();
130 
131         LOG.debug("subject: "+subject);
132         LOG.debug("sender: "+sender);
133         LOG.debug("recipient: "+recipientEmailAddress);
134         LOG.debug("content: "+content);
135 
136         // actually do the send
137         mailer.sendEmail(new EmailFrom(sender), new EmailTo(recipientEmailAddress), new EmailSubject(subject), new EmailBody(content), !FORMAT_TEXT_PLAIN.equals(format));
138 
139         return null;
140     }
141 }