package ibase.webitm.ejb.fin.adv;

import ibase.system.config.ConnDriver;
import ibase.webitm.utility.ITMException;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PayAprvActivityStatus 
{
  public String confirm(String tranId, String empCodeAprv, String xmlDataAll, String processId, String keyFlag) throws RemoteException, ITMException 
	{
		System.out.println("Activity Check JB");
		System.out.println("Parameters ::: tranId[" + tranId + "], empCodeAprv[" + empCodeAprv + "], xmlDataAll[" + xmlDataAll + "],processId["+processId+"],keyFlag["+keyFlag+"]");
		String retString = "";

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		ConnDriver connDriver = new ConnDriver();
		try 
		{
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			conn.setAutoCommit(false);

			empCodeAprv = empCodeAprv == null ? "" : empCodeAprv.trim();
			String signStatus1 = "", signStatus2 = "";
			String activityId1 = "", roleCode1 = "";
			String activityId2 = "", roleCode2 = "";
	
			if("CHKUSRCOND".equalsIgnoreCase(keyFlag)) 
			{
				sql = "select ROLE_CODE__SIGN from OBJ_SIGN_TRANS "
						+ "WHERE ltrim(rtrim(REF_SER)) = 'M-PAY' AND ltrim(rtrim(REF_ID)) = ? AND ltrim(rtrim(ROLE_CODE__SIGN)) IN('RLCDJ2L5KF','RLCDJ2L5KS','RLCDJ2L1F','RLCDJ2L1S','RLCDJ2L2F','RLCDJ2L2S','RLCDJ2L5F','RLCDJ2L5S')";
						
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, tranId);
				rs = pstmt.executeQuery();
				if (!rs.next()) 
				{
					retString = "1";
					
				}
				else
				{
					retString = "0";
				}
				System.out.println("retString = " + retString);
	
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;	
			}
			else
			{
				if("First".equalsIgnoreCase(keyFlag)) 
				{
					activityId1 = "J2_SIGN"; roleCode1 = "RLCDJ2F";
					activityId2 = "J21_SIGN"; roleCode2 = "RLCDJ2S";
				}
				else if("Second".equalsIgnoreCase(keyFlag)) 
				{
					activityId1 = "J2L5K_SIGN"; roleCode1 = "RLCDJ2L5KF";
					activityId2 = "J2L5K1_SIGN"; roleCode2 = "RLCDJ2L5KS";
				}
				else if("Third".equalsIgnoreCase(keyFlag))
				{
					activityId1 = "J2L1_SIGN"; roleCode1 = "RLCDJ2L1F";
					activityId2 = "J2L11_SIGN"; roleCode2 = "RLCDJ2L1S";
				}
				else if("Fourth".equalsIgnoreCase(keyFlag))
				{
					activityId1 = "J2L2_SIGN"; roleCode1 = "RLCDJ2L2F";
					activityId2 = "J2L21_SIGN"; roleCode2 = "RLCDJ2L2S";
				}
				else if("Fifth".equalsIgnoreCase(keyFlag))
				{
					activityId1 = "J2L5_SIGN"; roleCode1 = "RLCDJ2L5F";
					activityId2 = "J2L51_SIGN"; roleCode2 = "RLCDJ2L5S";
				}
				sql = "select SIGN_STATUS from OBJ_SIGN_TRANS "
						+ "WHERE ltrim(rtrim(REF_SER)) = 'M-PAY' AND ltrim(rtrim(REF_ID)) = ? AND ltrim(rtrim(ROLE_CODE__SIGN)) IN(?,?)";
						
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, tranId);
				pstmt.setString(2, roleCode1);
				pstmt.setString(3, roleCode2);
				rs = pstmt.executeQuery();
				if (rs.next()) 
				{
					signStatus1 = rs.getString("SIGN_STATUS");
					if (rs.next()) 
					{
						signStatus2 = rs.getString("SIGN_STATUS");
					}
				}
				System.out.println("signStatus1 = " + signStatus1 + "signStatus1 = " + signStatus2 );

				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				if(("S".equalsIgnoreCase(signStatus1)) && ("S".equalsIgnoreCase(signStatus2)))
				{
					retString = "1";
				} 
				else 
				{
					retString = "0";
				}
			}
		} 
		catch (Exception e) 
		{
			try 
			{
				conn.rollback();
			} 
			catch (SQLException e1) 
			{
				e1.printStackTrace();
			}
			e.printStackTrace();
			System.out.println("Exeption occured");

			throw new ITMException(e);
		} 
		finally 
		{
			if (conn != null) 
			{
				try 
				{
					conn.commit();
					conn.close();
					conn = null;
				} 
				catch (SQLException e) 
				{
					e.printStackTrace();
					System.out.println("Exeption in Finally");
				}
			}
		}

		return retString;
	}
}
