Coverage Report - org.kuali.rice.kew.mail.EmailNode
 
Classes in this File Line Coverage Branch Coverage Complexity
EmailNode
0%
0/77
0%
0/32
5.333
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.kuali.rice.kew.mail;
 18  
 
 19  
 import com.thoughtworks.xstream.XStream;
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.kuali.rice.core.api.CoreApiServiceLocator;
 22  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 23  
 import org.kuali.rice.core.api.util.xml.XmlHelper;
 24  
 import org.kuali.rice.core.api.util.xml.XmlJotter;
 25  
 import org.kuali.rice.core.mail.EmailBody;
 26  
 import org.kuali.rice.core.mail.EmailContent;
 27  
 import org.kuali.rice.core.mail.EmailFrom;
 28  
 import org.kuali.rice.core.mail.EmailSubject;
 29  
 import org.kuali.rice.core.mail.EmailTo;
 30  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 31  
 import org.kuali.rice.kew.dto.DTOConverter;
 32  
 import org.kuali.rice.kew.dto.RouteHeaderDTO;
 33  
 import org.kuali.rice.kew.dto.RouteNodeInstanceDTO;
 34  
 import org.kuali.rice.kew.engine.RouteContext;
 35  
 import org.kuali.rice.kew.engine.RouteHelper;
 36  
 import org.kuali.rice.kew.engine.node.SimpleNode;
 37  
 import org.kuali.rice.kew.engine.node.SimpleResult;
 38  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 39  
 import org.kuali.rice.kew.util.KEWConstants;
 40  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 41  
 import org.kuali.rice.kim.bo.Person;
 42  
 import org.w3c.dom.Document;
 43  
 import org.w3c.dom.Element;
 44  
 import org.w3c.dom.NodeList;
 45  
 import org.xml.sax.InputSource;
 46  
 
 47  
 import javax.xml.parsers.DocumentBuilder;
 48  
 import javax.xml.parsers.DocumentBuilderFactory;
 49  
 import javax.xml.transform.Templates;
 50  
 import javax.xml.transform.TransformerConfigurationException;
 51  
 import java.io.StringReader;
 52  
 
 53  
 
 54  
 /**
 55  
  * A node which will send emails using the configured stylesheet to generate the email content.
 56  
  *
 57  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 58  
  */
 59  0
 public class EmailNode implements SimpleNode {
 60  
 
 61  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EmailNode.class);
 62  
 
 63  0
     private EmailStyleHelper emailStyleHelper = new EmailStyleHelper();
 64  
     private String styleName;
 65  
     private String from;
 66  
     private String to;
 67  
     
 68  
     public SimpleResult process(RouteContext context, RouteHelper helper) throws Exception {
 69  0
             if (context.isSimulation()) {
 70  0
             if (!context.getActivationContext().isActivateRequests()) {
 71  0
                     return new SimpleResult(true);
 72  
             }
 73  
         } 
 74  0
         loadConfiguration(context);
 75  0
         Document document = generateXmlInput(context);
 76  0
         if (LOG.isDebugEnabled()) {
 77  0
             LOG.debug("XML input for email tranformation:\n" + XmlJotter.jotNode(document));
 78  
         }
 79  0
         Templates style = loadStyleSheet(styleName);
 80  0
         EmailContent emailContent = emailStyleHelper.generateEmailContent(style, document);
 81  0
         if (!StringUtils.isBlank(to)) {
 82  0
                 KEWServiceLocator.getMailer().sendEmail(new EmailFrom(from), new EmailTo(to), new EmailSubject(emailContent.getSubject()), new EmailBody(emailContent.getBody()), emailContent.isHtml());
 83  
         }
 84  0
         return new SimpleResult(true);
 85  
     }
 86  
 
 87  
     protected Document generateXmlInput(RouteContext context) throws Exception {
 88  0
         DocumentBuilder db = getDocumentBuilder(true);
 89  0
         Document doc = db.newDocument();
 90  0
         Element emailNodeElem = doc.createElement("emailNode");
 91  0
         doc.appendChild(emailNodeElem);
 92  0
         String principalId = null;  // Added to the convertRouteHeader is not ambigious.
 93  0
         RouteHeaderDTO routeHeaderVO = DTOConverter.convertRouteHeader(context.getDocument(), principalId);
 94  0
         RouteNodeInstanceDTO routeNodeInstanceVO = DTOConverter.convertRouteNodeInstance(context.getNodeInstance());
 95  0
         Document documentContent = context.getDocumentContent().getDocument();
 96  0
         XStream xstream = new XStream();
 97  0
         Element docElem = XmlHelper.readXml(xstream.toXML(routeHeaderVO)).getDocumentElement();
 98  0
         Element nodeElem = XmlHelper.readXml(xstream.toXML(routeNodeInstanceVO)).getDocumentElement();
 99  0
         emailNodeElem.appendChild(doc.importNode(docElem, true));
 100  0
         emailNodeElem.appendChild(doc.importNode(nodeElem, true));
 101  0
         emailNodeElem.appendChild(doc.importNode(documentContent.getDocumentElement(), true));
 102  0
         Element dConElem = context.getDocumentContent().getApplicationContent();//Add document Content element for
 103  0
                   emailNodeElem.appendChild(doc.importNode(dConElem, true));//access by the stylesheet when creating the email
 104  0
         return doc;
 105  
     }
 106  
 
 107  
     protected DocumentBuilder getDocumentBuilder(boolean coalesce) throws Exception {
 108  0
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 109  0
         dbf.setCoalescing(coalesce);
 110  0
         return dbf.newDocumentBuilder();
 111  
     }
 112  
 
 113  
     protected Templates loadStyleSheet(String styleName) {
 114  
         try {
 115  0
             Templates style = CoreApiServiceLocator.getStyleService().getStyleAsTranslet(styleName);
 116  0
             if (style == null) {
 117  0
                 throw new WorkflowRuntimeException("Failed to locate stylesheet with name '" + styleName + "'");
 118  
             }
 119  0
             return style;
 120  0
         } catch (TransformerConfigurationException tce) {
 121  0
             throw new WorkflowRuntimeException("Failed to load stylesheet with name '" + styleName + "'");
 122  
         }
 123  
     }
 124  
 
 125  
     protected boolean isProduction() {
 126  0
         return ConfigContext.getCurrentContextConfig().getProperty(KEWConstants.PROD_DEPLOYMENT_CODE).equalsIgnoreCase(
 127  
                         ConfigContext.getCurrentContextConfig().getEnvironment());
 128  
     }
 129  
 
 130  
     protected void loadConfiguration(RouteContext context) throws Exception {
 131  0
         String contentFragment = context.getNodeInstance().getRouteNode().getContentFragment();
 132  0
         DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 133  0
         Document document = db.parse(new InputSource(new StringReader(contentFragment)));
 134  0
         if (!isProduction()) {
 135  0
             NodeList testAddresses = document.getElementsByTagName("testAddress");
 136  0
             if (testAddresses.getLength() >= 1) {
 137  0
                 this.to = testAddresses.item(0).getTextContent();
 138  
             }
 139  0
         } else {
 140  0
             NodeList toAddresses = document.getElementsByTagName("to");
 141  0
             if (toAddresses.getLength() != 1) {
 142  0
                 throw new WorkflowRuntimeException("Must have exactly one 'to' address");
 143  
             }
 144  0
             to = toAddresses.item(0).getTextContent();
 145  0
             if ("initiator".equalsIgnoreCase(to))
 146  
             {        
 147  0
                     Person person = KimApiServiceLocator.getPersonService().getPerson(context.getDocument().getInitiatorWorkflowId());
 148  0
                         to = (person == null ? "" : person.getEmailAddressUnmasked());
 149  
             }
 150  0
             if (StringUtils.isBlank(to)) {
 151  0
                     throw new WorkflowRuntimeException("Email Address is missing from user's profile.");
 152  
             }
 153  
         }
 154  
 
 155  0
         NodeList fromAddresses = document.getElementsByTagName("from");
 156  0
         if (fromAddresses.getLength() != 1) {
 157  0
             throw new WorkflowRuntimeException("Must have exactly one 'from' address");
 158  
         }
 159  0
         this.from = fromAddresses.item(0).getTextContent();
 160  
 
 161  0
         if ("initiator".equalsIgnoreCase(this.from)) {
 162  0
                 Person initiator = KEWServiceLocator.getIdentityHelperService().getPerson(context.getDocument().getInitiatorWorkflowId());
 163  
                 // contructs the email from so that it includes name as well as address
 164  
                 // for example: "Doe, John D" <john@doe.com>
 165  0
                   this.from = "\"" + initiator.getName() + "\" <";
 166  0
                   this.from += initiator.getEmailAddress() + ">";
 167  
         }
 168  0
         if (StringUtils.isBlank(this.from)) {
 169  0
                 throw new WorkflowRuntimeException("No email address could be found found for principal with id " + context.getDocument().getInitiatorWorkflowId());
 170  
         }
 171  
         
 172  0
         if (LOG.isInfoEnabled()) {
 173  0
                   LOG.info("Email From is set to:" + this.from);
 174  0
                   LOG.info("Email To is set to:" + this.to);
 175  
         }
 176  
         
 177  0
         NodeList styleNames = document.getElementsByTagName("style");
 178  0
         if (styleNames.getLength() != 1) {
 179  0
             throw new WorkflowRuntimeException("Must have exactly one 'style'");
 180  
         }
 181  0
         this.styleName = styleNames.item(0).getTextContent();
 182  0
     }
 183  
 
 184  
 
 185  
 
 186  
 
 187  
 }