/**
 * Copyright (c) 2010 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.interbank;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

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

/**
 * Sets necessary display information for the desired account details into IBDynaBean.
 */
public class IBInterbankMaintAddConfirmServices extends IBEnterpriseServicesWorkspace
{
	private static Class c_oClass = IBInterbankMaintAddConfirmServices.class;

	private static String VIEWBEAN = InterbankConstants.INTERBANK_MAINTENANCE_VIEW;

	/**
	 * Class constructor specifying session object.
	 *
	 * @param m_oIBSessionObjects session object containing all information stored in HTTP session and BVHTTP session
	 */
	public IBInterbankMaintAddConfirmServices (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;
		Map<String, Map> m_oBeneficiaryBankListMap = null;
		Map<String, String> m_oBeneficiaryBankMap = null;
		String m_sBeneBankCode = "";
		String m_sBeneBankName = "";
		String m_sRoutingId = "";
		String m_sBeneAcctNo ="";
		List <String>m_sAccountLengthList = null;
		boolean isAccountLengthValid = false;
		try
		{

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

			m_sBeneBankCode = (String) getIBDynaBean ().get (InterbankConstants.BENE_BANK);
			m_oBeneficiaryBankListMap = (LinkedHashMap<String, Map>) m_oViewBean.get (InterbankConstants.BENE_BANK_LIST_MAP);


			m_sBeneAcctNo = (String) getIBDynaBean ().get (InterbankConstants.BENE_ACCOUNT_NO);

			m_oBeneficiaryBankMap = (HashMap<String, String>) m_oBeneficiaryBankListMap.get (m_sBeneBankCode);
			m_sBeneBankName = (String) m_oBeneficiaryBankMap.get (InterbankConstants.FULL_NAME);

			m_sRoutingId = (String) m_oBeneficiaryBankMap.get (InterbankConstants.IB_INTERBANK_LIST.IBG_MEMBER_ID.toString ());

			if (!DataValidator.isEmptyString (m_sRoutingId))
			{
				m_sAccountLengthList = ResourcesUtils.getIBGAccountLengthProperties (m_sRoutingId);
				
				isAccountLengthValid = InterbankDBUtils.validateAccountLength(m_sAccountLengthList , m_sBeneAcctNo.length ());
				
				if (!isAccountLengthValid)
				{
					throw IBErrorHandler.createBusinessException (IBBusinessException.INTERBANK_TRANSFER, InterbankConstants.ERR_ACCOUNT_LENGTH,
							new String [] {m_sBeneBankName, String.valueOf (m_sBeneAcctNo.trim ().length ())});
				}
			}

			m_oViewBean.set (InterbankConstants.BENE_ACCOUNT_NO, (String) getIBDynaBean ().get (InterbankConstants.BENE_ACCOUNT_NO));
			m_oViewBean.set (InterbankConstants.BENE_BANK_DIS, m_sBeneBankName);
			m_oViewBean.set (InterbankConstants.BENE_NAME, (String) getIBDynaBean ().get (InterbankConstants.BENE_NAME));
			//ibg transform 19/9/2013
			m_oViewBean.set (InterbankConstants.BENE_ID, DataValidator.isEmptyString ((String) getIBDynaBean ().get (InterbankConstants.BENE_ID))?"-":(String) getIBDynaBean ().get (InterbankConstants.BENE_ID));
			m_oViewBean.set (InterbankConstants.NEW_IC, DataValidator.isEmptyString ((String) getIBDynaBean ().get (InterbankConstants.NEW_IC))?"-":(String) getIBDynaBean ().get (InterbankConstants.NEW_IC));
			m_oViewBean.set (InterbankConstants.OLD_IC, DataValidator.isEmptyString ((String) getIBDynaBean ().get (InterbankConstants.OLD_IC))?"-":(String) getIBDynaBean ().get (InterbankConstants.OLD_IC));
			m_oViewBean.set (InterbankConstants.BUSINESS_REG, DataValidator.isEmptyString ((String) getIBDynaBean ().get (InterbankConstants.BUSINESS_REG))?"-":(String) getIBDynaBean ().get (InterbankConstants.BUSINESS_REG));
			m_oViewBean.set (InterbankConstants.OTHER_ID, DataValidator.isEmptyString ((String) getIBDynaBean ().get (InterbankConstants.OTHER_ID))?"-":(String) getIBDynaBean ().get (InterbankConstants.OTHER_ID));
		
			m_oViewBean.set (InterbankConstants.ROUTING_ID, m_sRoutingId);
			m_oViewBean.set (InterbankConstants.EMAIL_DIS, DataValidator.isEmptyString ((String)getIBDynaBean ().get (InterbankConstants.EMAIL))?"-":getIBDynaBean ().get (InterbankConstants.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);
				String beneName = (String) getIBDynaBean ().get (InterbankConstants.BENE_NAME);
				if(!DataValidator.isEmptyString(beneName)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, beneName.length()>25?beneName.substring(0, 25):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);
	}
}
