View Javadoc

1   /**
2    * Copyright 2005-2014 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 java.text.MessageFormat;
19  import java.util.Arrays;
20  import java.util.HashSet;
21  import java.util.Set;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.log4j.Logger;
25  import org.kuali.rice.core.api.mail.MailMessage;
26  import org.kuali.rice.core.api.mail.Mailer;
27  import org.kuali.rice.kim.api.identity.Person;
28  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29  import org.kuali.rice.krad.service.KRADServiceLocator;
30  import org.kuali.rice.krad.service.KualiFeedbackService;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.KRADConstants;
33  
34  /**
35   * This class implements the KualiFeedbackService and contains logic
36   * to send a feedback email to the feedback email list with information from
37   * users.
38   */
39  public class KualiFeedbackServiceImpl implements KualiFeedbackService {
40  	
41  	private static final Logger LOG = Logger.getLogger(KualiFeedbackServiceImpl.class);
42  	
43  	private static final String FEEDBACK_EMAIL_SUBJECT_PARAM = "feedback.email.subject";
44  	private static final String FEEDBACK_EMAIL_BODY_PARAM = "feedback.email.body";
45  
46  	/**
47  	 * A Mailer for sending report.
48  	 */
49  	private Mailer mailer;
50  	/**
51  	 * An email template is used to construct an email to be sent by the mail service.
52  	 */
53  	private MailMessage messageTemplate;
54  
55  	/**
56  	 * This mails the report using the mail service from the mail template.
57  	 * 
58  	 * @see org.kuali.rice.krad.service.KualiExceptionIncidentService#emailReport(String, String)
59  	 */
60  	@Override
61  	public void emailReport(String subject, String message) throws Exception {
62  		if (LOG.isTraceEnabled()) {
63  			String lm=String.format("ENTRY %s;%s",
64  					(subject==null)?"null":subject.toString(),
65  							(message==null)?"null":message.toString());
66  			LOG.trace(lm);
67  		}
68  
69  		if (mailer == null) {
70  			String errorMessage = "mailer property of KualiExceptionIncidentServiceImpl is null";
71  			LOG.fatal(errorMessage);
72  			throw new IllegalStateException(errorMessage);
73  		}
74  
75  		// Send mail
76  		MailMessage msg = createMailMessage(subject, message);
77  		mailer.sendEmail(msg);
78  
79  		if (LOG.isTraceEnabled()) {
80  			LOG.trace("EXIT");
81  		}
82  	}
83  
84  	@Override
85  	public void sendFeedback(String documentId, String componentName, String description) throws Exception {
86  		this.emailReport(this.createFeedbackReportSubject(), this.createFeedbackReportMessage(documentId, componentName, description));
87  	}
88  	
89  	private String createFeedbackReportSubject() {
90  		String env = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.ENVIRONMENT_KEY);
91  		String formatString = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(FEEDBACK_EMAIL_SUBJECT_PARAM);
92  		String subject = MessageFormat.format(formatString, env);
93  		return subject;
94  	}
95  
96  	private String createFeedbackReportMessage(String documentId, String componentName, String description) {
97  		documentId = (documentId == null) ? "" : documentId;
98  		componentName = (componentName == null) ? "" : componentName;
99  		description = (description == null) ? "" : description;
100 		
101 		String principalName = GlobalVariables.getUserSession().getLoggedInUserPrincipalName();
102 		principalName = (principalName == null) ? "" : principalName;
103 		
104 		String formatString = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(FEEDBACK_EMAIL_BODY_PARAM);
105 		String message = MessageFormat.format(formatString, documentId, principalName, componentName, description);
106 		return message;
107 	}
108 
109 	/**
110      * Creates an instance of MailMessage from the inputs using the given
111      * template.
112 	 *
113      * @param subject the subject line text
114      * @param message the body of the email message
115      * @return MailMessage
116      * @throws IllegalStateException if the <codeREPORT_MAIL_LIST</code> is not set
117      * or messageTemplate does not have ToAddresses already set.
118      */
119     @SuppressWarnings("unchecked")
120     protected MailMessage createMailMessage(String subject, String message)
121             throws Exception {
122 		if (LOG.isTraceEnabled()) {
123             String lm=String.format("ENTRY %s%n%s",
124                     (subject==null) ? "null" : subject.toString(),
125                     (message==null) ? "null" : message.toString());
126             LOG.trace(lm);
127         }
128         
129         MailMessage messageTemplate = this.getMessageTemplate();
130         if (messageTemplate == null) {
131             throw new IllegalStateException(String.format(
132                     "%s.templateMessage is null or not set",
133                     this.getClass().getName()));
134         }
135         
136         // Copy input message reference for creating an instance of mail message
137         MailMessage msg=new MailMessage();
138         
139         msg.setFromAddress(this.getFromAddress());
140     	msg.setToAddresses(this.getToAddresses());
141         msg.setBccAddresses(this.getBccAddresses());
142         msg.setCcAddresses(this.getCcAddresses());
143 
144         // Set mail message subject
145         msg.setSubject((subject==null) ? "" : subject);
146 
147         // Set mail message body
148         msg.setMessage((message==null) ? "" : message);
149         
150         if (LOG.isTraceEnabled()) {
151             String lm = String.format("EXIT %s", (msg==null) ? "null" : msg.toString());
152             LOG.trace(lm);
153         }
154 
155         return msg;
156 	}
157 
158 	protected String getFromAddress() {    
159 		Person actualUser = GlobalVariables.getUserSession().getActualPerson();
160         
161         String fromEmail = actualUser.getEmailAddress();
162         if (StringUtils.isNotBlank(fromEmail)) {
163         	return fromEmail;
164     	} else {
165         	return this.getMessageTemplate().getFromAddress();
166     	}
167 	}
168 	
169 	protected Set<String> getToAddresses() {
170 		// First check if message template already define mailing list
171         Set<String> emails = this.getMessageTemplate().getToAddresses();
172         if (emails == null || emails.isEmpty()) {
173     		String mailingList = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(this.getToAddressesPropertyName());
174             if (StringUtils.isBlank(mailingList)) {
175                 String em = REPORT_MAIL_LIST + " is not set or messageTemplate does not have ToAddresses already set.";
176                 LOG.error(em);
177                 throw new IllegalStateException(em);
178             } else {
179                 return new HashSet<String>(Arrays.asList(StringUtils.split(mailingList,
180                                 KRADConstants.FIELD_CONVERSIONS_SEPARATOR)));
181             }
182         } else {
183             return emails;
184         }
185 	}
186 	
187 	protected String getToAddressesPropertyName() {
188 		return REPORT_MAIL_LIST;
189 	}
190 	
191 	protected Set<String> getCcAddresses() {
192 		return this.getMessageTemplate().getCcAddresses();
193 	}
194 	
195 	protected Set<String> getBccAddresses() {
196 		return this.getMessageTemplate().getBccAddresses();
197 	}
198 
199 	public Mailer getMailer() {
200 		return mailer;
201 	}
202 
203 	public final void setMailer(Mailer mailer) {
204 		this.mailer = mailer;
205 	}
206 
207 	public MailMessage getMessageTemplate() {
208 		return messageTemplate;
209 	}
210 
211 	public void setMessageTemplate(MailMessage messageTemplate) {
212 		this.messageTemplate = messageTemplate;
213 	}
214 }