package WEB-INF.classes.wenrgise.hrms.webtier.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import wenrgise.common.bean.BaseDetailBean;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.businessdelegate.BaseBD;
import wenrgise.common.businessdelegate.SecurityBD;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.vo.BaseDetailVO;
import wenrgise.common.vo.DetailSizeValues;
import wenrgise.common.vo.ThisPageVO;
import wenrgise.common.webtier.action.BaseAction;
import wenrgise.common.webtier.form.BaseForm;
import wenrgise.ejb.common.session.UserSession;
import wenrgise.ejb.common.utility.ParamUtil;

public class AddAction extends BaseAction {
  public ActionForward executeImpl(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    BaseForm oBaseForm = (BaseForm)form;
    String sFormName = form.getClass().getName();
    String sScreenName = oBaseForm.getScreenName();
    String sHeaderPrimaryKey = oBaseForm.getHeaderPrimaryKey();
    String sNextPageEndField = null;
    UserSession oUser = getUserSessionBean(request);
    BaseDetailVO oBaseDetailVO = oUser.getBaseDetailVO(sFormName, sScreenName);
    boolean bGetDetailNotPressed = false;
    DetailSizeValues oDetailSizeValues = new DetailSizeValues();
    oDetailSizeValues.setDetailRecordPerPage(ParamUtil.getDetailRecordPerPage(sFormName, sScreenName));
    oDetailSizeValues.setMaxPages(ParamUtil.getMaxDetailPages(sFormName, sScreenName));
    BaseBD oBaseBD = null;
    if (null == oBaseDetailVO) {
      bGetDetailNotPressed = true;
      oBaseBD = getDetailBusinessDelegate(form, request);
      if (!oBaseForm.getScreenMode().equals("N")) {
        oBaseDetailVO = getDetailData(oBaseForm, sFormName, sScreenName, 1L, request, oBaseBD, false, oUser);
      } else {
        oBaseDetailVO = new BaseDetailVO();
        oBaseDetailVO.setMaxPage(oDetailSizeValues.getMaxPages());
        oBaseDetailVO.setRecordsPerPage(oDetailSizeValues.getDetailRecordPerPage());
      } 
      oBaseForm.setThisPageDetailCount((null != oBaseDetailVO.getOThisPageData()) ? oBaseDetailVO.getOThisPageData().size() : 0);
    } 
    long lPageRequested = getPageRequested(oBaseDetailVO, oDetailSizeValues);
    ArrayList oDetailList = null;
    if (!oBaseForm.getScreenMode().equals("N")) {
      if (oBaseDetailVO.getCurrentPage() != lPageRequested) {
        if (oBaseForm.isDetailDataChanged())
          throw new EnrgiseApplicationException("wenrgise.common.savefirst", "M"); 
        oBaseBD = getDetailBusinessDelegate(form, request);
        oBaseDetailVO.setRowAdded(oBaseDetailVO.getRowAdded() + 1);
        oBaseDetailVO = oBaseBD.getDetailRecord(sFormName, sScreenName, sHeaderPrimaryKey, lPageRequested, oDetailSizeValues, oBaseDetailVO, false, oUser);
        oBaseForm.setThisPageDetailCount((null != oBaseDetailVO.getOThisPageData()) ? oBaseDetailVO.getOThisPageData().size() : 0);
        oDetailList = oBaseDetailVO.getOThisPageData();
        if (oBaseDetailVO.getOAllPageData().size() > 0) {
          BaseDetailBean oNewBean = oBaseDetailVO.getOAllPageData().get(oBaseDetailVO.getOAllPageData().size() - 1);
          try {
            sNextPageEndField = BeanUtils.getProperty(oNewBean, "txtToAmount");
            long lEndField = Long.parseLong(sNextPageEndField);
            lEndField += 1L;
            sNextPageEndField = String.valueOf(lEndField);
          } catch (NoSuchMethodException noSuchMethodException) {}
        } 
      } else {
        if (bGetDetailNotPressed) {
          oDetailList = (oBaseDetailVO.getOThisPageData() != null) ? oBaseDetailVO.getOThisPageData() : new ArrayList();
        } else {
          oDetailList = getDetailArray(oBaseForm);
        } 
        oBaseDetailVO.setRowAdded(oBaseDetailVO.getRowAdded() + 1);
      } 
    } else {
      oBaseDetailVO.setRowAdded(oBaseDetailVO.getRowAdded() + 1);
      if (1L != lPageRequested)
        if (oBaseForm.isDetailDataChanged())
          throw new EnrgiseApplicationException("wenrgise.common.savefirst", "M");  
      if (null == oBaseForm.getStatus()) {
        oDetailList = new ArrayList();
      } else {
        oDetailList = getDetailArray(oBaseForm);
      } 
    } 
    String sNextStart = "0";
    try {
      if (oDetailList.size() > 0) {
        String[] sStatus = oBaseForm.getStatus();
        String[] sStartField = BeanUtils.getArrayProperty(oBaseForm, "txtFromAmount");
        String[] sEndField = BeanUtils.getArrayProperty(oBaseForm, "txtToAmount");
        int iIndex = oDetailList.size() - 1;
        boolean iFlag = true;
        while (sStatus[iIndex].equals("D")) {
          iIndex--;
          if (iIndex < 0) {
            iIndex = 0;
            iFlag = false;
            break;
          } 
        } 
        if (iFlag) {
          String sTemp = sEndField[iIndex];
          if (null == sTemp || sTemp.equals(""))
            throw new EnrgiseApplicationException("Payroll.common.addrow.mandatory.missing", "E"); 
          sNextStart = Long.toString(Long.parseLong(sEndField[iIndex]) + 1L);
        } else {
          sNextStart = "0";
        } 
      } 
      if (null != sNextPageEndField)
        sNextStart = sNextPageEndField; 
    } catch (NoSuchMethodException noSuchMethodException) {}
    BaseDetailBean oBaseDetailBean = getDetailBean(form);
    oBaseDetailBean.setStatus("N");
    BeanUtils.setProperty(oBaseDetailBean, "txtFromAmount", sNextStart);
    oDetailList.add(oBaseDetailBean);
    oBaseForm.setThisPageDetailCount(oBaseForm.getThisPageDetailCount() + 1);
    oBaseForm.setDetailDataChanged(true);
    BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oDetailList);
    oUser.putBaseDetailVO(sFormName, sScreenName, oBaseDetailVO);
    ArrayList oClicked = getDetailClicked();
    ArrayList oEnableList = new ArrayList();
    enableDisable(oBaseForm, oClicked, "D");
    enableDisable(oBaseForm, oEnableList, "E");
    controlDetailNavigation(oBaseForm);
    SecurityBD oSecBD = new SecurityBD();
    Map oSecMap = oSecBD.getDisabledFields(String.valueOf(oBaseForm.getScreenName()).concat(String.valueOf(".jsp")), "DETAIL", oBaseForm.getScreenMode(), oBaseForm.getHeaderStatus());
    if (null != oSecMap) {
      if (null != oSecMap.get("D"))
        enableDisable(oBaseForm, (ArrayList)oSecMap.get("D"), "D"); 
      if (null != oSecMap.get("E"))
        enableDisable(oBaseForm, (ArrayList)oSecMap.get("E"), "E"); 
    } 
    BaseHeaderBean oBaseHeaderBean = getBaseHeaderBean(sFormName);
    BeanUtils.copyProperties(oBaseHeaderBean, form);
    ThisPageVO oThisPageVO = new ThisPageVO();
    oThisPageVO.setOHeaderBean(oBaseHeaderBean);
    oThisPageVO.setScreenMode(oBaseForm.getScreenMode());
    oThisPageVO.setScreenName(oBaseForm.getScreenName());
    oBaseDetailVO = oUser.getBaseDetailVO(sFormName, oBaseForm.getScreenName());
    if (null != oBaseDetailVO)
      oThisPageVO.setODetailList(oDetailList); 
    oBaseBD = getDetailBusinessDelegate(form, request);
    Map oMap = oBaseBD.getDisabledFields(oThisPageVO);
    if (oMap != null) {
      if (oMap.containsKey("D"))
        enableDisable(oBaseForm, (ArrayList)oMap.get("D"), "D"); 
      if (oMap.containsKey("E"))
        enableDisable(oBaseForm, (ArrayList)oMap.get("E"), "E"); 
    } 
    BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oDetailList);
    oUser.putBaseDetailVO(sFormName, sScreenName, oBaseDetailVO);
    return mapping.findForward("success");
  }
  
  private void populateThisPageList(BaseDetailVO oBaseDetailVO, DetailSizeValues oDetailSizeValues) {
    long lGrandTotalDetailRecord = oBaseDetailVO.getTotalDetailRecord() - oBaseDetailVO.getRowDeleted() + oBaseDetailVO.getRowAdded();
    int iPage = (int)lGrandTotalDetailRecord % oDetailSizeValues.getDetailRecordPerPage() + 1;
    int iDetailStartPosition = (iPage - 1) * oDetailSizeValues.getDetailRecordPerPage() + 1;
    if (iDetailStartPosition < lGrandTotalDetailRecord + 1L);
  }
  
  private long getPageRequested(BaseDetailVO oBaseDetailVO, DetailSizeValues oDetailSizeValues) {
    long lLastRecordNumber = oBaseDetailVO.getTotalDetailRecord() - oBaseDetailVO.getRowDeleted() + oBaseDetailVO.getRowAdded() + 1L;
    return (lLastRecordNumber % oDetailSizeValues.getDetailRecordPerPage() != 0L) ? (lLastRecordNumber / oDetailSizeValues.getDetailRecordPerPage() + 1L) : (lLastRecordNumber / oDetailSizeValues.getDetailRecordPerPage());
  }
}
