//----------------------------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.jdbc;

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

import org.huihoo.workflow.coreImpl.xpdl.WorkflowApplicationImpl;
import org.huihoo.workflow.coreImpl.xpdl.WorkflowPackageHeaderImpl;
import org.huihoo.workflow.xpdl.WorkflowApplication;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowPackageHeader;
import org.huihoo.workflow.xpdl.WorkflowParameter;
import org.huihoo.workflow.xpdl.WorkflowScript;
import org.huihoo.workflow.xpdl.XPDLSchema;
import org.huihoo.workflow.xpdl.util.TypeDefinition;

import org.huihoo.commons.jdbc.JDBCUtilities;

public class WorkflowPackageParser
{
	public static void parseWorkflowPackage(Connection conn,WorkflowPackage workflowPackage)
	throws SQLException
	{
		parsePackageHeader(conn,workflowPackage);
		parseApplications(conn,workflowPackage);
		parseDataFields(conn,workflowPackage);
		parseScript(conn,workflowPackage);
		
		ExtendedAttributesParser.parseExtendedAttributes(conn,workflowPackage,workflowPackage);
	}
		
	protected static void parsePackageHeader(Connection conn, WorkflowPackage workflowPackage) throws SQLException
	{
		Statement stmt = null;
		try
		{
			String strSQL = "SELECT *  FROM " + XPDLSchema.SCHEMA_PACKAGEHEADER + " WHERE    vc_packageId='" + workflowPackage.getUUID() + "'";
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(strSQL);
			if(rs.next())
			{
				WorkflowPackageHeader comp = new WorkflowPackageHeaderImpl();
							
				comp.setXPDLVersion(rs.getString("vc_xpdlversion"));
				comp.setVendor(rs.getString("vc_vendor"));
				comp.setCreationTime(rs.getDate("dat_created"));
				comp.setDescription(rs.getString("vc_description"));
				
				workflowPackage.setPackageHeader(comp);
				
			}
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(stmt);
		}
	}
	

	protected static void parseApplications(Connection conn, WorkflowPackage workflowPackage) throws SQLException
	{
		
		Statement stmt = null;
		try
		{
			String strSQL = "SELECT *  FROM " + XPDLSchema.SCHEMA_APPLICATION + " WHERE    vc_packageId='" + workflowPackage.getUUID() + "'";
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(strSQL);
			if(rs.next())
			{
				WorkflowApplication comp = new WorkflowApplicationImpl();
					
				comp.setUUID(rs.getString("vc_uuid"));
				comp.setName(rs.getString("vc_name"));
				comp.setDescription(rs.getString("vc_description"));
			
				workflowPackage.addApplication(comp);
				FormalParametersParser.parseFormalParameters(conn,workflowPackage,comp);
				ExtendedAttributesParser.parseExtendedAttributes(conn,workflowPackage,comp);
			}
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(stmt);
		}
	}
	
	protected static void parseDataFields(Connection conn, WorkflowPackage workflowPackage) throws SQLException
	{
		
		Statement stmt = null;
		try
		{
			String strSQL = "SELECT *  FROM " + XPDLSchema.SCHEMA_DATAFIELD + "  WHERE    vc_packageId='" + workflowPackage.getUUID() + "'";
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(strSQL);
			if(rs.next())
			{
				WorkflowParameter comp = new WorkflowParameter();
					
				comp.setUUID(rs.getString("vc_uuid"));
				comp.setName(rs.getString("vc_name"));
				comp.setType(TypeDefinition.XPDLToJava(rs.getString("vc_type")));
				comp.setValue(rs.getString("vc_value"));
				comp.setDescription(rs.getString("vc_description"));
				
				workflowPackage.addDataField(comp);	
				ExtendedAttributesParser.parseExtendedAttributes(conn,workflowPackage,comp);
				
			}
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(stmt);
		}
	}
	protected static void parseScript(Connection conn, WorkflowPackage workflowPackage) throws SQLException
	{		
		Statement stmt = null;
		try
		{
			String strSQL = "SELECT *  FROM " + XPDLSchema.SCHEMA_SCRIPT + "  WHERE    vc_packageId='" + workflowPackage.getUUID() + "'";
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(strSQL);
			if(rs.next())
			{
				WorkflowScript comp = new WorkflowScript();
						
				comp.setGrammar(rs.getString("vc_grammar"));
				comp.setType(rs.getString("vc_type"));
				comp.setVersion(rs.getString("vc_version"));
				
				workflowPackage.setScript(comp);
			}
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(stmt);
		}
	}
}
