/*
 * Created on Mar 6, 2007
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ibase.webitm.ejb.mfg;

import ibase.webitm.utility.GenericUtility;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

/**
 * @author ashah
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MfgCommon {
	public String chkStkOpt(String siteCode, String itemCode, Connection conn) {
		String stkOpt = "", sql = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try
		{
			sql = " SELECT CASE WHEN STK_OPT IS NULL THEN 'N' ELSE STK_OPT END FROM SITEITEM " 
				+ " WHERE ITEM_CODE = ? AND SITE_CODE = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, itemCode);
			pstmt.setString(2, siteCode);
			System.out.println("Sql ::: " + sql);
			System.out.println("Setting params -- itemCode :: " + itemCode + " siteCode :: " + siteCode);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				stkOpt = rs.getString(1);
				System.out.println("stkOpt in chkStkOpt ::: " + stkOpt);
			}
			pstmt.close();
			rs.close();
			if (stkOpt.equalsIgnoreCase("N") || stkOpt == null)
			{
				sql = "SELECT CASE WHEN STK_OPT IS NULL THEN '0' ELSE STK_OPT END FROM ITEM WHERE ITEM_CODE = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, itemCode);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					stkOpt = rs.getString(1);
				}
				pstmt.close();
				rs.close();
			}
		}
		catch(Exception e)
		{}
		System.out.println("stkOpt in chkStkOpt :: " + stkOpt);
		return stkOpt;
		
	}

	public String chkQcReqd(String siteCode, String itemCode, Connection conn) {
		String qcReqd = "", sql = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try
		{
			System.out.println("In chkQcReqd :--: siteCode :: " + siteCode + " itemCode :: " + itemCode);
			sql = " SELECT CASE WHEN QC_REQD IS NULL THEN 'N' ELSE QC_REQD END FROM SITEITEM " 
				+ " WHERE ITEM_CODE = ? AND SITE_CODE = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, itemCode);
			pstmt.setString(2, siteCode);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				qcReqd = rs.getString(1);
			}
			pstmt.close();
			rs.close();
			if (qcReqd.equalsIgnoreCase("N") || qcReqd == null)
			{
				sql = "SELECT CASE WHEN QC_REQD IS NULL THEN 'N' ELSE QC_REQD END FROM ITEM WHERE ITEM_CODE = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, itemCode);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					qcReqd = rs.getString(1);
				}
				pstmt.close();
				rs.close();
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception in chkQcReqd" + e.getMessage());
		}
		return qcReqd;
	}
	
	public String chkAutoQcReqd(String siteCode, String itemCode, Connection conn) {
		String autoQcReqd = "", sql = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try
		{
			System.out.println("In chkAutoQcReqd :--: siteCode :: " + siteCode + " itemCode :: " + itemCode);
			sql = " SELECT CASE WHEN AUTO_REQC IS NULL THEN 'Y' ELSE AUTO_REQC END FROM SITEITEM " 
				+ " WHERE ITEM_CODE = ? AND SITE_CODE = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, itemCode);
			pstmt.setString(2, siteCode);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				autoQcReqd = rs.getString(1);
			}
			pstmt.close();
			rs.close();
			if (autoQcReqd.equalsIgnoreCase("N") || autoQcReqd == null)
			{
				sql = "SELECT CASE WHEN AUTO_REQC IS NULL THEN 'Y' ELSE AUTO_REQC END FROM ITEM WHERE ITEM_CODE = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, itemCode);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					autoQcReqd = rs.getString(1);
				}
				pstmt.close();
				rs.close();
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception in chkAutoQcReqd" + e.getMessage());
		}
		return autoQcReqd;
	}

	private Timestamp getTimestampFormat(String stDate) {
		Timestamp tsDate = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
			try {
				System.out.println(" Before converting into Timestamp ::: " + stDate);
				Object date = null;
				SimpleDateFormat sdfApp = new SimpleDateFormat(genericUtility.getApplDateFormat());
				date = sdfApp.parse(stDate);
				SimpleDateFormat sdfDB = new SimpleDateFormat(genericUtility.getDBDateFormat());
				tsDate = java.sql.Timestamp.valueOf(sdfDB.format(date).toString() + " 00:00:00.0");
			} catch (Exception e) {
				System.out.println("Exception in getTimestampFormat" + e.getMessage());
			}
			System.out.println(" After converted into Timestamp ::: " + tsDate);
			return tsDate;
	}
	
	public String getEnvMfg(String prdCode, String varName, Connection conn)
	  {
			String varValue = "";
			String sql = "";
			ResultSet rs = null;
			PreparedStatement pstmt = null;
			try
			{
				System.out.println("In getEnvMfg ");
				sql= "SELECT VAR_VALUE FROM MFGPARM WHERE PRD_CODE = ? AND VAR_NAME = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, prdCode);
				pstmt.setString(2, varName);
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					varValue =rs.getString(1);
					System.out.println("In getEnvMfg varValue..." + varValue);
				}
				else
				{
					System.out.println("No record found for PRD_CODE ='"+prdCode+"' AND VAR_NAME ='"+varName+"'");
					varValue = "NULLFOUND";
				}
				pstmt.close();
				rs.close();
			}
			catch(Exception e)
			{
				System.out.println("Exception in ..getEnvMfg....");
				e.printStackTrace();
			}
			return varValue;
	  }
}
