package wenrgise.common.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.form.BaseForm;
import wenrgise.ejb.common.session.UserSession;
import wenrgise.ejb.common.utility.ParamUtil;

public class AddRowAction 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();
    if (oBaseForm.getScreenMode().equals("N") || oBaseForm.getHeaderPrimaryKey() == null) {
      String sHeaderSave = ParamUtil.getHeaderSaveRequired(sFormName);
      if (null == sHeaderSave)
        throw new EnrgiseApplicationException("wenrgise.common.saveheaderrecord", "M"); 
    } 
    String sScreenName = oBaseForm.getScreenName();
    String sHeaderPrimaryKey = oBaseForm.getHeaderPrimaryKey();
    UserSession oUser = getUserSessionBean(request);
    String sComponentName = getComponentName(sFormName);
    checkAccessInfo(oUser, sComponentName, oBaseForm.getScreenMode(), "", 10, oBaseForm.getWorkListId());
    DetailSizeValues oDetailSizeValues = new DetailSizeValues();
    oDetailSizeValues.setDetailRecordPerPage(ParamUtil.getDetailRecordPerPage(sFormName, sScreenName));
    oDetailSizeValues.setMaxPages(ParamUtil.getMaxDetailPages(sFormName, sScreenName));
    BaseDetailVO oBaseDetailVO = oUser.getBaseDetailVO(sFormName, sScreenName);
    boolean bGetDetailNotPressed = false;
    BaseBD oBaseBD = getDetailBusinessDelegate((ActionForm)oBaseForm, request);
    if (null == oBaseDetailVO) {
      bGetDetailNotPressed = true;
      oBaseDetailVO = getDetailData(oBaseForm, sFormName, sScreenName, 1L, request, oBaseBD, false, oUser);
      long l = oBaseDetailVO.getTotalDetailRecord();
      oBaseForm.setTotalPageCount(calculateTotalDetailPage(sFormName, sScreenName, l));
    } 
    if (oBaseDetailVO.getCurrentPage() == 0L)
      oBaseDetailVO.setCurrentPage(1L); 
    long lPageRequested = getPageRequested(oBaseDetailVO, oDetailSizeValues);
    ArrayList oDetailList = null;
    if (oBaseDetailVO.getCurrentPage() != lPageRequested) {
      if (oBaseForm.isHeaderDataChanged() || oBaseForm.isDetailDataChanged()) {
        BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), getDetailArray(oBaseForm));
        throw new EnrgiseApplicationException("wenrgise.common.savefirst", "M");
      } 
      oBaseBD = getDetailBusinessDelegate(form, request);
      oBaseDetailVO = getDetailData(oBaseForm, sFormName, sScreenName, lPageRequested, request, oBaseBD, false, oUser);
      oBaseDetailVO.setRowAdded(oBaseDetailVO.getRowAdded() + 1);
      oDetailList = oBaseDetailVO.getOThisPageData();
    } else {
      if (bGetDetailNotPressed) {
        oDetailList = (oBaseDetailVO.getOThisPageData() != null) ? oBaseDetailVO.getOThisPageData() : new ArrayList();
      } else {
        oDetailList = getDetailArray(oBaseForm);
      } 
      oBaseDetailVO.setRowAdded(oBaseDetailVO.getRowAdded() + 1);
    } 
    BaseDetailBean oBaseDetailBean = getDetailBean(form);
    oBaseDetailBean.setStatus("N");
    oDetailList.add(oBaseDetailBean);
    oBaseForm.setThisPageDetailCount(oBaseForm.getThisPageDetailCount() + 1);
    oBaseForm.setDetailDataChanged(true);
    long totDetRecord = oBaseDetailVO.getTotalDetailRecord() - oBaseDetailVO.getRowDeleted() + oBaseDetailVO.getRowAdded();
    oBaseForm.setTotalPageCount(calculateTotalDetailPage(sFormName, sScreenName, totDetRecord));
    BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oDetailList);
    oUser.putBaseDetailVO(sFormName, sScreenName, oBaseDetailVO);
    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"); 
      ArrayList oEnableList = new ArrayList();
      oEnableList.add("butSave");
      if (null != oSecMap.get("E"))
        oEnableList.addAll((ArrayList)oSecMap.get("E")); 
      enableDisable(oBaseForm, oEnableList, "E");
    } 
    BaseHeaderBean oThisBaseHeaderBean = getBaseHeaderBean(sFormName);
    BeanUtils.copyProperties(oThisBaseHeaderBean, form);
    ThisPageVO oThisPageVO = new ThisPageVO();
    oThisPageVO.setOHeaderBean(oThisBaseHeaderBean);
    oThisPageVO.setScreenMode(oBaseForm.getScreenMode());
    oThisPageVO.setScreenName(oBaseForm.getScreenName());
    oThisPageVO.setActionName(14);
    oBaseDetailVO = null;
    oBaseDetailVO = oUser.getBaseDetailVO(sFormName, oBaseForm.getScreenName());
    if (null != oBaseDetailVO) {
      ArrayList oList = getDetailArrayList(oBaseForm, sFormName, sScreenName);
      oThisPageVO.setODetailList(oList);
    } 
    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"); 
    } 
    if (oThisPageVO.getODetailList() != null) {
      oBaseForm.setThisPageDetailCount(oThisPageVO.getODetailList().size());
      BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oThisPageVO.getODetailList());
    } 
    onLoad(form, request, 14);
    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());
  }
}
