/********************************************************
	Title 	 : DistRcpExShConf
	Date  	 : 13/MAR/15
	Developer: Pankaj R.
 ********************************************************/

package ibase.webitm.ejb.dis;

import java.rmi.RemoteException;
import java.sql.*;

import ibase.webitm.ejb.*;
import ibase.system.config.*;

import javax.ejb.Stateless;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;


@Stateless

public class DistRcpExShConf extends ActionHandlerEJB implements DistRcpExShConfLocal,DistRcpExShConfRemote //SessionBean
{
	public String confirm(String tranId,String xtraParams,String forcedFlag) throws RemoteException,ITMException
	{
		System.out.println("Inside Confirm Method");

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "",sql1="";
		ConnDriver connDriver = null;
		String loginEmpCode = "";
		String confirm = "",empCodeAprv="" ;
		String empFName = "",empMName="",empLName="";
		String errString = "" ; 
		ValidatorEJB validatorEJB = null;

		ITMDBAccessEJB itmDBAccessEJB = null;
		try
		{

			itmDBAccessEJB = new ITMDBAccessEJB();
			validatorEJB = new ValidatorEJB();
			connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			conn.setAutoCommit(false);
			empCodeAprv = validatorEJB.getValueFromXTRA_PARAMS(xtraParams, "loginEmpCode");
			
			System.out.println("empCodeAprv@@@@@@@"+empCodeAprv);
			//empCodeAprv = "loginEmpCode="+userInfo.getEmpCode();
			

			sql = "select confirmed,site_code from distrcp_exsh_hdr where tran_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tranId);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				confirm = rs.getString("confirmed");
			}
			System.out.println("Cconfirm@@@@@@@@"+confirm);
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;

			if(confirm != null && confirm.equalsIgnoreCase("Y"))
			{
				System.out.println("The Selected transaction is already confirmed");
				errString = itmDBAccessEJB.getErrorString("","VTMCONF1","","",conn);
				return errString;
			}
				
			
			else /*(confirm != null  && confirm.equalsIgnoreCase("N") */
			{

				sql = "update distrcp_exsh_hdr set confirmed = 'Y', conf_date = ?,emp_code__aprv = ? where tran_id = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setDate(1, new java.sql.Date(new java.util.Date().getTime()));
				pstmt.setString(2, empCodeAprv);
				pstmt.setString(3, tranId);
				int updateCoount = pstmt.executeUpdate();
				System.out.println("no of row update = "+updateCoount);
				System.out.println("Date@@@@@@@@@"+new java.sql.Date(new java.util.Date().getTime()));
				System.out.println("empCodeAprv+empCodeAprv"+empCodeAprv);
				System.out.println("tranId@@@@@@@@@"+tranId);
				pstmt.close();
				pstmt = null;
				
				sql1="select emp_fname,emp_mname,emp_lname from employee where emp_code= ?";
				pstmt = conn.prepareStatement(sql1);
				pstmt.setString(1, empCodeAprv);
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					empFName = rs.getString("emp_fname");
					empMName = rs.getString("emp_mname");
					empLName = rs.getString("emp_lname");
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				System.out.println("empFName@@@@@@@@@"+empFName);
				System.out.println("empMName@@@@@@@@"+empMName);
				System.out.println("empLName@@@@@@@@@"+empLName);
				
				
				if(updateCoount > 0)
				{
					errString = itmDBAccessEJB.getErrorString("","VTCICONF3 ","","",conn);
				}
				
					System.out.println("115 err String from confirm method.....");
			}
		}
		
		catch(Exception e)
		{
			System.out.println("Exception ::"+e.getMessage());
			errString = GenericUtility.getInstance().createErrorString(e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if(errString != null && errString.trim().length() > 0)
				{
					if(errString.indexOf("VTCICONF3") > -1)
					{
						conn.commit();
					}
					else
					{
						conn.rollback();
					}
				}
				if(rs != null)
				{
					rs.close();
					rs = null;
				}
				if(pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				conn.close();
			}
			catch(Exception e)
			{
				System.out.println("Exception : "+e);e.printStackTrace();
				throw new ITMException(e);
			}
		}
		return errString;
	
	}//end of confirm method
}//end of class
