/*
 * Created on 2004-6-19
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.huihoo.willow.client;

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

import org.huihoo.willow.naming.WillowContext;
import org.huihoo.willow.naming.ContextFactory;
import org.huihoo.willow.session.SessionManager;
import org.huihoo.willow.session.WillowSession;

import workflow.live.LiveService;
import workflow.live.pm.LiveParticipant;
import workflow.live.store.LiveUserDatabase;
import workflow.store.RealmDatabase;
import workflow.wfmc.pm.Participant;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class NamingImpl implements Naming, java.io.Serializable
{
	private  transient Vector __liveServices=new Vector();
	
	public NamingImpl() throws RemoteException
	{
	}
	
	public String  authenticate(String username,String password) throws RemoteException
	{
		WillowContext context=ContextFactory.getWillowContext();
		SessionManager manager=null;
		RealmDatabase  realmDatabase=null;
		Participant    participant=null;
		   
		manager = (SessionManager)context.getGlobalSessionManager();
		realmDatabase=(RealmDatabase)context.getGlobalRealmDatabase();
		
		participant=realmDatabase.authenticate(username,password);
		
		if(participant==null)
		{
			throw new RemoteException(
								"login engine unsuccessfully.username="
									+ username
									+ ",password="
									+ password);
		}
		
		WillowSession session=manager.createSession();
		session.setParticipant(participant);
		
		return session.getId();
	} 
	
	public void      disconnect(String sessionID) throws RemoteException
	{
		WillowContext context=ContextFactory.getWillowContext();
		SessionManager manager=null;
		WillowSession  session=null;   

		manager = (SessionManager)context.getGlobalSessionManager();
		session=manager.findSession(sessionID);
		
		if(session!=null)
		{
			session.invalidate();
		}
	}
	
	public int   getMaxInactiveInterval(String sessionID)throws RemoteException
	{
		WillowContext context=ContextFactory.getWillowContext();
		SessionManager manager=null;
		WillowSession  session=null;   

		manager = (SessionManager)context.getGlobalSessionManager();
		session=manager.findSession(sessionID);
			
		if(session==null)
		{
			throw new RemoteException("Session invalid or expired");
		}

		return session.getMaxInactiveInterval();
	}
	public void  setMaxInactiveInterval(String sessionID,int interval)throws RemoteException
	{
		WillowContext context=ContextFactory.getWillowContext();
		SessionManager manager=null;
		WillowSession  session=null;   

		manager = (SessionManager)context.getGlobalSessionManager();
		session=manager.findSession(sessionID);
			
		if(session==null)
		{
			throw new RemoteException("Session invalid or expired");
		}

		session.setMaxInactiveInterval(interval);
	}
	
	public void   keepAlive(String sessionID) throws RemoteException
	{
		WillowContext context=ContextFactory.getWillowContext();
		SessionManager manager=null;
		WillowSession  session=null;   

		manager = (SessionManager)context.getGlobalSessionManager();
		session=manager.findSession(sessionID);
		
		if(session==null)
		{
			throw new RemoteException("Session invalid or expired");
		}
		
		if(session!=null)
		{
			session.access();
		}
	}
	
	public LiveService findService(
		String workflowID,
		String sessionID)
		throws RemoteException
	{		
		WillowContext context=ContextFactory.getWillowContext();
		SessionManager manager=null;
		WillowSession  session=null;

		manager = (SessionManager)context.getGlobalSessionManager();
		session=manager.findSession(sessionID);
		
		if(session==null)
		{
			throw new RemoteException("Session invalid or expired");
		}		
		
		
		Participant participant=session.getParticipant();
		String userid=participant.getUUID();
		String password=participant.getPassword();
		
		LiveService liveService = null;
		int serviceCount=__liveServices.size();
		for (int i = 0; i < serviceCount; ++i)
		{								
			LiveService temp=(LiveService)__liveServices.elementAt(i);
			if (temp.getName().equals(workflowID))
			{
				liveService = temp;
				break;
			}
		}
		
		if (liveService != null)
		{
			if (!authenticate(liveService, userid, password))
			{
				throw new RemoteException(
					"login workflow unsuccessfully.userid="
						+ userid
						+ ",password="
						+ password);
			}
		}

		return liveService;
	}

	public LiveService[] findServices(String sessionID)
		throws RemoteException
	{
		WillowContext context=ContextFactory.getWillowContext();
		SessionManager manager=null;
		WillowSession  session=null;
		   
		manager = (SessionManager)context.getGlobalSessionManager();
		session=manager.findSession(sessionID);
		
		if(session==null)
		{
			throw new RemoteException("Session invalid or expired");
		}		
		
		
		Participant participant=session.getParticipant();
		String userid=participant.getUUID();
		String password=participant.getPassword();
		
		Vector vector = new Vector(0);

		int servicesCount=__liveServices.size();
		for (int i = 0; i < servicesCount; ++i)
		{								
			LiveService temp=(LiveService)__liveServices.elementAt(i);
			if (authenticate(temp, userid, password))
			{
				vector.add(temp);
			}
		}
		
		int size=vector.size();
		LiveService[] result=new LiveService[size];
		for(int i=0;i<size;++i)
		{
			result[i]=(LiveService)vector.elementAt(i);
		}
		return result;
	}
	
	//---------------------------------------------------------------------------
	// only used by container
	//---------------------------------------------------------------------------
	public void addLiveService(LiveService liveService)
	{
		if (liveService == null)
		{
			return;
		}
		
		int serviceCount=__liveServices.size();
		for (int i = 0; i < serviceCount; ++i)
		{								
			LiveService temp=(LiveService)__liveServices.elementAt(i);
			try
			{
				if (temp.getName().equals(liveService.getName()))
				{
					return;
				}
			}
			catch (RemoteException e)
			{
				e.printStackTrace();
			}
		}

		__liveServices.add(liveService);
	}	
	
	public void removeLiveService(String service_name)
	{
		if (service_name == null)
		{
			return;
		}
		
		int serviceCount=__liveServices.size();
		for (int i = 0; i < serviceCount; ++i)
		{								
			LiveService temp=(LiveService)__liveServices.elementAt(i);
			try
			{
				if (temp.getName().equals(service_name))
				{
					__liveServices.remove(temp);
					break;
				}
			}
			catch (RemoteException e)
			{
				//never reach here
				e.printStackTrace();
			}
		}
	}
	//-----------------------------------------------------------------------------

	private boolean authenticate(
		LiveService liveService,
		String userid,
		String password)
		throws RemoteException
	{
		
		LiveUserDatabase userDatabase = liveService.getUserDatabase();

		LiveParticipant user = userDatabase.findParticipant(userid);

		if (user != null && user.getPassword().equals(password))
		{
			return true;
		}

		return false;
	}
}
