Coverage Report - org.kuali.rice.kew.engine.node.var.SetVarNode
 
Classes in this File Line Coverage Branch Coverage Complexity
SetVarNode
0%
0/22
0%
0/6
5
 
 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  
 package org.kuali.rice.kew.engine.node.var;
 18  
 
 19  
 import java.io.StringReader;
 20  
 
 21  
 import javax.xml.parsers.DocumentBuilder;
 22  
 import javax.xml.parsers.DocumentBuilderFactory;
 23  
 
 24  
 import org.apache.log4j.Logger;
 25  
 import org.kuali.rice.kew.engine.RouteContext;
 26  
 import org.kuali.rice.kew.engine.RouteHelper;
 27  
 import org.kuali.rice.kew.engine.node.BranchState;
 28  
 import org.kuali.rice.kew.engine.node.PropertiesUtil;
 29  
 import org.kuali.rice.kew.engine.node.SimpleNode;
 30  
 import org.kuali.rice.kew.engine.node.SimpleResult;
 31  
 import org.kuali.rice.kew.exception.InvalidXmlException;
 32  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 33  
 import org.w3c.dom.Document;
 34  
 import org.w3c.dom.Element;
 35  
 import org.xml.sax.InputSource;
 36  
 
 37  
 
 38  
 /**
 39  
  * A simple node that allows setting of document variables.
 40  
  * The definition of SetVarNode takes the following config parameter elements:
 41  
  * <dl>
 42  
  *   <dt>name</dt>
 43  
  *   <dd>The name of the variable to set</dd>
 44  
  *   <dt>value</dt>
 45  
  *   <dd>The value to which to set the variable.  This value is parsed according to
 46  
  *       Property/PropertyScheme syntax</dd>
 47  
  * </dl>
 48  
  * The default PropertySchme is LiteralScheme, which will evaluate the value simply
 49  
  * as a literal (won't do anything but return it)
 50  
  * @see PropertyScheme
 51  
  * @see Property
 52  
  * 
 53  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 54  
  */
 55  0
 public class SetVarNode implements SimpleNode {
 56  0
     private static final Logger LOG = Logger.getLogger(SetVarNode.class);
 57  
 
 58  
     public SimpleResult process(RouteContext context, RouteHelper helper) throws Exception {
 59  0
         LOG.debug("processing");
 60  0
         DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 61  0
         String contentFragment = context.getNodeInstance().getRouteNode().getContentFragment();
 62  0
         LOG.debug("contentFragment=" + contentFragment);
 63  0
         Document d = db.parse(new InputSource(new StringReader(contentFragment)));
 64  0
         Element e = d.getDocumentElement();
 65  0
         String name = e.getElementsByTagName("name").item(0).getTextContent();
 66  
         // FIXME: validation should be checked in documenttypexmlparser to start with
 67  0
         if (name == null) {
 68  0
             throw new InvalidXmlException("SetVar node required 'name' attribute to be specified");
 69  
         }
 70  0
         String valueRef = e.getElementsByTagName("value").item(0).getTextContent();
 71  0
         Object retrievedVal = PropertiesUtil.retrieveProperty(valueRef, PropertyScheme.LITERAL_SCHEME, context);
 72  0
         LOG.debug("retrieved value '" + retrievedVal + " for value '" + valueRef);
 73  
         // hack to make variable value fit in column size
 74  
         // need to just alter that column
 75  0
         String stringVal = null;
 76  0
         if (retrievedVal != null) {
 77  0
             stringVal = retrievedVal.toString();
 78  0
             if (stringVal.length() > 255) {
 79  0
                 stringVal = stringVal.substring(0, 255);
 80  
             }
 81  
         }
 82  0
         LOG.debug("setting value '" + stringVal + "' for variable " + name);
 83  0
         KEWServiceLocator.getBranchService().setScopedVariableValue(context.getNodeInstance().getBranch(), BranchState.VARIABLE_PREFIX + name, stringVal);
 84  
 
 85  0
         return new SimpleResult(true);
 86  
     }
 87  
 
 88  
 }