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

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.CurrencyCode;
import my.com.kfh.web.utils.constant.ServiceInfo;

/**
 * <pre>
 * PROGRAMMER: Maliana Mustapha
 * CHANGE-NO:
 * TASK-NO:
 * DATE CREATED: Oct 23, 2008
 * TAG AS:
 * REASON(S):
 * MODIFICATION:
 * </pre>
 */

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

	/**
	 * Class constructor specifying session object.
	 *
	 * @param m_oIBSessionObjects Session object contains all information stored in HTTP session
	 */
	public IBOwnTransferConfirmServices (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 = "confirmOwnTransfer";
		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 (IBOwnTransferConstant.OWN_TRANSFER_BEAN);
			if(m_oViewBean == null)
			{
				m_oViewBean = IBDynaBeanManager.createIBDynaBean (IBOwnTransferConstant.OWN_TRANSFER_BEAN, getIBDynaBean ().getActionMapping ());
			}

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

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

			Map m_oFromAccountsMap = (Map)m_oViewBean.get (IBOwnTransferConstant.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 (IBOwnTransferConstant.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 (ServiceInfo.SERVICE_INFO_BEAN);
			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 (IBOwnTransferConstant.AMOUNT_DISP, m_sAmountDisplay);
			m_oViewBean.set (IBOwnTransferConstant.FROM_ACCOUNT_DISP, m_sFromAccountDisplay.toString ());
			m_oViewBean.set (IBOwnTransferConstant.TO_ACCOUNT_DISP, m_sToAccountDisplay.toString ());
			
			//SMS Notification START
			m_oViewBean = SMSServices.setupConfirmViewOwnBean(this, m_sAmount, CurrencyCode.MALAYSIA_RINGGIT,getIBDynaBean(), m_oViewBean, m_oServiceBean);
			// SMS Notification END
		
			getIBDynaBean().set(IBOwnTransferConstant.OWN_TRANSFER_BEAN, 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_sToAccNickname)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, m_sToAccNickname.length()>15?m_sToAccNickname.substring(0, 15):m_sToAccNickname);
				}
				m_oSMSViewBean.set(CommonConstants.SERVICE_ATTR, m_oServiceInfoBean.get(ServiceInfo.BEAN_ATT_SERVICE_CODE));
				m_oSMSViewBean.set(CommonConstants.TYPE_ATTR, CommonConstants.TYPE_TRANS);
				m_oSMSViewBean.set(CommonConstants.AMOUNT, m_sAmountDisplay);
				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);
	}

}
