/**
 * Copyright (c) 2009 Penril Datability (SEA) Sdn Bhd All rights reserved.
 *
 * This software is copyrighted. Under the copyright laws, this software
 * may not be copied, in whole or in part, without prior written consent
 * of Penril Datability (SEA) Sdn Bhd or its assignees. This software is
 * provided under the terms of a license between Penril Datability (SEA)
 * Sdn Bhd and the recipient, and its use is subject to the terms of that
 * license.
 */

package my.com.kfh.enterprise.services.ib104_fundtransfer.thirdparty;

import com.broadvision.data.common.RowData;
import com.broadvision.data.common.TableData;

import my.com.kfh.enterprise.services.IBEnterpriseServicesWorkspace;
import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.error.exception.IBBusinessException;
import my.com.kfh.error.exception.IBGenericException;
import my.com.kfh.error.exception.IBServiceException;
import my.com.kfh.error.handler.CommonErrorCode;
import my.com.kfh.error.handler.IBErrorHandler;
import my.com.kfh.esb.request.ESBAccountNumber;
import my.com.kfh.web.utils.DataValidator;
import my.com.kfh.web.utils.IBDynaBean;
import my.com.kfh.web.utils.IBDynaBeanManager;
import my.com.kfh.web.utils.IBLogger;
import my.com.kfh.web.utils.IBSessionObjects;
import my.com.kfh.web.utils.IBTimer;
import my.com.kfh.web.utils.constant.ServiceInfo;

/**
 * <pre>
 * PROGRAMMER: Latipah bt Abdul Rashid
 * CHANGE-NO:
 * TASK-NO:
 * DATE CREATED: Oct 16, 2008
 * TAG AS:
 * REASON(S):
 * MODIFICATION:
 * </pre>
 */

/**
 * Verify the selected account and sets necessary display information for the desired account details into IBDynaBean.
 */
public class IB3rdPartyMaintAddConfirmServices extends IBEnterpriseServicesWorkspace
{
	private static Class c_oClass = IB3rdPartyMaintAddConfirmServices.class;

	private static String VIEWBEAN = ThirdPartyTransferTags.THIRDPARTY_MAINTENANCE_VIEW;

	/**
	 * Class constructor specifying session object.
	 *
	 * @param m_oIBSessionObjects session object containing all information stored in HTTP session
	 */
	public IB3rdPartyMaintAddConfirmServices (IBSessionObjects m_oIBSessionObjects)
	{
		super (m_oIBSessionObjects);
	}

	/**
	 * Get selected values and formats them for display
	 *
	 * @throws IBGenericException if any Exception are thrown
	 */
	public void verifyInput () throws IBGenericException
	{
		String m_sMethodName = "verifyInput";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);

		IBDynaBean m_oViewBean = null;
		String m_sAcctHolderName = "";
		String m_sAccountNo = "";
		ESBAccountNumber m_oESBAccountNumber = null;

