//----------------------------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 org.huihoo.workflow.SchemaContext;
import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.WorkflowTransaction;
import org.huihoo.workflow.WorkflowUtilities;
import org.huihoo.workflow.desktop.CaseDatabase;
import org.huihoo.workflow.desktop.WorkflowImplementation;
import org.huihoo.workflow.server.WorkflowCase;
import org.huihoo.workflow.server.WorkflowPrimaryKey;
import org.huihoo.workflow.server.WorkflowWork;
import org.huihoo.workflow.server.WorkflowXPDL;
import org.huihoo.workflow.server.WorkflowXPDLRepository;
import org.huihoo.workflow.store.WorkflowCaseValidator;
import org.huihoo.workflow.store.WorkflowWorkValidator;
import org.huihoo.workflow.usermodel.WorkflowParticipant;
import org.huihoo.workflow.xpdl.WorkflowProcess;

import org.huihoo.commons.jdbc.Store;

public class RemoteCaseDatabaseImpl extends WorkflowRemoteObject implements CaseDatabase
{
	protected WorkflowImplementation workflow;
	protected String WorkflowTemplate_ID ;

	public RemoteCaseDatabaseImpl(WorkflowImplementation workflow, WorkflowXPDL workflowTemplate)
	throws RemoteException
	{
		super();
		
		this.workflow = workflow;
		this.WorkflowTemplate_ID = workflowTemplate.getWorkflowPackage().getUUID();
	}

	public WorkflowImplementation getWorkflowImplementation()throws RemoteException
	{
		return workflow;
	}

	public Store getStore()throws RemoteException
	{
		return WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().getStore();
	}

	public SchemaContext getSchemaContext()throws RemoteException
	{
		return WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().getSchemaContext();
	}

	public WorkflowTransaction getWorkflowTransaction() throws WorkflowException,RemoteException, RemoteException
	{
		return WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().getWorkflowTransaction();
	}

	//-------------------------------------------------------------------------------------	

	public String createCase(
		WorkflowProcess workflowProcess,
		WorkflowParticipant creator,
		java.util.Date creationTime,
		String primaryKey)
		throws WorkflowException,RemoteException
	{
		WorkflowCaseValidator caseValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseValidator();

		if (caseValidator != null)
		{
			caseValidator.checkCreate(workflow.getDesktop().getSession(), workflowProcess);
		}

		WorkflowTransaction transaction = null;
		try
		{
			transaction = getWorkflowTransaction();
			String caseid =
				WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().createCase(
					workflow.getDesktop().getSession(),
					transaction,
					workflowProcess,
					creator,
					creationTime,
					primaryKey,
					"",
					"");
			transaction.commit();

			return caseid;
		}
		catch (WorkflowException we)
		{
			WorkflowUtilities.rollbackNoThrow(transaction);
			throw we;
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}
	}
	public String createCase(
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowParticipant creator,
		java.util.Date creationTime,
		String primaryKey)
		throws WorkflowException,RemoteException
	{
		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}

