1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.kuali.rice.kew.mail.service.impl; |
19 | |
|
20 | |
import java.io.StringWriter; |
21 | |
import java.sql.Timestamp; |
22 | |
import java.util.Collection; |
23 | |
|
24 | |
import javax.xml.parsers.DocumentBuilder; |
25 | |
import javax.xml.parsers.DocumentBuilderFactory; |
26 | |
import javax.xml.parsers.ParserConfigurationException; |
27 | |
import javax.xml.transform.Templates; |
28 | |
import javax.xml.transform.TransformerConfigurationException; |
29 | |
import javax.xml.transform.TransformerException; |
30 | |
import javax.xml.transform.TransformerFactory; |
31 | |
import javax.xml.transform.dom.DOMSource; |
32 | |
import javax.xml.transform.stream.StreamResult; |
33 | |
import javax.xml.transform.stream.StreamSource; |
34 | |
|
35 | |
import org.apache.commons.lang.StringUtils; |
36 | |
import org.apache.log4j.Logger; |
37 | |
import org.kuali.rice.core.util.RiceConstants; |
38 | |
import org.kuali.rice.kew.actionitem.ActionItem; |
39 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
40 | |
import org.kuali.rice.kew.edl.service.StyleService; |
41 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
42 | |
import org.kuali.rice.kew.feedback.web.FeedbackForm; |
43 | |
import org.kuali.rice.kew.mail.CustomEmailAttribute; |
44 | |
import org.kuali.rice.kew.mail.EmailContent; |
45 | |
import org.kuali.rice.kew.mail.EmailStyleHelper; |
46 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
47 | |
import org.kuali.rice.kew.user.UserUtils; |
48 | |
import org.kuali.rice.kew.util.KEWConstants; |
49 | |
import org.kuali.rice.kew.util.Utilities; |
50 | |
import org.kuali.rice.kew.util.XmlHelper; |
51 | |
import org.kuali.rice.kew.web.session.UserSession; |
52 | |
import org.kuali.rice.kim.bo.Person; |
53 | |
import org.kuali.rice.kim.bo.entity.KimPrincipal; |
54 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
55 | |
import org.springframework.core.io.DefaultResourceLoader; |
56 | |
import org.w3c.dom.Document; |
57 | |
import org.w3c.dom.Element; |
58 | |
import org.w3c.dom.Node; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | 0 | public class StyleableEmailContentServiceImpl extends BaseEmailContentServiceImpl { |
70 | 0 | private static final Logger LOG = Logger.getLogger(StyleableEmailContentServiceImpl.class); |
71 | |
|
72 | 0 | protected final String DEFAULT_EMAIL_STYLESHEET_RESOURCE_LOC = "defaultEmailStyle.xsl"; |
73 | |
|
74 | |
protected StyleService styleService; |
75 | 0 | protected EmailStyleHelper styleHelper = new EmailStyleHelper(); |
76 | 0 | protected String globalEmailStyleSheet = KEWConstants.EMAIL_STYLESHEET_NAME; |
77 | |
|
78 | |
|
79 | |
public void setStyleService(StyleService styleService) { |
80 | 0 | this.styleService = styleService; |
81 | 0 | } |
82 | |
|
83 | |
public void setGlobalEmailStyleSheet(String globalEmailStyleSheet) { |
84 | 0 | this.globalEmailStyleSheet = globalEmailStyleSheet; |
85 | 0 | } |
86 | |
|
87 | |
protected static DocumentBuilder getDocumentBuilder(boolean coalesce) { |
88 | |
try { |
89 | 0 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
90 | 0 | dbf.setCoalescing(coalesce); |
91 | 0 | return dbf.newDocumentBuilder(); |
92 | 0 | } catch (ParserConfigurationException e) { |
93 | 0 | String message = "Error constructing document builder"; |
94 | 0 | LOG.error(message, e); |
95 | 0 | throw new WorkflowRuntimeException(message, e); |
96 | |
} |
97 | |
} |
98 | |
|
99 | |
protected static void addObjectXML(Document doc, Object o, Node node, String name) throws Exception { |
100 | 0 | Element element = XmlHelper.propertiesToXml(doc, o, name); |
101 | |
|
102 | 0 | if (LOG.isDebugEnabled()) { |
103 | 0 | LOG.debug(XmlHelper.jotNode(element)); |
104 | |
} |
105 | |
|
106 | 0 | if (node == null) { |
107 | 0 | node = doc; |
108 | |
} |
109 | |
|
110 | 0 | node.appendChild(element); |
111 | 0 | } |
112 | |
|
113 | |
protected static void addTextElement(Document doc, Element baseElement, String elementName, Object elementData) { |
114 | 0 | Element element = doc.createElement(elementName); |
115 | 0 | element.appendChild(doc.createTextNode(elementData.toString())); |
116 | 0 | baseElement.appendChild(element); |
117 | 0 | } |
118 | |
|
119 | |
protected static void addCDataElement(Document doc, Element baseElement, String elementName, Object elementData) { |
120 | 0 | Element element = doc.createElement(elementName); |
121 | 0 | String dataValue = ""; |
122 | 0 | if (elementData != null) { |
123 | 0 | dataValue = elementData.toString(); |
124 | |
} |
125 | 0 | element.appendChild(doc.createCDATASection(dataValue)); |
126 | 0 | baseElement.appendChild(element); |
127 | 0 | } |
128 | |
|
129 | |
protected static void addTimestampElement(Document doc, Element baseElement, String elementName, Timestamp elementData) { |
130 | 0 | addTextElement(doc, baseElement, elementName, RiceConstants.getDefaultDateFormat().format(elementData)); |
131 | 0 | } |
132 | |
|
133 | |
protected static void addDelegatorElement(Document doc, Element baseElement, ActionItem actionItem) { |
134 | 0 | Element delegatorElement = doc.createElement("delegator"); |
135 | 0 | if ( (actionItem.getDelegatorWorkflowId() != null) && (actionItem.getDelegatorWorkflowId() != null) ) { |
136 | |
|
137 | 0 | baseElement.appendChild(delegatorElement); |
138 | 0 | return; |
139 | |
} |
140 | 0 | String delegatorType = ""; |
141 | 0 | String delegatorId = ""; |
142 | 0 | String delegatorDisplayValue = ""; |
143 | 0 | if (actionItem.getDelegatorWorkflowId() != null) { |
144 | 0 | delegatorType = "user"; |
145 | 0 | delegatorId = actionItem.getDelegatorWorkflowId(); |
146 | 0 | KimPrincipal delegator = KIMServiceLocator.getIdentityService().getPrincipal(delegatorId); |
147 | 0 | if (delegator == null) { |
148 | 0 | LOG.error("Cannot find user for id " + delegatorId); |
149 | 0 | delegatorDisplayValue = "USER NOT FOUND"; |
150 | |
} else { |
151 | 0 | delegatorDisplayValue = UserUtils.getTransposedName(UserSession.getAuthenticatedUser(), delegator); |
152 | |
} |
153 | 0 | } else if (actionItem.getDelegatorWorkflowId() != null) { |
154 | 0 | delegatorType = "workgroup"; |
155 | 0 | delegatorId = actionItem.getDelegatorGroupId().toString(); |
156 | 0 | delegatorDisplayValue = KIMServiceLocator.getIdentityManagementService().getGroup(actionItem.getDelegatorGroupId()).getGroupName(); |
157 | |
} |
158 | 0 | delegatorElement.setAttribute("type", delegatorType); |
159 | |
|
160 | 0 | Element idElement = doc.createElement("id"); |
161 | 0 | idElement.appendChild(doc.createTextNode(delegatorId)); |
162 | 0 | delegatorElement.appendChild(idElement); |
163 | |
|
164 | 0 | Element displayValElement = doc.createElement("displayValue"); |
165 | 0 | displayValElement.appendChild(doc.createTextNode(delegatorDisplayValue)); |
166 | 0 | delegatorElement.appendChild(displayValElement); |
167 | 0 | baseElement.appendChild(delegatorElement); |
168 | 0 | } |
169 | |
|
170 | |
protected static void addWorkgroupRequestElement(Document doc, Element baseElement, ActionItem actionItem) { |
171 | 0 | Element workgroupElement = doc.createElement("workgroupRequest"); |
172 | 0 | if (actionItem.isWorkgroupItem()) { |
173 | |
|
174 | 0 | Element idElement = doc.createElement("id"); |
175 | 0 | idElement.appendChild(doc.createTextNode(actionItem.getGroupId())); |
176 | 0 | workgroupElement.appendChild(idElement); |
177 | |
|
178 | 0 | Element displayValElement = doc.createElement("displayValue"); |
179 | 0 | displayValElement.appendChild(doc.createTextNode(actionItem.getGroupId())); |
180 | 0 | workgroupElement.appendChild(displayValElement); |
181 | |
} |
182 | 0 | baseElement.appendChild(workgroupElement); |
183 | 0 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
protected void addSummarizedActionItem(Document doc, ActionItem actionItem, Person user, Node node) throws Exception { |
196 | 0 | if (node == null) { |
197 | 0 | node = doc; |
198 | |
} |
199 | |
|
200 | 0 | Element root = doc.createElement("summarizedActionItem"); |
201 | |
|
202 | |
|
203 | 0 | addTextElement(doc, root, "routeHeaderId", actionItem.getRouteHeaderId()); |
204 | 0 | addTextElement(doc, root, "docName", actionItem.getDocName()); |
205 | 0 | addCDataElement(doc, root, "docLabel", actionItem.getDocLabel()); |
206 | 0 | addCDataElement(doc, root, "docTitle", actionItem.getDocTitle()); |
207 | 0 | DocumentRouteHeaderValue routeHeader = getRouteHeader(actionItem); |
208 | 0 | addTextElement(doc, root, "docRouteStatus", routeHeader.getDocRouteStatus()); |
209 | 0 | addCDataElement(doc, root, "routeStatusLabel", routeHeader.getRouteStatusLabel()); |
210 | 0 | addTextElement(doc, root, "actionRequestCd", actionItem.getActionRequestCd()); |
211 | 0 | addTextElement(doc, root, "actionRequestLabel", actionItem.getActionRequestLabel()); |
212 | 0 | addDelegatorElement(doc, root, actionItem); |
213 | 0 | addTimestampElement(doc, root, "createDate", routeHeader.getCreateDate()); |
214 | 0 | addWorkgroupRequestElement(doc, root, actionItem); |
215 | 0 | addTimestampElement(doc, root, "dateAssigned", actionItem.getDateAssigned()); |
216 | |
|
217 | 0 | node.appendChild(root); |
218 | 0 | } |
219 | |
|
220 | |
public DocumentRouteHeaderValue getRouteHeader(ActionItem actionItem) { |
221 | 0 | return actionItem.getRouteHeader(); |
222 | |
} |
223 | |
|
224 | |
protected static String transform(Templates style, Document doc) { |
225 | 0 | StringWriter writer = new StringWriter(); |
226 | 0 | StreamResult result = new StreamResult(writer); |
227 | |
|
228 | |
try { |
229 | 0 | style.newTransformer().transform(new DOMSource(doc), result); |
230 | 0 | return writer.toString(); |
231 | 0 | } catch (TransformerException te) { |
232 | 0 | String message = "Error transforming DOM"; |
233 | 0 | LOG.error(message, te); |
234 | 0 | throw new WorkflowRuntimeException(message, te); |
235 | |
} |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
protected Templates getStyle(String styleName) { |
246 | 0 | Templates style = null; |
247 | |
try { |
248 | 0 | style = styleService.getStyleAsTranslet(styleName); |
249 | 0 | } catch (TransformerConfigurationException tce) { |
250 | 0 | String message = "Error obtaining style '" + styleName + "', using default"; |
251 | 0 | LOG.error(message, tce); |
252 | |
|
253 | 0 | } |
254 | |
|
255 | 0 | if (style == null) { |
256 | 0 | LOG.warn("Could not find specified style, " + styleName + ", using default"); |
257 | |
try { |
258 | |
|
259 | 0 | style = TransformerFactory.newInstance().newTemplates(new StreamSource(new DefaultResourceLoader().getResource("classpath:org/kuali/rice/kew/mail/" + DEFAULT_EMAIL_STYLESHEET_RESOURCE_LOC).getInputStream())); |
260 | 0 | } catch (Exception tce) { |
261 | 0 | String message = "Error obtaining default style from resource: " + DEFAULT_EMAIL_STYLESHEET_RESOURCE_LOC; |
262 | 0 | LOG.error(message, tce); |
263 | 0 | throw new WorkflowRuntimeException("Error obtaining style '" + styleName + "'", tce); |
264 | 0 | } |
265 | |
} |
266 | 0 | return style; |
267 | |
} |
268 | |
|
269 | |
protected EmailContent generateEmailContent(String styleName, Document doc) { |
270 | 0 | Templates style = getStyle(styleName); |
271 | 0 | return styleHelper.generateEmailContent(style, doc); |
272 | |
} |
273 | |
|
274 | |
protected EmailContent generateReminderForActionItems(Person user, Collection<ActionItem> actionItems, String name, String style) { |
275 | 0 | DocumentBuilder db = getDocumentBuilder(false); |
276 | 0 | Document doc = db.newDocument(); |
277 | 0 | Element element = doc.createElement(name); |
278 | 0 | setStandardAttributes(element); |
279 | 0 | doc.appendChild(element); |
280 | |
|
281 | |
try { |
282 | 0 | addObjectXML(doc, user, element, "user"); |
283 | 0 | for (ActionItem actionItem: actionItems) { |
284 | |
try { |
285 | 0 | addSummarizedActionItem(doc, actionItem, user, element); |
286 | 0 | } catch (Exception e) { |
287 | 0 | String message = "Error generating XML for action item: " + actionItem; |
288 | 0 | LOG.error(message, e); |
289 | 0 | throw new WorkflowRuntimeException(e); |
290 | 0 | } |
291 | |
} |
292 | |
|
293 | 0 | } catch (Exception e) { |
294 | 0 | String message = "Error generating XML for action items: " + actionItems; |
295 | 0 | LOG.error(message, e); |
296 | 0 | throw new WorkflowRuntimeException(e); |
297 | 0 | } |
298 | |
|
299 | 0 | return generateEmailContent(style, doc); |
300 | |
} |
301 | |
|
302 | |
protected void setStandardAttributes(Element e) { |
303 | 0 | e.setAttribute("env", getDeploymentEnvironment()); |
304 | 0 | e.setAttribute("applicationEmailAddress", getApplicationEmailAddress()); |
305 | 0 | e.setAttribute("actionListUrl", getActionListUrl()); |
306 | 0 | e.setAttribute("preferencesUrl", getPreferencesUrl()); |
307 | 0 | } |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
public EmailContent generateImmediateReminder(Person user, ActionItem actionItem, DocumentType documentType) { |
329 | |
|
330 | 0 | LOG.info("Starting generation of immediate email reminder..."); |
331 | 0 | LOG.info("Action Id: " + actionItem.getActionItemId() + |
332 | |
"; ActionRequestId: " + actionItem.getActionRequestId() + |
333 | |
"; Action Item Principal Name: " + actionItem.getPerson().getPrincipalName()); |
334 | 0 | LOG.info("User Principal Name: " + user.getPrincipalName()); |
335 | |
|
336 | 0 | String styleSheet = documentType.getCustomEmailStylesheet(); |
337 | 0 | LOG.debug(documentType.getName() + " style: " + styleSheet); |
338 | 0 | if (styleSheet == null) { |
339 | 0 | styleSheet = globalEmailStyleSheet; |
340 | |
} |
341 | |
|
342 | 0 | LOG.info("generateImmediateReminder using style sheet: "+ styleSheet + " for Document Type " + documentType.getName()); |
343 | |
|
344 | 0 | DocumentBuilder db = getDocumentBuilder(false); |
345 | 0 | Document doc = db.newDocument(); |
346 | 0 | Element element = doc.createElement("immediateReminder"); |
347 | 0 | setStandardAttributes(element); |
348 | 0 | doc.appendChild(element); |
349 | |
|
350 | |
try { |
351 | 0 | addObjectXML(doc, user, element, "user"); |
352 | |
|
353 | 0 | Node node = (Node) element; |
354 | 0 | if (node == null) { |
355 | 0 | node = doc; |
356 | |
} |
357 | |
|
358 | 0 | Element root = doc.createElement("actionItem"); |
359 | |
|
360 | |
try { |
361 | 0 | CustomEmailAttribute customEmailAttribute = getCustomEmailAttribute(user, actionItem); |
362 | 0 | if (customEmailAttribute != null) { |
363 | 0 | String customBody = customEmailAttribute.getCustomEmailBody(); |
364 | 0 | if (!Utilities.isEmpty(customBody)) { |
365 | 0 | Element bodyElement = doc.createElement("customBody"); |
366 | 0 | bodyElement.appendChild(doc.createTextNode(customBody)); |
367 | 0 | root.appendChild(bodyElement); |
368 | |
} |
369 | 0 | String customEmailSubject = customEmailAttribute.getCustomEmailSubject(); |
370 | 0 | if (!Utilities.isEmpty(customEmailSubject)) { |
371 | 0 | Element subjectElement = doc.createElement("customSubject"); |
372 | 0 | subjectElement.appendChild(doc.createTextNode(customEmailSubject)); |
373 | 0 | root.appendChild(subjectElement); |
374 | |
} |
375 | |
} |
376 | 0 | } catch (Exception e) { |
377 | 0 | LOG.error("Error when checking for custom email body and subject.", e); |
378 | 0 | } |
379 | 0 | Person person = actionItem.getPerson(); |
380 | 0 | DocumentRouteHeaderValue header = actionItem.getRouteHeader(); |
381 | |
|
382 | 0 | addObjectXML(doc, actionItem, root, "actionItem"); |
383 | 0 | addObjectXML(doc, person, root, "actionItemPerson"); |
384 | 0 | addTextElement(doc, root, "actionItemPrincipalId", person.getPrincipalId()); |
385 | 0 | addTextElement(doc, root, "actionItemPrincipalName", person.getPrincipalName()); |
386 | 0 | addDocumentHeaderXML(doc, header, root, "doc"); |
387 | 0 | addObjectXML(doc, header.getInitiatorPrincipal(), root, "docInitiator"); |
388 | 0 | addTextElement(doc, root, "docInitiatorDisplayName", header.getInitiatorDisplayName()); |
389 | 0 | addObjectXML(doc, header.getDocumentType(), root, "documentType"); |
390 | |
|
391 | 0 | node.appendChild(root); |
392 | 0 | } catch (Exception e) { |
393 | 0 | String message = "Error generating immediate reminder XML for action item: " + actionItem; |
394 | 0 | LOG.error(message, e); |
395 | 0 | throw new WorkflowRuntimeException(e); |
396 | 0 | } |
397 | 0 | LOG.info("Leaving generation of immeidate email reminder..."); |
398 | |
|
399 | |
|
400 | |
|
401 | 0 | return generateEmailContent(styleSheet, doc); |
402 | |
} |
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
protected void addDocumentHeaderXML(Document document, DocumentRouteHeaderValue documentHeader, Node node, String elementName) throws Exception { |
412 | 0 | Element element = XmlHelper.propertiesToXml(document, documentHeader, elementName); |
413 | |
|
414 | 0 | Element docContentElement = (Element)element.getElementsByTagName("docContent").item(0); |
415 | 0 | String documentContent = docContentElement.getTextContent(); |
416 | |
|
417 | 0 | if (!StringUtils.isBlank(documentContent) && documentContent.startsWith("<")) { |
418 | 0 | Document documentContentXML = XmlHelper.readXml(documentContent); |
419 | 0 | Element documentContentElement = documentContentXML.getDocumentElement(); |
420 | 0 | documentContentElement = (Element)document.importNode(documentContentElement, true); |
421 | |
|
422 | |
|
423 | 0 | docContentElement.removeChild(docContentElement.getFirstChild()); |
424 | |
|
425 | |
|
426 | 0 | docContentElement.appendChild(documentContentElement); |
427 | 0 | } else { |
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | 0 | docContentElement.removeChild(docContentElement.getFirstChild()); |
434 | |
} |
435 | |
|
436 | 0 | if (LOG.isDebugEnabled()) { |
437 | 0 | LOG.debug(XmlHelper.jotNode(element)); |
438 | |
} |
439 | |
|
440 | 0 | node.appendChild(element); |
441 | 0 | } |
442 | |
|
443 | |
public EmailContent generateWeeklyReminder(Person user, Collection<ActionItem> actionItems) { |
444 | 0 | return generateReminderForActionItems(user, actionItems, "weeklyReminder", globalEmailStyleSheet); |
445 | |
} |
446 | |
|
447 | |
public EmailContent generateDailyReminder(Person user, Collection<ActionItem> actionItems) { |
448 | 0 | return generateReminderForActionItems(user, actionItems, "dailyReminder", globalEmailStyleSheet); |
449 | |
} |
450 | |
|
451 | |
public EmailContent generateFeedback(FeedbackForm form) { |
452 | 0 | DocumentBuilder db = getDocumentBuilder(true); |
453 | 0 | Document doc = db.newDocument(); |
454 | 0 | String styleSheet = globalEmailStyleSheet; |
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | 0 | LOG.info("form: " + form.getRouteHeaderId()); |
470 | |
try { |
471 | 0 | addObjectXML(doc, form, null, "feedback"); |
472 | 0 | } catch (Exception e) { |
473 | 0 | String message = "Error generating XML for feedback form: " + form; |
474 | 0 | LOG.error(message, e); |
475 | 0 | throw new WorkflowRuntimeException(message, e); |
476 | 0 | } |
477 | 0 | setStandardAttributes(doc.getDocumentElement()); |
478 | |
|
479 | 0 | return generateEmailContent(styleSheet, doc); |
480 | |
} |
481 | |
} |