//----------------------------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.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.XPDLSchema;
import org.huihoo.workflow.xpdl.activity.SubFlowImplementation;
import org.huihoo.workflow.xpdl.activity.ToolImplementation;

import org.huihoo.commons.jdbc.JDBCUtilities;

public class ImplementationParse
{
	public static ToolImplementation parseToolImplementation(Connection conn,WorkflowPackage workflowPackage,String implID)
	throws SQLException
	{
		ToolImplementation toolImpl = null;
		String strSQL =
			"SELECT *  "
				+ "FROM "
				+ XPDLSchema.SCHEMA_TOOL
				+ "WHERE    vc_package='"
				+ workflowPackage.getUUID()
				+ "' AND vc_uuid = '"
				+ implID
				+ "' ";
		Statement stmt = null;
		try
		{
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(strSQL);
			if (rs.next())
			{
				toolImpl = new ToolImplementation();
				toolImpl.setDescription(rs.getString("vc_description"));
				toolImpl.setUUID(rs.getString("vc_uuid"));
				toolImpl.setValue(rs.getString("vc_appid"));
			}
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(stmt);
		}
		
		ActualParametersParser.parseActualParameters(conn, workflowPackage, toolImpl);
		ExtendedAttributesParser.parseExtendedAttributes(conn, workflowPackage, toolImpl);
						
		return toolImpl;
	}
	
	public static SubFlowImplementation parseSubFlowImplementation(Connection conn,WorkflowPackage workflowPackage,String implID)
	throws SQLException
	{
		SubFlowImplementation subflowImpl = null;
		String strSQL =
			"SELECT *  "
				+ "FROM "
				+ XPDLSchema.SCHEMA_SUBFLOW
				+ "WHERE    vc_package='"
				+ workflowPackage.getUUID()
				+ "' AND vc_uuid = '"
				+ implID
				+ "' ";
		Statement stmt = null;
		try
		{
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(strSQL);
			if (rs.next())
			{
				subflowImpl = new SubFlowImplementation();
				subflowImpl.setDescription(rs.getString("vc_description"));
				subflowImpl.setUUID(rs.getString("vc_uuid"));
				subflowImpl.setValue(rs.getString("vc_processid"));
			}
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(stmt);
		}
		
		ExtendedAttributesParser.parseExtendedAttributes(conn, workflowPackage, subflowImpl);
						
		return subflowImpl;
	}
}
