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.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 GetRefreshTabAction 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();
    if (oBaseForm.isHeaderDataChanged() || oBaseForm.isDetailDataChanged()) {
      if (oBaseForm.isDetailDataChanged()) {
        ArrayList arrayList = getDetailArray(oBaseForm);
        BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), arrayList);
      } 
      throw new EnrgiseApplicationException("wenrgise.common.datashouldbesaved", "M");
    } 
    BaseBD oBaseBD = getDetailBusinessDelegate(form, request);
    UserSession oUser = getUserSessionBean(request);
    String sForwardedPage = oBaseForm.getForwardedPage();
    BaseDetailVO oPresentBaseDetailVO = oUser.getBaseDetailVO(sFormName, sScreenName);
    if (null != oPresentBaseDetailVO)
      oPresentBaseDetailVO.setCurrentPage(oBaseForm.getPageRequested()); 
    oUser.putBaseDetailVO(sFormName, sScreenName, oPresentBaseDetailVO);
    BaseDetailVO oNextBaseDetailVO = oUser.getBaseDetailVO(sFormName, sForwardedPage);
    if (oPresentBaseDetailVO != null) {
      DetailSizeValues oDetailSizeValues = new DetailSizeValues();
      oDetailSizeValues.setDetailRecordPerPage(ParamUtil.getDetailRecordPerPage(sFormName, sForwardedPage));
      oDetailSizeValues.setMaxPages(ParamUtil.getMaxDetailPages(sFormName, sForwardedPage));
      oNextBaseDetailVO = getDetailData(oBaseForm, sFormName, sForwardedPage, 1L, request, oBaseBD, true, oUser);
      if (oNextBaseDetailVO.getOThisPageData() != null)
        BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sForwardedPage), oNextBaseDetailVO.getOThisPageData()); 
      oBaseForm.setPageRequested(oNextBaseDetailVO.getCurrentPage());
      oBaseForm.setThisPageDetailCount((null != oNextBaseDetailVO.getOThisPageData()) ? oNextBaseDetailVO.getOThisPageData().size() : 0);
      long lTotDetRecord = oNextBaseDetailVO.getTotalDetailRecord();
      oBaseForm.setTotalPageCount(calculateTotalDetailPage(sFormName, oBaseForm.getForwardedPage(), lTotDetRecord));
      oUser.putBaseDetailVO(sFormName, sForwardedPage, oNextBaseDetailVO);
      ArrayList oClicked = getDetailClicked();
      if (lTotDetRecord == 0L) {
        oBaseForm.setPageRequested(0L);
        oClicked.add("butDelRow");
      } 
      enableDisable(oBaseForm, oClicked, "D");
      controlDetailNavigation(oBaseForm);
    } 
    SecurityBD oSecBD = new SecurityBD();
    Map oSecMap = oSecBD.getDisabledFields(String.valueOf(sForwardedPage).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);
    ArrayList oDetailList = null;
    BeanUtils.copyProperties(oBaseHeaderBean, form);
    ThisPageVO oThisPageVO = new ThisPageVO();
    if (oNextBaseDetailVO != null) {
      oDetailList = (oNextBaseDetailVO.getOThisPageData() != null) ? oNextBaseDetailVO.getOThisPageData() : new ArrayList();
      oThisPageVO.setODetailList(oDetailList);
    } 
    oThisPageVO.setOHeaderBean(oBaseHeaderBean);
    oThisPageVO.setScreenMode(oBaseForm.getScreenMode());
    oThisPageVO.setScreenName(oBaseForm.getForwardedPage());
    oThisPageVO.setActionName(22);
    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());
    } 
    if (sForwardedPage != null)
      oBaseForm.setScreenName(sForwardedPage); 
    onLoad(form, request, 22);
    if (sForwardedPage != null)
      return mapping.findForward(sForwardedPage); 
    return mapping.findForward("success");
  }
}
