package wenrgise.common.businessdelegate;

import java.rmi.RemoteException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.EJBObject;
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.utility.UserInfo;
import wenrgise.common.vo.BaseDetailVO;
import wenrgise.common.vo.BaseHeaderVO;
import wenrgise.common.vo.BaseQueryVO;
import wenrgise.common.vo.DetailSizeValues;
import wenrgise.common.vo.ThisPageVO;
import wenrgise.ejb.common.session.UserSession;

public abstract class BaseBD {
  protected Timestamp oWhenDetailPicked = null;
  
  protected long lTotalRecordCount = 0L;
  
  protected long lStartPosition = 0L;
  
  protected long lLastPosition = 0L;
  
  protected long lDetailFirstPosition = 0L;
  
  protected long lDetailLastPosition = 0L;
  
  protected long lTotalDetailRecordCount = 0L;
  
  protected UserInfo oUserInfo = null;
  
  protected HashMap oMap = new HashMap();
  
  protected String moduleName = null;
  
  public long getLTotalRecordCount() {
    return this.lTotalRecordCount;
  }
  
  public void setLTotalRecordCount(long newLTotalRecordCount) {
    this.lTotalRecordCount = newLTotalRecordCount;
  }
  
  public long getLDetailFirstPosition() {
    return this.lDetailFirstPosition;
  }
  
  public void setLDetailFirstPosition(long newLDetailFirstPosition) {
    this.lDetailFirstPosition = newLDetailFirstPosition;
  }
  
  public long getLDetailLastPosition() {
    return this.lDetailLastPosition;
  }
  
  public void setLDetailLastPosition(long newLDetailLastPosition) {
    this.lDetailLastPosition = newLDetailLastPosition;
  }
  
  public long getLTotalDetailRecordCount() {
    return this.lTotalDetailRecordCount;
  }
  
  public void setLTotalDetailRecordCount(long newLTotalDetailRecordCount) {
    this.lTotalDetailRecordCount = newLTotalDetailRecordCount;
  }
  
  public BaseHeaderVO getHeaderRecord(BaseQueryVO oQueryVO, String sFormName, UserSession oUser) throws EnrgiseApplicationException, EnrgiseSystemException {
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    try {
      RecordMetaInfo oHeaderMetaInfo = getHeaderMetaImpl(oQueryVO);
      if (oHeaderMetaInfo != null) {
        oBaseHeaderVO.setOWhenPicked(oHeaderMetaInfo.getOWhenPicked());
        oBaseHeaderVO.setTotalCount(oHeaderMetaInfo.getRecordCount());
      } else {
        throw new EnrgiseApplicationException("wenrgise.common.norecordfound", "M");
      } 
      oBaseHeaderVO.setMaxHeaderSize(oQueryVO.getMaxHeaderSize());
      if (oHeaderMetaInfo.getRecordCount() == 0L)
        return oBaseHeaderVO; 
      oBaseHeaderVO.setPositionRequested(1L);
      oBaseHeaderVO.setCurrentSlot(oBaseHeaderVO.getSlot());
      ArrayList oHeaderList = getHeaderRecordImpl(oQueryVO, oBaseHeaderVO.getHeaderStartPosition(), oBaseHeaderVO.getHeaderLastPosition());
      if (oHeaderList == null)
        throw new EnrgiseApplicationException("wenrgise.common.norecordfound", "M"); 
      if (oUser == null)
        throw new EnrgiseApplicationException("wenrgise.common.sessiontimedout", "M"); 
      oBaseHeaderVO.setOHeaderVOTable(oHeaderList);
      oUser.putBaseHeaderVO(sFormName, oBaseHeaderVO);
      oUser.putBaseQueryVO(sFormName, oQueryVO);
    } catch (RemoteException oEx) {
      System.out.println(oEx.getMessage());
      throw new EnrgiseSystemException();
    } 
    return oBaseHeaderVO;
  }
  
