//----------------------------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 
 */
package org.huihoo.willow.desktop;

import java.rmi.RemoteException;
import java.util.Hashtable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.WorkflowSerializable;
import org.huihoo.workflow.desktop.WorkflowDesktop;
import org.huihoo.workflow.desktop.WorkflowImplementation;
import org.huihoo.workflow.server.WorkflowSession;

public class WorkflowRemoteDesktopImpl implements WorkflowDesktop,WorkflowSerializable
{
	private static Log log = LogFactory.getLog(WorkflowRemoteDesktopImpl.class);

	/**
	 * The environment associated with this WorkflowDesktop.
	 * 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;

	protected WorkflowSession session;

	protected DesktopService serviceContext;

	public WorkflowRemoteDesktopImpl(Hashtable environment) throws WorkflowException
	{
		if (environment == null)
		{
			this.myProps = new Hashtable();
		}
		else
		{
			this.myProps = (Hashtable) environment.clone();
		}

		serviceContext = DesktopImplUtils.getDesktopService((String) myProps.get(WorkflowDesktop.PROVIDER_URL), myProps);

		String username = (String) myProps.get(WorkflowDesktop.SECURITY_PRINCIPAL);
		String password = (String) myProps.get(WorkflowDesktop.SECURITY_CREDENTIALS);
		try
		{
			session = serviceContext.authenticate(username, password);
		}
		catch (RemoteException e)
		{
			WorkflowException ex = new WorkflowException(e.toString());
			ex.setRootCause(e);

			throw ex;
		}
	}

	public WorkflowSession getSession()
	{
		return session;
	}

	public WorkflowImplementation findImplementation(String xpdlid) throws WorkflowException
	{	
		try
		{
			WorkflowImplementation workflow = serviceContext.findImplementation(this, xpdlid);
			return workflow;
		}
		catch (RemoteException e)
		{
			WorkflowException ex = new WorkflowException(e.toString());
			ex.setRootCause(e);

			throw ex;
		}
	}

	public WorkflowImplementation[] findImplementations() throws WorkflowException
	{
		try
		{
			WorkflowImplementation[] workflows = serviceContext.findImplementations(this);
			return workflows;
		}
		catch (RemoteException e)
		{
			WorkflowException ex = new WorkflowException(e.toString());
			ex.setRootCause(e);

			throw ex;
		}
	}

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

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

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

	public void close() throws WorkflowException
	{
		try
		{
			serviceContext.close(this);
		}
		catch (RemoteException e)
		{
			new WorkflowException(e);
		}
		serviceContext = null;
	}
}
