001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.engine.node.var;
017
018import org.apache.log4j.Logger;
019import org.kuali.rice.core.api.util.xml.XmlException;
020import org.kuali.rice.kew.engine.RouteContext;
021import org.kuali.rice.kew.engine.RouteHelper;
022import org.kuali.rice.kew.engine.node.BranchState;
023import org.kuali.rice.kew.engine.node.PropertiesUtil;
024import org.kuali.rice.kew.engine.node.SimpleNode;
025import org.kuali.rice.kew.engine.node.SimpleResult;
026import org.kuali.rice.kew.service.KEWServiceLocator;
027import org.w3c.dom.Document;
028import org.w3c.dom.Element;
029import org.xml.sax.InputSource;
030
031import javax.xml.parsers.DocumentBuilder;
032import javax.xml.parsers.DocumentBuilderFactory;
033import java.io.StringReader;
034
035
036/**
037 * A simple node that allows setting of document variables.
038 * The definition of SetVarNode takes the following config parameter elements:
039 * <dl>
040 *   <dt>name</dt>
041 *   <dd>The name of the variable to set</dd>
042 *   <dt>value</dt>
043 *   <dd>The value to which to set the variable.  This value is parsed according to
044 *       Property/PropertyScheme syntax</dd>
045 * </dl>
046 * The default PropertySchme is LiteralScheme, which will evaluate the value simply
047 * as a literal (won't do anything but return it)
048 * @see PropertyScheme
049 * @see Property
050 * 
051 * @author Kuali Rice Team (rice.collab@kuali.org)
052 */
053public class SetVarNode implements SimpleNode {
054    private static final Logger LOG = Logger.getLogger(SetVarNode.class);
055
056    public SimpleResult process(RouteContext context, RouteHelper helper) throws Exception {
057        LOG.debug("processing");
058        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
059        String contentFragment = context.getNodeInstance().getRouteNode().getContentFragment();
060        LOG.debug("contentFragment=" + contentFragment);
061        Document d = db.parse(new InputSource(new StringReader(contentFragment)));
062        Element e = d.getDocumentElement();
063        String name = e.getElementsByTagName("name").item(0).getTextContent();
064        // FIXME: validation should be checked in documenttypexmlparser to start with
065        if (name == null) {
066            throw new XmlException("SetVar node required 'name' attribute to be specified");
067        }
068        String valueRef = e.getElementsByTagName("value").item(0).getTextContent();
069        Object retrievedVal = PropertiesUtil.retrieveProperty(valueRef, PropertyScheme.LITERAL_SCHEME, context);
070        LOG.debug("retrieved value '" + retrievedVal + " for value '" + valueRef);
071        // hack to make variable value fit in column size
072        // need to just alter that column
073        String stringVal = null;
074        if (retrievedVal != null) {
075            stringVal = retrievedVal.toString();
076            if (stringVal.length() > 255) {
077                stringVal = stringVal.substring(0, 255);
078            }
079        }
080        LOG.debug("setting value '" + stringVal + "' for variable " + name);
081        KEWServiceLocator.getBranchService().setScopedVariableValue(context.getNodeInstance().getBranch(), BranchState.VARIABLE_PREFIX + name, stringVal);
082
083        return new SimpleResult(true);
084    }
085
086}