//----------------------------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 org.huihoo.workflow.SchemaContext;
import org.huihoo.workflow.desktop.UserDatabase;
import org.huihoo.workflow.desktop.WorkflowImplementation;
import org.huihoo.workflow.server.WorkflowXPDL;
import org.huihoo.workflow.usermodel.WorkflowDepartment;
import org.huihoo.workflow.usermodel.WorkflowGroup;
import org.huihoo.workflow.usermodel.WorkflowParticipant;
import org.huihoo.workflow.usermodel.WorkflowRole;

import org.huihoo.commons.jdbc.Store;

public class UserDatabaseImpl implements UserDatabase
{
	protected WorkflowImplementation workflow;
	protected  WorkflowXPDL workflowTemplate;

	public UserDatabaseImpl(WorkflowImplementation workflow,WorkflowXPDL workflowTemplate)
	throws RemoteException
	{
		this.workflow = workflow;
		this.workflowTemplate = workflowTemplate;		
	}

	public WorkflowImplementation getWorkflowImplementation() throws RemoteException
	{
		return workflow;
	}
	
	public Store getStore() throws RemoteException
	{
		return workflowTemplate.getWorkflowTemplate().getUserDatabase().getStore();
	}
	
	public SchemaContext getSchemaContext() throws RemoteException
	{
		return workflowTemplate.getWorkflowTemplate().getUserDatabase().getSchemaContext();
	}
	
	/**
		 * Return the set of {@link Group}s defined in this user database.
		 */
	public Iterator getGroups() throws RemoteException
	{
		return workflowTemplate.getWorkflowTemplate().getUserDatabase().getGroups(workflow.getDesktop().getSession());
	}

	/**
	 * Return the set of {@link Department}s defined in this user database.
	 */
	public Iterator getDepartments() throws RemoteException
	{
		return workflowTemplate.getWorkflowTemplate().getUserDatabase().getDepartments(workflow.getDesktop().getSession());
	}

	/**
	 * Return the set of {@link WorkflowRole}s defined in this user database.
	 */
	public Iterator getRoles() throws RemoteException
	{
		return workflowTemplate.getWorkflowTemplate().getUserDatabase().getRoles(workflow.getDesktop().getSession());
	}

	/**
	 * Return the set of {@link WorkflowParticipant}s defined in this user database.
	 */
	public Iterator getParticipants() throws RemoteException
	{
		return workflowTemplate.getWorkflowTemplate().getUserDatabase().getParticipants(workflow.getDesktop().getSession());
	}

	/**
	 * Return the {@link Group} with the specified group id, if any;
	 * otherwise return <code>null</code>.
	 *
	 * @param groupid id of the group to return
	 */
	public WorkflowGroup findGroup(String groupid) throws RemoteException
	{ 
		return workflowTemplate.getWorkflowTemplate().getUserDatabase().findGroup(workflow.getDesktop().getSession(),groupid);
	}

	/**
	 * Return the {@link Department} with the specified Department id, if any;
	 * otherwise return <code>null</code>.
	 *
	 * @param departementid id of the Department to return
	 */
	public WorkflowDepartment findDepartment(String departementid) throws RemoteException
	{
		return workflowTemplate.getWorkflowTemplate().getUserDatabase().findDepartment(workflow.getDesktop().getSession(),departementid);
	}

	/**
	 * Return the {@link WorkflowRole} with the specified role id, if any;
	 * otherwise return <code>null</code>.
	 *
	 * @param roleid id of the WorkflowRole to return
	 */
	public WorkflowRole findRole(String roleid) throws RemoteException
	{
		return workflowTemplate.getWorkflowTemplate().getUserDatabase().findRole(workflow.getDesktop().getSession(),roleid);
	}

	/**
	 * Return the {@link WorkflowParticipant} with the specified user name, if any;
	 * otherwise return <code>null</code>.
	 *
	 * @param userid userid of the user to return
	 */
	public WorkflowParticipant findParticipant(String userid) throws RemoteException
	{
		return workflowTemplate.getWorkflowTemplate().getUserDatabase().findParticipant(workflow.getDesktop().getSession(),userid);
	}
}
