//----------------------------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.lang.reflect.Method;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.huihoo.workflow.WorkflowComplexComponent;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.XPDLSchema;
import org.huihoo.workflow.xpdl.XPDLScope;
import org.huihoo.workflow.xpdl.parameter.ActualParameter;

import org.huihoo.commons.jdbc.JDBCUtilities;

public class ActualParametersParser
{
	public static void parseActualParameters(
		Connection conn,
		WorkflowPackage workflowPackage,
		WorkflowComplexComponent comp)
		throws SQLException
	{
		String strSQL =
			"SELECT *  "
				+ "FROM "
				+ XPDLSchema.SCHEMA_ACTUALPARAMETER
				+ "WHERE    vc_scope='"
				+ XPDLScope.extractScope(comp)
				+ "' AND vc_compid = '"
				+ comp.getUUID()
				+ "' "
				+ "ORDER BY int_index";
		Statement stmt = null;
		try
		{
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(strSQL);
			while (rs.next())
			{

				ActualParameter acp = new ActualParameter(rs.getString("vc_name"));

				Class paramTypes[] = new Class[1];
				paramTypes[0] = acp.getClass();

				Object paramValues[] = new Object[1];
				paramValues[0] = acp;

				try
				{
					Method method = comp.getClass().getMethod("addActualParameter", paramTypes);
					method.invoke(comp, paramValues);
				}
				catch (Throwable t)
				{
					;
				}
			}
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(stmt);
		}
	}
}
