/*	
		Developed by	: Hatim Laxmidhar
		Started On		: 07/01/2006
		Purpose  		: Cancel Button
		Window			: w_loanschedule
*/

package ibase.webitm.ejb.adm.adv;

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

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

@Stateless // added for ejb3

public class LoanscheduleCnc extends ActionHandlerEJB implements LoanscheduleCncLocal,LoanscheduleCncRemote // 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 loanNo, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{

		String  retString = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			retString = actionCancel(loanNo, xtraParams, forcedFlag);
		}
		catch(Exception e)
		{
			System.out.println("Exception :LoanscheduleCncEJB :actionHandler :" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning String from LoanscheduleCncEJB :actionHandler :"+retString);
		return retString;
	}

	private String actionCancel(String loanNo, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		String status = "", sql = "";
		String errCode = "", errString = "";
		String loginCode = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		ConnDriver connDriver = new ConnDriver();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		GenericUtility genericUtility = GenericUtility.getInstance();
		int cnt = 0;
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			stmt = conn.createStatement();

			System.out.println("########### Inside LoanscheduleCncEJB[actionCancel]  ###########");
			System.out.println("Loan No: "+ loanNo);
			
			sql = "SELECT STATUS FROM LOANS WHERE LOAN_NO = '" + loanNo + "'";

			System.out.println("sql :\n" + sql +"\n");

			rs = stmt.executeQuery(sql);
			if (rs.next())
			{
				status = rs.getString(1);
			}
			
			System.out.println("Status :"+status);
			
			if (status.equalsIgnoreCase("U"))
			{
				loginCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
				if (loginCode == null || loginCode.trim().length() == 0)
				{
					errCode =  "EMPAPRV";
				}
				else 
				{
					stmt = conn.createStatement();
					cnt = stmt.executeUpdate("UPDATE LOANS SET STATUS = 'X' WHERE LOAN_NO = '" + loanNo + "'");
					if (cnt > 0)
					{
						errCode = "VTCANC1";
					}
				}
			}
			else 
			{
				errCode = "VTRCONF1";
			}
		}
		catch (SQLException sqx)
		{
			System.out.println("The sqlException occure in LoanscheduleCncEJB :"+sqx);
			throw new ITMException(sqx);
		}
		catch (Exception e)
		{
			System.out.println("The Exception occure in LoanscheduleCncEJB :"+e);
			throw new ITMException(e);
		}
		finally
		{
			errString = itmDBAccessEJB.getErrorString("",errCode,"","",conn);			
			try
			{	
				if (conn!=null)
				{
					conn.close();
					conn = null;
				}
			}
			catch (Exception e)
			{
				System.out.println("Exception in finally: " + e);
			}
		}
		return errString;
	}
}