package WEB-INF.classes.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.BaseQueryVO;
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 SaveDetailAction 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();
    BaseBD oBaseBD = getDetailBusinessDelegate(form, request);
    UserSession oUser = getUserSessionBean(request);
    String sComponentName = getComponentName(sFormName);
    checkAccessInfo(oUser, sComponentName, oBaseForm.getScreenMode(), "", 8, oBaseForm.getWorkListId());
    ArrayList oDetailBeanArray = null;
    BaseDetailVO oBaseDetailVO = null;
    Timestamp oDetailPicked = null;
    if (!oBaseForm.isDetailDataChanged())
      return mapping.findForward("success"); 
    oBaseDetailVO = oUser.getBaseDetailVO(sFormName, sScreenName);
    if (oBaseDetailVO != null)
      oDetailPicked = oBaseDetailVO.getOWhenPicked(); 
    oDetailBeanArray = getDetailArray(oBaseForm);
    BaseHeaderBean oBaseHeaderBean = getBaseHeaderBean(sFormName);
    BeanUtils.copyProperties(oBaseHeaderBean, form);
    String sHeaderPrimaryKey = oBaseBD.saveRecord(oBaseHeaderBean, null, oBaseForm.getScreenName(), oBaseForm.getScreenMode(), false, oDetailBeanArray, true, oDetailPicked);
    oBaseForm.setDetailDataChanged(false);
    if (null != oBaseDetailVO) {
      oBaseDetailVO.setRowDeleted(0);
      oBaseDetailVO.setRowAdded(0);
    } 
    BaseQueryVO oBaseQueryVO = null;
    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);
    oBaseDetailVO.setRowAdded(0);
    oBaseDetailVO.setRowDeleted(0);
    if (oBaseDetailVO.getOThisPageData() != null) {
      oBaseForm.setThisPageDetailCount(oBaseDetailVO.getOThisPageData().size());
      BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oBaseDetailVO.getOThisPageData());
    } else {
      oBaseForm.setThisPageDetailCount(0);
    } 
    if (oBaseDetailVO != null) {
      long totDetRecord = oBaseDetailVO.getTotalDetailRecord();
      oBaseForm.setTotalPageCount(calculateTotalDetailPage(sFormName, sScreenName, totDetRecord));
    } 
    BeanUtils.copyProperties(oBaseForm, oBaseDetailVO);
    oUser.putBaseDetailVO(sFormName, sScreenName, oBaseDetailVO);
    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(8);
    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, 8);
    throw new EnrgiseApplicationException("wenrgise.common.datasaved", "M");
  }
}
