//----------------------------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.desktop;

import java.util.Hashtable;

import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.desktop.helpers.NamingManager;
import org.huihoo.workflow.desktop.helpers.ResourceManager;
import org.huihoo.workflow.server.WorkflowSession;

public class WorkflowInitialDesktop implements WorkflowDesktop
{

	/**
	 * The environment associated with this InitialDesktop.
	 * It is initialized to null and is updated by the constructor
	 * that accepts an environment or by the <tt>init()</tt> method.
	 * @see #addToEnvironment
	 * @see #removeFromEnvironment
	 * @see #getEnvironment
	 */
	protected Hashtable myProps = null;

	/**
		* Field holding the result of calling DesktopManager.getInitialDesktop().
		* It is set by defaultInitDesktop() the first time getDefaultInitCtx()
		* is called. Subsequent invocations of getInitialDesktop() return
		* the value of defaultInitDesktop.
		* @see #defaultInitDesktop
		*/
	protected WorkflowDesktop defaultInitDesktop = null;

	/**
		* Field indicating whether the initial workflowclient has been obtained
		* by calling DesktopManager.getInitialDesktop().
		* If true, its result is in <code>defaultInitDesktop</code>.
		*/
	protected boolean gotDefault = false;

	public WorkflowSession getSession()
	{
		return defaultInitDesktop.getSession();
	}

	public WorkflowImplementation findImplementation(String xpdlid) throws WorkflowException
	{
		return defaultInitDesktop.findImplementation(xpdlid);
	}

	public WorkflowImplementation[] findImplementations() throws WorkflowException
	{
		return defaultInitDesktop.findImplementations();
	}

	//	=====================================================================
	//				 constructor  
	//	=====================================================================	
	public WorkflowInitialDesktop() throws WorkflowException
	{
		init(null);
	}

	public WorkflowInitialDesktop(Hashtable environment) throws WorkflowException
	{
		init(environment);
	}

	protected void init(Hashtable environment) throws WorkflowException
	{
		myProps = ResourceManager.getInitialEnvironment(environment);

		if (myProps.get(WorkflowDesktop.INITIAL_DESKTOP_FACTORY) == null)
		{
			//user has not specified initial desktop factory; try to set default
			myProps.put(WorkflowDesktop.INITIAL_DESKTOP_FACTORY, "org.huihoo.willow.desktop.WorkflowDesktopFactoryImpl");
		}

		if (myProps.get(WorkflowDesktop.INITIAL_DESKTOP_FACTORY) != null)
		{
			//user has specified initial client factory; try to get it
			getDefaultInitDesktop();
		}
	}

	protected WorkflowDesktop getDefaultInitDesktop() throws WorkflowException
	{
		if (!gotDefault)
		{
			defaultInitDesktop = NamingManager.getInitialDesktop(myProps);
			gotDefault = true;
		}

		if (defaultInitDesktop == null)
		{
			throw new NoInitialDeskopException();
		}
		
		return defaultInitDesktop;
	}

	public Object addToEnvironment(String propName, Object propVal) throws WorkflowException
	{
		myProps.put(propName, propVal);
		return getDefaultInitDesktop().addToEnvironment(propName, propVal);
	}

	public Object removeFromEnvironment(String propName) throws WorkflowException
	{
		myProps.remove(propName);
		return getDefaultInitDesktop().removeFromEnvironment(propName);
	}

	public Hashtable getEnvironment() throws WorkflowException
	{
		return getDefaultInitDesktop().getEnvironment();
	}

	public void close() throws WorkflowException
	{
		if (myProps != null)
		{
			//myProps.clear();
			myProps = null;
		}

		if (defaultInitDesktop != null)
		{
			defaultInitDesktop.close();
			defaultInitDesktop = null;
		}
		gotDefault = false;
	}
}
