package ibase.webitm.ejb.mfg.adv;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.rmi.RemoteException;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
//import javax.ejb.SessionBean;
import ibase.system.config.ConnDriver;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.ActionHandlerEJB;
import org.w3c.dom.Document;
import ibase.webitm.ejb.ITMDBAccessEJB;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class BatchsizeAprvConf extends ActionHandlerEJB implements  BatchsizeAprvConfLocal , BatchsizeAprvConfRemote //SessionBean
{
   /* public void ejbCreate() throws RemoteException,CreateException{
    }
    public void ejbRemove(){

    }
    public void ejbActivate(){

    }
    public void ejbPassivate(){

    }*/
    public void setSessionContext(SessionContext se){

    }

    public String confirm( String tranId,String xtraParams, String forcedFlag ) throws RemoteException,ITMException
    {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "";
        ConnDriver connDriver = null;
		String loginEmpCode = null;
        ibase.webitm.utility.GenericUtility genericUtility = null;
		Document dom = null;
		int count=0;
		ITMDBAccessEJB itmDBAccessEJB = null;
		String errString = null;
		int upd = 0;
        try
		{
            itmDBAccessEJB = new ITMDBAccessEJB();
			genericUtility = ibase.webitm.utility.GenericUtility.getInstance();
            connDriver = new ConnDriver();
            conn = connDriver.getConnectDB("DriverITM");
            conn.setAutoCommit(false);

			loginEmpCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginEmpCode");
			int countDet = 0;
			if( errString == null || errString.trim().length() == 0 )
			{
				sql = " select count( 1 ) cnt from batchsize_aprv where tran_id = '" + tranId.trim() + "' AND CONFIRMED = 'Y' ";
					
				pstmt = conn.prepareStatement( sql );
				
				rs = pstmt.executeQuery();
				
				if( rs.next() )
				{
					countDet = rs.getInt( "cnt" );
				}
				pstmt.close();
				pstmt = null;				
				rs.close();
				rs = null;
				
				if( countDet > 0 )
				{
					errString = itmDBAccessEJB.getErrorString("","VTCONFMD","","",conn);
				}
			}
			if( errString == null || errString.trim().length() == 0 )
			{
				sql = "update batchsize_aprv set conf_date = ? ,emp_code__aprv = ? ,confirmed = 'Y' where tran_id = '" + tranId.trim() + "'";
				pstmt = conn.prepareStatement( sql );
				pstmt.setTimestamp( 1, new java.sql.Timestamp( System.currentTimeMillis() ) );
				pstmt.setString( 2,loginEmpCode );
				pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;
			}
            if( errString != null && errString.trim().length() > 0 )
			{
                System.out.println("Returning Result "+errString);
                conn.rollback();
                return errString;
            }
			else
			{
				conn.commit();
				errString = itmDBAccessEJB.getErrorString("","VTCNFSUCC","");
				System.out.println("Returnng String From BatchsizeAprvConfEJB : .." + errString);
			}
            System.out.println("errString : "+errString);
        }catch(ITMException ie)
		{
            System.out.println("ITMException : "+ie);
            try{
				conn.rollback();
			}catch(Exception t){}
            ie.printStackTrace();
            errString = itmDBAccessEJB.getErrorString("","VTDESNCONF","","",conn);
            System.out.println("Returnng String From BatchsizeAprvConfEJB :"+errString);
            return errString;
        }catch(Exception e){
            System.out.println("Returnng String From BatchsizeAprvConfEJB :"+e);
            try{
				conn.rollback();
			}catch(Exception t){}
            e.printStackTrace();
            errString = itmDBAccessEJB.getErrorString("","VTDESNCONF","","",conn);
            System.out.println("Returnng String From BatchsizeAprvConfEJB :"+errString);
            return errString;
        }
		finally
		{
            try{
				if( pstmt != null )
				{
					pstmt.close();
				}
				pstmt = null;
				if(conn != null)
				{
					conn.close();
				}
				conn = null;
            }catch(Exception e){System.out.println("Exception : "+e);e.printStackTrace();}
        }
        System.out.println("BatchsizeAprvConfEJB :"+errString);
        return errString;
	}
}

