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.krad.service.impl;
017
018import javax.mail.MessagingException;
019
020import org.kuali.rice.core.api.CoreConstants;
021import org.kuali.rice.core.api.mail.MailMessage;
022import org.kuali.rice.core.api.mail.Mailer;
023import org.kuali.rice.krad.exception.InvalidAddressException;
024import org.kuali.rice.krad.service.KRADServiceLocator;
025import org.kuali.rice.krad.service.MailService;
026import org.kuali.rice.krad.util.KRADConstants;
027
028public class MailServiceImpl implements MailService {
029    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MailServiceImpl.class);
030
031    private String batchMailingList;
032    private Mailer mailer;
033
034    private String nonProductionNotificationMailingList;
035    private boolean realNotificationsEnabled = true;
036
037    /**
038     * The injected Mailer.
039     */
040    public void setMailer(Mailer mailer) {
041        this.mailer = mailer;
042    }
043    
044    /**
045     * 
046     */
047    public MailServiceImpl() {
048        super();
049    }
050
051    /**
052     * Sets the batchMailingList attribute value.
053     * @param batchMailingList The batchMailingList to set.
054     */
055    public void setBatchMailingList(String batchMailingList) {
056        this.batchMailingList = batchMailingList;
057    }
058
059    /**
060     * @see org.kuali.rice.krad.service.MailService#getBatchMailingList()
061     */
062    public String getBatchMailingList() {
063        return batchMailingList;
064    }
065
066        /**
067         * This overridden method ...
068         * @throws MessagingException 
069         * 
070         * @see org.kuali.rice.krad.service.MailService#sendMessage(org.kuali.rice.core.api.mail.MailMessage)
071         */
072        @Override
073        public void sendMessage(MailMessage message) throws InvalidAddressException, MessagingException {
074                mailer.sendEmail(composeMessage(message));              
075        }
076        
077    protected MailMessage composeMessage(MailMessage message){
078
079        MailMessage mm = new MailMessage();
080
081        // If realNotificationsEnabled is false, mails will be sent to nonProductionNotificationMailingList
082        if(!isRealNotificationsEnabled()){
083            getNonProductionMessage(message);
084        }
085
086        String app = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(CoreConstants.Config.APPLICATION_ID);
087        String env = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.ENVIRONMENT_KEY);
088        
089        mm.setToAddresses(message.getToAddresses());
090        mm.setBccAddresses(message.getBccAddresses());
091        mm.setCcAddresses(message.getCcAddresses());
092        mm.setSubject(app + " " + env + ": " + message.getSubject());
093        mm.setMessage(message.getMessage());
094        mm.setFromAddress(message.getFromAddress());
095        return mm;
096    }
097
098    public String getNonProductionNotificationMailingList() {
099        return this.nonProductionNotificationMailingList;
100    }
101
102    /**
103     * @param nonProductionNotificationMailingList the nonProductionNotificationMailingList to set
104     */
105    public void setNonProductionNotificationMailingList(
106            String nonProductionNotificationMailingList) {
107        this.nonProductionNotificationMailingList = nonProductionNotificationMailingList;
108    }
109
110    /**
111     * @return the realNotificationsEnabled
112     */
113    public boolean isRealNotificationsEnabled() {
114        return this.realNotificationsEnabled;
115    }
116
117    /**
118     * @param realNotificationsEnabled the realNotificationsEnabled to set
119     */
120    public void setRealNotificationsEnabled(boolean realNotificationsEnabled) {
121        this.realNotificationsEnabled = realNotificationsEnabled;
122    }
123
124    protected MailMessage getNonProductionMessage(MailMessage message){
125        StringBuilder buf = new StringBuilder();
126        buf.append("Email To: ").append(message.getToAddresses()).append("\n");
127        buf.append("Email CC: ").append(message.getCcAddresses()).append("\n");
128        buf.append("Email BCC: ").append(message.getBccAddresses()).append("\n\n");
129        buf.append(message.getMessage());
130
131        message.getToAddresses().clear();
132        //Note: If the non production notification mailing list is blank, sending this message will throw an exception
133        message.addToAddress(getNonProductionNotificationMailingList());
134        message.getBccAddresses().clear();
135        message.getCcAddresses().clear();
136        message.setMessage(buf.toString());
137
138        return message;
139    }
140}