/* 
	Window Name : w_retainer_contract
	Button Name : Cancel
	Action      : Transaction will be Canceled.
				  It will Update STATUS field of RETAINER_CONTRACT 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.system.config.*;

import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class RetainerContrCanc extends ActionHandlerEJB implements RetainerContrCancLocal,RetainerContrCancRemote //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
	{
		String  retString = null;
		try
		{
			retString = actionCancel(tranID,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception :RetainerContract :actionHandler:" + e.getMessage() + ":");
			throw new ITMException(e);
		}
	    return (retString);
	}

	private String actionCancel(String tranID,  String xtraParams)throws ITMException
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "";
		String updSql = "";
		String errCode="";
		String errString="";
		String format="";
		String status = "";
		int rows=0;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		ConnDriver connDriver = new ConnDriver();
		try
		{	
		   	conn = connDriver.getConnectDB("DriverITM");
			stmt = conn.createStatement();
			sql = "SELECT STATUS FROM RETAINER_CONTRACT WHERE CONTRACT_ID = '"+tranID+"'";
			rs = stmt.executeQuery(sql);
			if(rs.next())
			{
				status = rs.getString("status");
				System.out.println("status:"+status+":");
			}
			if(status != null && status.trim().length()>0)
			{
				if(status.equalsIgnoreCase("X"))
				{		
					errCode = "VTCANC2";
				}
		        else if(status.equalsIgnoreCase("E"))
				{
					errCode = "VTEXP1";
				}
				else if(status.equalsIgnoreCase("A"))
				{
					updSql = "UPDATE RETAINER_CONTRACT SET STATUS = 'X' WHERE CONTRACT_ID = '"+tranID+"'";
					rows = stmt.executeUpdate(updSql) ;
					if(rows > 0)
					{
						conn.commit();
						errCode = "VTCANC1";
						System.out.println("\n <==== Transaction Updated Successfully ====>");
					}					
				}
			}
			else
			{
				 errCode = "VMSTAT";
			}
			if (errCode != null  && errCode.trim().length() > 0)
			{
				System.out.println("RetainerContractEJB:errCode:"+errCode);
				errString = itmDBAccessEJB.getErrorString("", errCode, "", "", conn);
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception :RetainerContract :" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
	 	}
		finally
		{
			try
			{
				System.out.println("Closing Connection.......");
				conn.close();
				conn = null;
			}catch(Exception e){}
		}
		return errString;
	}
}