package ibase.webitm.ejb.mfg.adv;
import java.rmi.RemoteException;
import java.util.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import org.w3c.dom.*;

import javax.ejb.*;
import javax.naming.InitialContext;

import ibase.system.config.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class DefaultQcTab extends ActionHandlerEJB implements  DefaultQcTabLocal , DefaultQcTabRemote //SessionBean
{	
	
	/*public void ejbCreate() throws RemoteException, CreateException {}
	public void ejbRemove(){}
	public void ejbActivate(){}
	public void ejbPassivate(){}*/
    public String actionHandler() throws RemoteException,ITMException{return "";}
	public String actionHandler(String actionType, String xmlString, String objContext, String xtraParams) throws RemoteException,ITMException
	{
		String retString = "";
		Document dom = null;
		try
		{
			GenericUtility genericUtility = GenericUtility.getInstance();
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				System.out.println("XML String :"+xmlString);
				dom = genericUtility.parseString(xmlString);
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception :DefaultQcTabEJB :actionHandler(String xmlString):" + e.getMessage() + ":");
			throw new ITMException(e);
		}
		return retString;
	}

	public String actionHandler(String actionType, String xmlString, String xmlString1, String objContext, String xtraParams) throws RemoteException,ITMException
	{
		Connection conn=null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		Document dom = null;
		Document dom1 = null;
		String  retString = null;
		try
		{
			System.out.println("XML String1:::"+xmlString1);
			System.out.println("XML String:::"+xmlString);
			System.out.println("Default function executing in progress...................");
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				System.out.println("XML String:::"+xmlString);
				dom = GenericUtility.getInstance().parseString(xmlString); 
				System.out.println("dom :"+dom);
			}
			if(xmlString1 != null && xmlString1.trim().length()!=0)
			{
				System.out.println("XML String1:::"+xmlString1);
				dom1 = GenericUtility.getInstance().parseString(xmlString1);
				System.out.println("dom1 :"+dom1);
			}
			System.out.println("actionType:::["+actionType+"]");
			
			if (actionType.equalsIgnoreCase("Default"))
			{
					try
					{
						if(conn==null)
						{
							System.out.println("Getting Connection From Database......");
							ConnDriver connDriver = new ConnDriver();
							conn = connDriver.getConnectDB("DriverITM");
							conn.setAutoCommit(false);
							System.out.println("Connection Established......");
							DatabaseMetaData dbmd = conn.getMetaData();
							System.out.println("DriverName["+dbmd.getDriverName() );
							System.out.println("DriverURI["+dbmd.getURL()  );
							System.out.println("DriverUSER["+dbmd.getUserName());
							//System.out.println("ApplDateFormat["+genericUtility. ());
							System.out.println("DBDateFormat["+genericUtility.getDBDateFormat() );
							connDriver = null;
						}
					}
					catch (Exception e)
					{
						e.printStackTrace();
					}
					retString = actionDefault(dom, dom1, objContext, xtraParams);
			}
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :Dispatch :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning String from DefaultQcTabdEJB : actionHandler"+retString);
	    return retString;
	}
	private String actionDefault(Document dom, Document dom1, String objContext, String xtraParams) throws RemoteException, ITMException
	{
		System.out.println("Calling the Default function.........");
		GenericUtility genericUtility = GenericUtility.getInstance();
		Connection conn = null;
		PreparedStatement pstmt = null,pstmt1 = null , pstmtSpec = null ;
		ResultSet rs = null,rs1 = null,rsSpec = null;
		StringBuffer valueXmlString = null;
		valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");	
		int currentFormNo = 0;
		String sql1 = "";
		String qcTableNo = "";
		String tableDescr = "" ;
		Timestamp exprDate = null;
		Timestamp chgDate = null;
		String chgUser = "";
		String chgTerm = "";
		int lineNo = 0 ;
		String specCode = "";
		String expctedRes = "";
		String sqlSpec = "";
		String descr = "";
		try
		{	
			String detCnt = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"detCnt");
			System.out.println("detCnt ...... :"+detCnt);
			if(conn==null)
			{
				ConnDriver connDriver = new ConnDriver();
				conn = connDriver.getConnectDB("DriverITM");
				connDriver=null;
			}
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			System.out.println("[] :currentFormNo ....." +currentFormNo);
			if (dom1 != null)
			{
				qcTableNo = genericUtility.getColumnValue("qc_tableno",dom1);
				qcTableNo = qcTableNo == null ? "" :qcTableNo.trim();
			
			}
			System.out.println("qcTableNo>>>>>>>"+qcTableNo);
			
			sql1 = "SELECT b.QC_TABLENO ,b.LINE_NO , b.SPEC_CODE , b.EXPECTED_RESULT FROM QC_TABLES a, QC_TABLEDET b where  a.QC_TABLENO = b.QC_TABLENO  and b.QC_TABLENO = '"+qcTableNo+"' ";
			System.out.println("sql1 is :"+sql1);
		   pstmt = conn.prepareStatement(sql1);	
		   rs = pstmt.executeQuery();
			while (rs.next())
			{
				qcTableNo	=	rs.getString(1) == null ? "" :rs.getString(1);				
				lineNo	=	rs.getInt(2);
				specCode	=	rs.getString(3) == null ? "" :rs.getString(3);
				expctedRes	=	rs.getString(4) == null ? "" :rs.getString(4);					
				sqlSpec =  "SELECT SPEC_DESCR FROM  QLTY_SPECS  WHERE  SPEC_CODE ='"+specCode.trim()+"' " ;
				pstmtSpec = conn.prepareStatement(sqlSpec);	
				rsSpec = pstmtSpec.executeQuery();
				if(rsSpec.next())
				{
					descr	=	rsSpec.getString(1) == null ? "" :rsSpec.getString(1);
				
				}				
				rsSpec.close();
				rsSpec = null;
				pstmtSpec.close();
				pstmtSpec = null;
				valueXmlString.append("<Detail>\r\n");
				valueXmlString.append("<qc_tableno>").append("<![CDATA[").append(qcTableNo.trim()).append("]]>").append("</qc_tableno>\r\n");
				valueXmlString.append("<line_no>").append("<![CDATA[").append(lineNo).append("]]>").append("</line_no>\r\n");
				valueXmlString.append("<spec_code>").append("<![CDATA[").append(specCode).append("]]>").append("</spec_code>\r\n");
				valueXmlString.append("<qlty_specs_spec_descr>").append("<![CDATA[").append(descr).append("]]>").append("</qlty_specs_spec_descr>\r\n");
				valueXmlString.append("<expected_result>").append("<![CDATA[").append(expctedRes).append("]]>").append("</expected_result>\r\n");
				valueXmlString.append("</Detail>\r\n");	
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;		
			valueXmlString.append("</Root>\r\n");					
		}//tryBase1 greyd			
		catch(SQLException sqx)
		{
			System.out.println("The Exception caught from DefaultQcTabEJB :"+sqx);
			throw new ITMException(sqx);
		}
		catch(Exception e)
		{
			System.out.println("The Exception caught from DefaultQcTabEJB :"+e);
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				conn.close();
				conn = null;
				
			}catch(Exception e){}
		}
		System.out.println("valueXmlString.toString() :"+valueXmlString.toString());
		return valueXmlString.toString();
	}

	
}//end of Ejb