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.ejb.MasterStateful;
//import ibase.webitm.ejb.MasterStatefulHome;
import javax.ejb.Stateless;//added for ejb3


@Stateless
public class ConfirmCTFORM extends ActionHandlerEJB implements ConfirmCTFORMLocal,ConfirmCTFORMRemote
{
	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,psmt = null,updPsmt = 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 = "",qry = "",tranId = "",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 h.confirmed,sum(d.quantity) "
					+ " from ct3form_hdr h,ct3form_det d "
					+	" where h.tran_id = d.tran_id "
					+	" and h.tran_id = '" + tranID +"'"
					+	" group by h.confirmed,d.quantity ";
			System.out.println("sql----" + sql);
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next() )
			{
				confirmed = rs.getString(1);
				quantity = rs.getDouble(2);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			if(!confirmed.equalsIgnoreCase("Y") && quantity > 0 )
			{
				sql = " update ct3form_hdr "
						+ " set   confirmed  = 'Y',"
						+ " conf_date = ? ,"
						+ " emp_code__aprv ='" + userId + "'"
						+ " where tran_id = '" + 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("","CONCTSUCC","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
	
}
