/**
 * 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.ftt;

import java.util.List;

import com.broadvision.data.common.TableData;

import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.enterprise.services.ib104_fundtransfer.constant.FTTConstants;
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.web.utils.DataValidator;
import my.com.kfh.web.utils.IBDynaBean;
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: Jelson Low
 * CHANGE-NO:
 * TASK-NO:
 * DATE CREATED: Dec 13, 2008
 * TAG AS:
 * REASON(S):
 * MODIFICATION:
 * </pre>
 */

/**
 * This class will do back end process for favourite FTT add confirm services.
 */
public class IBFavFttAddConfirmServices extends IBGenericFttServices
{
	private static Class c_oClass = IBFavFttAddConfirmServices.class;

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

	/**
	 * <pre>
	 * To do FTT registration confirmation.
	 * If the beneficiary entered by the user is already exist in the database,
	 * throw an a business exception.
	 * Set all required data into view beans.
	 * </pre>
	 *
	 * @throws IBGenericException
	 */
	public void executeServices () throws IBGenericException
	{
		String m_sMethodName = "executeServices";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);

		String m_sBeneficiaryID = null;

		TableData m_oTableData = null;
		int m_oTableDataSize = 0;
		List m_alFttCountry = null;
		String m_sFttCountryIndex = null;
		String m_sCountryName = null;

		IBDynaBean m_oViewBean = null;

		try
		{

			m_oViewBean = (IBDynaBean) getIBDynaBean ().get (FTTConstants.FAV_ADD_VIEW_BEAN);

			m_oTableData = getFavFttTableData (getIBSessionObjects ());

			if (m_oTableData != null && m_oTableData.size () > 0)
			{
				m_oTableDataSize = m_oTableData.size ();
				for (int i = 0; i <m_oTableDataSize;  i++)
				{
					m_sBeneficiaryID = (m_oTableData.getStringValue (i, FTTConstants.COL_BENEFICIARY_NICK_NAME)).trim ();
					if (m_sBeneficiaryID.equals (getIBDynaBean ().get (FTTConstants.FBEAN_BENEFICIARY_NICK_NAME)))
					{
						throw IBErrorHandler.createBusinessException (IBBusinessException.FTT_FUND_TRANSFER_FAV_ADD,
								FTTConstants.ERROR_ID_ALREADY_EXIST);
					}
				}
			}

			//get country from bvmc IB_TT_COUNTRY_LIST(online)
			m_sCountryName = (String) getIBDynaBean ().get (FTTConstants.FBEAN_BENEFICIARY_COUNTRY_INDEX);
			m_sFttCountryIndex = (String) getIBDynaBean ().get (FTTConstants.FBEAN_FTT_COUNTRY_INDEX);
			int m_iFttCountryIndex = Integer.parseInt(m_sFttCountryIndex);
			m_alFttCountry = (List) m_oViewBean.get (FTTConstants.VBEAN_COUNTRY_LIST);

			String m_sFTTCont = (String) ((IBDynaBean)m_alFttCountry.get (m_iFttCountryIndex)).get (FTTConstants.VBEAN_COUNTRY_DISP);

			m_oViewBean.set (FTTConstants.VBEAN_SELECTED_COUNTRY_NAME, m_sCountryName);

			m_oViewBean.set (FTTConstants.VBEAN_FTT_COUNTRY, m_sFTTCont);
			
			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);
				String beneName = (String) getIBDynaBean ().get (FTTConstants.FBEAN_BENEFICIARY_NAME);
				if(!DataValidator.isEmptyString(beneName)){
					beneName = beneName.length()>25?beneName.substring(0, 25):beneName;
				}
				m_oSMSViewBean.set(CommonConstants.BENE_NAME, beneName);
				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);
	}

}
