/*
Window Name : w_srv_order(Approve)
Button Name : Approve
Action      : Puchase Order will be Approved i.e it will Update SRV_ORDER Table. 
*/
package ibase.webitm.ejb.fin.adv;

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

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.system.config.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class SrvOrderApprove extends ActionHandlerEJB implements SrvOrderApproveLocal,SrvOrderApproveRemote //SessionBean
{
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
	}

	public void ejbRemove()
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate() 
	{
	}*/

    public String actionHandler() throws RemoteException,ITMException
	{
		return "";
	}

	public String actionHandler(String tranID, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		System.out.println("Calling actionHandler from SrvOrderEJB");
		String varValue = "";
		String retString = "";
		try
		{
			retString = actionApprove(tranID,xtraParams);						
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :SrvOrderEJB :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("errString :"+retString);
		return retString;
	}

	private String actionApprove(String tranID, String xtraParams) throws RemoteException,ITMException
	{
		String confirm = "";
		String approve = "";
		String userId = "";
		String empCode = "";
		Connection conn = null;
		String sql = "";
		String errCode = "";
		ResultSet rs = null;
		Statement stmt = null;
		String errString = ""; 
	    ConnDriver connDriver = new ConnDriver();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		GenericUtility genericutility = GenericUtility.getInstance();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");			
			System.out.println("tranId :="+tranID);
			sql = "SELECT CASE WHEN CONFIRMED IS NULL THEN 'N' ELSE CONFIRMED END, EMP_CODE__APRV "
				  +"FROM SRV_ORDER WHERE TRAN_ID = '"+ tranID +"'";
			System.out.println("sql :"+sql);
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next())
			{
				confirm = rs.getString(1);
				System.out.println("confirm :"+confirm); 			
				approve = rs.getString(2);
				System.out.println("approve :"+approve); 			
			}
			if (confirm != null && confirm.equalsIgnoreCase("N"))
			{
				errCode = "VTPONCONF";
				return itmDBAccessEJB.getErrorString("",errCode, "", "", conn);			
			}
			else if (approve != null)
			{
				errCode = "VTPOAPRV1";
				return itmDBAccessEJB.getErrorString("",errCode, "", "", conn);
			}

			userId = genericutility.getValueFromXTRA_PARAMS(xtraParams, "userId");
			System.out.println("USERID :"+userId);
			sql = "SELECT EMP_CODE FROM USERS WHERE CODE = '"+userId+"'";
			System.out.println("SQL :: "+sql);
			rs = stmt.executeQuery(sql);
			if (rs.next())
			{
				empCode = rs.getString(1);
				System.out.println("empCode :"+empCode);
			}
			if (empCode == null || empCode.trim().length() == 0 )
			{
				errCode = "VTEMPNULL";
			}
			else 
			{
				sql = "UPDATE SRV_ORDER SET EMP_CODE__APRV ='"+empCode+"'"+" WHERE TRAN_ID = '"+tranID+"'";
				System.out.println("Updating SRV_ORDER sql:"+sql);
				int resultUp = conn.createStatement().executeUpdate(sql);
				System.out.println("The Update Count :"+resultUp);
				if (resultUp  > 0 )
				{
					conn.commit();
					errCode = "VTPOAPRV";
				}
				else
				{
					conn.rollback();
					errCode = "DS000NR";
				}
			}
			if (errCode != null  && errCode.trim().length() > 0)
			{
				errString = itmDBAccessEJB.getErrorString("",errCode, "", "", conn);
			}
		}
		catch(SQLException sq)
		{
			System.out.println("The SQLException occers :"+sq);
			sq.printStackTrace();
			throw new ITMException(sq);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			errCode = e.getMessage();
			throw new ITMException(e);
		}
		finally
		{
			System.out.println("Closing Connection.........");
			try{
				conn.close();
				conn = null;
			}catch(Exception e){}
		}
		return errString;
	} 
}