1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37
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
48
49 private Mailer mailer;
50
51
52
53 private MailMessage messageTemplate;
54
55
56
57
58
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
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
111
112
113
114
115
116
117
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
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
145 msg.setSubject((subject==null) ? "" : subject);
146
147
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
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 }