package wenrgise.hrms.businessdelegate;

import java.rmi.RemoteException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.CreateException;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.RecordMetaInfo;
import wenrgise.common.vo.BaseQueryVO;
import wenrgise.common.vo.ThisPageVO;
import wenrgise.ejb.common.utility.ServiceLocator;
import wenrgise.hrms.bean.HrmLvEncashHdrBean;
import wenrgise.hrms.ejb.facade.HrmSecondFacade;
import wenrgise.hrms.ejb.facade.HrmSecondFacadeHome;
import wenrgise.hrms.vo.HrmLvEncashQVO;

public class HrmLvEncashBD extends HrmBaseBD {
  public RecordMetaInfo getHeaderMetaImpl(BaseQueryVO oQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      HrmSecondFacadeHome oHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
      HrmSecondFacade oHrmSecondFacade = oHome.create();
      return oHrmSecondFacade.getHrmEmpLeaveEncashHdrMetaInfo((HrmLvEncashQVO)oQueryVO);
    } catch (RemoteException oRe) {
      throw new EnrgiseSystemException(oRe);
    } catch (CreateException oCrt) {
      throw new EnrgiseSystemException(oCrt);
    } 
  }
  
  public ArrayList getHeaderRecordImpl(BaseQueryVO oQueryVO, long lStartPosition, long lLastPosition) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    try {
      HrmSecondFacadeHome oHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
      HrmSecondFacade oHrmSecondFacade = oHome.create();
      ArrayList oHeaderList = oHrmSecondFacade.getHrmEmpLeaveEncashHdrInfo((HrmLvEncashQVO)oQueryVO, lStartPosition, lLastPosition);
      return oHeaderList;
    } catch (RemoteException oRe) {
      throw new EnrgiseSystemException(oRe);
    } catch (CreateException oCrt) {
      throw new EnrgiseSystemException(oCrt);
    } 
  }
  
  public RecordMetaInfo getTotalDetailRecordCountImpl(String lPrimaryKey, String sScreenName) throws EnrgiseApplicationException, EnrgiseSystemException {
    RecordMetaInfo oRecordMetaInfo = null;
    return oRecordMetaInfo;
  }
  
  public ArrayList getDetailRecordImpl(String lPrimaryKey, String sScreenName, long lAbsoluteStart, long lAbsoluteEnd) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oRet = null;
    return oRet;
  }
  
  public boolean updateHeaderRecordImpl(BaseHeaderBean oBaseHeaderBean) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    return true;
  }
  
  public String getInsertDataImpl(BaseQueryVO oQueryVO) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    return "Only Implementation";
  }
  
  public String saveImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    try {
      HrmSecondFacadeHome oHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
      HrmSecondFacade oHrmSecondFacade = oHome.create();
      return oHrmSecondFacade.saveData((HrmLvEncashHdrBean)oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked, this.oUserInfo);
    } catch (CreateException oCrtExcep) {
      throw new EnrgiseSystemException(oCrtExcep);
    } 
  }
  
  public Map getDisabledFieldsImpl(ThisPageVO oThisPageVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    HashMap oHashMap = new HashMap();
    ArrayList oEnableList = new ArrayList();
    ArrayList oDisableList = new ArrayList();
    HrmLvEncashHdrBean oHrmLvEncashHdrBean = (HrmLvEncashHdrBean)oThisPageVO.getOHeaderBean();
    ArrayList oDetailList = oThisPageVO.getODetailList();
    oDisableList.add("butDelete");
    if (!oHrmLvEncashHdrBean.equals(null))
      if (oThisPageVO.getScreenMode().equalsIgnoreCase("Q")) {
        oDisableList.add("taxPercentage");
        oDisableList.add("butLeaveCodes");
        oDisableList.add("butDateApplied");
        oDisableList.add("daysEncashed");
        oDisableList.add("butSubmit");
        oEnableList.add("butEmpNo");
        oEnableList.add("butExecute");
        if (EnrgiseUtil.checkString(oHrmLvEncashHdrBean.getLevel())) {
          if (oHrmLvEncashHdrBean.getLevel().equalsIgnoreCase("0")) {
            oDisableList.add("taxPercentage");
            oDisableList.add("taxPercentage");
            oEnableList.add("butLeaveCodes");
            oDisableList.add("butDateApplied");
            oDisableList.add("daysEncashed");
          } else {
            oEnableList.add("taxPercentage");
            oDisableList.add("taxPercentage");
            oEnableList.add("butLeaveCodes");
            oDisableList.add("butDateApplied");
            oDisableList.add("daysEncashed");
          } 
        } else {
          oDisableList.add("taxPercentage");
          oEnableList.add("butLeaveCodes");
          oDisableList.add("butDateApplied");
          oDisableList.add("daysEncashed");
        } 
        oDisableList.add("butSubmit");
        oDisableList.add("butApprove");
        oDisableList.add("butReject");
      } else if (oThisPageVO.getScreenMode().equalsIgnoreCase("N")) {
        oEnableList.add("butLeaveCodes");
        oEnableList.add("butDateApplied");
        oEnableList.add("daysEncashed");
        oDisableList.add("butApprove");
        oDisableList.add("butReject");
        oDisableList.add("butSubmit");
        oDisableList.add("taxAmount");
        oEnableList.add("butEmpNo");
      } else if (oThisPageVO.getScreenMode().equalsIgnoreCase("U")) {
        oDisableList.add("butEmpNo");
        if (EnrgiseUtil.checkString(oHrmLvEncashHdrBean.getLevel())) {
          if (oHrmLvEncashHdrBean.getLevel().equalsIgnoreCase("0")) {
            if (EnrgiseUtil.checkString(oHrmLvEncashHdrBean.getChangeStatus()))
              if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("N")) {
                oDisableList.add("taxPercentage");
                oEnableList.add("butLeaveCodes");
                oEnableList.add("butDateApplied");
                oEnableList.add("daysEncashed");
                oDisableList.add("butApprove");
                oDisableList.add("butReject");
                oEnableList.add("butSubmit");
                oEnableList.add("taxAmount");
              } else if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("A")) {
                oDisableList.add("butLeaveCodes");
                oDisableList.add("taxPercentage");
                oDisableList.add("butDateApplied");
                oDisableList.add("daysEncashed");
                oDisableList.add("butApprove");
                oDisableList.add("butReject");
                oDisableList.add("butSubmit");
                oDisableList.add("taxAmount");
              } else if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("R")) {
                oDisableList.add("butLeaveCodes");
                oDisableList.add("butDateApplied");
                oDisableList.add("taxPercentage");
                oDisableList.add("daysEncashed");
                oDisableList.add("butApprove");
                oDisableList.add("butReject");
                oDisableList.add("butSubmit");
                oDisableList.add("taxAmount");
              } else if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("P")) {
                oDisableList.add("butLeaveCodes");
                oDisableList.add("butDateApplied");
                oDisableList.add("daysEncashed");
                oDisableList.add("butApprove");
                oDisableList.add("butReject");
                oDisableList.add("butSubmit");
                oDisableList.add("taxPercentage");
                oDisableList.add("taxAmount");
              }  
          } else if (!oHrmLvEncashHdrBean.getLevel().equalsIgnoreCase("0")) {
            if (EnrgiseUtil.checkString(oHrmLvEncashHdrBean.getChangeStatus()))
              if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("N")) {
                oEnableList.add("butLeaveCodes");
                oEnableList.add("butDateApplied");
                oEnableList.add("daysEncashed");
                oDisableList.add("taxPercentage");
                oDisableList.add("butApprove");
                oDisableList.add("butReject");
                oEnableList.add("butSubmit");
                oEnableList.add("taxAmount");
              } else if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("A")) {
                oDisableList.add("butLeaveCodes");
                oDisableList.add("taxPercentage");
                oDisableList.add("butDateApplied");
                oDisableList.add("daysEncashed");
                oDisableList.add("butApprove");
                oDisableList.add("butReject");
                oDisableList.add("butSubmit");
                oDisableList.add("taxAmount");
              } else if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("R")) {
                oDisableList.add("butLeaveCodes");
                oDisableList.add("butDateApplied");
                oDisableList.add("taxPercentage");
                oDisableList.add("daysEncashed");
                oDisableList.add("butApprove");
                oDisableList.add("butReject");
                oDisableList.add("butSubmit");
                oDisableList.add("taxAmount");
              } else if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("P")) {
                oDisableList.add("butLeaveCodes");
                oDisableList.add("butDateApplied");
                oDisableList.add("daysEncashed");
                oEnableList.add("butApprove");
                oEnableList.add("butReject");
                oDisableList.add("butSubmit");
                oEnableList.add("taxPercentage");
                oDisableList.add("taxAmount");
              }  
          } 
        } else {
          if (EnrgiseUtil.checkString(oHrmLvEncashHdrBean.getChangeStatus()))
            if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("N")) {
              oEnableList.add("butLeaveCodes");
              oEnableList.add("butDateApplied");
              oEnableList.add("daysEncashed");
              oDisableList.add("taxPercentage");
              oDisableList.add("butApprove");
              oDisableList.add("butReject");
              oEnableList.add("butSubmit");
              oEnableList.add("taxAmount");
            } else if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("A")) {
              oDisableList.add("butLeaveCodes");
              oDisableList.add("taxPercentage");
              oDisableList.add("butDateApplied");
              oDisableList.add("daysEncashed");
              oDisableList.add("butApprove");
              oDisableList.add("butReject");
              oDisableList.add("butSubmit");
              oDisableList.add("taxAmount");
            } else if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("R")) {
              oDisableList.add("butLeaveCodes");
              oDisableList.add("butDateApplied");
              oDisableList.add("taxPercentage");
              oDisableList.add("daysEncashed");
              oDisableList.add("butApprove");
              oDisableList.add("butReject");
              oDisableList.add("butSubmit");
              oDisableList.add("taxAmount");
            } else if (oHrmLvEncashHdrBean.getChangeStatus().equalsIgnoreCase("P")) {
              oDisableList.add("butLeaveCodes");
              oDisableList.add("dateApplied");
              oDisableList.add("butDateApplied");
              oDisableList.add("daysEncashed");
              oEnableList.add("butApprove");
              oEnableList.add("butReject");
              oDisableList.add("butSubmit");
              oDisableList.add("taxAmount");
            }  
          oDisableList.add("taxPercentage");
        } 
      }  
    oHashMap.put("D", oDisableList);
    oHashMap.put("E", oEnableList);
    return oHashMap;
  }
  
  public String submitImpl(String sButtonName, BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    try {
      String sHeaderPrimaryKey = null;
      HrmSecondFacadeHome oHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
      HrmSecondFacade oHrmSecondFacade = oHome.create();
      if (sButtonName.equalsIgnoreCase("butApprove"))
        sHeaderPrimaryKey = oHrmSecondFacade.approveLeaveEncash((HrmLvEncashHdrBean)oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked, this.oUserInfo); 
      if (sButtonName.equalsIgnoreCase("butReject"))
        sHeaderPrimaryKey = oHrmSecondFacade.rejectLvEncash((HrmLvEncashHdrBean)oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked, this.oUserInfo); 
      if (sButtonName.equalsIgnoreCase("butSubmit"))
        sHeaderPrimaryKey = oHrmSecondFacade.submitLvEncash((HrmLvEncashHdrBean)oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked, this.oUserInfo); 
      return sHeaderPrimaryKey;
    } catch (RemoteException oRe) {
      throw new EnrgiseSystemException(oRe);
    } catch (CreateException oCrt) {
      throw new EnrgiseSystemException(oCrt);
    } 
  }
  
  public void onLoadAction(ThisPageVO oThisPageVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    if (oThisPageVO.getActionName() == 10) {
      HrmLvEncashHdrBean oHrmLvEncashHdrBean = (HrmLvEncashHdrBean)oThisPageVO.getOHeaderBean();
      oHrmLvEncashHdrBean.setEmployeeId(this.oUserInfo.getUserTypeId());
      oHrmLvEncashHdrBean.setEmployeeNumber(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmLvEncashHdrBean.setFirstName(oName[0]);
      if (oName.length == 2) {
        oHrmLvEncashHdrBean.setMiddleName(" ");
        oHrmLvEncashHdrBean.setLastName(oName[1]);
      } else if (oName.length > 2) {
        oHrmLvEncashHdrBean.setMiddleName(oName[1]);
        oHrmLvEncashHdrBean.setLastName(oName[2]);
      } else {
        oHrmLvEncashHdrBean.setMiddleName(" ");
        oHrmLvEncashHdrBean.setLastName(" ");
      } 
      oHrmLvEncashHdrBean.setLocation(this.oUserInfo.getSiteName());
      oHrmLvEncashHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmLvEncashHdrBean.setDesignation(this.oUserInfo.getDesignation());
    } 
    if (oThisPageVO.getActionName() == 13) {
      HrmLvEncashHdrBean oHrmLvEncashHdrBean = (HrmLvEncashHdrBean)oThisPageVO.getOHeaderBean();
      oHrmLvEncashHdrBean.setEmployeeId(this.oUserInfo.getUserTypeId());
      oHrmLvEncashHdrBean.setEmployeeNumber(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmLvEncashHdrBean.setFirstName(oName[0]);
      if (oName.length == 2) {
        oHrmLvEncashHdrBean.setMiddleName(" ");
        oHrmLvEncashHdrBean.setLastName(oName[1]);
      } else if (oName.length > 2) {
        oHrmLvEncashHdrBean.setMiddleName(oName[1]);
        oHrmLvEncashHdrBean.setLastName(oName[2]);
      } else {
        oHrmLvEncashHdrBean.setMiddleName(" ");
        oHrmLvEncashHdrBean.setLastName(" ");
      } 
      oHrmLvEncashHdrBean.setLocation(this.oUserInfo.getSiteName());
      oHrmLvEncashHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmLvEncashHdrBean.setDesignation(this.oUserInfo.getDesignation());
    } 
    if (oThisPageVO.getActionName() == 19) {
      HrmLvEncashHdrBean oHrmLvEncashHdrBean = (HrmLvEncashHdrBean)oThisPageVO.getOHeaderBean();
      oHrmLvEncashHdrBean.setEmployeeId(this.oUserInfo.getUserTypeId());
      oHrmLvEncashHdrBean.setEmployeeNumber(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmLvEncashHdrBean.setFirstName(oName[0]);
      if (oName.length == 2) {
        oHrmLvEncashHdrBean.setMiddleName(" ");
        oHrmLvEncashHdrBean.setLastName(oName[1]);
      } else if (oName.length > 2) {
        oHrmLvEncashHdrBean.setMiddleName(oName[1]);
        oHrmLvEncashHdrBean.setLastName(oName[2]);
      } else {
        oHrmLvEncashHdrBean.setMiddleName(" ");
        oHrmLvEncashHdrBean.setLastName(" ");
      } 
      oHrmLvEncashHdrBean.setLocation(this.oUserInfo.getSiteName());
      oHrmLvEncashHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmLvEncashHdrBean.setDesignation(this.oUserInfo.getDesignation());
    } 
  }
}
