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.WorkflowComplexComponent;
import org.huihoo.workflow.xpdl.ExtendedAttribute;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.XPDLSchema;
import org.huihoo.workflow.xpdl.XPDLScope;

import org.huihoo.commons.jdbc.JDBCUtilities;

public class ExtendedAttributesParser
{
	public static void parseExtendedAttributes(
		Connection conn,
		WorkflowPackage workflowPackage,
		WorkflowComplexComponent comp)
		throws SQLException
	{
		String strSQL =
			"SELECT *  "
				+ "FROM "
				+ XPDLSchema.SCHEMA_EXTENDEDATTRIBUTE
				+ " "
				+ "WHERE    vc_scope='"
				+ XPDLScope.extractScope(comp)
				+ "' AND vc_compId = '"
				+ comp.getUUID()
				+ "'";

		Statement stmt = null;
		try
		{
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(strSQL);
			while (rs.next())
			{
				String name = rs.getString("vc_name");
				if (name != null && name.length() > 0)
				{
					ExtendedAttribute ext = new ExtendedAttribute();
					ext.setName(name);
					ext.setValue(rs.getString("vc_value"));
					comp.addExtendedAttribute(ext);
				}
			}
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(stmt);
		}
	}
}
