package 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.utility.ServiceLocator;
import wenrgise.hrms.bean.HrmAppTempHdrBean;
import wenrgise.hrms.bean.HrmAppraisalTemplateKpaDtlBean;
import wenrgise.hrms.bean.HrmAppraisalTemplateSecDtlBean;
import wenrgise.hrms.ejb.facade.HrmThirdFacade;
import wenrgise.hrms.ejb.facade.HrmThirdFacadeHome;
import wenrgise.hrms.vo.HrmAppTempQueryVO;

public class HrmAppTempBD extends HrmBaseBD {
  public RecordMetaInfo getHeaderMetaImpl(BaseQueryVO oQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      HrmThirdFacadeHome oHome = (HrmThirdFacadeHome)ServiceLocator.getLocator().getService("HrmThirdFacade");
      HrmThirdFacade oHrmThirdFacade = oHome.create();
      return oHrmThirdFacade.getHrmAppTemplateHdrMetaInfo((HrmAppTempQueryVO)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 {
      HrmThirdFacadeHome oHome = (HrmThirdFacadeHome)ServiceLocator.getLocator().getService("HrmThirdFacade");
      HrmThirdFacade oHrmThirdFacade = oHome.create();
      ArrayList oHeaderList = oHrmThirdFacade.getHrmAppTemplateHeaderInfo((HrmAppTempQueryVO)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 = new RecordMetaInfo();
    try {
      HrmThirdFacadeHome oHome = (HrmThirdFacadeHome)ServiceLocator.getLocator().getService("HrmThirdFacade");
      HrmThirdFacade oHrmThirdFacade = oHome.create();
      if (sScreenName.equalsIgnoreCase("HrmAppraisalTemplateSection")) {
        oRecordMetaInfo = oHrmThirdFacade.getHrmAppTempSecDtlMetaInfo(lPrimaryKey);
      } else if (sScreenName.equalsIgnoreCase("HrmAppraisalTemplateKPA")) {
        oRecordMetaInfo = oHrmThirdFacade.getHrmAppTempSecKPADtlMetaInfo(lPrimaryKey);
      } 
    } catch (RemoteException oRe) {
      throw new EnrgiseSystemException(oRe);
    } catch (CreateException oCrt) {
      throw new EnrgiseSystemException(oCrt);
    } 
    return oRecordMetaInfo;
  }
  
  public ArrayList getDetailRecordImpl(String lPrimaryKey, String sScreenName, long lAbsoluteStart, long lAbsoluteEnd) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstRet = new ArrayList();
    try {
      HrmThirdFacadeHome oHome = (HrmThirdFacadeHome)ServiceLocator.getLocator().getService("HrmThirdFacade");
      HrmThirdFacade oHrmThirdFacade = oHome.create();
      if (sScreenName.equalsIgnoreCase("HrmAppraisalTemplateSection")) {
        arylstRet = oHrmThirdFacade.getHrmAppTempSecDetailInfo(lPrimaryKey, lAbsoluteStart, lAbsoluteEnd);
      } else if (sScreenName.equalsIgnoreCase("HrmAppraisalTemplateKPA")) {
        arylstRet = oHrmThirdFacade.getHrmAppTempSecKPADetailInfo(lPrimaryKey, lAbsoluteStart, lAbsoluteEnd);
      } 
    } catch (RemoteException oRe) {
      throw new EnrgiseSystemException(oRe);
    } catch (CreateException oCrt) {
      throw new EnrgiseSystemException(oCrt);
    } 
    return arylstRet;
  }
  
  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 {
      HrmThirdFacadeHome oHome = (HrmThirdFacadeHome)ServiceLocator.getLocator().getService("HrmThirdFacade");
      HrmThirdFacade oHrmThirdFacade = oHome.create();
      return oHrmThirdFacade.saveHrmAprslTmplSec((HrmAppTempHdrBean)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 oHashMap = new HashMap();
    ArrayList arylstEnableList = new ArrayList();
    ArrayList arylstDisableList = new ArrayList();
    arylstDisableList.add("butDelete");
    arylstDisableList.add("butDelRow");
    ArrayList oDetailList = oThisPageVO.getODetailList();
    HrmAppTempHdrBean oHrmAppTempHdrBean = (HrmAppTempHdrBean)oThisPageVO.getOHeaderBean();
    if (EnrgiseUtil.checkString(oHrmAppTempHdrBean.getHeaderFlag()))
      if (oHrmAppTempHdrBean.getHeaderFlag().equalsIgnoreCase("A")) {
        arylstDisableList.add("butAddRow");
        arylstDisableList.add("butRatingCode");
        arylstDisableList.add("obsolete");
        arylstDisableList.add("applicableToAll");
        arylstDisableList.add("butApprove");
        arylstDisableList.add("templateName");
        arylstDisableList.add("ratingCode");
      }  
    if (oThisPageVO.getScreenMode().equalsIgnoreCase("N") || oThisPageVO.getScreenMode().equalsIgnoreCase("U")) {
      arylstDisableList.add("butTemplateName");
      arylstDisableList.add("ratingCode");
    } 
    if (oThisPageVO.getScreenMode().equalsIgnoreCase("U"))
      arylstDisableList.add("templateName"); 
    if (oDetailList == null)
      arylstDisableList.add("butApprove"); 
    if (oThisPageVO.getScreenMode().equalsIgnoreCase("Q"))
      arylstDisableList.add("butRatingCode"); 
    if (oDetailList != null)
      if (oThisPageVO.getScreenName().equalsIgnoreCase("HrmAppraisalTemplateSection")) {
        Iterator oIt = oDetailList.iterator();
        while (oIt.hasNext()) {
          HrmAppraisalTemplateSecDtlBean oHrmAppraisalTemplateSecDtlBean = oIt.next();
          if (!oHrmAppraisalTemplateSecDtlBean.getStatus().equalsIgnoreCase("N")) {
            oHrmAppraisalTemplateSecDtlBean.setDisabtxtSrlNo("true");
            oHrmAppraisalTemplateSecDtlBean.setDisabtxtSectionCode("true");
            oHrmAppraisalTemplateSecDtlBean.setDisabbutParentSecCode("true");
            oHrmAppraisalTemplateSecDtlBean.setDisabtxtParentSection("true");
            if (EnrgiseUtil.checkString(oHrmAppTempHdrBean.getHeaderFlag()))
              if (oHrmAppTempHdrBean.getHeaderFlag().equalsIgnoreCase("A")) {
                oHrmAppraisalTemplateSecDtlBean.setDisabtxtAppraiserType("true");
                oHrmAppraisalTemplateSecDtlBean.setDisabtxtRatingReqd("true");
                oHrmAppraisalTemplateSecDtlBean.setDisabtxtSectionWeightage("true");
                oHrmAppraisalTemplateSecDtlBean.setDisabtxtTemplateRemarks("true");
              }  
          } 
          if (oHrmAppraisalTemplateSecDtlBean.getStatus().equalsIgnoreCase("N"))
            oHrmAppraisalTemplateSecDtlBean.setDisabtxtParentSection("true"); 
        } 
      }  
    if (oDetailList != null)
      if (oThisPageVO.getScreenName().equalsIgnoreCase("HrmAppraisalTemplateKPA")) {
        Iterator oIt = oDetailList.iterator();
        while (oIt.hasNext()) {
          HrmAppraisalTemplateKpaDtlBean oHrmAppraisalTemplateKpaDtlBean = oIt.next();
          if (!oHrmAppraisalTemplateKpaDtlBean.getStatus().equalsIgnoreCase("N")) {
            oHrmAppraisalTemplateKpaDtlBean.setDisabtxtKpaGroupCode("true");
            oHrmAppraisalTemplateKpaDtlBean.setDisabtxtApprKpa("true");
            if (EnrgiseUtil.checkString(oHrmAppTempHdrBean.getHeaderFlag()))
              if (oHrmAppTempHdrBean.getHeaderFlag().equalsIgnoreCase("A")) {
                oHrmAppraisalTemplateKpaDtlBean.setDisabbutKPAGroupCode("true");
                oHrmAppraisalTemplateKpaDtlBean.setDisabtxtWeightage("true");
              }  
          } 
          if (oHrmAppraisalTemplateKpaDtlBean.getStatus().equalsIgnoreCase("N")) {
            oHrmAppraisalTemplateKpaDtlBean.setDisabtxtKpaGroupCode("true");
            oHrmAppraisalTemplateKpaDtlBean.setDisabtxtApprKpa("true");
          } 
        } 
      }  
    oHashMap.put("D", arylstDisableList);
    oHashMap.put("E", arylstEnableList);
    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;
      HrmThirdFacadeHome oHome = (HrmThirdFacadeHome)ServiceLocator.getLocator().getService("HrmThirdFacade");
      HrmThirdFacade oHrmThirdFacade = oHome.create();
      if (sButtonName.equalsIgnoreCase("butApprove"))
        sHeaderPrimaryKey = oHrmThirdFacade.approveHrmAprslTmplMst((HrmAppTempHdrBean)oBaseHeaderBean, sScreenName); 
      return sHeaderPrimaryKey;
    } catch (RemoteException oRe) {
      throw new EnrgiseSystemException(oRe);
    } catch (CreateException oCrt) {
      throw new EnrgiseSystemException(oCrt);
    } 
  }
}
