package ibase.webitm.ejb.dis;

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ActionHandlerEJB;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.ejb.Stateless;

@Stateless
public class ProofOfDeliveryReject extends ActionHandlerEJB implements ProofOfDeliveryRejectLocal,ProofOfDeliveryRejectRemote 
{
	public String actionHandler(String tranId, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		System.out.println("--------------actionHandler method of ProofOfDeliveryReject ------------- : ");
		String retString = "";		
		try
		{						
				retString = reject(tranId,xtraParams,forcedFlag);			
		}
		catch(Exception e)
		{
			System.out.println("Exception :ProofOfDeliveryReject :actionHandler():" + e.getMessage() + ":");
			retString = GenericUtility.getInstance().createErrorString(e);
			e.printStackTrace();
		}
		
		return retString;
	}
	
	private String reject(String tranId,String xtraParams,String forcedFlag) 
	{
		System.out.println("---------Class : ProofOfDeliveryReject-->> reject method called-----------");		
		ResultSet rs=null;
		Connection conn=null;
		PreparedStatement pstmt=null;
		String errorString="",sql="",loginCode="";		
		String errString="";
		int updCnt=0;
		GenericUtility genericUtility=new GenericUtility();
		try
		{
			ITMDBAccessEJB itmDBAccess = new ITMDBAccessEJB();
			ConnDriver connDriver = null;
			connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverValidator");			
			conn.setAutoCommit(false);			
			String wfStatus="",confirmed="";
			sql = "select confirmed,wf_status from spl_sales_por_hdr WHERE tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, tranId);
			rs=pstmt.executeQuery();
			if( rs.next() )
			{
				confirmed =rs.getString(1)==null ? "N":rs.getString(1);
				wfStatus =rs.getString(1)==null ? "O":rs.getString(1);
			}	
			if( pstmt != null )
			{
				pstmt.close();
				pstmt = null;
			}
			if ( rs != null )
			{
				rs.close();
				rs = null;
			}
			if( confirmed != null && confirmed.equalsIgnoreCase("Y") )
			{
				//Confirmed Transaction can not be rejected 
				errString = itmDBAccess.getErrorString("", "VTCONFCRJ", "", "", conn);
				return errString;
			}	
			if( wfStatus != null && wfStatus.equalsIgnoreCase("R") )
			{
				errString = itmDBAccess.getErrorString("", "VTALREJ", "", "", conn);
				return errString;
			}	
			sql="";
			loginCode=genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			sql="update spl_sales_por_hdr set wf_status = ?, emp_code__aprv = ? where tran_id = ? ";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, "R");
			pstmt.setString(2, loginCode);
			pstmt.setString(3, tranId);	
			updCnt=pstmt.executeUpdate();			
			if (updCnt > 0)
			{
				conn.commit();
				errString = itmDBAccess.getErrorString("", "VTREJSUCF", "", "", conn);
				System.out.println("--------Column wf_status of spl_sales_por_hdr table updated successfully------- ");
				return errString;
			}			
		}
		catch(SQLException se)
		{
			System.out.println("SQLException : class ProofOfDeliveryReject : ");			
			se.printStackTrace();
			try
			{
			  conn.rollback();
			}
			catch(Exception e){
				System.out.println("Exception : Occure during rollback........");
				e.printStackTrace();
				}
		}
		catch(Exception e)
		{
			System.out.println("Exception : class ProofOfDeliveryReject : ");
			e.printStackTrace();
			try
			{
				conn.rollback();
			}
			catch(Exception se){
				System.out.println("Exception : Occure during rollback........");
				se.printStackTrace();
				}
		}
		
		finally
		{
		   try{
			    if (pstmt != null)
			    {
			    	pstmt.close();
					pstmt=null;
			    }
				if (rs !=null)
				{
					rs.close();
					rs=null;
				}
				if(conn!=null)
				{		
					conn.close();
					conn = null;
				}
				
		    }catch(Exception e)
			{
				System.out.println("inside Finally Exception ProofOfDeliveryReject class:reject() ");
				e.printStackTrace();
			}

		}
		return errorString;
	}
}
