/*	
		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";
					// Modified by Piyush on 23/12/2014 [To commit/rollback data].Start
						conn.commit(); 
					}
					else
					{
						errCode =  "VTCANC3";
						conn.rollback();
					}
					// Modified by Piyush on 23/12/2014 [To commit/rollback data].End
				}
			}
			// Modified by Piyush on 23/12/2014[To give proper message in case of already cancelled].Start
			else if (status.equalsIgnoreCase("X"))
			{
				errCode = "VTCANC2";
			}
			// Modified by Piyush on 23/12/2014[To give proper message in case of already cancelled].End
			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;
	}
}