		try
		{
			m_oViewBean = (IBDynaBean) getIBDynaBean ().get (VIEWBEAN) == null
				? IBDynaBeanManager.createIBDynaBean(VIEWBEAN, getIBDynaBean ().getActionMapping ())
						: (IBDynaBean) getIBDynaBean ().get (VIEWBEAN);

			m_sAccountNo = (String) getIBDynaBean ().get (ThirdPartyTransferTags.ACCOUNT_NUMBER);

			/*
			 * call host to verify 3rd party to account and get AccHolderName
			 */
			m_sAcctHolderName = getESBServices ().getAccountHolderName (m_sAccountNo);
			if (DataValidator.isEmptyString (m_sAcctHolderName))
			{
				throw IBErrorHandler.createBusinessException (
						IBBusinessException.FUNDTRANSFER_3RDPARTY_KEY, ThirdPartyTransferTags.VA_ERR_INVALID_ACCOUNT);
			}

			/*
			 * verify account's currency whether it is supported for this transaction
			 */
			m_oESBAccountNumber = new ESBAccountNumber ();
			if (!m_oESBAccountNumber.validateSupportAccountCurrency (m_sAccountNo))
			{
				throw IBErrorHandler.createBusinessException (IBBusinessException.ESB, String.valueOf (1));
			}

			/*
			 * check if account already exist in DB
			 */
			if (isAccountExist (m_sAccountNo))
			{
				throw IBErrorHandler.createBusinessException (
						IBBusinessException.FUNDTRANSFER_3RDPARTY_KEY, ThirdPartyTransferTags.VA_ERR_RECORD_EXISTED);
			}

			m_oViewBean.set (ThirdPartyTransferTags.ACCOUNT_NUMBER_DIS, m_sAccountNo);
			m_oViewBean.set (ThirdPartyTransferTags.NICKNAME_DIS, (String) getIBDynaBean ().get (ThirdPartyTransferTags.NICKNAME));
			m_oViewBean.set (ThirdPartyTransferTags.ACCOUNT_HOLDER_NAME, m_sAcctHolderName);
			m_oViewBean.set (ThirdPartyTransferTags.EMAIL_DIS, (String) getIBDynaBean ().get (ThirdPartyTransferTags.EMAIL));

			getIBDynaBean ().set (VIEWBEAN, m_oViewBean);
			
			IBDynaBean m_oServiceInfoBean = (IBDynaBean) m_oViewBean.get (ServiceInfo.SERVICE_INFO_BEAN);
			if (((Boolean) m_oServiceInfoBean.get (ServiceInfo.BEAN_ATT_TAC_REQUIRED)).booleanValue ())
			{
				IBDynaBean m_oSMSViewBean = (IBDynaBean) getIBSessionObjects().getHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN);

				if(!DataValidator.isEmptyString(m_sAcctHolderName)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, m_sAcctHolderName.length()>25?m_sAcctHolderName.substring(0, 25):m_sAcctHolderName);
				}

				getIBSessionObjects().setHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN, m_oSMSViewBean);
			}
		}
		catch (Exception e)
		{
			if (e instanceof IBGenericException)
			{
				throw (IBGenericException) e;
			}
			throw IBErrorHandler.createServiceException(
					IBServiceException.GENERAL_SERVICE_ERROR, true,
					new String[] {CommonErrorCode.GENERIC_CODE, "", "", "", IBErrorHandler.generateErrorMessage(e)}, c_oClass);
		}

		IBLogger.logBSFNMethodLeavingMessage (c_oClass, m_sMethodName, m_oTimer);
	}

	/**
	 * Check whether account number is already existed in database
	 *
	 * @param p_sAccountNo account number
	 * @return return true if account number already exist. otherwise, return false.
	 * @throws IBGenericException if any Exception are thrown
	 */
	public boolean isAccountExist (String p_sAccountNo) throws IBGenericException
	{
		String m_sMethodName = "isAccountExist";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName, String.class.getName ());

		TableData m_oTblData = null;
		boolean m_boStatus = false;
		int m_iLength = 0;
		RowData m_oRow = null;

		try
		{
			m_oTblData = IBFundTransferDBUtils.getAll3rdPartyAccounts (getIBSessionObjects ());
			if (m_oTblData != null && m_oTblData.size () > 0)
			{
				m_iLength = m_oTblData.size ();
			    for (int i = 0; i < m_iLength; i++)
			    {
			    	m_oRow = m_oTblData.getRow(i);
			        if (m_oRow.getStringValue(ThirdPartyTransferTags.COL_ACCOUNT_NUMBER)
			        		.equalsIgnoreCase (p_sAccountNo))
			        {
			        	m_boStatus = true;
			            break;
			        }
			    }
			}
		}
		catch (Exception e)
		{
			if (e instanceof IBGenericException)
			{
				throw (IBGenericException) e;
			}
			throw IBErrorHandler.createServiceException(
					IBServiceException.GENERAL_SERVICE_ERROR, true,
					new String[] {CommonErrorCode.GENERIC_CODE, "", "", "", IBErrorHandler.generateErrorMessage(e)}, c_oClass);
		}
		IBLogger.logBSFNMethodLeavingMessage (c_oClass, m_sMethodName, m_oTimer);

		return m_boStatus;
	}
}
