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