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

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

import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.desktop.CaseDatabase;
import org.huihoo.workflow.desktop.UserDatabase;
import org.huihoo.workflow.desktop.WorkflowDesktop;
import org.huihoo.workflow.desktop.WorkflowImplementation;
import org.huihoo.workflow.desktop.WorkflowInitialDesktop;
import org.huihoo.workflow.server.WorkflowCase;
import org.huihoo.workflow.server.WorkflowDispatcher;
import org.huihoo.workflow.server.WorkflowSession;
import org.huihoo.workflow.server.WorkflowWork;
import org.huihoo.workflow.usermodel.WorkflowParticipant;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowProcess;


public class WillowClientTest
{

	public static void main(String[] args) throws  WorkflowException, ClassNotFoundException, RemoteException
	{
		Hashtable env = new Hashtable();
		env.put(WorkflowDesktop.INITIAL_DESKTOP_FACTORY, "org.huihoo.willow.desktop.WorkflowRemoteDesktopFactoryImpl");
		env.put(WorkflowDesktop.PROVIDER_URL, "remote://localhost:2126");
		env.put(WorkflowDesktop.SECURITY_PRINCIPAL, "zosatapo");
		env.put(WorkflowDesktop.SECURITY_CREDENTIALS, "zosatapo");

		System.err.println("----------------------WorkflowInitialDesktop remoteDesktop-------------------------");
		
		WorkflowInitialDesktop remoteDesktop = new WorkflowInitialDesktop(env);

		String package_id = "willow-or-join";
		if (args.length > 0)
		{
			package_id = args[0];
		}

		System.err.println("----------------------Workflow remoteWorkflow -------------------------");
		WorkflowImplementation remoteWorkflow = remoteDesktop.findImplementation(package_id);
		
		WorkflowSession session = remoteDesktop.getSession();
		UserDatabase userDatabase = remoteWorkflow.getUserDatabase();
		CaseDatabase caseDatabase = remoteWorkflow.getCaseDatabase();	
		WorkflowDispatcher workflowDispacher = remoteWorkflow.getWorkflowDispatcher();	
		
		WorkflowPackage workflowpackage = remoteWorkflow.getWorkflowPackage();
		WorkflowProcess workflowprocess = workflowpackage.getMainWorkflowProcess();

		System.err.println("WorkflowPackage : " + workflowpackage);
		System.err.println("WorkflowProcess : " + workflowprocess);
		System.err.println("caseDatabase : " + caseDatabase);
		
		WorkflowParticipant zosatapo = userDatabase.findParticipant("zosatapo");
		String caseId = caseDatabase.createCase(workflowprocess, zosatapo, new Date(), "first case", " ", " ");

		WorkflowCase workflowCase = caseDatabase.findCase(workflowprocess, caseId);

		WorkflowWork workflowWork = caseDatabase.findWork(workflowprocess, workflowCase, workflowCase.getMagicUUID());
    System.err.println("workflowWork : " + workflowWork);
    workflowDispacher.submit(workflowprocess, workflowWork);
		
		remoteDesktop.close();
		System.err.println("-------workflow execution completed--------------");
	}
}
