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.Iterator;
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.helper.QueryRow;
import wenrgise.ejb.common.utility.DBUtilitiesBean;
import wenrgise.ejb.common.utility.ServiceLocator;
import wenrgise.hrms.bean.HrmLvApplnDtlBean;
import wenrgise.hrms.bean.HrmLvApplnHdrBean;
import wenrgise.hrms.businessdelegate.HrmBaseBD;
import wenrgise.hrms.ejb.facade.HrmSecondFacade;
import wenrgise.hrms.ejb.facade.HrmSecondFacadeHome;
import wenrgise.hrms.vo.HrmLvApplnQVO;

public class HrmLvApplnBD extends HrmBaseBD {
  public RecordMetaInfo getHeaderMetaImpl(BaseQueryVO oQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      HrmSecondFacadeHome oHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
      HrmSecondFacade oHrmSecondFacade = oHome.create();
      return oHrmSecondFacade.getHrmEmpLeaveAppQueryScreenDtlMetaInfo((HrmLvApplnQVO)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.getHrmEmpLeaveAppQueryScreenDtlInfo((HrmLvApplnQVO)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 {
    return null;
  }
  
  public boolean updateHeaderRecordImpl(BaseHeaderBean oBaseHeaderBean) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    return true;
  }
  
  public String getInsertDataImpl(BaseQueryVO oQueryVO) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    return new String();
  }
  
  public Map getDisabledFieldsImpl(ThisPageVO oThisPageVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    HashMap oHashMap = new HashMap();
    ArrayList oEnableList = new ArrayList();
    ArrayList oDisableList = new ArrayList();
    HrmLvApplnHdrBean oHrmLvApplnHdrBean = (HrmLvApplnHdrBean)oThisPageVO.getOHeaderBean();
    ArrayList oDetailList = null;
    oDisableList.add("butDelete");
    oDisableList.add("butApply");
    if (oHrmLvApplnHdrBean != null)
      if (oHrmLvApplnHdrBean.getDetailArray().size() != 0) {
        oThisPageVO.setODetailList(oHrmLvApplnHdrBean.getDetailArray());
        oDetailList = oThisPageVO.getODetailList();
      }  
    if (oThisPageVO.getScreenMode().equals("N")) {
      oDisableList.add("butApprove");
      oDisableList.add("butLedger");
      oDisableList.add("butReject");
    } 
    if (oThisPageVO.getScreenMode().equals("Q")) {
      oDisableList.add("butApprove");
      oDisableList.add("butLedger");
      oDisableList.add("butReject");
      oDisableList.add("ltaFlag");
      oDisableList.add("optedFor");
      oDisableList.add("fromYear");
      oDisableList.add("itPercentage");
      oDisableList.add("medFlag");
    } 
    if (oThisPageVO.getScreenMode().equals("N")) {
      if (oHrmLvApplnHdrBean.getStatusOfLeaves() == null) {
        oHrmLvApplnHdrBean.setStatusOfLeaves("N");
        oThisPageVO.setOHeaderBean((BaseHeaderBean)oHrmLvApplnHdrBean);
      } 
      if (oDetailList != null)
        if (oDetailList.size() != 0) {
          Iterator oIt = oDetailList.iterator();
          while (oIt.hasNext()) {
            HrmLvApplnDtlBean oDtlBean = oIt.next();
            if (oDtlBean.getTxtHalfDayFlag().equals("1")) {
              oDtlBean.setTxtHalfDayFlag("1");
              oDtlBean.setDisabtxtHalfDayFlag("true");
            } 
            if (oDtlBean.getTxtHalfDayFlag().equals("2"))
              oDtlBean.setTxtHalfDayFlag("2"); 
          } 
          oHrmLvApplnHdrBean.setDays(getHolidayCount(oDetailList));
          oThisPageVO.setODetailList(oDetailList);
          oHrmLvApplnHdrBean.setDetailArray(oDetailList);
        }  
      oEnableList.add("butApply");
      oEnableList.add("butGetDetail");
      oDisableList.add("butApprove");
      oDisableList.add("butLedger");
      oDisableList.add("butReject");
    } 
    if (oThisPageVO.getScreenMode().equals("U"))
      if (getNextLevelUserId(oHrmLvApplnHdrBean.getHeaderPrimaryKey()).equalsIgnoreCase(this.oUserInfo.getUserTypeId())) {
        String s2 = oHrmLvApplnHdrBean.getHeaderPrimaryKey();
        String s = getNextLevelUserId(oHrmLvApplnHdrBean.getHeaderPrimaryKey());
        oEnableList.add("butApprove");
        oEnableList.add("butLedger");
        oEnableList.add("butReject");
      } else {
        oEnableList.add("butGetDetail");
        oDisableList.add("butApprove");
        oDisableList.add("butLedger");
        oDisableList.add("butReject");
      }  
    if (oHrmLvApplnHdrBean != null) {
      if (EnrgiseUtil.checkString(oHrmLvApplnHdrBean.getStatusOfLeaves()))
        if (oHrmLvApplnHdrBean.getStatusOfLeaves().equalsIgnoreCase("N")) {
          oDisableList.add("leaveCodes");
          oEnableList.add("butLeaveCodes");
          oDisableList.add("description");
          oDisableList.add("dateApplied");
          oEnableList.add("butDateApplied");
          oDisableList.add("fromAppDate");
          oEnableList.add("butFromAppDate");
          oDisableList.add("toAppDate");
          oEnableList.add("butToAppDate");
          oDisableList.add("days");
          oEnableList.add("address");
          oEnableList.add("phone");
          oEnableList.add("purpose");
          oEnableList.add("remarks");
          if (EnrgiseUtil.checkString(oHrmLvApplnHdrBean.getLeaveCodes())) {
            if (oHrmLvApplnHdrBean.getLeaveCodes().equalsIgnoreCase("EL")) {
              oEnableList.add("ltaFlag");
              oEnableList.add("optedFor");
              oEnableList.add("itPercentage");
              oEnableList.add("fromYear");
              oDisableList.add("medFlag");
            } else {
              oDisableList.add("ltaFlag");
              oDisableList.add("optedFor");
              oDisableList.add("fromYear");
              oDisableList.add("itPercentage");
              oEnableList.add("medFlag");
            } 
          } else {
            oDisableList.add("ltaFlag");
            oDisableList.add("optedFor");
            oDisableList.add("fromYear");
            oDisableList.add("itPercentage");
            oDisableList.add("medFlag");
          } 
          oDisableList.add("butApprove");
          oDisableList.add("butLedger");
          oDisableList.add("butReject");
          oEnableList.add("butGetDetail");
        } else if (oHrmLvApplnHdrBean.getStatusOfLeaves().equalsIgnoreCase("A")) {
          oDisableList.add("leaveCodes");
          oDisableList.add("description");
          oDisableList.add("dateApplied");
          oDisableList.add("fromAppDate");
          oDisableList.add("toAppDate");
          oDisableList.add("days");
          oDisableList.add("butSave");
          oDisableList.add("butLeaveCodes");
          oDisableList.add("butDateApplied");
          oDisableList.add("butFromAppDate");
          oDisableList.add("butToAppDate");
          oDisableList.add("address");
          oDisableList.add("phone");
          oDisableList.add("purpose");
          oDisableList.add("remarks");
          oDisableList.add("butGetDetail");
          oDisableList.add("ltaFlag");
          oDisableList.add("optedFor");
          oDisableList.add("fromYear");
          oDisableList.add("itPercentage");
          oDisableList.add("medFlag");
          oDisableList.add("butApprove");
          oDisableList.add("butLedger");
          oDisableList.add("butReject");
          oDisableList.add("butDelete");
        } else if (oHrmLvApplnHdrBean.getStatusOfLeaves().equalsIgnoreCase("R")) {
          oDisableList.add("leaveCodes");
          oDisableList.add("butLeaveCodes");
          oDisableList.add("description");
          oDisableList.add("dateApplied");
          oDisableList.add("butDateApplied");
          oDisableList.add("fromAppDate");
          oDisableList.add("butFromAppDate");
          oDisableList.add("toAppDate");
          oDisableList.add("butToAppDate");
          oDisableList.add("days");
          oDisableList.add("address");
          oDisableList.add("phone");
          oDisableList.add("purpose");
          oDisableList.add("remarks");
          oDisableList.add("ltaFlag");
          oDisableList.add("optedFor");
          oDisableList.add("fromYear");
          oDisableList.add("itPercentage");
          oDisableList.add("butApprove");
          oDisableList.add("butLedger");
          oDisableList.add("butReject");
        } else if (oHrmLvApplnHdrBean.getStatusOfLeaves().equalsIgnoreCase("")) {
          oDisableList.add("leaveCodes");
          oDisableList.add("butLeaveCodes");
          oDisableList.add("description");
          oDisableList.add("dateApplied");
          oDisableList.add("butDateApplied");
          oDisableList.add("fromAppDate");
          oDisableList.add("butFromAppDate");
          oDisableList.add("toAppDate");
          oDisableList.add("butToAppDate");
          oDisableList.add("days");
          oDisableList.add("address");
          oDisableList.add("phone");
          oDisableList.add("purpose");
          oDisableList.add("remarks");
          oDisableList.add("ltaFlag");
          oDisableList.add("optedFor");
          oDisableList.add("fromYear");
          oDisableList.add("itPercentage");
          oDisableList.add("butApprove");
          oDisableList.add("butLedger");
          oDisableList.add("butReject");
        } else if (oHrmLvApplnHdrBean.getStatusOfLeaves().equalsIgnoreCase("P")) {
          oDisableList.add("leaveCodes");
          oDisableList.add("butLeaveCodes");
          oDisableList.add("description");
          oDisableList.add("dateApplied");
          oDisableList.add("butDateApplied");
          oDisableList.add("fromAppDate");
          oDisableList.add("butFromAppDate");
          oDisableList.add("toAppDate");
          oDisableList.add("butToAppDate");
          oDisableList.add("days");
          oDisableList.add("address");
          oDisableList.add("phone");
          oDisableList.add("purpose");
          oDisableList.add("remarks");
          oDisableList.add("statusOfLeaves");
          if (oHrmLvApplnHdrBean.getWorkListId() == null) {
            oDisableList.add("butApprove");
            oDisableList.add("butLedger");
            oDisableList.add("butReject");
            oDisableList.add("ltaFlag");
            oDisableList.add("optedFor");
            oDisableList.add("fromYear");
            oDisableList.add("itPercentage");
          } else {
            oEnableList.add("butApprove");
            oEnableList.add("butLedger");
            oEnableList.add("butReject");
            oDisableList.add("ltaFlag");
            oDisableList.add("optedFor");
            oDisableList.add("fromYear");
            oDisableList.add("itPercentage");
          } 
        }  
    } else {
      oDisableList.add("butApprove");
      oDisableList.add("butLedger");
      oDisableList.add("butReject");
    } 
    oHashMap.put("D", oDisableList);
    oHashMap.put("E", oEnableList);
    return oHashMap;
  }
  
  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((HrmLvApplnHdrBean)oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked, this.oUserInfo);
    } catch (CreateException oCrtExcep) {
      throw new EnrgiseSystemException(oCrtExcep);
    } 
  }
  
  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")) {
        ((HrmLvApplnHdrBean)oBaseHeaderBean).setButtonName("A");
        sHeaderPrimaryKey = oHrmSecondFacade.approveLeaveAppln((HrmLvApplnHdrBean)oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked, this.oUserInfo);
      } 
      if (sButtonName.equalsIgnoreCase("butReject")) {
        ((HrmLvApplnHdrBean)oBaseHeaderBean).setButtonName("R");
        sHeaderPrimaryKey = oHrmSecondFacade.approveLeaveAppln((HrmLvApplnHdrBean)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) {
    if (oThisPageVO.getActionName() == 10) {
      HrmLvApplnHdrBean oHrmLvApplnHdrBean = (HrmLvApplnHdrBean)oThisPageVO.getOHeaderBean();
      oHrmLvApplnHdrBean.setEmployeeId(this.oUserInfo.getUserTypeId());
      oHrmLvApplnHdrBean.setEmployeeNumber(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmLvApplnHdrBean.setFirstName(oName[0]);
      if (oName.length == 2) {
        oHrmLvApplnHdrBean.setMiddleName(" ");
        oHrmLvApplnHdrBean.setLastName(oName[1]);
      } else if (oName.length > 2) {
        oHrmLvApplnHdrBean.setMiddleName(oName[1]);
        oHrmLvApplnHdrBean.setLastName(oName[2]);
      } else {
        oHrmLvApplnHdrBean.setMiddleName(" ");
        oHrmLvApplnHdrBean.setLastName(" ");
      } 
      oHrmLvApplnHdrBean.setLocation(this.oUserInfo.getSiteName());
      oHrmLvApplnHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmLvApplnHdrBean.setDesignation(this.oUserInfo.getDesignation());
      oHrmLvApplnHdrBean.setSiteId(this.oUserInfo.getSiteId());
    } else if (oThisPageVO.getActionName() == 13) {
      HrmLvApplnHdrBean oHrmLvApplnHdrBean = (HrmLvApplnHdrBean)oThisPageVO.getOHeaderBean();
      oHrmLvApplnHdrBean.setEmployeeId(this.oUserInfo.getUserTypeId());
      oHrmLvApplnHdrBean.setEmployeeNumber(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmLvApplnHdrBean.setFirstName(oName[0]);
      if (oName.length == 2) {
        oHrmLvApplnHdrBean.setMiddleName(" ");
        oHrmLvApplnHdrBean.setLastName(oName[1]);
      } else if (oName.length > 2) {
        oHrmLvApplnHdrBean.setMiddleName(oName[1]);
        oHrmLvApplnHdrBean.setLastName(oName[2]);
      } else {
        oHrmLvApplnHdrBean.setMiddleName(" ");
        oHrmLvApplnHdrBean.setLastName(" ");
      } 
      oHrmLvApplnHdrBean.setLocation(this.oUserInfo.getSiteName());
      oHrmLvApplnHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmLvApplnHdrBean.setDesignation(this.oUserInfo.getDesignation());
      oHrmLvApplnHdrBean.setSiteId(this.oUserInfo.getSiteId());
    } else if (oThisPageVO.getActionName() == 19) {
      HrmLvApplnHdrBean oHrmLvApplnHdrBean = (HrmLvApplnHdrBean)oThisPageVO.getOHeaderBean();
      oHrmLvApplnHdrBean.setEmployeeId(this.oUserInfo.getUserTypeId());
      oHrmLvApplnHdrBean.setEmployeeNumber(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmLvApplnHdrBean.setFirstName(oName[0]);
      if (oName.length == 2) {
        oHrmLvApplnHdrBean.setMiddleName(" ");
        oHrmLvApplnHdrBean.setLastName(oName[1]);
      } else if (oName.length > 2) {
        oHrmLvApplnHdrBean.setMiddleName(oName[1]);
        oHrmLvApplnHdrBean.setLastName(oName[2]);
      } else {
        oHrmLvApplnHdrBean.setMiddleName(" ");
        oHrmLvApplnHdrBean.setLastName(" ");
      } 
      oHrmLvApplnHdrBean.setLocation(this.oUserInfo.getSiteName());
      oHrmLvApplnHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmLvApplnHdrBean.setDesignation(this.oUserInfo.getDesignation());
      oHrmLvApplnHdrBean.setSiteId(this.oUserInfo.getSiteId());
    } 
  }
  
  private String getHolidayCount(ArrayList arylstDtl) {
    double dHolidayCount = 0.0D;
    if (arylstDtl != null) {
      Iterator itrDtlList = arylstDtl.iterator();
      while (itrDtlList.hasNext()) {
        HrmLvApplnDtlBean oDtlBean = itrDtlList.next();
        if (!oDtlBean.getTxtHolidayFlag().equalsIgnoreCase("H")) {
          if (!oDtlBean.getTxtHalfDayFlag().equalsIgnoreCase("3")) {
            dHolidayCount += 0.5D;
            continue;
          } 
          dHolidayCount++;
        } 
      } 
    } 
    return Double.toString(dHolidayCount);
  }
  
  public ArrayList getLeaveBreakup(BaseHeaderBean oBaseHeaderBean) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    try {
      HrmSecondFacadeHome oHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
      HrmSecondFacade oHrmSecondFacade = oHome.create();
      ArrayList arylstDtl = oHrmSecondFacade.getLeaveDates((HrmLvApplnHdrBean)oBaseHeaderBean);
      return arylstDtl;
    } catch (CreateException oCrt) {
      throw new EnrgiseSystemException(oCrt);
    } 
  }
  
  private String getNextLevelUserId(String headerPrimaryKey) throws EnrgiseApplicationException, EnrgiseSystemException {
    String NextLevelUserId = new String();
    String sQuery = String.valueOf(" select t.delegator_emp_id from workflow_tasklist t where t.status = 'P' and t.doc_dtl_id = ").concat(String.valueOf(headerPrimaryKey));
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oList = oBean.executeQuery(sQuery);
    if (oList.size() != 0) {
      QueryRow oRow = null;
      Iterator oIt = oList.iterator();
      while (oIt.hasNext()) {
        oRow = oIt.next();
        NextLevelUserId = oRow.get("delegator_emp_id").getString();
      } 
    } 
    return NextLevelUserId;
  }
}
