View Javadoc

1   /**
2    * Copyright 2005-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  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   * A simple node that allows setting of document variables.
38   * The definition of SetVarNode takes the following config parameter elements:
39   * <dl>
40   *   <dt>name</dt>
41   *   <dd>The name of the variable to set</dd>
42   *   <dt>value</dt>
43   *   <dd>The value to which to set the variable.  This value is parsed according to
44   *       Property/PropertyScheme syntax</dd>
45   * </dl>
46   * The default PropertySchme is LiteralScheme, which will evaluate the value simply
47   * as a literal (won't do anything but return it)
48   * @see PropertyScheme
49   * @see Property
50   * 
51   * @author Kuali Rice Team (rice.collab@kuali.org)
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          // FIXME: validation should be checked in documenttypexmlparser to start with
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          // hack to make variable value fit in column size
72          // need to just alter that column
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  }