|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
   package org.kuali.rice.kew.engine.node.var;  | 
  |  18 |     | 
     | 
  |  19 |     | 
   import org.apache.log4j.Logger;  | 
  |  20 |     | 
   import org.kuali.rice.core.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 |     | 
     | 
  |  39 |     | 
     | 
  |  40 |     | 
     | 
  |  41 |     | 
     | 
  |  42 |     | 
     | 
  |  43 |     | 
     | 
  |  44 |     | 
     | 
  |  45 |     | 
     | 
  |  46 |     | 
     | 
  |  47 |     | 
     | 
  |  48 |     | 
     | 
  |  49 |     | 
     | 
  |  50 |     | 
     | 
  |  51 |     | 
     | 
  |  52 |     | 
     | 
  |  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 |     | 
             | 
  |  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 |     | 
             | 
  |  73 |     | 
             | 
  |  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 |     | 
   }  |