//----------------------------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.coreImpl.store;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.huihoo.commons.jdbc.JDBCUtilities;
import org.huihoo.commons.jdbc.Store;
import org.huihoo.workflow.SchemaContext;
import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.WorkflowSimpleComponent;
import org.huihoo.workflow.WorkflowUtilities;
import org.huihoo.workflow.coreImpl.server.WeightableWorkImpl;
import org.huihoo.workflow.coreImpl.server.WorkflowCaseImpl;
import org.huihoo.workflow.coreImpl.server.WorkflowKeyImpl;
import org.huihoo.workflow.coreImpl.server.WorkflowPathImpl;
import org.huihoo.workflow.coreImpl.server.WorkflowWorkImpl;
import org.huihoo.workflow.event.CaseEvent;
import org.huihoo.workflow.event.WorkEvent;
import org.huihoo.workflow.server.Weightable;
import org.huihoo.workflow.server.WorkflowCase;
import org.huihoo.workflow.server.WorkflowCaseContext;
import org.huihoo.workflow.server.WorkflowPath;
import org.huihoo.workflow.server.WorkflowSession;
import org.huihoo.workflow.server.WorkflowWork;
import org.huihoo.workflow.store.WorkflowUUIDGenerator;
import org.huihoo.workflow.usermodel.WorkflowParticipant;
import org.huihoo.workflow.xpdl.WorkflowActivity;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowParameter;
import org.huihoo.workflow.xpdl.WorkflowProcess;
import org.huihoo.workflow.xpdl.WorkflowTransition;
import org.huihoo.workflow.xpdl.util.DateTools;
import org.huihoo.workflow.xpdl.util.TypeDefinition;

class InternalCaseDatabase
{
	InternalCaseDatabase()
	{
	}

