
package my.com.kfh.enterprise.services.ib115_gold.gold_transfer_own;

/**
 * PROGRAMMER: Goh Kee Kun
 * COMPANY: Pixakraft
 * DATE CREATED: 15-Oct-2012
 */

import java.util.Map;

import my.com.kfh.enterprise.services.IBEnterpriseServicesWorkspace;
import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.enterprise.services.ib112_si.IBGenericSIServices;
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.sms.services.SMSServices;
import my.com.kfh.web.utils.DataValidator;
import my.com.kfh.web.utils.FormatterUtils;
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.ServiceInfoExtend;
import my.com.kfh.web.utils.constant.ServiceInfo;


/**
 * This class handles the back end process of own account transfer confirm services.
 */
public class IBOwnGoldTransferConfirmServices extends IBEnterpriseServicesWorkspace
{
	private static Class c_oClass = IBOwnGoldTransferConfirmServices.class;

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

	/**
	 * <pre>
	 * To get the from account, to account and amount value from form bean
	 * and format it for user display.
	 * Set all the required data into view beans.
	 * </pre>
	 *
	 * @throws IBGenericException
	 */
	public void confirmOwnTransfer () throws IBGenericException
	{
		String m_sMethodName = "confirmOwnGoldTransfer";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName, Map.class.getName ());

		String m_sAmount = "";
		String m_sFromAccount = "";
		String m_sToAccount = "";
		String m_sAmountDisplay = "";
		StringBuffer m_sFromAccountDisplay = new StringBuffer();
		StringBuffer m_sToAccountDisplay = new StringBuffer();
		String m_sFromAccNickname = "";
		String m_sToAccNickname = "";

		IBDynaBean m_oViewBean = null;
		IBDynaBean m_oServiceBean = null;
		IBDynaBean m_oOwnAccInfoBean = null;

		try
		{
			m_oViewBean = (IBDynaBean) getIBDynaBean ().get (IBOwnGoldTransferConstant.OWN_GOLD_TRANSFER_BEAN);
			if(m_oViewBean == null)
			{
				m_oViewBean = IBDynaBeanManager.createIBDynaBean (IBOwnGoldTransferConstant.OWN_GOLD_TRANSFER_BEAN, getIBDynaBean ().getActionMapping ());
			}

			m_sAmount = (String) getIBDynaBean ().get (IBOwnGoldTransferConstant.AMOUNT);
			m_sFromAccount = (String) getIBDynaBean ().get (IBOwnGoldTransferConstant.FROM_ACCOUNT);
			m_sToAccount = (String) getIBDynaBean ().get (IBOwnGoldTransferConstant.TO_ACCOUNT);

			if(m_sAmount != null)
			{
//				m_sAmountDisplay = FormatterUtils.formatMoneyDisplay (m_sAmount, "RM", true);
				m_sAmountDisplay = FormatterUtils.formatGoldUnitDisplay (m_sAmount, true);
			}

			Map m_oFromAccountsMap = (Map)m_oViewBean.get (IBOwnGoldTransferConstant.FROM_ACCOUNT_MAP);
			m_oOwnAccInfoBean = (IBDynaBean) m_oFromAccountsMap.get (m_sFromAccount);
			m_sFromAccNickname = (String) m_oOwnAccInfoBean.get (CommonConstants.ACCOUNT_NICKNAME);
			if(m_sFromAccount != null)
			{
				m_sFromAccountDisplay.append (m_sFromAccount).append (" ").append (m_sFromAccNickname);
			}

			Map m_oToAccountsMap = (Map)m_oViewBean.get (IBOwnGoldTransferConstant.TO_ACCOUNT_MAP);
			m_oOwnAccInfoBean = (IBDynaBean) m_oToAccountsMap.get (m_sToAccount);
			m_sToAccNickname = (String) m_oOwnAccInfoBean.get (CommonConstants.ACCOUNT_NICKNAME);
			if(m_sToAccount != null)
			{
				m_sToAccountDisplay.append (m_sToAccount).append (" ").append (m_sToAccNickname);
			}

			m_oServiceBean = (IBDynaBean)m_oViewBean.get (ServiceInfoExtend.SERVICE_INFO_EXTEND);
			if (m_oServiceBean != null && ((Boolean)m_oServiceBean.get (ServiceInfo.BEAN_ATT_FUTURE_TRANSACTION_FLAG)).booleanValue ())
			{
				IBGenericSIServices m_oGenericSIServices = new IBGenericSIServices (getIBSessionObjects ());
				m_oGenericSIServices.setIBDynaBean (getIBDynaBean ());
				m_oViewBean = m_oGenericSIServices.confirmSIInput (m_oViewBean);
			}

			m_oViewBean.set (IBOwnGoldTransferConstant.AMOUNT_DISP, m_sAmountDisplay);
			m_oViewBean.set (IBOwnGoldTransferConstant.FROM_ACCOUNT_DISP, m_sFromAccountDisplay.toString ());
			m_oViewBean.set (IBOwnGoldTransferConstant.TO_ACCOUNT_DISP, m_sToAccountDisplay.toString ());
			
			//SMS Notification START
			m_oViewBean = SMSServices.setupConfirmViewBean(this, m_sAmount, getIBDynaBean(), m_oViewBean, m_oServiceBean);
			// SMS Notification END
		
			//Pixakarft Bank Negara Changes 30th Mar 2013 start
			if (((Boolean) m_oServiceBean.get (ServiceInfo.BEAN_ATT_TAC_REQUIRED)).booleanValue ())
			{
				IBDynaBean m_oSMSViewBean = (IBDynaBean) getIBSessionObjects().getHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN);
				m_oSMSViewBean.set(CommonConstants.AMOUNT, m_sAmountDisplay);
				if(!DataValidator.isEmptyString(m_sToAccNickname)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, m_sToAccNickname.length()>14?m_sToAccNickname.substring(0, 14):m_sToAccNickname);
				}
			
				getIBSessionObjects().setHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN, m_oSMSViewBean);
			}
			//Pixakarft Bank Negara Changes 30th Mar 2013 start
			
			getIBDynaBean().set(IBOwnGoldTransferConstant.OWN_GOLD_TRANSFER_BEAN, m_oViewBean);

		}
		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);
	}

}
