package WEB-INF.classes.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.BaseHeaderVO;
import wenrgise.common.vo.BaseQueryVO;
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 RefreshAction 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();
    UserSession oUser = getUserSessionBean(request);
    BaseBD oBaseBD = getDetailBusinessDelegate(form, request);
    BaseDetailVO oBaseDetailVO = null;
    oBaseForm.setHeaderDataChanged(false);
    oBaseForm.setDetailDataChanged(false);
    String sScreenMode = oBaseForm.getScreenMode();
    oBaseForm.setUserPositionRequested(null);
    oBaseForm.setUserPageRequested(null);
    if (sScreenMode.equals("N") || sScreenMode.equals("Q")) {
      changeMode(oBaseForm, request);
      oBaseForm.setScreenMode(sScreenMode);
    } else {
      BaseQueryVO oBaseQueryVO = null;
      BaseHeaderVO oBaseHeaderVO = null;
      if (oBaseForm.isQueried()) {
        oBaseQueryVO = oUser.getBaseQueryVO(sFormName);
        if (oBaseQueryVO != null) {
          BeanUtils.copyProperties(oBaseQueryVO, form);
          oBaseQueryVO.setHeaderPrimaryKey(null);
          oBaseHeaderVO = oBaseBD.getNextHeaderRecord(oBaseQueryVO, oBaseForm.getPositionRequested(), sFormName, false, oUser);
          int iRelativePosition = (int)(oBaseHeaderVO.getPositionRequested() - oBaseHeaderVO.getHeaderStartPosition() + 1L);
          BeanUtils.copyProperties(form, oBaseHeaderVO.getHeaderRecord(iRelativePosition));
          BeanUtils.copyProperties(form, oBaseHeaderVO);
        } 
      } else {
        oBaseQueryVO = getQueryVO(form);
        BeanUtils.copyProperties(oBaseQueryVO, form);
        if (oBaseForm.getHeaderPrimaryKey() != null) {
          oBaseQueryVO.setHeaderPrimaryKey(oBaseForm.getHeaderPrimaryKey());
          oBaseHeaderVO = oBaseBD.getHeaderRecord(oBaseQueryVO, sFormName, oUser);
          BeanUtils.copyProperties(form, oBaseHeaderVO.getHeaderRecord(1));
          BeanUtils.copyProperties(form, oBaseHeaderVO);
        } 
      } 
      oBaseDetailVO = oUser.getBaseDetailVO(sFormName, sScreenName);
      if (null != oBaseDetailVO) {
        oBaseDetailVO = getDetailData(oBaseForm, sFormName, sScreenName, 1L, request, oBaseBD, false, oUser);
        if (oBaseDetailVO.getOThisPageData() != null) {
          oBaseForm.setThisPageDetailCount(oBaseDetailVO.getOThisPageData().size());
          BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oBaseDetailVO.getOThisPageData());
        } else {
          oBaseForm.setThisPageDetailCount(0);
        } 
        long totDetRecord = oBaseDetailVO.getTotalDetailRecord();
        oBaseForm.setTotalPageCount(calculateTotalDetailPage(sFormName, sScreenName, totDetRecord));
        controlDetailNavigation(oBaseForm);
      } 
    } 
    SecurityBD oSecBD = new SecurityBD();
    Map oSecMap = oSecBD.getDisabledFields(String.valueOf(oBaseForm.getScreenName()).concat(String.valueOf(".jsp")), "ALL", oBaseForm.getScreenMode(), oBaseForm.getHeaderStatus());
    if (null != oSecMap)
      enableDisable(oBaseForm, (ArrayList)oSecMap.get("D"), "D"); 
    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(19);
    oBaseDetailVO = null;
    oBaseDetailVO = oUser.getBaseDetailVO(sFormName, oBaseForm.getScreenName());
    if (null != oBaseDetailVO) {
      ArrayList oDetailList = (oBaseDetailVO.getOThisPageData() != null) ? oBaseDetailVO.getOThisPageData() : new ArrayList();
      oThisPageVO.setODetailList(oDetailList);
    } 
    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, 19);
    return mapping.findForward("success");
  }
}
