//----------------------------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.workflow.coreImpl.server;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import org.huihoo.workflow.server.WorkflowCase;
import org.huihoo.workflow.server.WorkflowCaseContext;
import org.huihoo.workflow.xpdl.WorkflowParameter;
import org.huihoo.workflow.xpdl.WorkflowTransition;

public class WorkflowCaseContextImpl implements WorkflowCaseContext
{
	protected Hashtable parameters = new Hashtable();
	protected Hashtable attributes = new Hashtable();

	protected Hashtable performers = new Hashtable();
	protected Hashtable weights = new Hashtable();

	protected WorkflowCase workflowCase;

	public WorkflowCaseContextImpl(WorkflowCase workflowCase_, List initParams)
	{
		this.workflowCase = workflowCase_;
		fillParameters(initParams);
	}

	public WorkflowParameter getParameterByName(String name)
	{
		if (name == null)
		{
			return null;
		}

		Enumeration enum = parameters.elements();
		while (enum.hasMoreElements())
		{
			WorkflowParameter param = (WorkflowParameter) enum.nextElement();
			if (name.equals(param.getName()))
			{
				return param;
			}
		}

		return null;

	}

	public WorkflowParameter getParameterByID(String uuid)
	{
		if (uuid == null)
		{
			return null;
		}
		return (WorkflowParameter) parameters.get(uuid);
	}

	public Enumeration getParameterNames()
	{
		return parameters.keys();
	}

	public Object getAttribute(String name)
	{
		return attributes.get(name);
	}

	public void setAttribute(String name, Object obj)
	{
		attributes.put(name, obj);
	}

	public Object removeAttribute(String name)
	{
		return attributes.remove(name);
	}
	public Enumeration getAttributeNames()
	{
		return attributes.keys();
	}

	public WorkflowCase getWorkflowCase()
	{
		return workflowCase;
	}

	public List getPerformersByTransition(WorkflowTransition workflowTransition)
	{
		List paricipant_id_list = (List) performers.get(workflowTransition);
		return paricipant_id_list;
	}

	public void setPerformersByTransition(WorkflowTransition workflowTransition, List paricipant_id_list)
	{
		performers.put(workflowTransition, paricipant_id_list);
	}

	public double getWeightByTransition(WorkflowTransition workflowTransition)
	{
		Double dbl = (Double) weights.get(workflowTransition);
		if (dbl != null)
		{
			return dbl.doubleValue();
		}

		return 0;
	}

	public void setWeightByTransition(WorkflowTransition workflowTransition, double weight)
	{
		weights.put(workflowTransition, new Double(weight));
	}

	//--------------------------------------------------------------------------

	private void fillParameters(List paramList)
	{
		if (paramList == null)
		{
			return;
		}

		int sizeParam = paramList.size();

		WorkflowParameter parameter = null;

		for (int i = 0; i < sizeParam; ++i)
		{
			parameter = (WorkflowParameter) paramList.get(i);
			parameters.put(parameter.getUUID(), parameter);
		}
	}
}
