001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kcb.service.impl;
017
018import javax.mail.internet.AddressException;
019import javax.mail.internet.InternetAddress;
020
021import org.apache.commons.lang.StringUtils;
022import org.apache.log4j.Logger;
023import org.kuali.rice.core.api.mail.EmailBody;
024import org.kuali.rice.core.api.mail.EmailFrom;
025import org.kuali.rice.core.api.mail.EmailSubject;
026import org.kuali.rice.core.api.mail.EmailTo;
027import org.kuali.rice.core.api.mail.Mailer;
028import org.kuali.rice.kcb.bo.Message;
029import org.kuali.rice.kcb.bo.MessageDelivery;
030import org.kuali.rice.kcb.service.EmailService;
031import org.kuali.rice.ken.util.NotificationConstants;
032import org.springframework.beans.factory.annotation.Required;
033
034/**
035 * This class is responsible for implementing the service that sends emails to individuals.
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 * @auther Aaron Godert (ag266 at cornell dot edu)
038 */
039public class EmailServiceImpl implements EmailService {
040
041        private static Logger LOG = Logger.getLogger(EmailServiceImpl.class);
042
043        private static final String FORMAT_TEXT_HTML = "text/html";
044        private static final String FORMAT_TEXT_PLAIN = "text/plain";
045
046    // values injected into these from Spring
047    private String weburl;
048    private String defaultSender = "kcb@localhost";
049
050    private final String DETAILACTION = "DetailView.form";
051    
052    private Mailer mailer;
053    
054    public void setMailer(Mailer mailer){
055        this.mailer = mailer;
056    }
057
058    /**
059     * Sets the weburl attribute value (injected from Spring).
060     * @param weburl
061     */
062    @Required
063    public void setWeburl(String weburl) {
064        this.weburl = weburl;
065    }
066
067    /**
068     * Sets the default sender address to use if no valid producer email address
069     * was specified in the message
070     * @param defaultSender the default sender address to use if no valid producer email address was specified in the message
071     */
072    public void setDefaultSender(String defaultSender) {
073        this.defaultSender = defaultSender;
074    }
075
076    /**
077     * First constructs the appropriately formatted mail message then sends it off.
078     * @see org.kuali.rice.kcb.service.EmailService#sendNotificationEmail(org.kuali.rice.kcb.bo.MessageDelivery, java.lang.String, java.lang.String)
079     */
080    public Long sendEmail(MessageDelivery messageDelivery, String recipientEmailAddress, String emailFormat) throws Exception {
081        // reconcile the need for custom rendering depending on message producer
082        // or can we just have a single URL that redirects to original dochandler?
083
084        Message message = messageDelivery.getMessage();
085        String channelName = message.getChannel();
086
087        String producer = message.getProducer();
088        String sender = defaultSender;
089        if (producer != null) {
090            try {
091                InternetAddress[] addresses = InternetAddress.parse(producer, false);
092                if (addresses.length > 0) {
093                    sender = addresses[0].toString();
094                }
095            } catch (AddressException ae) {
096                // not a valid email address
097            }
098        }
099
100        String title = messageDelivery.getMessage().getTitle();
101        String subject = (channelName == null ? "" : channelName + " ") + (!StringUtils.isBlank(title) ? " - " + title : "");
102
103        String format = FORMAT_TEXT_PLAIN;
104        String linebreak = "\n\n";
105
106        // NOTE: we don't set the docId parameter in the link
107        // This forces the detail view to not render an acknowledge
108        // button
109        String link = weburl +"/"+ DETAILACTION +"?" 
110        + NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID +"="+ messageDelivery.getId();
111
112        if (emailFormat == null || emailFormat.equals("text")) {
113                // defaults values are good for text
114        } else {  // html format
115            format = FORMAT_TEXT_HTML;
116            link = "<a href='"+ link +"'>Notification Detail</a>";
117            linebreak = "<br /><br />";
118        }
119
120        LOG.debug("link: "+link);
121
122        // construct the message
123        StringBuffer sb = new StringBuffer();
124        sb.append("You have a new notification. Click the link below to review its details.");
125        sb.append(linebreak);
126        sb.append(linebreak);
127        sb.append(link);
128        String content = sb.toString();
129
130        LOG.debug("subject: "+subject);
131        LOG.debug("sender: "+sender);
132        LOG.debug("recipient: "+recipientEmailAddress);
133        LOG.debug("content: "+content);
134
135        // actually do the send
136        mailer.sendEmail(new EmailFrom(sender), new EmailTo(recipientEmailAddress), new EmailSubject(subject), new EmailBody(content), !FORMAT_TEXT_PLAIN.equals(format));
137
138        return null;
139    }
140}