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 javax.ejb.Stateless;//added for ejb3

@Stateless
//import ibase.webitm.ejb.MasterStateful;
//import ibase.webitm.ejb.MasterStatefulHome;


//public class CloseCTEJB extends ActionHandlerEJB implements SessionBean

public class CloseCT extends ActionHandlerEJB implements CloseCTLocal, CloseCTRemote
{
	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,qtyUsed =0 ,qty =0;
		Timestamp tranDate = null;
		double ConvQty = 0, finalTot = 0,ConvQtyTot = 0;
		String lotSl = "",lineNo = "",locCode= "",lotNo = "",qtySample = "",convQty = "",unitStd = "",qtyStd = "",remarks = "";
		String errString = "",qorderNo = "",status = "",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 status,confirmed from ct3form_hdr where tran_id = '" + tranID +"'";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next() )
			{
				status = rs.getString(1);
				confirmed = rs.getString(2);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			if((!status.equalsIgnoreCase("C") || !status.equalsIgnoreCase("X")) && confirmed.equalsIgnoreCase("Y") )
			{
				sql = "select qty_used,quantity from ct3form_det where tran_id = '" + tranID +"'";
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				while(rs.next() )
				{
					qtyUsed = rs.getDouble(1);
					qty = rs.getDouble(2);
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				if( qtyUsed == 0)
				{
					sql = " update ct3form_hdr "
								+ " set status = 'X',"
								+ " status_date = ? "
								+ " where tran_id = '" + tranID + "'";
					System.out.println("sql update status:- " + sql);
					pstmt = conn.prepareStatement(sql);			
					pstmt.setTimestamp(1,currDate);
					pstmt.executeUpdate();
				}
				else if( qtyUsed < qty)
				{
					sql = " update ct3form_hdr "
								+ " set status = 'C',"
								+ " status_date = ? "
								+ " where tran_id = '" + tranID + "'";
					System.out.println("sql update status:- " + sql);
					pstmt = conn.prepareStatement(sql);			
					pstmt.setTimestamp(1,currDate);
					pstmt.executeUpdate();
				}
				errString = itmDBAccessEJB.getErrorString("","CTCLOSUCC","BASE");
				System.out.println("case !status.equalsIgnoreCase(C) && confirmed.equalsIgnoreCase(Y)");
			}
			else if(status.equalsIgnoreCase("C") && !confirmed.equalsIgnoreCase("Y") )
			{
				errString = itmDBAccessEJB.getErrorString("","VTCX","BASE");
				System.out.println("case status.equalsIgnoreCase(C) && !confirmed.equalsIgnoreCase(Y)");
			}
			else if(!status.equalsIgnoreCase("C") && !confirmed.equalsIgnoreCase("Y") )
			{
				errString = itmDBAccessEJB.getErrorString("","CLOCTN","BASE");
				System.out.println("case !status.equalsIgnoreCase(C) && !confirmed.equalsIgnoreCase(Y)");
			}
		}
		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
	
}
