package com.cim.idm.wmsextend.generic.util;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.util.sys.SystemPropHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PolicyUtil {
	private static Log logger = LogFactory.getLog(PolicyUtil.class);
	public static String NEWLINE = SystemPropHelper.CR;
	/*
	* Name : getMachineRecipeName1
	* Desc : This function is getMachineRecipeName
	* Author : ZXY Systems, Inc
	* Date : 2013.01.19
	*/
	public static String getMachineRecipeName1(String factoryName, 
											   String productSpecName,
											   String processOperationName,
											   String machineName)
	{
		String sqlStmt =  " SELECT MACHINERECIPENAME " + NEWLINE
		                + "   FROM POSMACHINERECIPE " + NEWLINE
		                + "  WHERE CONDITIONID = " + NEWLINE
			            + "      ( SELECT CONDITIONID " + NEWLINE
		                + "          FROM TPOMPOLICY " + NEWLINE
		                + "         WHERE FACTORYNAME = ? " + NEWLINE
		                + "           AND PRODUCTSPECNAME = ? " + NEWLINE
		                + "           AND PROCESSOPERATIONNAME = ? " + NEWLINE
		                + "           AND MACHINENAME = ?) " + NEWLINE;
		
		Object[] bindSet = new String[]{factoryName, productSpecName, processOperationName, machineName};
		
		String[][] sqlResult = IDMFrameServiceProxy.getSqlTemplate().queryForStringArray(sqlStmt, bindSet);
		
		if(sqlResult.length > 0 && sqlResult[0].length > 0)
			return sqlResult[0][0];
		else 
			return "";
	}
	/*
	* Name : getMachineRecipeName
	* Desc : This function is getMachineRecipeName
	* Author : ZXY Systems, Inc
	* Date : 2013.01.19
	*/
	public static String getMachineRecipeName(String factoryName, 
											  String productSpecName, 
											  String processFlowName, 
											  String processOperationName, 
											  String machineName)
	{
		String sqlStmt =  " SELECT MACHINERECIPENAME " + NEWLINE
		                + "   FROM POSMACHINERECIPE " + NEWLINE
		                + "  WHERE CONDITIONID = " + NEWLINE
			            + "      (SELECT CONDITIONID " + NEWLINE
		                + "   		FROM TPFOMPOLICY " + NEWLINE
		                + "  		WHERE FACTORYNAME = ? " + NEWLINE
		                + "  		  AND PRODUCTSPECNAME = ? " + NEWLINE
		                + "  		  AND PROCESSFLOWNAME = ? " + NEWLINE
		                + "   		  AND PROCESSOPERATIONNAME = ? " + NEWLINE
		                + "  		  AND MACHINENAME = ?) " + NEWLINE;
		
		Object[] bindSet = new String[]{factoryName, productSpecName, processFlowName, processOperationName, machineName};
		
		String[][] sqlResult = IDMFrameServiceProxy.getSqlTemplate().queryForStringArray(sqlStmt, bindSet);
		
		if(sqlResult.length > 0 && sqlResult[0].length > 0)
			return sqlResult[0][0];
		else 
			return "";
	}
	/*
	* Name : getAvailableMachine
	* Desc : This function is getAvailableMachine
	* Author : ZXY Systems, Inc
	* Date : 2013.01.19
	*/
	public static String[][] getAvailableMachine(String factoryName,
											 String productSpecName, 
											 String processFlowName, 
											 String processOperationName)
	{
		String sqlStmt =  " SELECT MACHINENAME " + NEWLINE 
						+ "   FROM POSMACHINE " + NEWLINE
						+ "  WHERE CONDITIONID = " + NEWLINE 
						+ "      ( SELECT CONDITIONID " + NEWLINE 
						+ "          FROM TPFOPOLICY " + NEWLINE
						+ "   	    WHERE FACTORYNAME= ?" + NEWLINE 
						+ "           AND PRODUCTSPECNAME = ? " + NEWLINE
						+ "           AND PROCESSFLOWNAME = ? " + NEWLINE
						+ "           AND PROCESSOPERATIONNAME= ? " + NEWLINE;
		
		Object[] bindSet = new String[]{factoryName, productSpecName, processFlowName, processOperationName};
		String[][] sqlResult = IDMFrameServiceProxy.getSqlTemplate().queryForStringArray(sqlStmt, bindSet);
		
		if(sqlResult.length > 0 && sqlResult[0].length > 0)
			return sqlResult;
		else 
			return null;
	}
	/*
	* Name : getAvailableMachineTest
	* Desc : This function is getAvailableMachineTest
	* Author : ZXY Systems, Inc
	* Date : 2013.01.19
	*/
	public static String[] getAvailableMachineTest(String factoryName,
												   String productSpecName, 
												   String processFlowName, 
												   String processOperationName)
	{
		String sqlStmt =  " SELECT MACHINENAME " + NEWLINE 
						+ "   FROM POSMACHINE " + NEWLINE
						+ "  WHERE CONDITIONID = " + NEWLINE 
						+ "      ( SELECT CONDITIONID " + NEWLINE 
						+ "          FROM TPFOPOLICY " + NEWLINE
						+ "   	    WHERE FACTORYNAME= ?" + NEWLINE 
						+ "           AND PRODUCTSPECNAME = ? " + NEWLINE
						+ "           AND PROCESSOPERATIONNAME= ? " + NEWLINE;
		
		Object[] bindSet = new String[]{factoryName, productSpecName, processOperationName};
		String[][] sqlResult = IDMFrameServiceProxy.getSqlTemplate().queryForStringArray(sqlStmt, bindSet);
		
		if(sqlResult != null)
		{
			if(sqlResult.length > 0)
			{
				String[] returnMachineList = new String[sqlResult.length];
				for(int i = 0;i < returnMachineList.length; i++)
				{
					returnMachineList[i] = sqlResult[i][0];
				}
				return returnMachineList;
			}
			return new String[0];
		}
		return null;
	}
	/*
	* Name : getProductSample
	* Desc : This function is getProductSample
	* Author : ZXY Systems, Inc
	* Date : 2013.01.19
	*/
	public static String getProductSample(String factoryName, 
			   							  String productSpecName, 
			   							  String processFlowName, 
			   							  String processOperationName)
	{
		String sqlStmt =  " SELECT SAMPLEPOSITION " + NEWLINE
						+ "   FROM POSPRODUCTSAMPLE " + NEWLINE
						+ "  WHERE CONDITIONID = " + NEWLINE
						+ "      (SELECT CONDITIONID " + NEWLINE
						+ "   		FROM TPFOPOLICY " + NEWLINE
						+ "  		WHERE FACTORYNAME = ? " + NEWLINE
						+ "  		  AND PRODUCTSPECNAME = ? " + NEWLINE
						+ "  		  AND PROCESSFLOWNAME = ? " + NEWLINE
						+ "   		  AND PROCESSOPERATIONNAME = ? " + NEWLINE;
		
		Object[] bindSet = new String[]{factoryName, productSpecName, processFlowName, processOperationName};
		
		String[][] sqlResult = IDMFrameServiceProxy.getSqlTemplate().queryForStringArray(sqlStmt, bindSet);
		
		if(sqlResult.length > 0 && sqlResult[0].length > 0)
		return sqlResult[0][0];
		else 
		return "";
	}
	/*
	* Name : getDynamicGlassJudge
	* Desc : This function is getDynamicGlassJudge
	* Author : ZXY Systems, Inc
	* Date : 2013.01.19
	*/
	public static String getDynamicGlassJudge(String factoryName, 
											  String productSpecName)
	{
		String sqlStmt =  " SELECT JUDGELIMIT " + NEWLINE
						+ "   FROM POSDYNAMICGLASSJUDGE " + NEWLINE
						+ "  WHERE CONDITIONID = " + NEWLINE
						+ "      (SELECT CONDITIONID " + NEWLINE
						+ "   		FROM TPPOLICY " + NEWLINE
						+ "  	   WHERE FACTORYNAME = ? " + NEWLINE
						+ "  		 AND PRODUCTSPECNAME = ? " + NEWLINE;
		
		Object[] bindSet = new String[]{factoryName, productSpecName};
		
		String[][] sqlResult = IDMFrameServiceProxy.getSqlTemplate().queryForStringArray(sqlStmt, bindSet);
		
		if(sqlResult.length > 0 && sqlResult[0].length > 0)
			return sqlResult[0][0];
		else 
			return "";
	}
	/*
	* Name : getDynamicGlassJudge
	* Desc : This function is getDynamicGlassJudge
	* Author : ZXY Systems, Inc
	* Date : 2013.01.19
	*/
	public static String getDynamicGlassJudge(String factoryName, 
										  	  String productSpecName, 
										  	  String processFlowName, 
										  	  String processOperationName)
	{
		String sqlStmt =  " SELECT JUDGELIMIT " + NEWLINE
						+ "   FROM POSDYNAMICGLASSJUDGE " + NEWLINE
						+ "  WHERE CONDITIONID = " + NEWLINE
						+ "      (SELECT CONDITIONID " + NEWLINE
						+ "   		FROM TPFOPOLICY " + NEWLINE
						+ "  		WHERE FACTORYNAME = ? " + NEWLINE
						+ "  		  AND PRODUCTSPECNAME = ? " + NEWLINE
						+ "  		  AND PROCESSFLOWNAME = ? " + NEWLINE
						+ "   		  AND PROCESSOPERATIONNAME = ? " + NEWLINE;
		
		Object[] bindSet = new String[]{factoryName, productSpecName, processFlowName, processOperationName};
		
		String[][] sqlResult = IDMFrameServiceProxy.getSqlTemplate().queryForStringArray(sqlStmt, bindSet);
		
		if(sqlResult.length > 0 && sqlResult[0].length > 0)
			return sqlResult[0][0];
		else 
			return "";
	}
	/*
	* Name : getDCSpecPrefix
	* Desc : This function is getDCSpecPrefix
	* Author : ZXY Systems, Inc
	* Date : 2013.01.19
	*/
	public static String[][] getDCSpecPrefix(String factoryName, 
									      	 String productSpecName, 
									      	 String processFlowName, 
									      	 String processOperationName, 
									      	 String machineName)
	{
		String sqlStmt =  " SELECT ITEMPREFIX, SITEPREFIX " + NEWLINE
						+ "   FROM POSDCSPECPREFIX " + NEWLINE
						+ "  WHERE CONDITIONID = " + NEWLINE
						+ "      (SELECT CONDITIONID " + NEWLINE
						+ "   		FROM TPFOMPOLICY " + NEWLINE
						+ "  		WHERE FACTORYNAME = ? " + NEWLINE
						+ "  		  AND PRODUCTSPECNAME = ? " + NEWLINE
						+ "  		  AND PROCESSFLOWNAME = ? " + NEWLINE
						+ "   		  AND PROCESSOPERATIONNAME = ? " + NEWLINE
						+ "  		  AND MACHINENAME = ?) " + NEWLINE;
		
		Object[] bindSet = new String[]{factoryName, productSpecName, processFlowName, processOperationName, machineName};
		
		String[][] sqlResult = IDMFrameServiceProxy.getSqlTemplate().queryForStringArray(sqlStmt, bindSet);
		
		if(sqlResult.length > 0 && sqlResult[0].length > 0)
			return sqlResult;
		else 
			return null;
	}
}
