[Generator-rt-devel] generator_runtime/src/java/generator/runtime/automailer VelocityMailRenderCon
Brought to you by:
rickknowles
|
From: Rick K. <ric...@us...> - 2010-03-30 02:46:44
|
Update of /cvsroot/generator-rt/generator_runtime/src/java/generator/runtime/automailer In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv15375/src/java/generator/runtime/automailer Added Files: VelocityMailRenderController.java Log Message: velocity mail transformer and the translate tag easy parameters method --- NEW FILE: VelocityMailRenderController.java --- /* * Keystone Development Framework * Copyright (C) 2004-2009 Rick Knowles * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License * Version 2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License Version 2 for more details. * * You should have received a copy of the GNU Library General Public License * Version 2 along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package generator.runtime.automailer; import generator.runtime.controller.Controller; import generator.runtime.exception.ApplicationException; import generator.runtime.log.Log; import generator.runtime.log.LogManager; import generator.runtime.utils.CachingTextProcessor; import generator.runtime.utils.ParamUtils; import generator.runtime.velocity.VelocityContextInitializer; import generator.runtime.xml.XMLUtils; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * Controller to render mail messages using the velocity templates in our /WEB-INF/velocity/mail/* * folder. */ public class VelocityMailRenderController extends Controller { private final Log log = LogManager.getLog(MailRenderController.class); public String execute(Map attributes) { String mailTemplateName = getFlowParameter("mailTemplate", null); String dateFormat = getFlowParameter("dateFormat", "yyyy-MM-dd HH:mm:ss"); if (mailTemplateName == null) { throw new ApplicationException("No mail template specified"); } CachingTextProcessor textProcessor = getTextProcessor(); // Build the input context Map velocityAtts = new HashMap(attributes); velocityAtts.put("properties", getProperties()); velocityAtts.put("dateFormatter", new SimpleDateFormat(dateFormat)); velocityAtts.put("now", new Date()); log.debug("Rendering email contents with velocity"); try { StringWriter xml = new StringWriter(); textProcessor.processTemplate(xml, velocityAtts, mailTemplateName, null, null, null); log.debug("Rendered mail xml: " + xml); parseMessageXML(xml.toString(), attributes); return "OK"; } catch (IOException err) { throw new ApplicationException("Error processing mail template", err); } } protected CachingTextProcessor getTextProcessor() { String textProcessorName = getFlowParameter("textProcessor", VelocityContextInitializer.ATTR_VELOCITY_ENGINE); return (CachingTextProcessor) getServerSideApplicationState().getAttribute(textProcessorName); } protected void parseMessageXML(String xml, Map attributes) { String toAddressFieldParam = getFlowParameter("toAddressFieldParam", "toAddress"); String ccAddressFieldParam = getFlowParameter("ccAddressFieldParam", "ccAddress"); String bccAddressFieldParam = getFlowParameter("bccAddressFieldParam", "bccAddress"); String replyToAddressFieldParam = getFlowParameter("replyToAddressFieldParam", "replyToAddress"); String smtpMessageIdParam = getFlowParameter("smtpMessageIdParam", "smtpMessageId"); String inReplyToParam = getFlowParameter("inReplyToParam", "inReplyTo"); String subjectParam = getFlowParameter("subjectParam", "subject"); String bodyTextParam = getFlowParameter("bodyTextParam", "bodyText"); String bodyHtmlParam = getFlowParameter("bodyHtmlParam", "bodyHtml"); String senderParam = getFlowParameter("senderParam", "sender"); Document doc = XMLUtils.parseStreamToXML(new StringReader(xml)); Node root = doc.getDocumentElement(); NodeList children = root.getChildNodes(); for (int n = 0; n < children.getLength(); n++) { Node node = children.item(n); if (node.getNodeType() != Node.ELEMENT_NODE) { continue; } else if (node.getNodeName().equals("subject")) { attributes.put(subjectParam, XMLUtils.extractStringFromElement(node)); } else if (node.getNodeName().equals("bodyText")) { attributes.put(bodyTextParam, XMLUtils.extractStringFromElement(node)); } else if (node.getNodeName().equals("bodyHtml")) { attributes.put(bodyHtmlParam, XMLUtils.extractStringFromElement(node)); } else if (node.getNodeName().equals("address")) { NodeList grandchildren = node.getChildNodes(); for (int k = 0; k < grandchildren.getLength(); k++) { Node grandchild = grandchildren.item(k); if (grandchild.getNodeType() != Node.ELEMENT_NODE) { continue; } else if (grandchild.getNodeName().equals("to")) { String name = XMLUtils.getAttributeByName(grandchild, "name"); String email = XMLUtils.getAttributeByName(grandchild, "email"); addAddress(attributes, toAddressFieldParam, formatAddress(name, email)); } else if (grandchild.getNodeName().equals("cc")) { String name = XMLUtils.getAttributeByName(grandchild, "name"); String email = XMLUtils.getAttributeByName(grandchild, "email"); addAddress(attributes, ccAddressFieldParam, formatAddress(name, email)); } else if (grandchild.getNodeName().equals("bcc")) { String name = XMLUtils.getAttributeByName(grandchild, "name"); String email = XMLUtils.getAttributeByName(grandchild, "email"); addAddress(attributes, bccAddressFieldParam, formatAddress(name, email)); } else if (grandchild.getNodeName().equals("sender")) { String name = XMLUtils.getAttributeByName(grandchild, "name"); String email = XMLUtils.getAttributeByName(grandchild, "email"); attributes.put(senderParam, formatAddress(name, email)); } else if (grandchild.getNodeName().equals("replyTo")) { String name = XMLUtils.getAttributeByName(grandchild, "name"); String email = XMLUtils.getAttributeByName(grandchild, "email"); attributes.put(replyToAddressFieldParam, formatAddress(name, email)); } } } else if (node.getNodeName().equals("smtp")) { NodeList grandchildren = node.getChildNodes(); for (int k = 0; k < grandchildren.getLength(); k++) { Node grandchild = grandchildren.item(n); if (grandchild.getNodeType() != Node.ELEMENT_NODE) { continue; } else if (node.getNodeName().equals("messageId")) { attributes.put(smtpMessageIdParam, XMLUtils.extractStringFromElement(node)); } else if (node.getNodeName().equals("inReplyToId")) { attributes.put(inReplyToParam, XMLUtils.extractStringFromElement(node)); } } } } } public static String formatAddress(String name, String email) { if ((name != null) && !name.equals("")) { return name + "<" + email + ">"; } else { return email; } } public static void addAddress(Map attributes, String key, String newAddress) { String address = ParamUtils.getParameter(attributes, key, ""); if (address.equals("")) { attributes.put(key, newAddress); } else { attributes.put(key, address + "," + newAddress); } } } |