View Javadoc

1   /**
2    * Copyright 2005-2013 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.krad.service.impl;
17  
18  import javax.mail.MessagingException;
19  
20  import org.kuali.rice.core.api.CoreApiServiceLocator;
21  import org.kuali.rice.core.api.CoreConstants;
22  import org.kuali.rice.core.api.mail.MailMessage;
23  import org.kuali.rice.core.api.mail.Mailer;
24  import org.kuali.rice.krad.exception.InvalidAddressException;
25  import org.kuali.rice.krad.service.MailService;
26  import org.kuali.rice.krad.util.KRADConstants;
27  
28  /**
29   * Default implementation of mail service
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class MailServiceImpl implements MailService {
34      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MailServiceImpl.class);
35  
36      private String batchMailingList;
37      private Mailer mailer;
38  
39      private String nonProductionNotificationMailingList;
40      private boolean realNotificationsEnabled = true;
41  
42      /**
43       * The injected Mailer.
44       */
45      public void setMailer(Mailer mailer) {
46      	this.mailer = mailer;
47      }
48      
49      /**
50       * 
51       */
52      public MailServiceImpl() {
53          super();
54      }
55  
56      /**
57       * Sets the batchMailingList attribute value.
58       * @param batchMailingList The batchMailingList to set.
59       */
60      public void setBatchMailingList(String batchMailingList) {
61          this.batchMailingList = batchMailingList;
62      }
63  
64      /**
65       * @see org.kuali.rice.krad.service.MailService#getBatchMailingList()
66       */
67      public String getBatchMailingList() {
68          return batchMailingList;
69      }
70  
71  	/**
72  	 * This overridden method ...
73  	 * @throws MessagingException 
74  	 * 
75  	 * @see org.kuali.rice.krad.service.MailService#sendMessage(org.kuali.rice.core.api.mail.MailMessage)
76  	 */
77  	@Override
78  	public void sendMessage(MailMessage message) throws InvalidAddressException, MessagingException {
79  		mailer.sendEmail(composeMessage(message));		
80  	}
81  	
82      protected MailMessage composeMessage(MailMessage message){
83  
84          MailMessage mm = new MailMessage();
85  
86          // If realNotificationsEnabled is false, mails will be sent to nonProductionNotificationMailingList
87          if(!isRealNotificationsEnabled()){
88              getNonProductionMessage(message);
89          }
90  
91          String app = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(CoreConstants.Config.APPLICATION_ID);
92          String env = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.ENVIRONMENT_KEY);
93          
94          mm.setToAddresses(message.getToAddresses());
95          mm.setBccAddresses(message.getBccAddresses());
96          mm.setCcAddresses(message.getCcAddresses());
97          mm.setSubject(app + " " + env + ": " + message.getSubject());
98          mm.setMessage(message.getMessage());
99          mm.setFromAddress(message.getFromAddress());
100         return mm;
101     }
102 
103     public String getNonProductionNotificationMailingList() {
104         return this.nonProductionNotificationMailingList;
105     }
106 
107     /**
108      * @param nonProductionNotificationMailingList the nonProductionNotificationMailingList to set
109      */
110     public void setNonProductionNotificationMailingList(
111             String nonProductionNotificationMailingList) {
112         this.nonProductionNotificationMailingList = nonProductionNotificationMailingList;
113     }
114 
115     /**
116      * @return the realNotificationsEnabled
117      */
118     public boolean isRealNotificationsEnabled() {
119         return this.realNotificationsEnabled;
120     }
121 
122     /**
123      * @param realNotificationsEnabled the realNotificationsEnabled to set
124      */
125     public void setRealNotificationsEnabled(boolean realNotificationsEnabled) {
126         this.realNotificationsEnabled = realNotificationsEnabled;
127     }
128 
129     protected MailMessage getNonProductionMessage(MailMessage message){
130         StringBuilder buf = new StringBuilder();
131         buf.append("Email To: ").append(message.getToAddresses()).append("\n");
132         buf.append("Email CC: ").append(message.getCcAddresses()).append("\n");
133         buf.append("Email BCC: ").append(message.getBccAddresses()).append("\n\n");
134         buf.append(message.getMessage());
135 
136         message.getToAddresses().clear();
137         //Note: If the non production notification mailing list is blank, sending this message will throw an exception
138         message.addToAddress(getNonProductionNotificationMailingList());
139         message.getBccAddresses().clear();
140         message.getCcAddresses().clear();
141         message.setMessage(buf.toString());
142 
143         return message;
144     }
145 }