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

import com.broadvision.data.common.RowData;
import com.broadvision.data.common.TableData;

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.constant.ServiceInfo;

/**
 * <pre>
 * PROGRAMMER: Chang Giat Yeng
 * CHANGE-NO:
 * TASK-NO:
 * DATE CREATED: Dec 11, 2009
 * TAG AS:
 * REASON(S):
 * MODIFICATION:
 * </pre>
 */

/**
 * <Class description>
 */
public class IBEpfMaintAddConfirmServices extends IBEnterpriseServicesWorkspace
{
	private static Class c_oClass = IBEpfMaintAddConfirmServices.class;

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

	public void verifyEpfNo () throws IBGenericException
	{
		String m_sMethodName = "verifyInput";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);

		String m_sEpfNo = "";

		try
		{
			m_sEpfNo = (String)getIBDynaBean ().get (EPFConstants.EPF_NO);
			if (isEpfNoExist (m_sEpfNo))
			{
				throw IBErrorHandler.createBusinessException (IBBusinessException.EPF_PAYMENT, EPFConstants.VA_ERR_RECORD_EXISTED);
			}
			
			IBDynaBean m_oViewBean = (IBDynaBean) getIBDynaBean().get(EPFConstants.EPF_MAINTENANCE_VIEW);
			IBDynaBean m_oServiceInfoBean = (IBDynaBean) m_oViewBean.get (ServiceInfo.SERVICE_INFO_BEAN);
			if (((Boolean) m_oServiceInfoBean.get (ServiceInfo.BEAN_ATT_TAC_REQUIRED)).booleanValue ())
			{
				String beneName = (String)getIBDynaBean ().get (EPFConstants.EPF_BENE_NAME);
				IBDynaBean m_oSMSViewBean = (IBDynaBean) getIBSessionObjects().getHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN);
				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);
	}

	/**
	 * Check whether EPF number is already existed in database
	 *
	 * @param p_sEpfNo account number
	 * @return return true if EPF number already exist. otherwise, return false.
	 * @throws IBGenericException if any Exception are thrown
	 */
	private boolean isEpfNoExist (String p_sEpfNo) throws IBGenericException
	{
		String m_sMethodName = "isEpfNoExist";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName, String.class.getName ());

		TableData m_oTblData = null;
		boolean m_boEpfExist = false;
		int m_iLength = 0;
		RowData m_oRow = null;

		try
		{
			m_oTblData = IBEpfDBUtils.getAllEpfAccounts (getIBSessionObjects ());
			if (m_oTblData != null && m_oTblData.size () > 0)
			{
				m_iLength = m_oTblData.size ();
			    for (int i = 0; i < m_iLength; i++)
			    {
			    	m_oRow = m_oTblData.getRow(i);
			        if (m_oRow.getStringValue(EPFConstants.COL_EPF_NUMBER).equalsIgnoreCase (p_sEpfNo))
			        {
			        	m_boEpfExist = true;
			            break;
			        }
			    }
			}
		}
		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);

		return m_boEpfExist;
	}
}
