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.HrmTrngEmpReqHdrBean;
import wenrgise.hrms.ejb.facade.HrmSecondFacade;
import wenrgise.hrms.ejb.facade.HrmSecondFacadeHome;
import wenrgise.hrms.vo.HrmTrngEmpReqQVO;

public class HrmTrngEmpReqBD extends HrmBaseBD {
  public RecordMetaInfo getHeaderMetaImpl(BaseQueryVO oQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      HrmSecondFacadeHome oHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
      HrmSecondFacade oHrmSecondFacade = oHome.create();
      return oHrmSecondFacade.getHrmEmpTrngReqHeaderMetaInfo((HrmTrngEmpReqQVO)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.getHrmEmpTrngReqHeader((HrmTrngEmpReqQVO)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 {
    try {
      HrmSecondFacadeHome oHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
      HrmSecondFacade oHrmSecondFacade = oHome.create();
      return oHrmSecondFacade.getHrmEmpReqDetailMetaInfo(lPrimaryKey);
    } catch (RemoteException oRe) {
      throw new EnrgiseSystemException(oRe);
    } catch (CreateException oCrt) {
      throw new EnrgiseSystemException(oCrt);
    } 
  }
  
  public ArrayList getDetailRecordImpl(String lPrimaryKey, String sScreenName, long lAbsoluteStart, long lAbsoluteEnd) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    try {
      HrmSecondFacadeHome oHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
      HrmSecondFacade oHrmSecondFacade = oHome.create();
      return oHrmSecondFacade.getHrmEmpReqDetail(lPrimaryKey, lAbsoluteStart, lAbsoluteEnd);
    } catch (RemoteException oRe) {
      throw new EnrgiseSystemException(oRe);
    } catch (CreateException oCrt) {
      throw new EnrgiseSystemException(oCrt);
    } 
  }
  
  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.saveHrmEmpTrngReq((HrmTrngEmpReqHdrBean)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 Map getDisabledFieldsImpl(ThisPageVO oThisPageVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    HashMap oMap = new HashMap();
    ArrayList arylstDisbledFields = new ArrayList();
    ArrayList arylstEnabledFields = new ArrayList();
    String groupStatus = new String();
    arylstDisbledFields.add("butAddRow");
    arylstDisbledFields.add("butDelRow");
    HrmTrngEmpReqHdrBean oHrmTrngEmpReqHdrBean = (HrmTrngEmpReqHdrBean)oThisPageVO.getOHeaderBean();
    try {
      HrmSecondFacadeHome oHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
      HrmSecondFacade oHrmSecondFacade = oHome.create();
      String sUserTypeId = this.oUserInfo.getUserTypeId();
      groupStatus = oHrmSecondFacade.getGroupInformation(sUserTypeId);
    } catch (CreateException oCrtExcep) {
      throw new EnrgiseSystemException(oCrtExcep);
    } catch (RemoteException oRmtExcep) {
      throw new EnrgiseSystemException(oRmtExcep);
    } 
    if (EnrgiseUtil.checkString(groupStatus))
      if (groupStatus.equalsIgnoreCase("S")) {
        arylstEnabledFields.add("butEmployeeName");
      } else {
        arylstDisbledFields.add("butEmployeeName");
      }  
    if (oThisPageVO.getScreenMode().equals("Q")) {
      arylstDisbledFields.add("butSubmit");
      arylstDisbledFields.add("butApprove");
      arylstDisbledFields.add("butReject");
    } 
    if (oThisPageVO.getScreenMode().equals("N")) {
      arylstDisbledFields.add("butApprove");
      arylstDisbledFields.add("butReject");
      arylstDisbledFields.add("butSubmit");
      arylstEnabledFields.add("nature");
      arylstEnabledFields.add("purpose");
      arylstDisbledFields.add("rejectionRemarks");
      arylstEnabledFields.add("fullPart");
      arylstEnabledFields.add("butRecommender");
      arylstEnabledFields.add("butStartDate");
      arylstEnabledFields.add("butEndDate");
      arylstEnabledFields.add("butCourseName");
      if (EnrgiseUtil.checkString(oHrmTrngEmpReqHdrBean.getCourseId())) {
        arylstEnabledFields.add("butTrainingCalendar");
      } else {
        arylstDisbledFields.add("butTrainingCalendar");
      } 
      arylstEnabledFields.add("butDateSubmitted");
      arylstDisbledFields.add("date");
      arylstEnabledFields.add("participation");
    } else if (oThisPageVO.getScreenMode().equals("U")) {
      if (oHrmTrngEmpReqHdrBean.getApprovalStatus().equalsIgnoreCase("N")) {
        arylstEnabledFields.add("nature");
        arylstEnabledFields.add("purpose");
        arylstDisbledFields.add("rejectionRemarks");
        arylstEnabledFields.add("fullPart");
        arylstEnabledFields.add("butRecommender");
        arylstEnabledFields.add("butStartDate");
        arylstEnabledFields.add("butEndDate");
        arylstEnabledFields.add("butTrainingCalendar");
        arylstEnabledFields.add("butCourseName");
        arylstEnabledFields.add("butDateSubmitted");
        arylstDisbledFields.add("date");
        arylstEnabledFields.add("participation");
        arylstDisbledFields.add("butApprove");
        arylstDisbledFields.add("butReject");
        arylstEnabledFields.add("butGetDetail");
        if (oThisPageVO.getODetailList() != null) {
          arylstEnabledFields.add("butSubmit");
        } else {
          arylstDisbledFields.add("butSubmit");
        } 
      } 
      if (oHrmTrngEmpReqHdrBean.getApprovalStatus().equalsIgnoreCase("A")) {
        arylstDisbledFields.add("butApprove");
        arylstDisbledFields.add("butReject");
        arylstDisbledFields.add("butSubmit");
        arylstDisbledFields.add("nature");
        arylstDisbledFields.add("purpose");
        arylstDisbledFields.add("rejectionRemarks");
        arylstDisbledFields.add("fullPart");
        arylstDisbledFields.add("butRecommender");
        arylstDisbledFields.add("butStartDate");
        arylstDisbledFields.add("butEndDate");
        arylstDisbledFields.add("butTrainingCalendar");
        arylstDisbledFields.add("butCourseName");
        arylstDisbledFields.add("butDateSubmitted");
        arylstDisbledFields.add("date");
        arylstDisbledFields.add("participation");
      } 
      if (oHrmTrngEmpReqHdrBean.getApprovalStatus().equalsIgnoreCase("P")) {
        arylstEnabledFields.add("butApprove");
        arylstEnabledFields.add("butReject");
        arylstDisbledFields.add("butSubmit");
        arylstDisbledFields.add("nature");
        arylstDisbledFields.add("purpose");
        arylstDisbledFields.add("rejectionRemarks");
        arylstDisbledFields.add("fullPart");
        arylstDisbledFields.add("butRecommender");
        arylstDisbledFields.add("butStartDate");
        arylstDisbledFields.add("butEndDate");
        arylstDisbledFields.add("butTrainingCalendar");
        arylstDisbledFields.add("butCourseName");
        arylstDisbledFields.add("butDateSubmitted");
        arylstDisbledFields.add("date");
        arylstDisbledFields.add("participation");
      } 
      if (oHrmTrngEmpReqHdrBean.getApprovalStatus().equalsIgnoreCase("R")) {
        arylstDisbledFields.add("butApprove");
        arylstDisbledFields.add("butReject");
        arylstDisbledFields.add("butSubmit");
        arylstDisbledFields.add("nature");
        arylstDisbledFields.add("purpose");
        arylstEnabledFields.add("rejectionRemarks");
        arylstDisbledFields.add("fullPart");
        arylstDisbledFields.add("butRecommender");
        arylstDisbledFields.add("butStartDate");
        arylstDisbledFields.add("butEndDate");
        arylstDisbledFields.add("butTrainingCalendar");
        arylstDisbledFields.add("butCourseName");
        arylstDisbledFields.add("butDateSubmitted");
        arylstDisbledFields.add("date");
        arylstDisbledFields.add("participation");
      } 
    } 
    oMap.put("D", arylstDisbledFields);
    oMap.put("E", arylstEnabledFields);
    return oMap;
  }
  
  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("butSubmit"))
        sHeaderPrimaryKey = oHrmSecondFacade.submitEmpTrngReq((HrmTrngEmpReqHdrBean)oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked, this.oUserInfo); 
      if (sButtonName.equalsIgnoreCase("butApprove"))
        sHeaderPrimaryKey = oHrmSecondFacade.approveEmpTrngReq((HrmTrngEmpReqHdrBean)oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked, this.oUserInfo); 
      if (sButtonName.equalsIgnoreCase("butReject"))
        sHeaderPrimaryKey = oHrmSecondFacade.rejectEmpTrngReq((HrmTrngEmpReqHdrBean)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) {
      HrmTrngEmpReqHdrBean oHrmTrngEmpReqHdrBean = (HrmTrngEmpReqHdrBean)oThisPageVO.getOHeaderBean();
      oHrmTrngEmpReqHdrBean.setEmpId(this.oUserInfo.getUserTypeId());
      oHrmTrngEmpReqHdrBean.setEmployeeNumber(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmTrngEmpReqHdrBean.setFirstName(oName[0]);
      oHrmTrngEmpReqHdrBean.setMiddleName(oName[1]);
      oHrmTrngEmpReqHdrBean.setLastName(oName[2]);
      oHrmTrngEmpReqHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmTrngEmpReqHdrBean.setDesig(this.oUserInfo.getDesignation());
      oHrmTrngEmpReqHdrBean.setLocation(this.oUserInfo.getSiteName());
    } 
    if (oThisPageVO.getActionName() == 13) {
      HrmTrngEmpReqHdrBean oHrmTrngEmpReqHdrBean = (HrmTrngEmpReqHdrBean)oThisPageVO.getOHeaderBean();
      oHrmTrngEmpReqHdrBean.setEmpId(this.oUserInfo.getUserTypeId());
      oHrmTrngEmpReqHdrBean.setEmployeeNumber(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmTrngEmpReqHdrBean.setFirstName(oName[0]);
      oHrmTrngEmpReqHdrBean.setMiddleName(oName[1]);
      oHrmTrngEmpReqHdrBean.setLastName(oName[2]);
      oHrmTrngEmpReqHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmTrngEmpReqHdrBean.setDesig(this.oUserInfo.getDesignation());
      oHrmTrngEmpReqHdrBean.setLocation(this.oUserInfo.getSiteName());
    } else if (oThisPageVO.getActionName() == 19) {
      HrmTrngEmpReqHdrBean oHrmTrngEmpReqHdrBean = (HrmTrngEmpReqHdrBean)oThisPageVO.getOHeaderBean();
      oHrmTrngEmpReqHdrBean.setEmpId(this.oUserInfo.getUserTypeId());
      oHrmTrngEmpReqHdrBean.setEmployeeNumber(this.oUserInfo.getUserId());
      String[] oName = this.oUserInfo.getUserName().split(" ");
      oHrmTrngEmpReqHdrBean.setFirstName(oName[0]);
      oHrmTrngEmpReqHdrBean.setMiddleName(oName[1]);
      oHrmTrngEmpReqHdrBean.setLastName(oName[2]);
      oHrmTrngEmpReqHdrBean.setGrade(this.oUserInfo.getGrade());
      oHrmTrngEmpReqHdrBean.setDesig(this.oUserInfo.getDesignation());
      oHrmTrngEmpReqHdrBean.setLocation(this.oUserInfo.getSiteName());
    } 
  }
}
