package com.c2kernel.lifecycle.instance; import java.util.HashMap; import com.c2kernel.common.InvalidDataException; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.graph.model.GraphableVertex; import com.c2kernel.lifecycle.instance.stateMachine.Transitions; import com.c2kernel.lifecycle.routingHelpers.ViewpointDataHelper; import com.c2kernel.lookup.AgentPath; import com.c2kernel.lookup.EntityPath; import com.c2kernel.persistency.ClusterStorage; import com.c2kernel.process.Gateway; import com.c2kernel.scripting.Script; import com.c2kernel.scripting.ScriptingEngineException; import com.c2kernel.utils.KeyValuePair; import com.c2kernel.utils.Logger; /** * @version $Revision: 1.38 $ $Date: 2005/09/07 13:46:31 $ * @author $Author: abranson $ */ public abstract class WfVertex extends GraphableVertex { /**sets the activity available to be executed on start of Workflow or composite activity (when it is the first one of the * (sub)process*/ public abstract void runfirst(AgentPath agent) throws ScriptingEngineException; /** * @see java.lang.Object#Object() */ public WfVertex() { super(); setIsLayoutable(true); setIsComposite(false); } /** * Method runNext. */ public void runNext(AgentPath agent) throws ScriptingEngineException { try { ((CompositeActivity)getParent()).request(agent, Transitions.COMPLETE, null); } catch (Exception e) { //Logger.error(e); } } /** * Method reinit. * @param idLoop */ public abstract void reinit( int idLoop ); /** * Method verify. * @return boolean */ public abstract boolean verify(); /** * Method getErrors. * @return String */ public abstract String getErrors(); /** * Method run. */ public abstract void run(AgentPath agent) throws ScriptingEngineException; /** * Method loop. * @return boolean */ public abstract boolean loop(); /** * Method addNext. * @param vertex */ public abstract Next addNext(WfVertex vertex); protected Object evaluateScript(String scriptName, String scriptVersion) throws ScriptingEngineException { try { EntityPath entity = ((CompositeActivity) getParent()).getWf().getItemEntityPath(); Script script = getScript(scriptName, scriptVersion); KeyValuePair[] k = getProperties().getKeyValuePairs(); HashMap requiredInput = script.getAllInputParams(); for (KeyValuePair element : k) { if (requiredInput.containsKey(element.getKey())) { String value = element.getStringValue(); Object inputParam = value; if (value.startsWith("viewpoint//")) { value = value.substring(11); if (value.startsWith(".")) value = entity.getSysKey() + value.substring(1); try { inputParam = ViewpointDataHelper.get(value)[0]; } catch (ArrayIndexOutOfBoundsException ex) { throw new InvalidDataException("Could not retrieve data from viewpoint: "+value, ""); } } if (value.startsWith("property//")) { value = value.substring(10); try { inputParam = Gateway.getStorage().get(entity.getSysKey(), ClusterStorage.PROPERTY+"/"+value, null); } catch (ObjectNotFoundException ex) { inputParam = null; } } Logger.msg(5, "Split.evaluateScript() - Setting param " + element.getKey() + " to " + inputParam.toString()); script.setInputParamValue(element.getKey(), inputParam); } } if (requiredInput.containsKey("item")) { script.setInputParamValue("item", Gateway.getProxyManager().getProxy(entity)); } if (requiredInput.containsKey("agent")) { AgentPath systemAgent = Gateway.getLDAPLookup().getRoleManager().getAgentPath("system"); script.setInputParamValue("agent", Gateway.getProxyManager().getProxy(systemAgent)); } Object retVal = script.execute(); Logger.msg(2, "Split.evaluateScript() - Script returned "+retVal); if (retVal == null) retVal = ""; return retVal; } catch (Exception e) { Logger.msg(1, "Split.evaluateScript() - Error: Script " + scriptName); Logger.error(e); throw new ScriptingEngineException(); } } private static Script getScript(String name, String version) throws ScriptingEngineException { Script script; try { script = new Script(name, Integer.parseInt(version)); } catch (NumberFormatException e) { // version not valid int split = name.indexOf(":"); if (split > -1) { script = new Script(name.substring(0, split), name.substring(split + 1)); } else throw new ScriptingEngineException("Could not find script " + name + " v" + version); } return script; } public Workflow getWf() { return ((CompositeActivity)getParent()).getWf(); } }