	//------------------------------------------------------------
	//
	//------------------------------------------------------------ 
	String createCase_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowParticipant creator,
		Date creationTime,
		String primaryKey,
		String name,
		String description)
		throws WorkflowException
	{
		WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
		SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
		PreparedStatement pstmt = null;
		String caseId = null;
		try
		{

			caseId = caseIdGenerator.nextCaseUUID(workflowProcess);

			String pkgId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();

			if (primaryKey == null)
			{
				primaryKey = "";
			}

			String strSQL =
				"INSERT INTO "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_CASE)
					+ "(vc_packageid,vc_processid,vc_uuid,vc_primarykey,vc_creator,dat_creationTime,int_state,vc_name,vc_description,vc_magicid) "
					+ "VALUES(?,?,?,?,?,?,?,?,?,?)";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, pkgId);
			pstmt.setString(2, processId);
			pstmt.setString(3, caseId);
			pstmt.setString(4, primaryKey);
			pstmt.setString(5, creator.getUUID());
			pstmt.setTimestamp(6, DateTools.toSqlTimestamp(creationTime));
			pstmt.setInt(7, WorkflowCase.CREATED);
			pstmt.setString(8, name);
			pstmt.setString(9, description);
			pstmt.setString(10, "TO_SET_VALUE");

			pstmt.executeUpdate();

			createCaseEvent_impl(
				workflowsession,
				gCaseDatabaseImpl,
				workflowProcess,
				conn,
				false,
				pkgId,
				processId,
				caseId,
				new Date(),
				CaseEvent.CASE_CREATED,
				"");

			WorkflowParticipant operator = creator;
			WorkflowActivity firstActivity = WorkflowUtilities.getInitialActivity(workflowProcess);

			WorkflowCase workflowCase =
				this.findCase_impl(workflowsession, gCaseDatabaseImpl, workflowProcess, conn, false, caseId, false);

			String workId =
				createWork_magic(
					workflowsession,
					gCaseDatabaseImpl,
					conn,
					workflowProcess,
					workflowCase,
					firstActivity,
					creator,
					creationTime,
					WorkEvent.CREATED);

			createPath_impl(gCaseDatabaseImpl, workflowProcess, conn, workflowCase, null, workId, WorkEvent.CREATED);

			createCaseContext(workflowsession, gCaseDatabaseImpl, workflowProcess, conn, caseId);

			workflowCase.setMagicUUID(workId);
			this.updateCase_impl(workflowsession, gCaseDatabaseImpl, workflowProcess, conn, false, workflowCase, "");

		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not create workflow case");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}

		return caseId;
	}

	WorkflowCase findCase_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		String caseId,
		boolean isPK)
		throws WorkflowException
	{
		WorkflowCaseImpl workflowCase = null;
		ResultSet jrs = null;
		PreparedStatement pstmt = null;

		try
		{
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			String packageId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String strSQL = null;

			if (isPK)
			{
				strSQL =
					"SELECT * FROM "
						+ schemaContext.getTableName(SchemaContext.SCHEMA_CASE)
						+ " "
						+ "WHERE  vc_packageId=? AND    vc_processid=? AND    vc_primaryKey=?";

				pstmt = conn.prepareStatement(strSQL);
				pstmt.setString(1, packageId);
				pstmt.setString(2, processId);
				pstmt.setString(3, caseId);
			}
			else
			{
				strSQL = "SELECT * FROM " + schemaContext.getTableName(SchemaContext.SCHEMA_CASE) + " " + "WHERE   vc_uuid=?";
				pstmt = conn.prepareStatement(strSQL);
				pstmt.setString(1, caseId);
			}

			jrs = pstmt.executeQuery();
			if (jrs.next())
			{
				workflowCase = new WorkflowCaseImpl();
				workflowCase.setWorkflowProcess(workflowProcess);
				workflowCase.setName(jrs.getString("vc_name"));
				workflowCase.setUUID(jrs.getString("vc_uuid"));
				workflowCase.setCreationTime(DateTools.toJavaDate(jrs.getTimestamp("dat_creationTime")));
				workflowCase.setState(jrs.getInt("int_state"));
				workflowCase.setMagicUUID(jrs.getString("vc_magicid"));

				workflowCase.setCreator(
					gCaseDatabaseImpl.getWorkflowTemplate().getUserDatabase().findParticipant(
						workflowsession,
						jrs.getString("vc_creator")));
				workflowCase.setPrimaryKey(new WorkflowKeyImpl(jrs.getString("vc_primaryKey")));
				WorkflowCaseContext caseContext = CaseContexUtilities.createContext(storeConfig, schemaContext, workflowCase);
				workflowCase.setCaseContext(caseContext);
			}
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not find workflow case");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}

		return workflowCase;
	}

	void updateCase_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowCase workflowCase,
		String description)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();

			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();

			String packageId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String strSQL =
				"UPDATE "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_CASE)
					+ " "
					+ "SET  vc_name=?,"
					+ "vc_description=?,"
					+ "dat_creationTime=?,"
					+ "vc_primaryKey=?,"
					+ "vc_creator=?,"
					+ "int_state=?,"
					+ "vc_magicid=?"
					+ " "
					+ "WHERE    vc_uuid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, workflowCase.getName());
			pstmt.setString(2, workflowCase.getDescription());
			pstmt.setTimestamp(3, DateTools.toSqlTimestamp(workflowCase.getCreationTime()));
			pstmt.setString(4, workflowCase.getPrimaryKey().getUUID());
			pstmt.setString(5, workflowCase.getCreator().getUUID());
			pstmt.setInt(6, workflowCase.getState());
			pstmt.setString(7, workflowCase.getMagicUUID());
			pstmt.setString(8, workflowCase.getUUID());

			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not update workflow case");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
	}

	void deleteCase_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowCase workflowCase,
		String description)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;

		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			String packageId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();

			if (canRelease)
			{
				conn.setAutoCommit(false);
			}

			String strSQL =
				"DELETE FROM " + schemaContext.getTableName(SchemaContext.SCHEMA_CASE) + " " + "WHERE    vc_uuid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, workflowCase.getUUID());
			pstmt.executeUpdate();
			JDBCUtilities.cleanupNoThrow(pstmt);
			pstmt = null;

			strSQL =
				"DELETE FROM " + schemaContext.getTableName(SchemaContext.SCHEMA_CASE_DATAFIELD) + " " + "WHERE   vc_caseid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, workflowCase.getUUID());
			pstmt.executeUpdate();
			JDBCUtilities.cleanupNoThrow(pstmt);
			pstmt = null;

			strSQL = "DELETE FROM " + schemaContext.getTableName(SchemaContext.SCHEMA_WORK) + " " + "WHERE    vc_caseid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, workflowCase.getUUID());
			pstmt.executeUpdate();
			JDBCUtilities.cleanupNoThrow(pstmt);
			pstmt = null;

			strSQL = "DELETE FROM " + schemaContext.getTableName(SchemaContext.SCHEMA_PATH) + " " + "WHERE    vc_caseid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, workflowCase.getUUID());
			pstmt.executeUpdate();
			JDBCUtilities.cleanupNoThrow(pstmt);
			pstmt = null;

			strSQL =
				"DELETE FROM " + schemaContext.getTableName(SchemaContext.SCHEMA_CASE_EVENT) + " " + "WHERE    vc_caseid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, workflowCase.getUUID());
			pstmt.executeUpdate();
			JDBCUtilities.cleanupNoThrow(pstmt);
			pstmt = null;

			strSQL =
				"DELETE FROM " + schemaContext.getTableName(SchemaContext.SCHEMA_WORK_EVENT) + " " + "WHERE   vc_caseid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, workflowCase.getUUID());
			pstmt.executeUpdate();
			JDBCUtilities.cleanupNoThrow(pstmt);
			pstmt = null;

			strSQL =
				"DELETE FROM "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_ACTIVITY_DISPATCH)
					+ " "
					+ "WHERE  vc_caseid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, workflowCase.getUUID());
			pstmt.executeUpdate();
			JDBCUtilities.cleanupNoThrow(pstmt);
			pstmt = null;

			createCaseEvent_impl(
				workflowsession,
				gCaseDatabaseImpl,
				workflowProcess,
				conn,
				false,
				workflowCase,
				new Date(),
				CaseEvent.DELETED,
				"");

			if (canRelease)
			{
				conn.commit();
			}
		}
		catch (SQLException sqlex)
		{
			if (canRelease)
			{
				try
				{
					conn.rollback();
				}
				catch (SQLException ex)
				{
				}
			}
			throw new WorkflowException(sqlex);
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(pstmt);

			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn);
			}
		}
	}

	void changeCaseState_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowCase workflowCase,
		int case_state,
		String description)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			String packageId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String strSQL =
				"UPDATE "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_CASE)
					+ " "
					+ "SET  int_state=?"
					+ " "
					+ "WHERE    vc_uuid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setInt(1, case_state);
			pstmt.setString(2, workflowCase.getUUID());

			pstmt.executeUpdate();

			int eventType = CaseEvent.CASE_CREATED;

			switch (case_state)
			{
				case WorkflowCase.RUNNING :
					eventType = CaseEvent.RESUMED;
					break;

				case WorkflowCase.CREATED :
					eventType = CaseEvent.CASE_CREATED;
					break;

				case WorkflowCase.COMPLETED :
					eventType = CaseEvent.COMPLETED;
					break;

				case WorkflowCase.SUSPENDED :
					eventType = CaseEvent.SUSPENDED;
					break;

				case WorkflowCase.CANCELLED :
					eventType = CaseEvent.CANCELED;
					break;

			}

			createCaseEvent_impl(
				workflowsession,
				gCaseDatabaseImpl,
				workflowProcess,
				conn,
				false,
				workflowCase,
				new Date(),
				eventType,
				"");

		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not change workflow case status");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
	}

	String createWork_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowCase workflowCase,
		WorkflowActivity activity,
		WorkflowWork parentWork,
		WorkflowParticipant performer,
		java.util.Date arriveTime,
		int eventType,
		int priority)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		String workId = null;
		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			String pkgId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();

			workId = caseIdGenerator.nextWorkUUID(workflowCase);
			String strSQL =
				"INSERT INTO "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_WORK)
					+ "(vc_packageid,vc_processid,vc_caseid,vc_owner_processid,vc_activityid,vc_uuid,vc_performer,dat_arriveTime,vc_parent_workid,int_eventType,int_priority) "
					+ " "
					+ "VALUES(?,?,?,?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, pkgId);
			pstmt.setString(2, processId);
			pstmt.setString(3, caseId);
			pstmt.setString(4, activity.getWorkflowProcess().getUUID());
			pstmt.setString(5, activity.getUUID());
			pstmt.setString(6, workId);
			pstmt.setString(7, valueOf(performer));
			pstmt.setTimestamp(8, DateTools.toSqlTimestamp(arriveTime));
			pstmt.setString(9, ((parentWork == null) ? null : parentWork.getUUID()));
			pstmt.setInt(10, eventType);
			pstmt.setInt(11, priority);
			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not create workflow sub work");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
		return workId;
	}

	void updateWork_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowWork item,
		String description)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			WorkflowCase workflowCase = item.getCase();

			String packageId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();
			String workId = item.getUUID();
			String strSQL = null;

			if (item instanceof Weightable)
			{
				Weightable weight = (Weightable) item;

				strSQL =
					"UPDATE "
						+ schemaContext.getTableName(SchemaContext.SCHEMA_WORK)
						+ " "
						+ "SET  vc_performer=?,"
						+ "dat_arriveTime=?,"
						+ "dat_acceptTime=?,"
						+ "dat_dispatchTime=?, "
						+ "vc_userobject=?,  "
						+ "dbl_weight=?  "
						+ " "
						+ "WHERE    vc_uuid=?";
				pstmt = conn.prepareStatement(strSQL);

				pstmt.setString(1, valueOf(item.getPerformer()));
				pstmt.setTimestamp(2, DateTools.toSqlTimestamp(item.getArriveTime()));
				pstmt.setTimestamp(3, DateTools.toSqlTimestamp(item.getAcceptTime()));
				pstmt.setTimestamp(4, DateTools.toSqlTimestamp(item.getDispatchTime()));
				pstmt.setString(5, item.getUserObject());
				pstmt.setDouble(6, weight.getWeight());

				pstmt.setString(7, workId);
			}
			else
			{
				strSQL =
					"UPDATE "
						+ schemaContext.getTableName(SchemaContext.SCHEMA_WORK)
						+ " "
						+ "SET  vc_performer=?,"
						+ "dat_arriveTime=?,"
						+ "dat_acceptTime=?,"
						+ "dat_dispatchTime=?, "
						+ "vc_userobject=? "
						+ " "
						+ "WHERE    vc_uuid=?";
				pstmt = conn.prepareStatement(strSQL);

				pstmt.setString(1, valueOf(item.getPerformer()));
				pstmt.setTimestamp(2, DateTools.toSqlTimestamp(item.getArriveTime()));
				pstmt.setTimestamp(3, DateTools.toSqlTimestamp(item.getAcceptTime()));
				pstmt.setTimestamp(4, DateTools.toSqlTimestamp(item.getDispatchTime()));
				pstmt.setString(5, item.getUserObject());
				pstmt.setString(6, workId);
			}

			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not update workflow work");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
	}

	void deleteWork_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowWork item,
		String description)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			WorkflowCase workflowCase = item.getCase();

			String packageId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();
			String workId = item.getUUID();
			String strSQL =
				"DELETE FROM " + schemaContext.getTableName(SchemaContext.SCHEMA_WORK) + " " + "WHERE    vc_uuid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, workId);

			pstmt.executeUpdate();
			JDBCUtilities.cleanupNoThrow(pstmt);
			pstmt = null;

			//			createWorkEvent_impl(
			//				workflowsession,
			//				gCaseDatabaseImpl,
			//				workflowProcess,
			//				conn,
			//				false,
			//				item,
			//				new Date(),
			//				WorkEvent.DELETED,
			//				description);
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not delete workflow work");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
	}

	WorkflowWork findWork_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowCase workflowCase,
		String workId)
		throws WorkflowException
	{
		WorkflowWorkImpl item = null;

		PreparedStatement pstmt = null;
		ResultSet jrs = null;

		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			WorkflowPackage workflowPackage = workflowProcess.getWorkflowPackage();
			String packageId = workflowPackage.getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();
			String strSQL =
				"SELECT * FROM " + schemaContext.getTableName(SchemaContext.SCHEMA_WORK) + " " + "WHERE  vc_uuId=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, workId);

			jrs = pstmt.executeQuery();

			String parent_workId = null;
			String vc_performer = null;
			String vc_activityId = null;
			String vc_owner_processId = null;
			WorkflowProcess ownerProcess = null;
			int int_weight_flag = 0;

			if (jrs.next())
			{
				parent_workId = jrs.getString("vc_parent_workId");
				vc_performer = jrs.getString("vc_performer");
				vc_activityId = jrs.getString("vc_activityId");
				vc_owner_processId = jrs.getString("vc_owner_processId");

				ownerProcess = workflowPackage.findWorkflowProcess(vc_owner_processId);
				WorkflowActivity activity = ownerProcess.findActivity(vc_activityId);

				if (int_weight_flag != 0)
				{
					WeightableWorkImpl wworkImpl = new WeightableWorkImpl();
					wworkImpl.setWeight(jrs.getDouble("dbl_weight"));
					item = wworkImpl;
				}
				else
				{
					item = new WorkflowWorkImpl();
				}

				item.setCase(workflowCase);
				item.setUUID(jrs.getString("vc_uuid"));

				ownerProcess = workflowPackage.findWorkflowProcess(vc_owner_processId);
				item.setActivity(activity);

				item.setArriveTime(DateTools.toJavaDate(jrs.getTimestamp("dat_arriveTime")));
				item.setAcceptTime(DateTools.toJavaDate(jrs.getTimestamp("dat_acceptTime")));
				item.setDispatchTime(DateTools.toJavaDate(jrs.getTimestamp("dat_dispatchTime")));

				item.setPerformer(
					gCaseDatabaseImpl.getWorkflowTemplate().getUserDatabase().findParticipant(workflowsession, vc_performer));

				//System.err.println("setPerformer ==>"+vc_performer+","+item.getPerformer());

				item.setEventType(jrs.getInt("int_eventType"));
				item.setPriority(jrs.getInt("int_priority"));

				item.setUserObject(jrs.getString("vc_userobject"));

				if (parent_workId != null)
				{

					item.setParentWork(
						findWork_impl(
							workflowsession,
							gCaseDatabaseImpl,
							workflowProcess,
							conn,
							false,
							item.getCase(),
							parent_workId));

				}
			}
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not find workflow work");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
		return item;
	}

	String createPath_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowCase workflowCase,
		WorkflowWork fromWork,
		WorkflowWork toWork,
		String batchNo,
		int eventType)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		String pathId = null;
		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			String pkgId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();

			String transitionId = null;
			String owner_processId = null;
			WorkflowActivity fromActivity = (fromWork != null) ? fromWork.getActivity() : null;
			WorkflowActivity toActivity = (toWork != null) ? toWork.getActivity() : null;

			if (fromActivity != null && toActivity != null)
			{
				WorkflowProcess owner_process = fromActivity.getWorkflowProcess();
				owner_processId = owner_process.getUUID();

				//				System.err.println("--------------------------------------------------------------");
				//				System.err.println("owner_process : "+owner_process);
				//				System.err.println("fromActivity : "+fromActivity);
				//				System.err.println("toActivity : "+toActivity);
				//				System.err.println("--------------------------------------------------------------");

				WorkflowTransition transition = owner_process.findTransition(fromActivity, toActivity);
				if (transition == null)
				{
					transition = owner_process.findTransition(toActivity, fromActivity);
				}

				transitionId = transition.getUUID();
			}
			pathId = caseIdGenerator.nextPathUUID(workflowCase);
			String strSQL =
				"INSERT INTO "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_PATH)
					+ "(vc_packageid,vc_processid,vc_caseId,vc_uuid,vc_from,vc_to,vc_batchNo,int_eventType,vc_owner_processid,vc_transitionId) "
					+ "VALUES(?,?,?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, pkgId);
			pstmt.setString(2, processId);
			pstmt.setString(3, caseId);
			pstmt.setString(4, pathId);
			pstmt.setString(5, (fromWork == null) ? null : fromWork.getUUID());
			pstmt.setString(6, (toWork == null) ? null : toWork.getUUID());
			pstmt.setString(7, batchNo);
			pstmt.setInt(8, eventType);
			pstmt.setString(9, owner_processId);
			pstmt.setString(10, transitionId);
			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not create workflow path");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
		return pathId;
	}

	WorkflowPath findPath_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowCase workflowCase,
		String pathId)
		throws WorkflowException
	{
		WorkflowPathImpl workflowPath = null;
		ResultSet jrs = null;
		PreparedStatement pstmt = null;

		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			WorkflowPackage workflowPackage = workflowProcess.getWorkflowPackage();
			String packageId = workflowPackage.getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();
			String strSQL =
				"SELECT * FROM " + schemaContext.getTableName(SchemaContext.SCHEMA_PATH) + " " + "WHERE  vc_uuid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, pathId);

			jrs = pstmt.executeQuery();

			if (jrs.next())
			{
				workflowPath = new WorkflowPathImpl();
				workflowPath.setUUID(jrs.getString("vc_uuid"));
				workflowPath.setBacthNo(jrs.getString("vc_batchNo"));
				workflowPath.setEventType(jrs.getInt("int_eventType"));
				workflowPath.setFromWork(
					findWork_impl(
						workflowsession,
						gCaseDatabaseImpl,
						workflowProcess,
						conn,
						false,
						workflowCase,
						jrs.getString("vc_from")));
				workflowPath.setToWork(
					findWork_impl(
						workflowsession,
						gCaseDatabaseImpl,
						workflowProcess,
						conn,
						false,
						workflowCase,
						jrs.getString("vc_to")));
			}
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not find workflow path");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
		return workflowPath;
	}

	void deletePath_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowPath workflowPath)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			WorkflowCase workflowCase = workflowPath.getFromWork().getCase();

			String packageId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();
			String strSQL = "DELETE FROM " + schemaContext.getTableName(SchemaContext.SCHEMA_PATH) + " " + "WHERE  vc_uuid=?";

			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, workflowPath.getUUID());

			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not delete workflow path");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
	}

	void createCaseEvent_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		String packageId,
		String processId,
		String caseId,
		Date opTime,
		int eventType,
		String description)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		try
		{
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();

			String strSQL =
				"INSERT INTO "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_CASE_EVENT)
					+ "(vc_packageid,vc_processid,vc_caseid,int_type,vc_operator,dat_opTime,vc_description) "
					+ "VALUES(?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, packageId);
			pstmt.setString(2, processId);
			pstmt.setString(3, caseId);
			pstmt.setInt(4, eventType);
			pstmt.setString(5, workflowsession.getParticipant().getUUID());
			pstmt.setTimestamp(6, DateTools.toSqlTimestamp(opTime));
			pstmt.setString(7, description);
			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			throw new WorkflowException(sqlex);
		}
		catch (RemoteException remoteEx)
		{
			throw new WorkflowException(remoteEx);
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
	}

	void createCaseEvent_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowCase workflowCase,
		Date opTime,
		int eventType,
		String description)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		String caseId = null;
		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			String pkgId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			caseId = workflowCase.getUUID();
			String strSQL =
				"INSERT INTO "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_CASE_EVENT)
					+ "(vc_packageid,vc_processid,vc_caseid,int_type,vc_operator,dat_opTime,vc_description) "
					+ "VALUES(?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, pkgId);
			pstmt.setString(2, processId);
			pstmt.setString(3, caseId);
			pstmt.setInt(4, eventType);
			pstmt.setString(5, workflowsession.getParticipant().getUUID());
			pstmt.setTimestamp(6, DateTools.toSqlTimestamp(opTime));
			pstmt.setString(7, description);
			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			throw new WorkflowException(sqlex);
		}
		catch (RemoteException remoteEx)
		{
			throw new WorkflowException(remoteEx);
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
	}

	public void createWorkEvent_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowWork item,
		Date opTime,
		int eventType,
		String description)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			WorkflowCase workflowCase = item.getCase();

			String pkgId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();

			String workId = item.getUUID();
			String strSQL =
				"INSERT INTO "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_WORK_EVENT)
					+ "(vc_packageid,vc_processid,vc_caseid,int_type,vc_operator,dat_opTime,vc_workid,vc_description) "
					+ "VALUES(?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, pkgId);
			pstmt.setString(2, processId);
			pstmt.setString(3, caseId);
			pstmt.setInt(4, eventType);
			pstmt.setString(5, workflowsession.getParticipant().getUUID());
			pstmt.setTimestamp(6, DateTools.toSqlTimestamp(opTime));
			pstmt.setString(7, workId);
			pstmt.setString(8, description);
			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not create workflow work event");
			ne.setRootCause(sqlex);
			throw ne;
		}
		catch (RemoteException remoteEx)
		{
			throw new WorkflowException(remoteEx);
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
	}

	void createCaseParameter_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowCase workflowCase,
		WorkflowParameter parameter)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		try
		{

			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			String packageId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();

			String strSQL =
				"INSERT INTO "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_CASE_DATAFIELD)
					+ "(vc_packageid,vc_processid,vc_caseid,vc_uuid,vc_type,vc_value) "
					+ "VALUES(?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, packageId);
			pstmt.setString(2, processId);
			pstmt.setString(3, caseId);
			pstmt.setString(4, parameter.getUUID());
			pstmt.setString(5, TypeDefinition.javaToXPDL(parameter.getType()));
			pstmt.setString(6, valueOf(parameter.getValue()));
			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not create workflow case parameter");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
	}
	public void updateCaseParameter_impl(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		boolean canRelease,
		WorkflowCase workflowCase,
		WorkflowParameter parameter)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			String packageId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();
			String paramId = parameter.getUUID();
			String strSQL =
				"UPDATE "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_CASE_DATAFIELD)
					+ " "
					+ "SET      vc_value=? "
					+ " "
					+ "WHERE    vc_packageId='"
					+ packageId
					+ "' "
					+ "AND      vc_processid='"
					+ processId
					+ "' "
					+ "AND      vc_caseid='"
					+ caseId
					+ "' "
					+ "AND      vc_uuid='"
					+ paramId
					+ "'";
			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, valueOf(parameter.getValue()));
			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not update workflow case parameter");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			if (canRelease)
			{
				JDBCUtilities.cleanupNoThrow(conn, pstmt);
			}
			else
			{
				JDBCUtilities.cleanupNoThrow(pstmt);
			}
		}
	}

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

	String createWork_magic(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		Connection conn,
		WorkflowProcess workflowProcess,
		WorkflowCase workflowCase,
		WorkflowActivity activity,
		WorkflowParticipant performer,
		Date arriveTime,
		int eventType)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;
		String workId = null;
		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();
			Store storeConfig = gCaseDatabaseImpl.getStore();

			workId = caseIdGenerator.nextWorkUUID(workflowCase);

			String pkgId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();

			String strSQL =
				"INSERT INTO "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_WORK)
					+ "(vc_packageid,vc_processid,vc_caseid,vc_owner_processid,vc_activityid,vc_uuid,vc_performer,dat_arriveTime,dat_acceptTime,int_eventType,int_priority,int_weight_flag) "
					+ " "
					+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, pkgId);
			pstmt.setString(2, processId);
			pstmt.setString(3, caseId);
			pstmt.setString(4, activity.getWorkflowProcess().getUUID());
			pstmt.setString(5, activity.getUUID());
			pstmt.setString(6, workId);
			pstmt.setString(7, valueOf(performer));
			pstmt.setTimestamp(8, DateTools.toSqlTimestamp(arriveTime));
			pstmt.setTimestamp(9, DateTools.toSqlTimestamp(arriveTime));
			pstmt.setInt(10, WorkEvent.CREATED);
			pstmt.setInt(11, WorkflowWork.DEFAULT_PRIORITY);
			pstmt.setInt(12, 0);

			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not create workflow work");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(pstmt);
		}

		return workId;
	}

	void createPath_impl(
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		WorkflowCase workflowCase,
		String fromWorkId,
		String toWorkId,
		int eventType)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;

		try
		{
			WorkflowUUIDGenerator caseIdGenerator = gCaseDatabaseImpl.getWorkflowTemplate().getUUIDGenerator();
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();

			String pkgId = workflowProcess.getWorkflowPackage().getUUID();
			String processId = workflowProcess.getUUID();
			String caseId = workflowCase.getUUID();

			String pathId = caseIdGenerator.nextPathUUID(workflowCase);

			String strSQL =
				"INSERT INTO "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_PATH)
					+ "(vc_packageid,vc_processid,vc_caseId,vc_uuid,vc_from,vc_to,int_eventType) "
					+ "VALUES(?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(strSQL);
			pstmt.setString(1, pkgId);
			pstmt.setString(2, processId);
			pstmt.setString(3, caseId);
			pstmt.setString(4, pathId);
			pstmt.setString(5, fromWorkId);
			pstmt.setString(6, toWorkId);
			pstmt.setInt(7, eventType);
			pstmt.executeUpdate();
		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not create workflow path");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(pstmt);
		}
	}

	void createCaseContext(
		WorkflowSession workflowsession,
		WorkflowCaseDatabaseImpl gCaseDatabaseImpl,
		WorkflowProcess workflowProcess,
		Connection conn,
		String caseId)
		throws WorkflowException
	{
		PreparedStatement pstmt = null;

		try
		{
			SchemaContext schemaContext = gCaseDatabaseImpl.getSchemaContext();

			WorkflowPackage workflowPackage = workflowProcess.getWorkflowPackage();
			String pkgId = workflowPackage.getUUID();
			String processId = workflowProcess.getUUID();

			String strSQL =
				"INSERT INTO "
					+ schemaContext.getTableName(SchemaContext.SCHEMA_CASE_DATAFIELD)
					+ "(vc_packageid,vc_processid,vc_caseid,vc_uuid,vc_type,vc_value) "
					+ "VALUES(?,?,?,?,?,?)";

			List paramList = workflowPackage.getDataFields();

			int sizeParam = paramList.size();
			if (sizeParam > 0)
			{
				pstmt = conn.prepareStatement(strSQL);

				for (int i = 0; i < sizeParam; ++i)
				{
					WorkflowParameter parameter = (WorkflowParameter) paramList.get(i);
					pstmt.setString(1, pkgId);
					pstmt.setString(2, processId);
					pstmt.setString(3, caseId);

					pstmt.setString(4, parameter.getUUID());
					pstmt.setString(5, TypeDefinition.javaToXPDL(parameter.getType()));
					pstmt.setString(6, valueOf(parameter.getValue()));

					pstmt.addBatch();
				}

				pstmt.executeBatch();
			}

		}
		catch (SQLException sqlex)
		{
			WorkflowException ne = new WorkflowException("can not create workflow case context");
			ne.setRootCause(sqlex);
			throw ne;
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(pstmt);
		}
	}

	//---------------------------------------------------------------
	//---------------------------------------------------------------
	String valueOf(Object obj)
	{
		return (obj == null) ? "" : obj.toString();
	}

	String valueOf(WorkflowSimpleComponent simpleComponent)
	{
		if (simpleComponent == null)
		{
			return null;
		}

		return simpleComponent.getUUID();
	}
}
