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

import com.broadvision.data.common.TableData;

import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.enterprise.services.ib105_billpayment.constant.BillPaymentContants;
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 9, 2008
 * TAG AS:
 * REASON(S):
 * MODIFICATION:
 * </pre>
 */

/**
 * This class handles the back end process of IBFavPayeeAddConfirm.
 */
public class IBFavPayeeAddConfirmServices extends IBGenericPaymentServices
{
	private static Class c_oClass = IBFavPayeeAddConfirmServices.class;

	/**
	 * Class constructor specifying session object.
	 *
	 * @param m_oIBSessionObjects Session object contains all information stored in HTTP session
	 */
	public IBFavPayeeAddConfirmServices (IBSessionObjects m_oIBSessionObjects)
	{
		super (m_oIBSessionObjects);
		// TODO Auto-generated constructor stub
	}

	/**
	 * Check whether Bill Account no is already exist.
	 * if exist throw Business Exception
	 * Set some necessary data into beans.
	 *
	 * @throws IBGenericException Exception thrown for generic purposes
	 */
	public void executeServices () throws IBGenericException
	{
		String m_sMethodName = "executeServices";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);

		IBDynaBean m_oFavPayeeAddViewBean = null;
		String m_sNewBillAcctNo = null;

		TableData m_oTableData = null;
		int m_oTableDataSize = 0;
		
		String m_sPayeeCode = null;
		Boolean isCheckDigit = null;
		String m_sCheckDigitIndicator = null;
		IBDynaBean m_oPayeeDetailsBean = null;
		boolean isDIGI = false;

		try
		{
			m_oFavPayeeAddViewBean = (IBDynaBean) getIBDynaBean ().get (BillPaymentContants.FAV_PAYEE_ADD_VIEW_BEAN);
			m_oPayeeDetailsBean = (IBDynaBean) m_oFavPayeeAddViewBean.get(BillPaymentContants.PAYEE_DETAILS_VIEW_BEAN);
			
			m_sCheckDigitIndicator = (String) m_oPayeeDetailsBean.get(BillPaymentContants.VBEAN_CHECK_DIGIT_INDI);
			m_sPayeeCode = (String) m_oPayeeDetailsBean.get(BillPaymentContants.VBEAN_PAYEE_CODE);
			m_sNewBillAcctNo = (String) getIBDynaBean().get(BillPaymentContants.BILL_ACCOUNT_NO_ADDPAYEE);
			
			m_oTableData = getFavPayeeTableData(false);

			if (m_oTableData != null && m_oTableData.size () > 0)
			{
				m_oTableDataSize = m_oTableData.size ();
				String m_sBillAcctNo = "";
				for (int i = 0; i <m_oTableDataSize;  i++)
				{
					m_sBillAcctNo = (m_oTableData.getStringValue (i, BillPaymentContants.COL_BILL_ACCOUNT_NUMBER)).trim ();

					if (m_sNewBillAcctNo.equals (m_sBillAcctNo))
					{
						throw IBErrorHandler.createBusinessException (IBBusinessException.BILL_ACCOUNT_FAV_ADD,
								BillPaymentContants.ERROR_ID_ALREADY_EXIST);
					}
				}
			}
			String SHORTNAME = "";
			
			SHORTNAME = (String)m_oPayeeDetailsBean.get(BillPaymentContants.VBPAYEE_SHORT_NAME);
			if(SHORTNAME.equalsIgnoreCase("DIGIKL"))
			{
				isDIGI=true;
				m_sCheckDigitIndicator = "No";
			}
			if(isDIGI)
			{
				int length =0;
				String checkBillAccNo = "";
				boolean isNumeric = false;
				
				isNumeric = isNumeric(m_sNewBillAcctNo);
				length = m_sNewBillAcctNo.length();
				checkBillAccNo = m_sNewBillAcctNo.substring(0, 1);
				if(isNumeric)
				{
					//valid
					for (int j = 0; j < m_sNewBillAcctNo.length(); j++)
					{
						int code = m_sNewBillAcctNo.charAt(j);

						if (!(code >= 48 && code <= 57))
						{
							//error
							throw IBErrorHandler.createBusinessException(IBBusinessException.BILL_PAYMENT, BillPaymentContants.ERR_DIGI_BILL_ACC_NO);
						}
					}
				}
				else
				{
					throw IBErrorHandler.createBusinessException(IBBusinessException.BILL_PAYMENT, BillPaymentContants.ERR_DIGI_BILL_ACC_NO);
				}
				if(length != 13)
				{
					throw IBErrorHandler.createBusinessException(IBBusinessException.BILL_PAYMENT, BillPaymentContants.ERR_DIGI_BILL_ACC_LENGTH);
				}
				else
				{
					//valid
				}
				if(checkBillAccNo.equalsIgnoreCase("1"))
				{
					//valid
				}
				if(checkBillAccNo.equalsIgnoreCase("2"))
				{
					//valid
				}
				else if (!checkBillAccNo.equalsIgnoreCase("1") && !checkBillAccNo.equalsIgnoreCase("2"))
				{
						throw IBErrorHandler.createBusinessException(IBBusinessException.BILL_PAYMENT, BillPaymentContants.ERR_DIGI_BILL_ACC_NO);
				}
			}

			if(CommonConstants.isTrue (m_sCheckDigitIndicator))
			{
				if(!DataValidator.isEmptyString(m_sNewBillAcctNo))
				{
					try
					{
						isCheckDigit = BillPaymentDBUtils.BillerCheckDigit(this, m_sNewBillAcctNo, m_sPayeeCode);
						if(!isCheckDigit)
						{
							throw IBErrorHandler.createBusinessException (IBBusinessException.BILL_PAYMENT, "invalid.bill.no");
						}
					}
					catch (Exception e)
					{
						throw IBErrorHandler.createBusinessException (IBBusinessException.BILL_PAYMENT, "invalid.bill.no");
					}
				}
			}

			m_oFavPayeeAddViewBean.set(BillPaymentContants.BILL_ACCOUNT_NO_ADDPAYEE, m_sNewBillAcctNo);
			m_oFavPayeeAddViewBean.set(BillPaymentContants.BILL_HOLDER_ADDPAYEE, getIBDynaBean().get(BillPaymentContants.BILL_HOLDER_ADDPAYEE));
			
			IBDynaBean m_oServiceInfoBean = (IBDynaBean) m_oFavPayeeAddViewBean.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(SHORTNAME)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, SHORTNAME.length()>25?SHORTNAME.substring(0, 25):SHORTNAME);
				}

				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);
	}
	public static boolean isNumeric(String s) {
	    try { 
	        double d = Double.parseDouble(s);
	    } catch(NumberFormatException e) { 
	        return false; 
	    }
	    // only got here if we didn't return false
	    return true;
	}

}