	public String createCase(
		WorkflowProcess workflowProcess,
		WorkflowParticipant creator,
		java.util.Date creationTime,
		String primaryKey,
		String name,
		String description)
		throws WorkflowException,RemoteException
	{
		WorkflowCaseValidator caseValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseValidator();

		if (caseValidator != null)
		{

			caseValidator.checkCreate(workflow.getDesktop().getSession(), workflowProcess);
		}

		WorkflowTransaction transaction = null;
		try
		{
			transaction = getWorkflowTransaction();
			String caseid =
				WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().createCase(
					workflow.getDesktop().getSession(),
					transaction,
					workflowProcess,
					creator,
					creationTime,
					primaryKey,
					name,
					description);
			transaction.commit();

			return caseid;
		}
		catch (WorkflowException we)
		{
			WorkflowUtilities.rollbackNoThrow(transaction);
			throw we;
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}
	}
	public String createCase(
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowParticipant creator,
		java.util.Date creationTime,
		String primaryKey,
		String name,
		String description)
		throws WorkflowException,RemoteException
	{
		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}
	public WorkflowCase findCase(WorkflowProcess workflowProcess, String caseId) throws WorkflowException,RemoteException
	{

		WorkflowTransaction transaction = null;
		WorkflowCase workflowCase = null;
		try
		{
			transaction = getWorkflowTransaction();
			workflowCase =
				WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().findCase(
					workflow.getDesktop().getSession(),
					transaction,
					workflowProcess,
					caseId);
			transaction.commit();

		}
		catch (WorkflowException we)
		{
			WorkflowUtilities.rollbackNoThrow(transaction);
			throw we;
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}

		WorkflowCaseValidator caseValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseValidator();
		if (caseValidator != null)
		{
			caseValidator.checkFind(workflow.getDesktop().getSession(), workflowCase);
		}
		return workflowCase;
	}
	public WorkflowCase findCase(WorkflowTransaction userTransaction, WorkflowProcess workflowProcess, String caseId)
		throws WorkflowException,RemoteException
	{
		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}
	public WorkflowCase findCase(WorkflowProcess workflowProcess, WorkflowPrimaryKey primaryKey) throws WorkflowException,RemoteException
	{
		WorkflowTransaction transaction = null;
		WorkflowCase workflowCase = null;
		try
		{
			transaction = getWorkflowTransaction();
			workflowCase =
				WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().findCase(
					workflow.getDesktop().getSession(),
					transaction,
					workflowProcess,
					primaryKey);
			transaction.commit();

		}
		catch (WorkflowException we)
		{
			WorkflowUtilities.rollbackNoThrow(transaction);
			throw we;
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}

		WorkflowCaseValidator caseValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseValidator();

		if (caseValidator != null)
		{
			caseValidator.checkFind(workflow.getDesktop().getSession(), workflowCase);
		}
		return workflowCase;
	}

	public WorkflowCase findCase(
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowPrimaryKey primaryKey)
		throws WorkflowException,RemoteException
	{

		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}

	public void updateCase(WorkflowProcess workflowProcess, WorkflowCase workflowCase) throws WorkflowException,RemoteException
	{
		WorkflowCaseValidator caseValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseValidator();
		if (caseValidator != null)
		{
			caseValidator.checkUpdate(workflow.getDesktop().getSession(), workflowCase);
		}

		WorkflowTransaction transaction = null;
		try
		{
			transaction = getWorkflowTransaction();
			WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().updateCase(
				workflow.getDesktop().getSession(),
				transaction,
				workflowProcess,
				workflowCase,"");
			transaction.commit();
		}
		catch (WorkflowException we)
		{
			WorkflowUtilities.rollbackNoThrow(transaction);
			throw we;
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}
	}
	public void updateCase(
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowCase workflowCase)
		throws WorkflowException,RemoteException
	{
		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}
	public void deleteCase(WorkflowProcess workflowProcess, WorkflowCase workflowCase) throws WorkflowException,RemoteException
	{
		WorkflowCaseValidator caseValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseValidator();

		if (caseValidator != null)
		{
			caseValidator.checkDelete(workflow.getDesktop().getSession(), workflowCase);
		}

		WorkflowTransaction transaction = null;
		try
		{
			transaction = getWorkflowTransaction();
			WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().deleteCase(
				workflow.getDesktop().getSession(),
				transaction,
				workflowProcess,
				workflowCase,"");
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}
	}

	public void deleteCase(
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowCase workflowCase)
		throws WorkflowException,RemoteException
	{
		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}
	public void cancelCase(WorkflowProcess workflowProcess, WorkflowCase workflowCase) throws WorkflowException,RemoteException
	{
		WorkflowCaseValidator caseValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseValidator();

		if (caseValidator != null)
		{
			caseValidator.checkCancel(workflow.getDesktop().getSession(), workflowCase);
		}

		WorkflowTransaction transaction = null;
		try
		{
			transaction = getWorkflowTransaction();
			WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().cancelCase(
				workflow.getDesktop().getSession(),
				transaction,
				workflowProcess,
				workflowCase,"");
			transaction.commit();
		}
		catch (WorkflowException we)
		{
			WorkflowUtilities.rollbackNoThrow(transaction);
			throw we;
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}
	}
	public void cancelCase(
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowCase workflowCase)
		throws WorkflowException,RemoteException
	{
		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}
	public void completeCase(WorkflowProcess workflowProcess, WorkflowCase workflowCase) throws WorkflowException,RemoteException
	{
		WorkflowCaseValidator caseValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseValidator();

		if (caseValidator != null)
		{
			caseValidator.checkComplete(workflow.getDesktop().getSession(), workflowCase);
		}

		WorkflowTransaction transaction = null;
		try
		{
			transaction = getWorkflowTransaction();
			WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().completeCase(
				workflow.getDesktop().getSession(),
				transaction,
				workflowProcess,
				workflowCase);
			transaction.commit();
		}
		catch (WorkflowException we)
		{
			WorkflowUtilities.rollbackNoThrow(transaction);
			throw we;
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}
	}
	public void completeCase(
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowCase workflowCase)
		throws WorkflowException,RemoteException
	{
		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}

