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.utility.UserInfo;
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.action.BaseAction;
import wenrgise.common.webtier.form.BaseForm;
import wenrgise.ejb.common.session.UserSession;
import wenrgise.ejb.common.utility.ParamUtil;

public class GetInsertAction 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();
    BaseQueryVO oBaseQueryVO = getQueryVO(form);
    System.out.println(String.valueOf("Class name ").concat(String.valueOf(oBaseQueryVO.getClass().getName())));
    BaseBD oBaseBD = getHeaderBusinessDelegate(form, request);
    BeanUtils.copyProperties(oBaseQueryVO, form);
    oBaseQueryVO.setMaxHeaderSize(ParamUtil.getHeaderSize(sFormName));
    UserSession oUser = getUserSessionBean(request);
    String sComponentName = getComponentName(sFormName);
    checkAccessInfo(oUser, sComponentName, oBaseForm.getScreenMode(), "", 8, oBaseForm.getWorkListId());
    UserInfo oUserInfo = oUser.getUserInfo();
    SecurityBD oSecBD = new SecurityBD();
    System.out.println(oBaseForm.getHeaderPrimaryKey());
    String sNewPK = oBaseBD.getInsertDataImpl(oBaseQueryVO);
    oBaseQueryVO = getQueryVO(form);
    BeanUtils.copyProperties(oBaseQueryVO, form);
    oBaseQueryVO.setHeaderPrimaryKey(sNewPK);
    oBaseQueryVO.setMaxHeaderSize(ParamUtil.getHeaderSize(sFormName));
    oUser = getUserSessionBean(request);
    BaseHeaderVO oBaseHeaderVO = oBaseBD.getHeaderRecord(oBaseQueryVO, sFormName, oUser);
    oUser.putBaseHeaderVO(sFormName, oBaseHeaderVO);
    oUser.putBaseQueryVO(sFormName, oBaseQueryVO);
    BeanUtils.copyProperties(form, oBaseHeaderVO.getHeaderRecord(1));
    BeanUtils.copyProperties(form, oBaseHeaderVO);
    DetailSizeValues oDetailSizeValues = new DetailSizeValues();
    oDetailSizeValues.setDetailRecordPerPage(ParamUtil.getDetailRecordPerPage(sFormName, sScreenName));
    oDetailSizeValues.setMaxPages(ParamUtil.getMaxDetailPages(sFormName, sScreenName));
    System.out.println(oBaseHeaderVO.getHeaderRecord(1).getHeaderPrimaryKey());
    BaseDetailVO oBaseDetailVO = getDetailData(oBaseForm, sFormName, sScreenName, 1L, request, oBaseBD, true, oUser);
    oBaseForm.setDetailStartPage(1);
    oBaseDetailVO.setCurrentPage(1L);
    oBaseForm.setThisPageDetailCount((null != oBaseDetailVO.getOThisPageData()) ? oBaseDetailVO.getOThisPageData().size() : 0);
    if (oBaseDetailVO.getOThisPageData() != null)
      BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oBaseDetailVO.getOThisPageData()); 
    BeanUtils.copyProperties(oBaseForm, oBaseDetailVO);
    oBaseForm.setHeaderDataChanged(false);
    oBaseForm.setDetailDataChanged(false);
    oBaseForm.setScreenMode("U");
    enableAll(oBaseForm);
    enableDisable(oBaseForm, nextHeaderClicked(), "D");
    controlHeaderNavigation(oBaseForm);
    enableDisable(oBaseForm, getDetailClicked(), "D");
    controlDetailNavigation(oBaseForm);
    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 oBaseHeaderBean = getBaseHeaderBean(sFormName);
    BeanUtils.copyProperties(oBaseHeaderBean, form);
    ThisPageVO oThisPageVO = new ThisPageVO();
    oThisPageVO.setOHeaderBean(oBaseHeaderBean);
    oThisPageVO.setScreenMode(oBaseForm.getScreenMode());
    oThisPageVO.setScreenName(oBaseForm.getScreenName());
    oThisPageVO.setActionName(23);
    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, 23);
    return mapping.findForward("success");
  }
}
