package WEB-INF.classes.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.HrmLvCancHdrBean;
import wenrgise.hrms.businessdelegate.HrmBaseBD;
import wenrgise.hrms.ejb.facade.HrmSecondFacade;
import wenrgise.hrms.ejb.facade.HrmSecondFacadeHome;
import wenrgise.hrms.vo.HrmLvCancQVO;

public class HrmLvCancBD extends HrmBaseBD {
  public RecordMetaInfo getHeaderMetaImpl(BaseQueryVO oQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      HrmSecondFacadeHome oHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
      HrmSecondFacade oHrmSecondFacade = oHome.create();
      return oHrmSecondFacade.getHrmEmpLeaveCanQueryScreenDtlMetaInfo((HrmLvCancQVO)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.getHrmEmpLeaveCanQueryScreenDtlInfo((HrmLvCancQVO)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 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((HrmLvCancHdrBean)oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked, this.oUserInfo);
    } catch (CreateException oCrtExcep) {
      throw new EnrgiseSystemException(oCrtExcep);
    } 
  }
  
  public String getInsertDataImpl(BaseQueryVO oQueryVO) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    return "Only Implementation";
  }
  
  public void onLoadAction(ThisPageVO oThisPageVO) {
    if (oThisPageVO.getActionName() == 10) {
      HrmLvCancHdrBean oHrmLvCancHdrBean = (HrmLvCancHdrBean)oThisPageVO.getOHeaderBean();
      oHrmLvCancHdrBean.setEmployeeId(this.oUserInfo.getUserTypeId());
      oHrmLvCancHdrBean.setEmployeeNo(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmLvCancHdrBean.setFirstName(oName[0]);
      if (oName.length == 2) {
        oHrmLvCancHdrBean.setMiddleName(" ");
        oHrmLvCancHdrBean.setLastName(oName[1]);
      } else if (oName.length > 2) {
        oHrmLvCancHdrBean.setMiddleName(oName[1]);
        oHrmLvCancHdrBean.setLastName(oName[2]);
      } else {
        oHrmLvCancHdrBean.setMiddleName(" ");
        oHrmLvCancHdrBean.setLastName(" ");
      } 
      oHrmLvCancHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmLvCancHdrBean.setDesig(this.oUserInfo.getDesignation());
      oHrmLvCancHdrBean.setLocation(this.oUserInfo.getSiteName());
    } else if (oThisPageVO.getActionName() == 10) {
      HrmLvCancHdrBean oHrmLvCancHdrBean = (HrmLvCancHdrBean)oThisPageVO.getOHeaderBean();
      oHrmLvCancHdrBean.setEmployeeId(this.oUserInfo.getUserTypeId());
      oHrmLvCancHdrBean.setEmployeeNo(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmLvCancHdrBean.setFirstName(oName[0]);
      if (oName.length == 2) {
        oHrmLvCancHdrBean.setMiddleName(" ");
        oHrmLvCancHdrBean.setLastName(oName[1]);
      } else if (oName.length > 2) {
        oHrmLvCancHdrBean.setMiddleName(oName[1]);
        oHrmLvCancHdrBean.setLastName(oName[2]);
      } else {
        oHrmLvCancHdrBean.setMiddleName(" ");
        oHrmLvCancHdrBean.setLastName(" ");
      } 
      oHrmLvCancHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmLvCancHdrBean.setDesig(this.oUserInfo.getDesignation());
      oHrmLvCancHdrBean.setLocation(this.oUserInfo.getSiteName());
    } else if (oThisPageVO.getActionName() == 13) {
      HrmLvCancHdrBean oHrmLvCancHdrBean = (HrmLvCancHdrBean)oThisPageVO.getOHeaderBean();
      oHrmLvCancHdrBean.setEmployeeId(this.oUserInfo.getUserTypeId());
      oHrmLvCancHdrBean.setEmployeeNo(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmLvCancHdrBean.setFirstName(oName[0]);
      if (oName.length == 2) {
        oHrmLvCancHdrBean.setMiddleName(" ");
        oHrmLvCancHdrBean.setLastName(oName[1]);
      } else if (oName.length > 2) {
        oHrmLvCancHdrBean.setMiddleName(oName[1]);
        oHrmLvCancHdrBean.setLastName(oName[2]);
      } else {
        oHrmLvCancHdrBean.setMiddleName(" ");
        oHrmLvCancHdrBean.setLastName(" ");
      } 
      oHrmLvCancHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmLvCancHdrBean.setDesig(this.oUserInfo.getDesignation());
      oHrmLvCancHdrBean.setLocation(this.oUserInfo.getSiteName());
    } else if (oThisPageVO.getActionName() == 19) {
      HrmLvCancHdrBean oHrmLvCancHdrBean = (HrmLvCancHdrBean)oThisPageVO.getOHeaderBean();
      oHrmLvCancHdrBean.setEmployeeId(this.oUserInfo.getUserTypeId());
      oHrmLvCancHdrBean.setEmployeeNo(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmLvCancHdrBean.setFirstName(oName[0]);
      if (oName.length == 2) {
        oHrmLvCancHdrBean.setMiddleName(" ");
        oHrmLvCancHdrBean.setLastName(oName[1]);
      } else if (oName.length > 2) {
        oHrmLvCancHdrBean.setMiddleName(oName[1]);
        oHrmLvCancHdrBean.setLastName(oName[2]);
      } else {
        oHrmLvCancHdrBean.setMiddleName(" ");
        oHrmLvCancHdrBean.setLastName(" ");
      } 
      oHrmLvCancHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmLvCancHdrBean.setDesig(this.oUserInfo.getDesignation());
      oHrmLvCancHdrBean.setLocation(this.oUserInfo.getSiteName());
    } 
  }
  
  public Map getDisabledFieldsImpl(ThisPageVO oThisPageVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    HashMap oHashMap = new HashMap();
    ArrayList oEnableList = new ArrayList();
    ArrayList oDisableList = new ArrayList();
    HrmLvCancHdrBean oHrmLvCancHdrBean = (HrmLvCancHdrBean)oThisPageVO.getOHeaderBean();
    ArrayList oDetailList = oThisPageVO.getODetailList();
    if (oHrmLvCancHdrBean.getCancelStatus() == null) {
      oDisableList.add("butApprove");
      oDisableList.add("butReject");
    } 
    if (oHrmLvCancHdrBean.getCancSide() != null) {
      if (oHrmLvCancHdrBean.getCancSide().equals("L")) {
        oHrmLvCancHdrBean.setFromCancelDate(oHrmLvCancHdrBean.getFromDate());
        oDisableList.add("butFromCancelDate");
      } 
      if (oHrmLvCancHdrBean.getCancSide().equals("R")) {
        oHrmLvCancHdrBean.setToCancelDate(oHrmLvCancHdrBean.getToDate());
        oDisableList.add("butToCancelDate");
      } 
      if (oHrmLvCancHdrBean.getCancSide().equals("F")) {
        oHrmLvCancHdrBean.setFromCancelDate(oHrmLvCancHdrBean.getFromDate());
        oHrmLvCancHdrBean.setToCancelDate(oHrmLvCancHdrBean.getToDate());
        oDisableList.add("butFromCancelDate");
        oDisableList.add("butToCancelDate");
      } 
    } 
    if (!oHrmLvCancHdrBean.equals(null))
      if (EnrgiseUtil.checkString(oHrmLvCancHdrBean.getCancelStatus()))
        if (oHrmLvCancHdrBean.getCancelStatus().equalsIgnoreCase("N")) {
          oDisableList.add("fromCancelDate");
          oEnableList.add("butFromCancelDate");
          oDisableList.add("toCancelDate");
          oEnableList.add("butToCancelDate");
          oDisableList.add("cancelDays");
          oDisableList.add("cancelDateApplied");
          oEnableList.add("butCancelDateApplied");
          oEnableList.add("cancelRemarks");
          oDisableList.add("cancelStatus");
          oEnableList.add("butApprove");
          oEnableList.add("butReject");
        } else if (oHrmLvCancHdrBean.getCancelStatus().equalsIgnoreCase("A")) {
          oDisableList.add("fromCancelDate");
          oDisableList.add("butFromCancelDate");
          oDisableList.add("toCancelDate");
          oDisableList.add("butToCancelDate");
          oDisableList.add("cancelDays");
          oDisableList.add("cancelDateApplied");
          oDisableList.add("butCancelDateApplied");
          oDisableList.add("cancelRemarks");
          oDisableList.add("cancelStatus");
          oDisableList.add("butApprove");
          oDisableList.add("butReject");
        } else if (oHrmLvCancHdrBean.getCancelStatus().equalsIgnoreCase("R")) {
          oDisableList.add("fromCancelDate");
          oDisableList.add("butFromCancelDate");
          oDisableList.add("toCancelDate");
          oDisableList.add("butToCancelDate");
          oDisableList.add("cancelDays");
          oDisableList.add("cancelDateApplied");
          oDisableList.add("butCancelDateApplied");
          oDisableList.add("cancelRemarks");
          oDisableList.add("cancelStatus");
          oDisableList.add("butApprove");
          oDisableList.add("butReject");
        } else if (oHrmLvCancHdrBean.getCancelStatus().equalsIgnoreCase("P")) {
          oDisableList.add("fromCancelDate");
          oDisableList.add("butFromCancelDate");
          oDisableList.add("toCancelDate");
          oDisableList.add("butToCancelDate");
          oDisableList.add("cancelDays");
          oDisableList.add("cancelDateApplied");
          oDisableList.add("butCancelDateApplied");
          oDisableList.add("cancelRemarks");
          oDisableList.add("cancelStatus");
          oEnableList.add("butApprove");
          oEnableList.add("butReject");
        }   
    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")) {
        ((HrmLvCancHdrBean)oBaseHeaderBean).setButtonName("A");
        sHeaderPrimaryKey = oHrmSecondFacade.approveLeaveCanc((HrmLvCancHdrBean)oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked, this.oUserInfo);
      } 
      if (sButtonName.equalsIgnoreCase("butReject")) {
        ((HrmLvCancHdrBean)oBaseHeaderBean).setButtonName("R");
        sHeaderPrimaryKey = oHrmSecondFacade.approveLeaveCanc((HrmLvCancHdrBean)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);
    } 
  }
}
