package WEB-INF.classes.wenrgise.hrms.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.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 SaveDetailPageAction 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 = getHeaderBusinessDelegate(form, request);
    UserSession oUser = getUserSessionBean(request);
    BaseHeaderBean oBaseHeaderBean = getBaseHeaderBean(sFormName);
    ArrayList oDetailBeanArray = null;
    BaseDetailVO oBaseDetailVO = null;
    BaseHeaderVO oBaseHeaderVO = null;
    Timestamp oHeaderPicked = null;
    Timestamp oDetailPicked = null;
    if (!oBaseForm.isDetailDataChanged() && !oBaseForm.isHeaderDataChanged())
      return mapping.findForward("success"); 
    BeanUtils.copyProperties(oBaseHeaderBean, form);
    String sHeaderSave = ParamUtil.getHeaderSaveRequired(sFormName);
    if (oBaseForm.getScreenMode().equals("N") && null == sHeaderSave) {
      oHeaderPicked = null;
    } else {
      if (oBaseForm.isHeaderDataChanged()) {
        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(); 
      if (oBaseForm.isDetailDataChanged())
        oDetailBeanArray = getDetailArray(oBaseForm); 
    } 
    String sHeaderPrimaryKey = oBaseBD.saveRecord(oBaseHeaderBean, null, oBaseForm.getScreenName(), oBaseForm.getScreenMode(), false, oDetailBeanArray, oBaseForm.isDetailDataChanged(), oDetailPicked);
    oBaseForm.setHeaderDataChanged(false);
    oBaseForm.setDetailDataChanged(false);
    oBaseForm.setHeaderPrimaryKey(sHeaderPrimaryKey);
    if (null != oBaseHeaderVO) {
      BaseQueryVO 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 (!oBaseForm.getScreenMode().equals("N") || null != sHeaderSave)
      if (null != oBaseDetailVO) {
        if (oBaseDetailVO.getTotalDetailRecord() + oBaseDetailVO.getRowAdded() - oBaseDetailVO.getRowDeleted() >= 0L) {
          oBaseDetailVO.setRowDeleted(0);
          oBaseDetailVO.setRowAdded(0);
          oBaseDetailVO = getDetailData(oBaseForm, sFormName, sScreenName, 1L, request, oBaseBD, true, oUser);
          oBaseForm.setPageRequested(1L);
          long totDetRecord = oBaseDetailVO.getTotalDetailRecord();
          oBaseForm.setTotalPageCount(calculateTotalDetailPage(sFormName, oBaseForm.getScreenName(), totDetRecord));
          if (oBaseDetailVO.getOThisPageData() != null) {
            oBaseForm.setThisPageDetailCount(oBaseDetailVO.getOThisPageData().size());
            BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oBaseDetailVO.getOThisPageData());
          } else {
            oBaseForm.setThisPageDetailCount(0);
          } 
          BeanUtils.copyProperties(oBaseForm, oBaseDetailVO);
        } 
        oUser.putBaseDetailVO(sFormName, sScreenName, oBaseDetailVO);
      }  
    oBaseForm.setScreenMode("U");
    enableAll(oBaseForm);
    enableDisable(oBaseForm, saveClicked(), "D");
    controlHeaderNavigation(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());
    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());
    } 
    throw new EnrgiseApplicationException("wenrgise.common.datasaved", "M");
  }
}
