//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.willow.core;

import java.io.IOException;

import org.huihoo.willow.Context;
import org.huihoo.willow.RouteEvalutor;
import org.huihoo.willow.Wrapper;
import org.huihoo.workflow.selector.Selector;
import org.huihoo.workflow.selector.SelectorContext;
import org.huihoo.workflow.selector.SelectorException;
import org.huihoo.workflow.server.WorkflowWork;
import org.huihoo.workflow.xpdl.WorkflowCondition;
import org.huihoo.workflow.xpdl.WorkflowTransition;

public class StandardRouteEvalutor implements RouteEvalutor
{
	protected Context context;
	public StandardRouteEvalutor()
	{
	}

	public Context getContainer()
	{
		return context;

	}
	public void setContainer(Context context)
	{
		this.context = context;
	}

	public boolean evalute(WorkflowWork workflowWork, WorkflowTransition workflowTransition, SelectorContext context)
		throws SelectorException, IOException
	{
		WorkflowCondition workflowCondition = workflowTransition.getWorkflowCondition();
		if (workflowCondition == null
			|| workflowCondition.getContent() == null
			|| workflowCondition.getContent().trim().length() == 0)
		{
			return true;
		}

		Wrapper wrapper = (Wrapper) getContainer().findWrapper(Constants.SCRIPT_SELECTOR_NAME);
		Selector selector = wrapper.allocate();
		boolean rv = selector.evalute(workflowWork, workflowTransition, context);
		wrapper.deallocate(selector);

		return rv;
	}
}
