package wenrgise.common.webtier.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
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.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 GetButtonAction 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;
    if (oBaseForm.getScreenMode().equals("N"))
      throw new EnrgiseApplicationException("wenrgise.common.saveheaderrecord", "M"); 
    if (oBaseForm.isHeaderDataChanged() || oBaseForm.isDetailDataChanged())
      throw new EnrgiseApplicationException("wenrgise.common.datashouldbesaved", "M"); 
    String sFormName = form.getClass().getName();
    String sScreenName = oBaseForm.getScreenName();
    BaseBD oBaseBD = getHeaderBusinessDelegate(form, request);
    UserSession oUser = getUserSessionBean(request);
    String sComponentName = getComponentName(sFormName);
    BaseHeaderBean oBaseHeaderBean = getBaseHeaderBean(sFormName);
    ArrayList oDetailBeanArray = null;
    BaseDetailVO oBaseDetailVO = null;
    BaseHeaderVO oBaseHeaderVO = null;
    Timestamp oHeaderPicked = null;
    Timestamp oDetailPicked = null;
    oBaseHeaderVO = oUser.getBaseHeaderVO(sFormName);
    if (oBaseHeaderVO != null) {
      oHeaderPicked = oBaseHeaderVO.getOWhenPicked();
      oBaseHeaderBean = getBaseHeaderBean(sFormName);
    } 
    BeanUtils.copyProperties(oBaseHeaderBean, form);
    oBaseDetailVO = oUser.getBaseDetailVO(sFormName, sScreenName);
    if (oBaseDetailVO != null) {
      oDetailPicked = oBaseDetailVO.getOWhenPicked();
      oDetailBeanArray = getDetailArray(oBaseForm);
    } 
    String sHeaderPrimaryKey = oBaseBD.submit(oBaseForm.getButtonName(), oBaseHeaderBean, oHeaderPicked, oBaseForm.getScreenName(), oBaseForm.getScreenMode(), oBaseForm.isHeaderDataChanged(), oDetailBeanArray, oBaseForm.isDetailDataChanged(), oDetailPicked);
    oBaseForm.setHeaderPrimaryKey(sHeaderPrimaryKey);
    BaseQueryVO oBaseQueryVO = null;
    oBaseQueryVO = getQueryVO(form);
    BeanUtils.copyProperties(oBaseQueryVO, form);
    oBaseQueryVO.setHeaderPrimaryKey(sHeaderPrimaryKey);
    oBaseQueryVO.setMaxHeaderSize(ParamUtil.getHeaderSize(sFormName));
    oBaseHeaderVO = oBaseBD.getHeaderRecord(oBaseQueryVO, sFormName, oUser);
    BeanUtils.copyProperties(form, oBaseHeaderVO.getHeaderRecord(1));
    BeanUtils.copyProperties(form, oBaseHeaderVO);
    oUser.putBaseHeaderVO(sFormName, oBaseHeaderVO);
    if (null != oBaseDetailVO) {
      if (oBaseDetailVO.getTotalDetailRecord() + oBaseDetailVO.getRowAdded() - oBaseDetailVO.getRowDeleted() > 0L) {
        oBaseDetailVO.setRowDeleted(0);
        oBaseDetailVO.setRowAdded(0);
        DetailSizeValues oDetailSizeValues = new DetailSizeValues();
        oDetailSizeValues.setDetailRecordPerPage(ParamUtil.getDetailRecordPerPage(sFormName, sScreenName));
        oDetailSizeValues.setMaxPages(ParamUtil.getMaxDetailPages(sFormName, sScreenName));
        oBaseDetailVO = getDetailData(oBaseForm, sFormName, sScreenName, 1L, request, oBaseBD, true, oUser);
        oBaseForm.setPageRequested(1L);
        long totDetRecord = oBaseDetailVO.getTotalDetailRecord();
        oBaseForm.setTotalPageCount(calculateTotalDetailPage(sFormName, sScreenName, totDetRecord));
        if (oBaseDetailVO.getOThisPageData() != null) {
          oBaseForm.setThisPageDetailCount(oBaseDetailVO.getOThisPageData().size());
          BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oBaseDetailVO.getOThisPageData());
        } else {
          oBaseForm.setThisPageDetailCount(0);
        } 
        checkDetailNavigation(oBaseForm, oUser.getBaseDetailVO(sFormName, sScreenName));
        BeanUtils.copyProperties(oBaseForm, oBaseDetailVO);
      } 
      oBaseDetailVO.setRowAdded(0);
      oBaseDetailVO.setRowDeleted(0);
      oUser.putBaseDetailVO(sFormName, sScreenName, oBaseDetailVO);
    } 
    oBaseForm.setHeaderDataChanged(false);
    oBaseForm.setDetailDataChanged(false);
    oBaseForm.setScreenMode("U");
    enableAll(oBaseForm);
    enableDisable(oBaseForm, saveClicked(), "D");
    controlHeaderNavigation(oBaseForm);
    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(18);
    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, 18);
    throw new EnrgiseApplicationException("wenrgise.common.datasaved", "M");
  }
}
