/*
 * Created on 2004-9-5
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package workflow.client;

import java.util.Hashtable;

import javax.naming.NamingException;

import workflow.WorkflowException;
import workflow.client.factory.*;
import workflow.services.*;
import workflow.services.WorkflowService;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class InitialWorkflowClient implements WorkflowClient
{

	/**
	 * The environment associated with this InitialContext.
	 * 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 WorkflowNamingManager.getInitialWorkflowClient().
	* It is set by defaultInitWFClient() the first time getDefaultInitCtx()
	* is called. Subsequent invocations of getInitialWorkflowClient() return
	* the value of defaultInitWFClient.
	* @see #defaultInitWFClient
	*/
	protected WorkflowClient defaultInitWFClient = null;

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

	public WorkflowService     findService(String serviceName) throws WorkflowException
	{
		return defaultInitWFClient.findService(serviceName);
	}	 	
	public WorkflowService[]   findServices() throws WorkflowException
	{
		return defaultInitWFClient.findServices();
	}
	
	public WorkflowSession getSession() throws WorkflowException
	{
		return defaultInitWFClient.getSession();
	}

	public void disconnect() throws WorkflowException
	{
		defaultInitWFClient.disconnect();
	}

	//	=====================================================================
	//				 constructor  
	//	=====================================================================	
	public InitialWorkflowClient() throws WorkflowException, NamingException
	{
		init(null,null);
	}
	
	public InitialWorkflowClient(WorkflowClientFactory factory) throws WorkflowException, NamingException
	{
		init(null,factory);
	}
	
	public InitialWorkflowClient(Hashtable environment) throws WorkflowException, NamingException
	{
		init(environment,null);
	}

	public InitialWorkflowClient(Hashtable environment,WorkflowClientFactory factory) throws WorkflowException, NamingException
	{
		init(environment,factory);
	}
	
	protected void init(Hashtable environment,WorkflowClientFactory factory) throws WorkflowException, NamingException
	{
		myProps = ResourceManager.getInitialEnvironment(environment);
		
		if(factory!=null)
		{
			try
			{
				defaultInitWFClient=factory.getInitialLocalClient(myProps);						
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
		}
		
		if(defaultInitWFClient!=null)
		{
			gotDefault=true;
		}
		else
		{
			if (myProps.get(WorkflowClient.INITIAL_CLIENT_FACTORY) == null)
			{			
				myProps.put(WorkflowClient.INITIAL_CLIENT_FACTORY,"org.huihoo.willow.client.local.WorkflowClientFactoryImpl");			
			}
			
	    //user has specified initial client factory; try to get it
		  getDefaultInitWFClient();
		}
	}

	protected WorkflowClient getDefaultInitWFClient() throws WorkflowException, NamingException
	{
		if (!gotDefault)
		{
			defaultInitWFClient = WorkflowNamingManager.getInitialLocalClient(myProps);
			gotDefault = true;
		}
		if (defaultInitWFClient == null)
		{
			throw new NoWorkflowClientException();
		}

		return defaultInitWFClient;
	}
}
