1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.engine.node.var;
17
18 import org.apache.log4j.Logger;
19 import org.kuali.rice.core.api.util.xml.XmlException;
20 import org.kuali.rice.kew.engine.RouteContext;
21 import org.kuali.rice.kew.engine.RouteHelper;
22 import org.kuali.rice.kew.engine.node.BranchState;
23 import org.kuali.rice.kew.engine.node.PropertiesUtil;
24 import org.kuali.rice.kew.engine.node.SimpleNode;
25 import org.kuali.rice.kew.engine.node.SimpleResult;
26 import org.kuali.rice.kew.service.KEWServiceLocator;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
29 import org.xml.sax.InputSource;
30
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import java.io.StringReader;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class SetVarNode implements SimpleNode {
54 private static final Logger LOG = Logger.getLogger(SetVarNode.class);
55
56 public SimpleResult process(RouteContext context, RouteHelper helper) throws Exception {
57 LOG.debug("processing");
58 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
59 String contentFragment = context.getNodeInstance().getRouteNode().getContentFragment();
60 LOG.debug("contentFragment=" + contentFragment);
61 Document d = db.parse(new InputSource(new StringReader(contentFragment)));
62 Element e = d.getDocumentElement();
63 String name = e.getElementsByTagName("name").item(0).getTextContent();
64
65 if (name == null) {
66 throw new XmlException("SetVar node required 'name' attribute to be specified");
67 }
68 String valueRef = e.getElementsByTagName("value").item(0).getTextContent();
69 Object retrievedVal = PropertiesUtil.retrieveProperty(valueRef, PropertyScheme.LITERAL_SCHEME, context);
70 LOG.debug("retrieved value '" + retrievedVal + " for value '" + valueRef);
71
72
73 String stringVal = null;
74 if (retrievedVal != null) {
75 stringVal = retrievedVal.toString();
76 if (stringVal.length() > 255) {
77 stringVal = stringVal.substring(0, 255);
78 }
79 }
80 LOG.debug("setting value '" + stringVal + "' for variable " + name);
81 KEWServiceLocator.getBranchService().setScopedVariableValue(context.getNodeInstance().getBranch(), BranchState.VARIABLE_PREFIX + name, stringVal);
82
83 return new SimpleResult(true);
84 }
85
86 }