View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.kuali.rice.kew.mail.service.impl;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.log4j.Logger;
22  import org.kuali.rice.core.api.style.StyleService;
23  import org.kuali.rice.core.mail.EmailContent;
24  import org.kuali.rice.core.util.RiceConstants;
25  import org.kuali.rice.core.util.xml.XmlHelper;
26  import org.kuali.rice.core.util.xml.XmlJotter;
27  import org.kuali.rice.kew.actionitem.ActionItem;
28  import org.kuali.rice.kew.api.WorkflowRuntimeException;
29  import org.kuali.rice.kew.doctype.bo.DocumentType;
30  import org.kuali.rice.kew.feedback.web.FeedbackForm;
31  import org.kuali.rice.kew.mail.CustomEmailAttribute;
32  import org.kuali.rice.kew.mail.EmailStyleHelper;
33  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
34  import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
35  import org.kuali.rice.kew.service.KEWServiceLocator;
36  import org.kuali.rice.kew.user.UserUtils;
37  import org.kuali.rice.kew.util.KEWConstants;
38  import org.kuali.rice.kim.api.identity.principal.Principal;
39  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
40  import org.kuali.rice.kim.bo.Person;
41  import org.kuali.rice.krad.util.GlobalVariables;
42  import org.springframework.core.io.DefaultResourceLoader;
43  import org.w3c.dom.Document;
44  import org.w3c.dom.Element;
45  import org.w3c.dom.Node;
46  
47  import javax.xml.parsers.DocumentBuilder;
48  import javax.xml.parsers.DocumentBuilderFactory;
49  import javax.xml.parsers.ParserConfigurationException;
50  import javax.xml.transform.Templates;
51  import javax.xml.transform.TransformerConfigurationException;
52  import javax.xml.transform.TransformerException;
53  import javax.xml.transform.TransformerFactory;
54  import javax.xml.transform.dom.DOMSource;
55  import javax.xml.transform.stream.StreamResult;
56  import javax.xml.transform.stream.StreamSource;
57  import java.io.StringWriter;
58  import java.sql.Timestamp;
59  import java.util.Collection;
60  import java.util.Map;
61  
62  
63  
64  /**
65   * EmailContentService that serves EmailContent customizable via XSLT style sheets
66   * The global email style name is: kew.email.style
67   * If this style is not found, the resource 'defaultEmailStyle.xsl' will be retrieved
68   * relative to this class.
69   * @author Kuali Rice Team (rice.collab@kuali.org)
70   */
71  public class StyleableEmailContentServiceImpl extends BaseEmailContentServiceImpl {
72      private static final Logger LOG = Logger.getLogger(StyleableEmailContentServiceImpl.class);
73  
74      protected final String DEFAULT_EMAIL_STYLESHEET_RESOURCE_LOC = "defaultEmailStyle.xsl";
75  
76      protected StyleService styleService;
77      protected EmailStyleHelper styleHelper = new EmailStyleHelper();
78      protected String globalEmailStyleSheet = KEWConstants.EMAIL_STYLESHEET_NAME;
79  
80      protected RouteHeaderService routeHeaderService;
81  
82      public void setStyleService(StyleService styleService) {
83          this.styleService = styleService;
84      }
85  
86      public void setGlobalEmailStyleSheet(String globalEmailStyleSheet) {
87          this.globalEmailStyleSheet = globalEmailStyleSheet;
88      }
89  
90      protected static DocumentBuilder getDocumentBuilder(boolean coalesce) {
91          try {
92              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
93              dbf.setCoalescing(coalesce);
94              return dbf.newDocumentBuilder();
95          } catch (ParserConfigurationException e) {
96              String message = "Error constructing document builder";
97              LOG.error(message, e);
98              throw new WorkflowRuntimeException(message, e);
99          }
100     }
101 
102     protected static void addObjectXML(Document doc, Object o, Node node, String name) throws Exception {
103         Element element = XmlHelper.propertiesToXml(doc, o, name);
104 
105         if (LOG.isDebugEnabled()) {
106             LOG.debug(XmlJotter.jotNode(element));
107         }
108 
109         if (node == null) {
110             node = doc;
111         }
112 
113         node.appendChild(element);
114     }
115 
116     protected static void addTextElement(Document doc, Element baseElement, String elementName, Object elementData) {
117         Element element = doc.createElement(elementName);
118         String dataValue = "";
119         if (elementData != null) {
120         	dataValue = elementData.toString();
121         }
122         element.appendChild(doc.createTextNode(dataValue));
123         baseElement.appendChild(element);
124     }
125 
126     protected static void addCDataElement(Document doc, Element baseElement, String elementName, Object elementData) {
127         Element element = doc.createElement(elementName);
128         String dataValue = "";
129         if (elementData != null) {
130             dataValue = elementData.toString();
131         }
132         element.appendChild(doc.createCDATASection(dataValue));
133         baseElement.appendChild(element);
134     }
135 
136     protected static void addTimestampElement(Document doc, Element baseElement, String elementName, Timestamp elementData) {
137         addTextElement(doc, baseElement, elementName, RiceConstants.getDefaultDateFormat().format(elementData));
138     }
139 
140     protected static void addDelegatorElement(Document doc, Element baseElement, ActionItem actionItem) {
141         Element delegatorElement = doc.createElement("delegator");
142         if ( (actionItem.getDelegatorWorkflowId() != null) && (actionItem.getDelegatorWorkflowId() != null) ) {
143             // add empty delegator element
144             baseElement.appendChild(delegatorElement);
145             return;
146         }
147         String delegatorType = "";
148         String delegatorId = "";
149         String delegatorDisplayValue = "";
150         if (actionItem.getDelegatorWorkflowId() != null) {
151             delegatorType = "user";
152             delegatorId = actionItem.getDelegatorWorkflowId();
153             Principal delegator = KimApiServiceLocator.getIdentityService().getPrincipal(delegatorId);
154             
155             if (delegator == null) {
156             	LOG.error("Cannot find user for id " + delegatorId);
157             	delegatorDisplayValue = "USER NOT FOUND";
158             } else {
159             	delegatorDisplayValue = UserUtils.getTransposedName(GlobalVariables.getUserSession(), delegator);
160             }
161         } else if (actionItem.getDelegatorWorkflowId() != null) {
162             delegatorType = "workgroup";
163             delegatorId = actionItem.getDelegatorGroupId().toString();
164             delegatorDisplayValue = KimApiServiceLocator.getGroupService().getGroup(actionItem.getDelegatorGroupId()).getName();
165         }
166         delegatorElement.setAttribute("type", delegatorType);
167         // add the id element
168         Element idElement = doc.createElement("id");
169         idElement.appendChild(doc.createTextNode(delegatorId));
170         delegatorElement.appendChild(idElement);
171         // add the display value element
172         Element displayValElement = doc.createElement("displayValue");
173         displayValElement.appendChild(doc.createTextNode(delegatorDisplayValue));
174         delegatorElement.appendChild(displayValElement);
175         baseElement.appendChild(delegatorElement);
176     }
177 
178     protected static void addWorkgroupRequestElement(Document doc, Element baseElement, ActionItem actionItem) {
179         Element workgroupElement = doc.createElement("workgroupRequest");
180         if (actionItem.isWorkgroupItem()) {
181             // add the id element
182             Element idElement = doc.createElement("id");
183             idElement.appendChild(doc.createTextNode(actionItem.getGroupId()));
184             workgroupElement.appendChild(idElement);
185             // add the display value element
186             Element displayValElement = doc.createElement("displayValue");
187             displayValElement.appendChild(doc.createTextNode(actionItem.getGroupId()));
188             workgroupElement.appendChild(displayValElement);
189         }
190         baseElement.appendChild(workgroupElement);
191     }
192 
193     /**
194      * This method is used to add the given {@link ActionItem} to the given {@link org.w3c.dom.Document} in a summarized
195      * form for use in weekly or daily type reminder e-mails.
196      *
197      * @param doc - Document to have the ActionItem added to
198      * @param actionItem - the action item being added
199      * @param user - the current user
200      * @param node - the node object to add the actionItem XML to (defaults to the doc variable if null is passed in)
201      * @throws Exception
202      */
203     protected void addSummarizedActionItem(Document doc, ActionItem actionItem, Person user, Node node, DocumentRouteHeaderValue routeHeader) throws Exception {
204         if (node == null) {
205             node = doc;
206         }
207 
208         Element root = doc.createElement("summarizedActionItem");
209 
210         // add in all items from action list as preliminary default dataset
211         addTextElement(doc, root, "documentId", actionItem.getDocumentId());
212         addTextElement(doc, root, "docName", actionItem.getDocName());
213         addCDataElement(doc, root, "docLabel", actionItem.getDocLabel());
214         addCDataElement(doc, root, "docTitle", actionItem.getDocTitle());
215         //DocumentRouteHeaderValue routeHeader = getRouteHeader(actionItem);
216         addTextElement(doc, root, "docRouteStatus", routeHeader.getDocRouteStatus());
217         addCDataElement(doc, root, "routeStatusLabel", routeHeader.getRouteStatusLabel());
218         addTextElement(doc, root, "actionRequestCd", actionItem.getActionRequestCd());
219         addTextElement(doc, root, "actionRequestLabel", actionItem.getActionRequestLabel());
220         addDelegatorElement(doc, root, actionItem);
221         addTimestampElement(doc, root, "createDate", routeHeader.getCreateDate());
222         addWorkgroupRequestElement(doc, root, actionItem);
223         addTimestampElement(doc, root, "dateAssigned", actionItem.getDateAssigned());
224 
225         node.appendChild(root);
226     }
227 
228     public DocumentRouteHeaderValue getRouteHeader(ActionItem actionItem) {
229     	if (routeHeaderService == null) {
230     		routeHeaderService = KEWServiceLocator.getRouteHeaderService();
231     	}
232         return routeHeaderService.getRouteHeader(actionItem.getDocumentId());
233     }
234 
235     protected Map<String,DocumentRouteHeaderValue> getRouteHeaders(Collection<ActionItem> actionItems) {
236     	if (routeHeaderService == null) {
237     		routeHeaderService = KEWServiceLocator.getRouteHeaderService();
238     	}
239     	return routeHeaderService.getRouteHeadersForActionItems(actionItems);
240     }
241     
242     protected static String transform(Templates style, Document doc) {
243         StringWriter writer = new StringWriter();
244         StreamResult result = new StreamResult(writer);
245 
246         try {
247             style.newTransformer().transform(new DOMSource(doc), result);
248             return writer.toString();
249         } catch (TransformerException te) {
250             String message = "Error transforming DOM";
251             LOG.error(message, te);
252             throw new WorkflowRuntimeException(message, te);
253         }
254     }
255 
256     /**
257      * This method retrieves the style from the system using the given name. If none is found the default style xsl file
258      * defined by {@link #DEFAULT_EMAIL_STYLESHEET_RESOURCE_LOC} is used.
259      *
260      * @param styleName
261      * @return a valid {@link javax.xml.transform.Templates} using either the given styleName or the default xsl style file
262      */
263     protected Templates getStyle(String styleName) {
264         Templates style = null;
265         try {
266             style = styleService.getStyleAsTranslet(styleName);
267         } catch (TransformerConfigurationException tce) {
268             String message = "Error obtaining style '" + styleName + "', using default";
269             LOG.error(message, tce);
270             // throw new WorkflowRuntimeException("Error obtaining style '" + styleName + "'", tce);
271         }
272 
273         if (style == null) {
274             LOG.warn("Could not find specified style, " + styleName + ", using default");
275             try {
276 
277                 style = TransformerFactory.newInstance().newTemplates(new StreamSource(new DefaultResourceLoader().getResource("classpath:org/kuali/rice/kew/mail/" + DEFAULT_EMAIL_STYLESHEET_RESOURCE_LOC).getInputStream()));
278             } catch (Exception tce) {
279                 String message = "Error obtaining default style from resource: " + DEFAULT_EMAIL_STYLESHEET_RESOURCE_LOC;
280                 LOG.error(message, tce);
281                 throw new WorkflowRuntimeException("Error obtaining style '" + styleName + "'", tce);
282             }
283         }
284         return style;
285     }
286 
287     protected EmailContent generateEmailContent(String styleName, Document doc) {
288         Templates style = getStyle(styleName);
289         return styleHelper.generateEmailContent(style, doc);
290     }
291 
292     protected EmailContent generateReminderForActionItems(Person user, Collection<ActionItem> actionItems, String name, String style) {
293         DocumentBuilder db = getDocumentBuilder(false);
294         Document doc = db.newDocument();
295         Element element = doc.createElement(name);
296         Map<String,DocumentRouteHeaderValue> routeHeaders = getRouteHeaders(actionItems);
297         
298         setStandardAttributes(element);
299         doc.appendChild(element);
300 
301         try {
302             addObjectXML(doc, user, element, "user");
303             for (ActionItem actionItem: actionItems) {
304                 try {
305                     addSummarizedActionItem(doc, actionItem, user, element, routeHeaders.get(actionItem.getDocumentId()));
306                 } catch (Exception e) {
307                     String message = "Error generating XML for action item: " + actionItem;
308                     LOG.error(message, e);
309                     throw new WorkflowRuntimeException(e);
310                 }
311             }
312 
313         } catch (Exception e) {
314             String message = "Error generating XML for action items: " + actionItems;
315             LOG.error(message, e);
316             throw new WorkflowRuntimeException(e);
317         }
318 
319         return generateEmailContent(style, doc);
320     }
321 
322     protected void setStandardAttributes(Element e) {
323         e.setAttribute("env", getDeploymentEnvironment());
324         e.setAttribute("applicationEmailAddress", getApplicationEmailAddress());
325         e.setAttribute("actionListUrl", getActionListUrl());
326         e.setAttribute("preferencesUrl", getPreferencesUrl());
327     }
328 
329     /**
330      * This method generates an {@link EmailContent} object using the given parameters.  Part of this operation includes
331      * serializing the given {@link ActionItem} to XML. The following objects and methods are included in the serialization:
332      *
333      * <ul>
334      * <li>{@link Person}</li>
335      * <li>{@link Person#getPrincipalName()}</li>
336      * <li>{@link DocumentRouteHeaderValue}</li>
337      * <li>{@link DocumentRouteHeaderValue#getInitiatorUser()}</li>
338      * <li>{@link DocumentRouteHeaderValue#getDocumentType()}</li>
339      * <li>{@link Person}</li>
340      * </ul>
341      *
342      * @param user - the current user
343      * @param actionItem - the action item being added
344      * @param documentType - the document type that the custom email style sheet will come from
345      * @param node - the node object to add the actionItem XML to (defaults to the doc variable if null is passed in)
346      * @throws Exception
347      */
348     @Override
349 	public EmailContent generateImmediateReminder(Person user, ActionItem actionItem, DocumentType documentType) {
350     	
351     	LOG.info("Starting generation of immediate email reminder...");
352     	LOG.info("Action Id: " + actionItem.getActionItemId() + 
353     			 ";  ActionRequestId: " + actionItem.getActionRequestId() + 
354     			 ";  Action Item Principal Name: " + actionItem.getPerson().getPrincipalName());
355     	LOG.info("User Principal Name: " + user.getPrincipalName());
356         // change style name based on documentType when configurable email style on document is implemented...
357         String styleSheet = documentType.getCustomEmailStylesheet();
358         LOG.debug(documentType.getName() + " style: " + styleSheet);
359         if (styleSheet == null) {
360             styleSheet = globalEmailStyleSheet;
361         }
362 
363         LOG.info("generateImmediateReminder using style sheet: "+ styleSheet + " for Document Type " + documentType.getName());
364 //        return generateReminderForActionItems(user, actionItems, "immediateReminder", styleSheet);
365         DocumentBuilder db = getDocumentBuilder(false);
366         Document doc = db.newDocument();
367         Element element = doc.createElement("immediateReminder");
368         setStandardAttributes(element);
369         doc.appendChild(element);
370 
371         try {
372             addObjectXML(doc, user, element, "user");
373 //            addActionItem(doc, actionItem, user, node);
374             Node node = element;
375             if (node == null) {
376                 node = doc;
377             }
378 
379             Element root = doc.createElement("actionItem");
380             // append the custom body and subject if they exist
381             try {
382                 CustomEmailAttribute customEmailAttribute = getCustomEmailAttribute(user, actionItem);
383                 if (customEmailAttribute != null) {
384                     String customBody = customEmailAttribute.getCustomEmailBody();
385                     if (!org.apache.commons.lang.StringUtils.isEmpty(customBody)) {
386                         Element bodyElement = doc.createElement("customBody");
387                         bodyElement.appendChild(doc.createTextNode(customBody));
388                         root.appendChild(bodyElement);
389                     }
390                     String customEmailSubject = customEmailAttribute.getCustomEmailSubject();
391                     if (!org.apache.commons.lang.StringUtils.isEmpty(customEmailSubject)) {
392                         Element subjectElement = doc.createElement("customSubject");
393                         subjectElement.appendChild(doc.createTextNode(customEmailSubject));
394                         root.appendChild(subjectElement);
395                     }
396                 }
397             } catch (Exception e) {
398                 LOG.error("Error when checking for custom email body and subject.", e);
399             }
400             Person person = actionItem.getPerson();
401             DocumentRouteHeaderValue header = getRouteHeader(actionItem);
402             // keep adding stuff until we have all the xml we need to formulate the message :/
403             addObjectXML(doc, actionItem, root, "actionItem");
404             addObjectXML(doc, person, root, "actionItemPerson");
405             addTextElement(doc, root, "actionItemPrincipalId", person.getPrincipalId());
406             addTextElement(doc, root, "actionItemPrincipalName", person.getPrincipalName());
407             addDocumentHeaderXML(doc, header, root, "doc");
408             addObjectXML(doc, header.getInitiatorPrincipal(), root, "docInitiator");
409             addTextElement(doc, root, "docInitiatorDisplayName", header.getInitiatorDisplayName());
410             addObjectXML(doc, header.getDocumentType(), root, "documentType");
411 
412             node.appendChild(root);
413         } catch (Exception e) {
414             String message = "Error generating immediate reminder XML for action item: " + actionItem;
415             LOG.error(message, e);
416             throw new WorkflowRuntimeException(e);
417         }
418         LOG.info("Leaving generation of immeidate email reminder...");
419     	/**
420     	 * End IU customization
421     	 */
422         return generateEmailContent(styleSheet, doc);
423     }
424     
425     /**
426      * This method handles converting the DocumentRouteHeaderValue into an XML representation.  The reason we can't just use
427      * propertiesToXml like we have elsewhere is because the doc header has a String attached to it that has the XML document
428      * content in it.  The default serialization of this will serialize this as a String so we will end up with escaped XML
429      * in our output which we won't be able to process with the email stylesheet.  So we need to read the xml content from
430      * the document and parse it into a DOM object so it can be appended to our output.
431      */
432     protected void addDocumentHeaderXML(Document document, DocumentRouteHeaderValue documentHeader, Node node, String elementName) throws Exception {
433     	Element element = XmlHelper.propertiesToXml(document, documentHeader, elementName);
434     	// now we need to "fix" the xml document content because it's going to be in there as escaped XML
435     	Element docContentElement = (Element)element.getElementsByTagName("docContent").item(0);
436     	String documentContent = docContentElement.getTextContent();
437     	
438     	if (!StringUtils.isBlank(documentContent) && documentContent.startsWith("<")) {
439     		Document documentContentXML = XmlHelper.readXml(documentContent);
440     		Element documentContentElement = documentContentXML.getDocumentElement();
441     		documentContentElement = (Element)document.importNode(documentContentElement, true);
442     	
443     		// remove the old, bad text content
444     		docContentElement.removeChild(docContentElement.getFirstChild());
445     	
446     		// replace with actual XML
447     		docContentElement.appendChild(documentContentElement);
448     	} else {
449     		// in this case it means that the XML is encrypted, unfortunately, we have no way to decrypt it since
450     		// the key is stored in the client application.  We will just include the doc content since none of our
451     		// current IU clients will be using this feature right away
452 
453     		// remove the old, bad text content
454     		docContentElement.removeChild(docContentElement.getFirstChild());
455     	}
456     	
457     	if (LOG.isDebugEnabled()) {
458             LOG.debug(XmlJotter.jotNode(element));
459         }
460 
461         node.appendChild(element);
462     }
463 
464     @Override
465 	public EmailContent generateWeeklyReminder(Person user, Collection<ActionItem> actionItems) {
466         return generateReminderForActionItems(user, actionItems, "weeklyReminder", globalEmailStyleSheet);
467     }
468 
469     @Override
470 	public EmailContent generateDailyReminder(Person user, Collection<ActionItem> actionItems) {
471         return generateReminderForActionItems(user, actionItems, "dailyReminder", globalEmailStyleSheet);
472     }
473 
474     @Override
475 	public EmailContent generateFeedback(FeedbackForm form) {
476         DocumentBuilder db = getDocumentBuilder(true);
477         Document doc = db.newDocument();
478         String styleSheet = globalEmailStyleSheet;
479 
480         // if the doc type is specified, see if that doc has a custom email stylesheet and use it
481         // NOTE: do we need to do this for feedback? presumably feedback will be going back to admins
482         /*String docTypeName = form.getDocumentType();
483         if (!StringUtils.isBlank(docTypeName)) {
484             DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(docTypeName);
485             if (docType == null) {
486                 LOG.error("User specified document type '" + docTypeName + "' in feedback form, but the document type was not found in the system");
487             } else {
488                 if (docType.getCustomEmailStylesheet() != null) {
489                     styleSheet = docType.getCustomEmailStylesheet();
490                 }
491             }
492         }*/
493         LOG.info("form: " + form.getDocumentId());
494         try {
495             addObjectXML(doc, form, null, "feedback");
496         } catch (Exception e) {
497             String message = "Error generating XML for feedback form: " + form;
498             LOG.error(message, e);
499             throw new WorkflowRuntimeException(message, e);
500         }
501         setStandardAttributes(doc.getDocumentElement());
502 
503         return generateEmailContent(styleSheet, doc);
504     }
505 }