  public BaseHeaderVO getNextHeaderRecord(BaseQueryVO oQueryVO, long lPositionRequested, String sFormName, boolean bForce, UserSession oUser) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      BaseHeaderVO oBaseHeaderVO = oUser.getBaseHeaderVO(sFormName);
      oBaseHeaderVO.setPositionRequested(lPositionRequested);
      if (oBaseHeaderVO.getCurrentSlot() == oBaseHeaderVO.getSlot() && !bForce)
        return oBaseHeaderVO; 
      ArrayList oHeaderList = getHeaderRecordImpl(oQueryVO, oBaseHeaderVO.getHeaderStartPosition(), oBaseHeaderVO.getHeaderLastPosition());
      oBaseHeaderVO.setCurrentSlot(oBaseHeaderVO.getSlot());
      oBaseHeaderVO.setOHeaderVOTable(oHeaderList);
      oUser.putBaseHeaderVO(sFormName, oBaseHeaderVO);
      return oBaseHeaderVO;
    } catch (RemoteException oEx) {
      System.out.println(oEx.getMessage());
      throw new EnrgiseSystemException();
    } 
  }
  
  public BaseDetailVO getDetailRecord(String sFormName, String sScreenName, String sPrimaryKey, long lPageRequested, DetailSizeValues oDetailSizeValues, BaseDetailVO oBaseDetailVO, boolean bForce, UserSession oUser) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oDetailList = null;
    try {
      if (oBaseDetailVO == null || bForce) {
        oBaseDetailVO = new BaseDetailVO();
        oBaseDetailVO.setMaxPage(oDetailSizeValues.getMaxPages());
        oBaseDetailVO.setRecordsPerPage(oDetailSizeValues.getDetailRecordPerPage());
        RecordMetaInfo oRecordMetaInfo = getTotalDetailRecordCountImpl(sPrimaryKey, sScreenName);
        oBaseDetailVO.setTotalDetailRecord(oRecordMetaInfo.getRecordCount());
        oBaseDetailVO.setOWhenPicked(oRecordMetaInfo.getOWhenPicked());
        if (oRecordMetaInfo.getRecordCount() == 0L)
          return oBaseDetailVO; 
      } 
      long lSlotRequested = oBaseDetailVO.getSlot(lPageRequested);
      if (!bForce) {
        if (oBaseDetailVO.getCurrentPage() == lPageRequested)
          return oBaseDetailVO; 
        if (lSlotRequested == oBaseDetailVO.getCurrentSlot()) {
          int iRelativeStartIndex = oBaseDetailVO.getRelativeStart(lPageRequested);
          int iRelativeEndIndex = oBaseDetailVO.getRelativeEnd(lPageRequested);
          EnrgiseUtil.pageArrayCopier(oBaseDetailVO.getOAllPageData(), oBaseDetailVO.getOThisPageData(), iRelativeStartIndex, iRelativeEndIndex - iRelativeStartIndex + 1);
        } else if (oBaseDetailVO.getTotalDetailRecord() % (oDetailSizeValues.getMaxPages() * oDetailSizeValues.getDetailRecordPerPage()) == 0L && oBaseDetailVO.getRowAdded() == 1) {
          oBaseDetailVO.setOAllPageData(new ArrayList());
          oBaseDetailVO.setOThisPageData(new ArrayList());
        } else {
          oBaseDetailVO.setCurrentSlot(lSlotRequested);
          oDetailList = getDetailRecordImpl(sPrimaryKey, sScreenName, oBaseDetailVO.getSlotStartPosition(), oBaseDetailVO.getSlotLastPosition());
          oBaseDetailVO.setOAllPageData(oDetailList);
          int iRelativeStartIndex = oBaseDetailVO.getRelativeStart(lPageRequested);
          int iRelativeEndIndex = oBaseDetailVO.getRelativeEnd(lPageRequested);
          EnrgiseUtil.pageArrayCopier(oBaseDetailVO.getOAllPageData(), oBaseDetailVO.getOThisPageData(), iRelativeStartIndex, iRelativeEndIndex - iRelativeStartIndex + 1);
        } 
      } else {
        oBaseDetailVO.setCurrentSlot(lSlotRequested);
        oDetailList = getDetailRecordImpl(sPrimaryKey, sScreenName, oBaseDetailVO.getSlotStartPosition(), oBaseDetailVO.getSlotLastPosition());
        oBaseDetailVO.setOAllPageData(oDetailList);
        int iRelativeStartIndex = oBaseDetailVO.getRelativeStart(lPageRequested);
        int iRelativeEndIndex = oBaseDetailVO.getRelativeEnd(lPageRequested);
        EnrgiseUtil.pageArrayCopier(oBaseDetailVO.getOAllPageData(), oBaseDetailVO.getOThisPageData(), iRelativeStartIndex, iRelativeEndIndex - iRelativeStartIndex + 1);
      } 
      oBaseDetailVO.setCurrentPage(lPageRequested);
      oBaseDetailVO.setCurrentSlot(lSlotRequested);
      oUser.putBaseDetailVO(sFormName, sScreenName, oBaseDetailVO);
      return oBaseDetailVO;
    } catch (RemoteException oEx) {
      System.out.println(oEx.getMessage());
      throw new EnrgiseSystemException();
    } 
  }
  
  public BaseDetailVO getDetailRecord(String sFormName, String sScreenName, BaseQueryVO oBaseQueryVO, long lPageRequested, DetailSizeValues oDetailSizeValues, BaseDetailVO oBaseDetailVO, boolean bForce, UserSession oUser) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oDetailList = null;
    try {
      if (oBaseDetailVO == null || bForce) {
        oBaseDetailVO = new BaseDetailVO();
        oBaseDetailVO.setMaxPage(oDetailSizeValues.getMaxPages());
        oBaseDetailVO.setRecordsPerPage(oDetailSizeValues.getDetailRecordPerPage());
        RecordMetaInfo oRecordMetaInfo = getTotalDetailRecordCountImpl(oBaseQueryVO, sScreenName);
        oBaseDetailVO.setTotalDetailRecord(oRecordMetaInfo.getRecordCount());
        oBaseDetailVO.setOWhenPicked(oRecordMetaInfo.getOWhenPicked());
        if (oRecordMetaInfo.getRecordCount() == 0L)
          return oBaseDetailVO; 
      } 
      long lSlotRequested = oBaseDetailVO.getSlot(lPageRequested);
      if (!bForce) {
        if (oBaseDetailVO.getCurrentPage() == lPageRequested)
          return oBaseDetailVO; 
        if (lSlotRequested == oBaseDetailVO.getCurrentSlot()) {
          int iRelativeStartIndex = oBaseDetailVO.getRelativeStart(lPageRequested);
          int iRelativeEndIndex = oBaseDetailVO.getRelativeEnd(lPageRequested);
          EnrgiseUtil.pageArrayCopier(oBaseDetailVO.getOAllPageData(), oBaseDetailVO.getOThisPageData(), iRelativeStartIndex, iRelativeEndIndex - iRelativeStartIndex + 1);
        } else if (oBaseDetailVO.getTotalDetailRecord() % (oDetailSizeValues.getMaxPages() * oDetailSizeValues.getDetailRecordPerPage()) == 0L && oBaseDetailVO.getRowAdded() == 1) {
          oBaseDetailVO.setOAllPageData(new ArrayList());
          oBaseDetailVO.setOThisPageData(new ArrayList());
        } else {
          oBaseDetailVO.setCurrentSlot(lSlotRequested);
          oDetailList = getDetailRecordImpl(oBaseQueryVO, sScreenName, oBaseDetailVO.getSlotStartPosition(), oBaseDetailVO.getSlotLastPosition());
          oBaseDetailVO.setOAllPageData(oDetailList);
          int iRelativeStartIndex = oBaseDetailVO.getRelativeStart(lPageRequested);
          int iRelativeEndIndex = oBaseDetailVO.getRelativeEnd(lPageRequested);
          EnrgiseUtil.pageArrayCopier(oBaseDetailVO.getOAllPageData(), oBaseDetailVO.getOThisPageData(), iRelativeStartIndex, iRelativeEndIndex - iRelativeStartIndex + 1);
        } 
      } else {
        oBaseDetailVO.setCurrentSlot(lSlotRequested);
        oDetailList = getDetailRecordImpl(oBaseQueryVO, sScreenName, oBaseDetailVO.getSlotStartPosition(), oBaseDetailVO.getSlotLastPosition());
        oBaseDetailVO.setOAllPageData(oDetailList);
        int iRelativeStartIndex = oBaseDetailVO.getRelativeStart(lPageRequested);
        int iRelativeEndIndex = oBaseDetailVO.getRelativeEnd(lPageRequested);
        EnrgiseUtil.pageArrayCopier(oBaseDetailVO.getOAllPageData(), oBaseDetailVO.getOThisPageData(), iRelativeStartIndex, iRelativeEndIndex - iRelativeStartIndex + 1);
      } 
      oBaseDetailVO.setCurrentPage(lPageRequested);
      oBaseDetailVO.setCurrentSlot(lSlotRequested);
      oUser.putBaseDetailVO(sFormName, sScreenName, oBaseDetailVO);
      return oBaseDetailVO;
    } catch (RemoteException oEx) {
      System.out.println(oEx.getMessage());
      throw new EnrgiseSystemException();
    } 
  }
  
  public Map getDisabledFields(ThisPageVO oThisPageVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    return getDisabledFieldsImpl(oThisPageVO);
  }
  
  public void onLoadAction(ThisPageVO oThisPageVO) throws EnrgiseApplicationException, EnrgiseSystemException {}
  
  public String saveRecord(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    return saveImpl(oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked);
  }
  
  public abstract ArrayList getHeaderRecordImpl(BaseQueryVO paramBaseQueryVO, long paramLong1, long paramLong2) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException;
  
  public abstract Map getDisabledFieldsImpl(ThisPageVO paramThisPageVO) throws EnrgiseApplicationException, EnrgiseSystemException;
  
  public abstract RecordMetaInfo getHeaderMetaImpl(BaseQueryVO paramBaseQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException;
  
  public abstract RecordMetaInfo getTotalDetailRecordCountImpl(String paramString1, String paramString2) throws EnrgiseApplicationException, EnrgiseSystemException;
  
  public abstract ArrayList getDetailRecordImpl(String paramString1, String paramString2, long paramLong1, long paramLong2) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException;
  
  public RecordMetaInfo getTotalDetailRecordCountImpl(BaseQueryVO oBaseQueryVO, String sScreenName) throws EnrgiseApplicationException, EnrgiseSystemException {
    return null;
  }
  
  public ArrayList getDetailRecordImpl(BaseQueryVO oBaseQueryVO, String sScreenName, long lAbsoluteStart, long lAbsoluteEnd) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    return null;
  }
  
  public abstract boolean updateHeaderRecordImpl(BaseHeaderBean paramBaseHeaderBean) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException;
  
  public abstract String saveImpl(BaseHeaderBean paramBaseHeaderBean, Timestamp paramTimestamp1, String paramString1, String paramString2, boolean paramBoolean1, ArrayList paramArrayList, boolean paramBoolean2, Timestamp paramTimestamp2) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException;
  
  public abstract String getInsertDataImpl(BaseQueryVO paramBaseQueryVO) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException;
  
  public long getLStartPosition() {
    return this.lStartPosition;
  }
  
  public void setLStartPosition(long newLStartPosition) {
    this.lStartPosition = newLStartPosition;
  }
  
  public long getLLastPosition() {
    return this.lLastPosition;
  }
  
  public void setLLastPosition(long newLLastPosition) {
    this.lLastPosition = newLLastPosition;
  }
  
  public UserInfo getOUserInfo() {
    return this.oUserInfo;
  }
  
  public void setOUserInfo(UserInfo newOUserInfo) {
    this.oUserInfo = newOUserInfo;
  }
  
  public EJBObject getModuleFacade() {
    return (EJBObject)this.oMap.get(this.moduleName);
  }
  
  public EJBObject getModuleFacade(String key) {
    return (EJBObject)this.oMap.get(key);
  }
  
  public void setModuleFacade(HashMap oMap) {
    this.oMap = oMap;
  }
  
  public HashMap getComboDetails() throws EnrgiseApplicationException, EnrgiseSystemException {
    return null;
  }
  
  public HashMap getDetailComboDetails() throws EnrgiseApplicationException, EnrgiseSystemException {
    return null;
  }
  
  public String submit(String sButtonName, BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    return submitImpl(sButtonName, oBaseHeaderBean, oWhenPicked, sScreenName, sScreenMode, bHeaderDataChanged, oDetailBeanArray, bDetailDataChanged, oDetailPicked);
  }
  
  public abstract String submitImpl(String paramString1, BaseHeaderBean paramBaseHeaderBean, Timestamp paramTimestamp1, String paramString2, String paramString3, boolean paramBoolean1, ArrayList paramArrayList, boolean paramBoolean2, Timestamp paramTimestamp2) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException;
  
  public String getModuleName() {
    return this.moduleName;
  }
  
  public void setModuleName(String newModuleName) {
    this.moduleName = newModuleName;
  }
}
