package ibase.webitm.ejb.eou;
import ibase.webitm.utility.*;
import ibase.utility.CommonConstants;
import ibase.webitm.ejb.*;
import ibase.system.config.*;
import java.rmi.RemoteException;
import java.util.*;
import java.text.*;
import java.sql.*;
import javax.ejb.*;
import javax.naming.InitialContext;
import ibase.system.config.AppConnectParm;
import ibase.webitm.utility.ITMException;
import org.w3c.dom.*;
//import ibase.webitm.ejb.MasterStateful;
//import ibase.webitm.ejb.MasterStatefulHome;

@Stateless // added for ejb3
//public class ConfirmLopEJB extends ActionHandlerEJB implements SessionBean
public class ConfirmLop extends ActionHandlerEJB implements ConfirmLopLocal,ConfirmLopRemote
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	String errorString = null;
	
	/*
	public void ejbCreate() throws RemoteException, CreateException
	{
		System.out.println("Create Method Called....");
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate()
	{
	}
	public void ejbPassivate()
	{
	}
	*/
	public String actionHandler() throws RemoteException,ITMException
	{
		System.out.println("actionHandler() Method Called....");
		return "";
	}
	public String actionHandler(String xmlString, String xtraParams, String objContext) throws RemoteException,ITMException
	{
		String  retString = null;
		System.out.println("Xtra Params : " + xtraParams);
		//genericUtility = GenericUtility.getInstance();
		try
		{
			retString = actionConfirm(xmlString, xtraParams);
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from  actionHandler"+retString);
	    return (retString);
	}
	private String actionConfirm(String tranID, String xtraParams) throws RemoteException,ITMException, Exception
	{
		Connection conn = null;
		PreparedStatement pstmt = null;
		PreparedStatement Dtlpstmt = null;
		ResultSet rs = null;
		ResultSet Dtlrs = null;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		ConnDriver connDriver = new ConnDriver();
		String userId ="",loginSite ="",empCode="";
		userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
		loginSite = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
		empCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginEmpCode");
		System.out.println("empCode.......... !"+empCode);
		String sql = "";
        String retString = "";
		int count =0,i=0,upd=0;
		System.out.println("tranID ...."+tranID );
		ArrayList arrayList =new ArrayList();
		ArrayList itemList =new ArrayList();
		ArrayList arrayListItem =new ArrayList();
		HashMap hm =new HashMap();
		HashMap currentMap =new HashMap();
		HashMap itemLiscontracmap =new HashMap();
		String lsConf = "Y";
		String  itemCode ="",siteCode ="",unit ="",phyAttrib1="",phyAttrib2 ="",phyAttrib3 ="",phyAttrib4 ="",phyAttrib5 ="",phyAttrib6 ="";
		String suppSour="",itemCodenew="",returnString="",item_type="",item_ser="";
		String contract="",errorCode ="";
		HashMap stkUpdMap = null;
		double quantity = 0.0;
		double qtyTotal = 0;
		double qtySamDbSum = 0.0;
		Timestamp tranDate = null;
		double ConvQty = 0, finalTot = 0,ConvQtyTot = 0;
		String qty = "",lotSl = "",lineNo = "",locCode= "",lotNo = "",qtySample = "",convQty = "",unitStd = "",qtyStd = "",remarks = "";
		String errString = "",qorderNo = "",confirmed = "";
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			Timestamp currDate = null;
			String currAppdate = null;
			currDate = new java.sql.Timestamp(System.currentTimeMillis()) ;
			System.out.println("\n today date----" + currDate);
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginEmpCode");
			currAppdate = new SimpleDateFormat(genericUtility.getApplDateFormat()).format(currDate).toString();
			System.out.println("\n today currDate in apl----" + currAppdate);
			currAppdate = genericUtility.getValidDateString(currAppdate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
			System.out.println("\n today currDate in DB----" + currAppdate);
			currDate = Timestamp.valueOf(currAppdate + " 00:00:00.00");
			System.out.println("\n today currDate in timestamp----" + currDate);
			sql = "select confirmed from lop_hdr where lop_ref_no = '" + tranID +"'";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next() )
			{
				confirmed = rs.getString(1);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			if(confirmed == null || confirmed.trim().length() == 0 || !confirmed.equalsIgnoreCase("Y") )
			{
				sql = " update lop_hdr "
						+ " set   confirmed  = 'Y',"
						+ " conf_date = ? ,"
						+ " emp_code__aprv ='" + userId + "'"
						+ " where lop_ref_no = '" + tranID+ "'";
				System.out.println("\n sql for update----" + sql);
				pstmt = conn.prepareStatement(sql);
				pstmt.setTimestamp(1, currDate);
				pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;
				errString = itmDBAccessEJB.getErrorString("","CONLOPSUCC","BASE");
			}
			else
			{
				errString = itmDBAccessEJB.getErrorString("","VTRCONF1","BASE");
			}
		}
		catch(Exception e)
		{
			System.out.println("ConfirmMPSOrderEJB..."+e.getMessage());
			e.printStackTrace();
			try
			{
				conn.rollback();
			}
			catch(Exception e1)
			{
			   System.out.println("ConfirmMPSOrderEJB..."+e1.getMessage());
			   e1.printStackTrace();
			}
		}
		finally
		{
			try
			{
				conn.commit();
				if(pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if(conn != null)
				{
					conn.close();
					conn = null;
				}
				if(Dtlrs != null )Dtlrs.close();
				Dtlrs = null;
				if(Dtlpstmt != null) Dtlpstmt.close();
				Dtlpstmt = null;
			}
			catch(Exception e)
			{
				System.out.println("Exception ConfirmMPSOrderEJB....... :\n"+e.getMessage());
			}
		}
		System.out.println("retString ::" + errString);
		return errString ;
	}// end actConfirm
	
}
