//----------------------------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.workflow.tools;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.WorkflowUtilities;
import org.huihoo.workflow.desktop.CaseDatabase;
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.WorkflowSession;
import org.huihoo.workflow.server.WorkflowWork;
import org.huihoo.workflow.store.Constants;
import org.huihoo.workflow.xpdl.ExtendedAttribute;
import org.huihoo.workflow.xpdl.WorkflowActivity;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowProcess;
import org.huihoo.workflow.xpdl.XPDLGlobals;


public final class ServletServer
{
	public static StringManager sm = StringManager.getManager(ServletConstants.PACKAGE);

	public static boolean serviceCase(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command) throws ServletException, IOException,WorkflowException
	{
		if (ServletConstants.COMMAND_CREATE_CASE.equals(command))
		{
			caseCreate(servlet,request, response,command);
		}
		else if (ServletConstants.COMMAND_OPEN_CASE.equals(command))
		{
			caseOpen(servlet,request, response,command);
		}
		else if (ServletConstants.COMMAND_DELETE_CASE.equals(command))
		{
			caseDelete(servlet,request, response,command);
		}
		else if (ServletConstants.COMMAND_ABORT_CASE.equals(command))
		{
			caseAbort(servlet,request, response,command);
		}
		else
		{
			servlet.log(sm.getString("servlet.invalidCommand", servlet, command));
			throw new ServletException(sm.getString("servlet.invalidCommand", servlet, command));				
		}
		return true;
	}

	public static boolean serviceWork(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command) throws ServletException, IOException,WorkflowException
	{
		if (ServletConstants.COMMAND_ACCEPT_WORK.equals(command))
		{
			workAccept(servlet,request,response,command);
		}
		else if (ServletConstants.COMMAND_OPEN_WORK.equals(command))
		{
			workOpen(servlet,request,response,command);
		}
		else if (ServletConstants.COMMAND_SUBMIT_WORK.equals(command))
		{
			workSubmit(servlet,request,response,command);
		}
		else if (ServletConstants.COMMAND_RESUBMIT_WORK.equals(command))
		{
			workReSubmit(servlet,request,response,command);
		}
		else if (ServletConstants.COMMAND_BACKTRACK_WORK.equals(command))
		{
			workBackTrack(servlet,request,response,command);
		}
		else if (ServletConstants.COMMAND_REVERSE_WORK.equals(command))
		{
			workReverse(servlet,request,response,command);
		}
		else if (ServletConstants.COMMAND_FETCH_WORK.equals(command))
		{
			workFetch(servlet,request,response,command);
		}
		else
		{
			servlet.log(sm.getString("servlet.invalidCommand", servlet, command));
			throw new ServletException(sm.getString("servlet.invalidCommand", servlet, command));				
		}
		return true;
	}
	
