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

import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.Vector;

import org.huihoo.willow.Manager;
import org.huihoo.willow.RealmDatabase;
import org.huihoo.willow.core.DesktopConnector;
import org.huihoo.workflow.desktop.WorkflowDesktop;
import org.huihoo.workflow.desktop.WorkflowImplementation;
import org.huihoo.workflow.server.WorkflowXPDL;
import org.huihoo.workflow.server.WorkflowSession;
import org.huihoo.workflow.server.WorkflowXPDLRepository;

public class RemoteDesktopServiceImpl implements DesktopService
{
	protected transient DesktopConnector serviceDesktop;

	public RemoteDesktopServiceImpl(DesktopConnector serviceDesktop) throws RemoteException
	{
		this.serviceDesktop = serviceDesktop;
	}

	public WorkflowSession authenticate(String username, String password) throws RemoteException
	{
		RealmDatabase realmDatabase = serviceDesktop.getService().getRealmDatabase();
		Manager manager = serviceDesktop.getService().getManager();

		return DesktopImplUtils.authenticate(manager, realmDatabase, username, password);
	}

	public WorkflowImplementation findImplementation(WorkflowDesktop desktop, String xpdlid) throws RemoteException
	{
		WorkflowXPDL workflowRuntime = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(xpdlid);
		if (workflowRuntime != null)
		{
			return new WorkflowImplementationRemote(desktop,workflowRuntime);
		}
		
		return null;

	}
	public WorkflowImplementation[] findImplementations(WorkflowDesktop desktop) throws RemoteException
	{
		Vector vector = new Vector(0);
		Iterator iterator = WorkflowXPDLRepository.getInstance().findWorkflowXPDLs();
		while (iterator.hasNext())
		{
			vector.addElement(WorkflowXPDLRepository.getInstance().findWorkflowXPDL((String) iterator.next()));
		}

		int size = vector.size();
		WorkflowImplementation[] result = new WorkflowImplementation[size];
		for (int i = 0; i < size; ++i)
		{
			result[i] = new WorkflowImplementationRemote(desktop,(WorkflowXPDL) vector.elementAt(i));

		}
		return result;
	}

	public void close(WorkflowDesktop desktop) throws RemoteException
	{
		WorkflowSession session = checkExpired(desktop.getSession().getId());
		session.invalidate();
		session = null;
	}

	protected WorkflowSession checkExpired(String sessionid) throws RemoteException
	{
		Manager manager = serviceDesktop.getService().getManager();
		WorkflowSession session = manager.findSession(sessionid);

		if (session == null)
		{
			throw new RemoteException("session invalid or expired");
		}

		return session;
	}
}
