1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.service.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashSet; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
import java.util.Set; |
23 | |
|
24 | |
import org.apache.log4j.Logger; |
25 | |
import org.kuali.rice.kew.web.session.UserSession; |
26 | |
import org.kuali.rice.kim.bo.Person; |
27 | |
import org.kuali.rice.kns.exception.ExceptionIncident; |
28 | |
import org.kuali.rice.kns.exception.KualiException; |
29 | |
import org.kuali.rice.kns.exception.KualiExceptionIncident; |
30 | |
import org.kuali.rice.kns.mail.MailMessage; |
31 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
32 | |
import org.kuali.rice.kns.service.KualiExceptionIncidentService; |
33 | |
import org.kuali.rice.kns.service.MailService; |
34 | |
import org.kuali.rice.kns.util.KNSConstants; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class KualiExceptionIncidentServiceImpl implements KualiExceptionIncidentService { |
44 | 0 | private Logger LOG=Logger.getLogger(KualiExceptionIncidentServiceImpl.class); |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | public static final String REPORT_MAIL_LIST=String.format( |
54 | |
"%s.REPORT_MAIL_LIST",KualiExceptionIncidentServiceImpl.class.getSimpleName()); |
55 | |
|
56 | |
|
57 | |
|
58 | |
private MailService mailService; |
59 | |
|
60 | |
|
61 | |
|
62 | |
private MailMessage messageTemplate; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public void emailReport(String subject, String message) throws Exception { |
70 | 0 | if (LOG.isTraceEnabled()) { |
71 | 0 | String lm=String.format("ENTRY %s;%s", |
72 | |
(subject==null)?"null":subject.toString(), |
73 | |
(message==null)?"null":message.toString()); |
74 | 0 | LOG.trace(lm); |
75 | |
} |
76 | |
|
77 | 0 | if (mailService == null) { |
78 | 0 | String errMessage="MailService ?"; |
79 | 0 | LOG.fatal(errMessage); |
80 | 0 | throw new KualiException(errMessage); |
81 | |
} |
82 | |
|
83 | |
|
84 | 0 | MailMessage msg=createMailMessage(subject, message); |
85 | 0 | mailService.sendMessage(msg); |
86 | |
|
87 | 0 | if (LOG.isTraceEnabled()) { |
88 | 0 | LOG.trace("EXIT"); |
89 | |
} |
90 | 0 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
@SuppressWarnings("unchecked") |
102 | |
private MailMessage createMailMessage(String subject, String message) |
103 | |
throws Exception{ |
104 | 0 | if (LOG.isTraceEnabled()) { |
105 | 0 | String lm=String.format("ENTRY %s%n%s", |
106 | |
(subject==null)?"null":subject.toString(), |
107 | |
(message==null)?"null":message.toString()); |
108 | 0 | LOG.trace(lm); |
109 | |
} |
110 | |
|
111 | 0 | if (messageTemplate == null) { |
112 | 0 | throw new KualiException(String.format( |
113 | |
"%s.templateMessage is null or not set", |
114 | |
this.getClass().getName())); |
115 | |
} |
116 | |
|
117 | |
|
118 | 0 | MailMessage msg=new MailMessage(); |
119 | |
|
120 | 0 | Person actualUser = UserSession.getAuthenticatedUser().getActualPerson(); |
121 | 0 | String fromEmail = actualUser.getEmailAddress(); |
122 | 0 | if ((fromEmail != null) && (fromEmail != "")) { |
123 | 0 | msg.setFromAddress(fromEmail); |
124 | |
} else { |
125 | 0 | msg.setFromAddress(messageTemplate.getFromAddress()); |
126 | |
} |
127 | |
|
128 | 0 | msg.setBccAddresses(messageTemplate.getBccAddresses()); |
129 | 0 | msg.setCcAddresses(messageTemplate.getCcAddresses()); |
130 | |
|
131 | 0 | Set emails=messageTemplate.getToAddresses(); |
132 | 0 | if (emails == null || emails.isEmpty()) { |
133 | 0 | String mailingList=KNSServiceLocator.getKualiConfigurationService().getPropertyString(REPORT_MAIL_LIST); |
134 | 0 | if (mailingList == null || mailingList.trim().length() == 0) { |
135 | 0 | String em=REPORT_MAIL_LIST+"?"; |
136 | 0 | LOG.error(em); |
137 | 0 | throw new KualiException(em); |
138 | |
} else { |
139 | 0 | msg.setToAddresses(new HashSet<String>(split(mailingList, |
140 | |
KNSConstants.FIELD_CONVERSIONS_SEPARATOR))); |
141 | |
} |
142 | 0 | } else { |
143 | 0 | msg.setToAddresses(emails); |
144 | |
} |
145 | |
|
146 | |
|
147 | 0 | msg.setSubject((subject==null)?"":subject); |
148 | |
|
149 | |
|
150 | 0 | msg.setMessage((message==null)?"":message); |
151 | |
|
152 | 0 | if (LOG.isTraceEnabled()) { |
153 | 0 | String lm=String.format("EXIT %s", |
154 | |
(msg==null)?"null":msg.toString()); |
155 | 0 | LOG.trace(lm); |
156 | |
} |
157 | |
|
158 | 0 | return msg; |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public void report(KualiExceptionIncident exceptionIncident) throws Exception { |
167 | 0 | if (LOG.isTraceEnabled()) { |
168 | 0 | String lm=String.format("ENTRY %s", |
169 | |
(exceptionIncident==null)?"null":exceptionIncident.toString()); |
170 | 0 | LOG.trace(lm); |
171 | |
} |
172 | |
|
173 | 0 | emailReport( |
174 | |
exceptionIncident.getProperty( |
175 | |
KualiExceptionIncident.EXCEPTION_REPORT_SUBJECT), |
176 | |
exceptionIncident.getProperty( |
177 | |
KualiExceptionIncident.EXCEPTION_REPORT_MESSAGE)); |
178 | |
|
179 | 0 | if (LOG.isTraceEnabled()) { |
180 | 0 | String lm=String.format("EXIT"); |
181 | 0 | LOG.trace(lm); |
182 | |
} |
183 | |
|
184 | 0 | } |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public List<String> split(String s, String token) { |
196 | 0 | if (LOG.isTraceEnabled()) { |
197 | 0 | String lm=String.format("ENTRY %s;%s", s, token); |
198 | 0 | LOG.trace(lm); |
199 | |
} |
200 | |
|
201 | 0 | String[] sarray=s.split(token); |
202 | 0 | List<String> list=new ArrayList<String>(); |
203 | 0 | for (int i=0; i<sarray.length && sarray[i].length() > 0; i++) { |
204 | 0 | list.add(sarray[i]); |
205 | |
} |
206 | |
|
207 | 0 | if (LOG.isTraceEnabled()) { |
208 | 0 | String lm=String.format("EXIT %s", list.toString()); |
209 | 0 | LOG.trace(lm); |
210 | |
} |
211 | |
|
212 | 0 | return list; |
213 | |
} |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
public final MailService getMailService() { |
219 | 0 | return this.mailService; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public final void setMailService(MailService mailService) { |
226 | 0 | this.mailService = mailService; |
227 | 0 | } |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
public final MailMessage getMessageTemplate() { |
233 | 0 | return this.messageTemplate; |
234 | |
} |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
public final void setMessageTemplate(MailMessage messageTemplate) { |
240 | 0 | this.messageTemplate = messageTemplate; |
241 | 0 | } |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
public KualiExceptionIncident getExceptionIncident(Exception exception, |
250 | |
Map<String, String> properties) { |
251 | 0 | if ( exception == null ) { |
252 | 0 | return getExceptionIncident(properties); |
253 | |
} |
254 | 0 | if (LOG.isTraceEnabled()) { |
255 | 0 | String lm=String.format("ENTRY %s;%s", exception.getMessage(), |
256 | |
properties.toString()); |
257 | 0 | LOG.trace(lm); |
258 | |
} |
259 | |
|
260 | 0 | KualiExceptionIncident ei=new ExceptionIncident(exception, properties); |
261 | |
|
262 | 0 | if (LOG.isTraceEnabled()) { |
263 | 0 | String lm=String.format("EXIT %s", ei.toProperties().toString()); |
264 | 0 | LOG.trace(lm); |
265 | |
} |
266 | |
|
267 | 0 | return ei; |
268 | |
} |
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public KualiExceptionIncident getExceptionIncident(Map<String, String> properties) { |
277 | 0 | if (LOG.isTraceEnabled()) { |
278 | 0 | String lm=String.format("ENTRY %s", properties.toString()); |
279 | 0 | LOG.trace(lm); |
280 | |
} |
281 | |
|
282 | 0 | ExceptionIncident ei=new ExceptionIncident(properties); |
283 | |
|
284 | 0 | if (LOG.isTraceEnabled()) { |
285 | 0 | String lm=String.format("EXIT %s", ei.toProperties().toString()); |
286 | 0 | LOG.trace(lm); |
287 | |
} |
288 | |
|
289 | 0 | return ei; |
290 | |
} |
291 | |
|
292 | |
} |