	public static WorkflowCase caseCreate(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws WorkflowException,RemoteException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		
		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		WorkflowCase wCase = null;

		try
		{
			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String pky = request.getParameter(ServletConstants.WORKFLOW_PRIMARY_KEY);
			String name = request.getParameter(ServletConstants.NAME_KEY);
			String desc = request.getParameter(ServletConstants.DESCRIPTION_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (pky == null)
			{
				pky = (String) request.getAttribute(ServletConstants.WORKFLOW_PRIMARY_KEY);
			}

			if (name == null)
			{
				name = (String) request.getAttribute(ServletConstants.NAME_KEY);
			}

			if (desc == null)
			{
				desc = (String) request.getAttribute(ServletConstants.DESCRIPTION_KEY);
			}

			WorkflowImplementation objWorkflow  = objDesktop.findImplementation(pkg);
		
			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			String caseid =
				caseDatabase.createCase(
					wPrc,
					workflowSession.getParticipant(),
					new java.util.Date(),
					pky,
					name,
					desc);
			wCase = caseDatabase.findCase(wPrc, caseid);	
		}
		finally
		{
			objDesktop.close();
		}
		return wCase;
	}

	public static void caseDelete(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws WorkflowException,RemoteException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		
		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		try
		{
			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String cas = request.getParameter(ServletConstants.WORKFLOW_CASE_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (cas == null)
			{
				cas = (String) request.getAttribute(ServletConstants.WORKFLOW_CASE_KEY);
			}

			WorkflowImplementation objWorkflow  = objDesktop.findImplementation(pkg);

			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			WorkflowCase wCase = caseDatabase.findCase(wPrc, cas);
			caseDatabase.deleteCase(wPrc, wCase);
		}
		finally
		{
			objDesktop.close();
		}

	}

	public static void caseAbort(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws WorkflowException,RemoteException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		
		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		try
		{

			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String cas = request.getParameter(ServletConstants.WORKFLOW_CASE_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (cas == null)
			{
				cas = (String) request.getAttribute(ServletConstants.WORKFLOW_CASE_KEY);
			}

			WorkflowImplementation objWorkflow  = objDesktop.findImplementation(pkg);

			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			WorkflowCase wCase = caseDatabase.findCase(wPrc, cas);
			caseDatabase.cancelCase(wPrc, wCase);	
		}
		finally
		{
			objDesktop.close();
		}

	}

	public static WorkflowCase  caseFind(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws WorkflowException,RemoteException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		
		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		WorkflowCase wCase = null;
		try
		{

			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String cas = request.getParameter(ServletConstants.WORKFLOW_CASE_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (cas == null)
			{
				cas = (String) request.getAttribute(ServletConstants.WORKFLOW_CASE_KEY);
			}

			WorkflowImplementation objWorkflow  = objDesktop.findImplementation(pkg);

			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			wCase = caseDatabase.findCase(wPrc, cas);
			
		}
		finally
		{
			objDesktop.close();
		}
		
		return wCase;
	}
	
	public static void caseOpen(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws ServletException,IOException, WorkflowException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		
		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		try
		{
			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String cas = request.getParameter(ServletConstants.WORKFLOW_CASE_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (cas == null)
			{
				cas = (String) request.getAttribute(ServletConstants.WORKFLOW_CASE_KEY);
			}

			WorkflowImplementation objWorkflow  = objDesktop.findImplementation(pkg);

			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			WorkflowActivity firstActivity = WorkflowUtilities.getInitialActivity(wPrc);
			
			ExtendedAttribute attrPageURL = firstActivity.findExtendedAttribute(XPDLGlobals.PAGE_URL);
			if ((attrPageURL == null) || (attrPageURL.getValue() == null))
			{
				throw new ServletException(sm.getString("servlet.pageNotFound",servlet,command));			
			}
			
			String pageURL=attrPageURL.getValue();
			servlet.getServletContext().getRequestDispatcher("/"+pageURL).include(request,response);
		}
		finally
		{
			objDesktop.close();
		}

	}
	public static void workAccept(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws WorkflowException,RemoteException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		

		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		try
		{
			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String cas = request.getParameter(ServletConstants.WORKFLOW_CASE_KEY);
			String wrk = request.getParameter(ServletConstants.WORKFLOW_WORK_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (cas == null)
			{
				cas = (String) request.getAttribute(ServletConstants.WORKFLOW_CASE_KEY);
			}

			if (wrk == null)
			{
				wrk = (String) request.getAttribute(ServletConstants.WORKFLOW_WORK_KEY);
			}

			WorkflowImplementation objWorkflow  = objDesktop.findImplementation(pkg);

			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			WorkflowCase wCase = caseDatabase.findCase(wPrc, cas);
			WorkflowWork wWork = caseDatabase.findWork(wPrc, wCase, wrk);
			objWorkflow.getWorkflowDispatcher().accept(wPrc,wWork);
		}
		finally
		{
			objDesktop.close();
		}

	}

	public static void workOpen(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws WorkflowException,RemoteException, IOException,ServletException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		
		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		try
		{
			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String cas = request.getParameter(ServletConstants.WORKFLOW_CASE_KEY);
			String wrk = request.getParameter(ServletConstants.WORKFLOW_WORK_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (cas == null)
			{
				cas = (String) request.getAttribute(ServletConstants.WORKFLOW_CASE_KEY);
			}

			if (wrk == null)
			{
				wrk = (String) request.getAttribute(ServletConstants.WORKFLOW_WORK_KEY);
			}

			WorkflowImplementation objWorkflow = objDesktop.findImplementation(pkg);

			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			WorkflowCase wCase = caseDatabase.findCase(wPrc, cas);
			
			WorkflowWork wWork = caseDatabase.findWork(wPrc, wCase, wrk);
			WorkflowActivity wAct = wWork.getActivity();

			ExtendedAttribute attrPageURL = wAct.findExtendedAttribute(XPDLGlobals.PAGE_URL);
			if ((attrPageURL == null) || (attrPageURL.getValue() == null))
			{
				throw new ServletException(sm.getString("servlet.pageNotFound",servlet,command));			
			}
			
			String pageURL=attrPageURL.getValue();
			servlet.getServletContext().getRequestDispatcher("/"+pageURL).include(request,response);
	
		}
		finally
		{
			objDesktop.close();
		}

	}

	public static void workSubmit(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws WorkflowException,RemoteException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		
		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		try
		{
			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String cas = request.getParameter(ServletConstants.WORKFLOW_CASE_KEY);
			String wrk = request.getParameter(ServletConstants.WORKFLOW_WORK_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (cas == null)
			{
				cas = (String) request.getAttribute(ServletConstants.WORKFLOW_CASE_KEY);
			}

			if (wrk == null)
			{
				wrk = (String) request.getAttribute(ServletConstants.WORKFLOW_WORK_KEY);
			}

			WorkflowImplementation objWorkflow  = objDesktop.findImplementation(pkg);

			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			WorkflowCase wCase = caseDatabase.findCase(wPrc, cas);
			WorkflowWork wWork = caseDatabase.findWork(wPrc, wCase, wrk);

			String usr = request.getParameter(ServletConstants.WORKFLOW_USER_KEY);

			if (usr == null)
			{
				usr = (String) request.getAttribute(ServletConstants.WORKFLOW_USER_KEY);
			}

			Map infoMap = Constants.createMap();
			Constants.setDispatchStyle(infoMap, Constants.SUBMIT);
			
			if (usr != null && !"".equals(usr))
			{
				List paricipant_id_list = new ArrayList();
				StringTokenizer tokens = new StringTokenizer(usr, XPDLGlobals.PERFORMER_SEPARATOR);
				while (tokens.hasMoreTokens())
				{
					paricipant_id_list.add(tokens.nextToken());
				}
				
				Constants.setParticipants(infoMap, paricipant_id_list);
				objWorkflow.getWorkflowDispatcher().submit(wPrc,wWork, infoMap);
			}
			else
			{
				objWorkflow.getWorkflowDispatcher().submit(wPrc,wWork);
			}
		}
		finally
		{
			objDesktop.close();
		}
	}
	
	public static void workReSubmit(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws WorkflowException,RemoteException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		
		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		try
		{
			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String cas = request.getParameter(ServletConstants.WORKFLOW_CASE_KEY);
			String wrk = request.getParameter(ServletConstants.WORKFLOW_WORK_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (cas == null)
			{
				cas = (String) request.getAttribute(ServletConstants.WORKFLOW_CASE_KEY);
			}

			if (wrk == null)
			{
				wrk = (String) request.getAttribute(ServletConstants.WORKFLOW_WORK_KEY);
			}

			WorkflowImplementation objWorkflow  = objDesktop.findImplementation(pkg);

			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			WorkflowCase wCase = caseDatabase.findCase(wPrc, cas);
			WorkflowWork wWork = caseDatabase.findWork(wPrc, wCase, wrk);

			String usr = request.getParameter(ServletConstants.WORKFLOW_USER_KEY);

			if (usr == null)
			{
				usr = (String) request.getAttribute(ServletConstants.WORKFLOW_USER_KEY);
			}

			Map infoMap = Constants.createMap();
			Constants.setDispatchStyle(infoMap,Constants.RESUBMIT);
			
			if (usr != null && !"".equals(usr))
			{
				List paricipant_id_list = new ArrayList();
				StringTokenizer tokens = new StringTokenizer(usr, XPDLGlobals.PERFORMER_SEPARATOR);
				while (tokens.hasMoreTokens())
				{
					paricipant_id_list.add(tokens.nextToken());
				}
				
				Constants.setParticipants(infoMap, paricipant_id_list);
				objWorkflow.getWorkflowDispatcher().submit(wPrc,wWork, infoMap);
			}
			else
			{
				objWorkflow.getWorkflowDispatcher().submit(wPrc,wWork,infoMap);
			}
		}
		finally
		{
			objDesktop.close();
		}
	}
	
	public static void workBackTrack(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws WorkflowException,RemoteException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		
		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		try
		{
			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String cas = request.getParameter(ServletConstants.WORKFLOW_CASE_KEY);
			String wrk = request.getParameter(ServletConstants.WORKFLOW_WORK_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (cas == null)
			{
				cas = (String) request.getAttribute(ServletConstants.WORKFLOW_CASE_KEY);
			}

			if (wrk == null)
			{
				wrk = (String) request.getAttribute(ServletConstants.WORKFLOW_WORK_KEY);
			}

			WorkflowImplementation objWorkflow  = objDesktop.findImplementation(pkg);

			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			WorkflowCase wCase = caseDatabase.findCase(wPrc, cas);
			WorkflowWork wWork = caseDatabase.findWork(wPrc, wCase, wrk);
			
			Map infoMap = Constants.createMap();
			objWorkflow.getWorkflowDispatcher().backtrack(wPrc, wWork);	
		}
		finally
		{
			objDesktop.close();
		}

	}

	public static void workReverse(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws WorkflowException,RemoteException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		
		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		try
		{
			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String cas = request.getParameter(ServletConstants.WORKFLOW_CASE_KEY);
			String wrk = request.getParameter(ServletConstants.WORKFLOW_WORK_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (cas == null)
			{
				cas = (String) request.getAttribute(ServletConstants.WORKFLOW_CASE_KEY);
			}

			if (wrk == null)
			{
				wrk = (String) request.getAttribute(ServletConstants.WORKFLOW_WORK_KEY);
			}

			WorkflowImplementation objWorkflow  = objDesktop.findImplementation(pkg);

			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			WorkflowCase wCase = caseDatabase.findCase( wPrc, cas);
			WorkflowWork wWork = caseDatabase.findWork(wPrc, wCase, wrk);
			
			System.err.println("workReverse===>objWorkflow.getWorkflowDispatcher().reverse(wPrc,wWork);	");
			objWorkflow.getWorkflowDispatcher().reverse(wPrc,wWork);	
		}
		finally
		{
			objDesktop.close();
		}

	}
	
	public static void workFetch(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response,String command)
		throws WorkflowException,RemoteException
	{
		HttpSession session = request.getSession(true);

		Hashtable env = (Hashtable) session.getAttribute(ServletConstants.WORKFLOW_VARIABLES_KEY);
		WorkflowDesktop objDesktop = new WorkflowInitialDesktop(env);
		
		WorkflowSession workflowSession = (WorkflowSession) objDesktop.getSession();

		try
		{
			String pkg = request.getParameter(ServletConstants.WORKFLOW_PACKAGE_KEY);
			String cas = request.getParameter(ServletConstants.WORKFLOW_CASE_KEY);
			String wrk = request.getParameter(ServletConstants.WORKFLOW_WORK_KEY);

			if (pkg == null)
			{
				pkg = (String) request.getAttribute(ServletConstants.WORKFLOW_PACKAGE_KEY);
			}

			if (cas == null)
			{
				cas = (String) request.getAttribute(ServletConstants.WORKFLOW_CASE_KEY);
			}

			if (wrk == null)
			{
				wrk = (String) request.getAttribute(ServletConstants.WORKFLOW_WORK_KEY);
			}

			WorkflowImplementation objWorkflow  = objDesktop.findImplementation(pkg);

			CaseDatabase caseDatabase = objWorkflow.getCaseDatabase();
			WorkflowPackage wPkg = objWorkflow.getWorkflowPackage();

			WorkflowProcess wPrc = wPkg.getMainWorkflowProcess();

			WorkflowCase wCase = caseDatabase.findCase(wPrc, cas);
			WorkflowWork wWork = caseDatabase.findWork( wPrc, wCase, wrk);
			objWorkflow.getWorkflowDispatcher().fetch(wPrc,wWork);		
		}
		finally
		{
			objDesktop.close();
		}
	}
}
