//----------------------------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.xpdl.parser;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.huihoo.commons.jdbc.JDBCFactory;
import org.huihoo.commons.jdbc.JDBCUtilities;
import org.huihoo.commons.jdbc.Store;
import org.huihoo.workflow.coreImpl.xpdl.WorkflowPackageImpl;
import org.huihoo.workflow.coreImpl.xpdl.WorkflowProcessImpl;
import org.huihoo.workflow.coreImpl.xpdl.parser.jdbc.WorkflowPackageParser;
import org.huihoo.workflow.coreImpl.xpdl.parser.jdbc.WorkflowProcessParser;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowProcess;
import org.huihoo.workflow.xpdl.XPDLSchema;
import org.huihoo.workflow.xpdl.parser.XPDLParseException;
import org.huihoo.workflow.xpdl.parser.XPDLParserTools;

public class XPDLParserJDBC
{	
	public static WorkflowPackage parseJDBC(DefaultXPDLParser parser, Store store) throws XPDLParseException
	{
		Connection conn = null;
		WorkflowPackage workflowPackage = null;

		try
		{
			conn = JDBCFactory.getConnection(store);
			workflowPackage = parseWorkflowPackage(conn, store.getProperty(org.huihoo.workflow.WorkflowGlobals.PACKAGE_ID_KEY));
			parseWorkflowProcesses(conn,workflowPackage);
		}
		catch (Exception ex)
		{
			XPDLParseException ne = new XPDLParseException("parse XPDL from JDBC");
			ne.setRootCause(ex);

			throw ne;
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(conn);
		}
		
		if (parser.isXPDLValidating())
		{
			parser.errorList = XPDLParserTools.internalValidate(workflowPackage);
			XPDLParserTools.dumpErrorList(parser.errorList,System.err);
		}
		XPDLParserTools.internalFormat(workflowPackage);
		
		return workflowPackage;
	}

	protected static WorkflowPackage parseWorkflowPackage(Connection conn, String packageID) throws SQLException
	{
		WorkflowPackage comp =null;
		Statement stmt = null;
		try
		{
			String strSQL = "SELECT *  FROM " + XPDLSchema.SCHEMA_PACKAGE + "  WHERE    vc_uuId='" + packageID + "'";
			
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(strSQL);
			if(rs.next())
			{
				comp = new WorkflowPackageImpl();
				comp.setUUID(packageID);
				comp.setName(rs.getString("vc_name"));
			}
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(stmt);
		}
		
		WorkflowPackageParser.parseWorkflowPackage(conn,comp);
		
		return comp;
	}

	protected static void parseWorkflowProcesses(Connection conn, WorkflowPackage workflowPackage) throws SQLException
	{	
		Statement stmt = null;
		try
		{
			String strSQL = "SELECT *  FROM " + XPDLSchema.SCHEMA_PROCESS + "  "+"WHERE    vc_packageId='" + workflowPackage.getUUID() + "'";
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(strSQL);
			while(rs.next())
			{
				WorkflowProcess comp = new WorkflowProcessImpl();
				comp.setName(rs.getString("vc_name"));
				comp.setUUID(rs.getString("vc_uuid"));
				
				workflowPackage.addWorkflowProcess(comp);				
				WorkflowProcessParser.parseWorkflowProcess(conn,workflowPackage,comp);
			}
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(stmt);
		}
	}
}
