View Javadoc

1   /**
2    * Copyright 2005-2012 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.CoreConstants;
21  import org.kuali.rice.core.api.mail.MailMessage;
22  import org.kuali.rice.core.api.mail.Mailer;
23  import org.kuali.rice.krad.exception.InvalidAddressException;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.krad.service.MailService;
26  import org.kuali.rice.krad.util.KRADConstants;
27  
28  public class MailServiceImpl implements MailService {
29      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MailServiceImpl.class);
30  
31      private String batchMailingList;
32      private Mailer mailer;
33  
34      private String nonProductionNotificationMailingList;
35      private boolean realNotificationsEnabled = true;
36  
37      /**
38       * The injected Mailer.
39       */
40      public void setMailer(Mailer mailer) {
41      	this.mailer = mailer;
42      }
43      
44      /**
45       * 
46       */
47      public MailServiceImpl() {
48          super();
49      }
50  
51      /**
52       * Sets the batchMailingList attribute value.
53       * @param batchMailingList The batchMailingList to set.
54       */
55      public void setBatchMailingList(String batchMailingList) {
56          this.batchMailingList = batchMailingList;
57      }
58  
59      /**
60       * @see org.kuali.rice.krad.service.MailService#getBatchMailingList()
61       */
62      public String getBatchMailingList() {
63          return batchMailingList;
64      }
65  
66  	/**
67  	 * This overridden method ...
68  	 * @throws MessagingException 
69  	 * 
70  	 * @see org.kuali.rice.krad.service.MailService#sendMessage(org.kuali.rice.core.api.mail.MailMessage)
71  	 */
72  	@Override
73  	public void sendMessage(MailMessage message) throws InvalidAddressException, MessagingException {
74  		mailer.sendEmail(composeMessage(message));		
75  	}
76  	
77      protected MailMessage composeMessage(MailMessage message){
78  
79          MailMessage mm = new MailMessage();
80  
81          // If realNotificationsEnabled is false, mails will be sent to nonProductionNotificationMailingList
82          if(!isRealNotificationsEnabled()){
83              getNonProductionMessage(message);
84          }
85  
86          String app = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(CoreConstants.Config.APPLICATION_ID);
87          String env = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.ENVIRONMENT_KEY);
88          
89          mm.setToAddresses(message.getToAddresses());
90          mm.setBccAddresses(message.getBccAddresses());
91          mm.setCcAddresses(message.getCcAddresses());
92          mm.setSubject(app + " " + env + ": " + message.getSubject());
93          mm.setMessage(message.getMessage());
94          mm.setFromAddress(message.getFromAddress());
95          return mm;
96      }
97  
98      public String getNonProductionNotificationMailingList() {
99          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 }