	public void confirmCase(WorkflowProcess workflowProcess, WorkflowCase workflowCase) throws WorkflowException,RemoteException
	{
		WorkflowCaseValidator caseValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseValidator();

		if (caseValidator != null)
		{
			caseValidator.checkConfirm(workflow.getDesktop().getSession(), workflowCase);
		}

		WorkflowTransaction transaction = null;
		try
		{
			transaction = getWorkflowTransaction();
			WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().confirmCase(
				workflow.getDesktop().getSession(),
				transaction,
				workflowProcess,
				workflowCase);
			transaction.commit();
		}
		catch (WorkflowException we)
		{
			WorkflowUtilities.rollbackNoThrow(transaction);
			throw we;
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}

	}
	public void confirmCase(
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowCase workflowCase)
		throws WorkflowException,RemoteException
	{
		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}

	public void suspendCase(WorkflowProcess workflowProcess, WorkflowCase workflowCase) throws WorkflowException,RemoteException
	{
		WorkflowCaseValidator caseValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseValidator();
		if (caseValidator != null)
		{
			caseValidator.checkSuspend(workflow.getDesktop().getSession(), workflowCase);
		}

		WorkflowTransaction transaction = null;
		try
		{
			transaction = getWorkflowTransaction();
			WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().suspendCase(
				workflow.getDesktop().getSession(),
				transaction,
				workflowProcess,
				workflowCase,"");
			transaction.commit();
		}
		catch (WorkflowException we)
		{
			WorkflowUtilities.rollbackNoThrow(transaction);
			throw we;
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}
	}
	public void suspendCase(
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowCase workflowCase)
		throws WorkflowException,RemoteException
	{
		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}
	public void resumeCase(WorkflowProcess workflowProcess, WorkflowCase workflowCase) throws WorkflowException,RemoteException
	{
		WorkflowCaseValidator caseValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseValidator();

		if (caseValidator != null)
		{
			caseValidator.checkResume(workflow.getDesktop().getSession(), workflowCase);
		}

		WorkflowTransaction transaction = null;
		try
		{
			transaction = getWorkflowTransaction();
			WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().resumeCase(
				workflow.getDesktop().getSession(),
				transaction,
				workflowProcess,
				workflowCase,"");
			transaction.commit();
		}
		catch (WorkflowException we)
		{
			WorkflowUtilities.rollbackNoThrow(transaction);
			throw we;
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}
	}
	public void resumeCase(
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowCase workflowCase)
		throws WorkflowException,RemoteException
	{
		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}
	public WorkflowWork findWork(WorkflowProcess workflowProcess, WorkflowCase workflowCase, String workId)
		throws WorkflowException,RemoteException
	{
		WorkflowWork workflowItem = null;
		WorkflowTransaction transaction = null;
		try
		{
			transaction = getWorkflowTransaction();
			workflowItem =
				WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getCaseDatabase().findWork(
					workflow.getDesktop().getSession(),
					transaction,
					workflowProcess,
					workflowCase,
					workId);
			transaction.commit();
		}
		catch (WorkflowException we)
		{
			WorkflowUtilities.rollbackNoThrow(transaction);
			throw we;
		}
		finally
		{
			WorkflowUtilities.releaseNoThrow(transaction);
		}

		WorkflowWorkValidator workValidator = WorkflowXPDLRepository.getInstance().findWorkflowXPDL(WorkflowTemplate_ID).getWorkflowTemplate().getWorkValidator();

		if (workValidator != null)
		{
			workValidator.checkFind(workflow.getDesktop().getSession(), workflowItem);
		}
		return workflowItem;
	}
	public WorkflowWork findWork(
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowCase workflowCase,
		String workId)
		throws WorkflowException,RemoteException
	{
		throw new RemoteException("WorkflowTransaction not implements Remote interface");
	}
}
