package 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.BaseHeaderVO;
import wenrgise.common.vo.BaseQueryVO;
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 GetHeaderAction 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.isHeaderDataChanged() || oBaseForm.isDetailDataChanged())
      throw new EnrgiseApplicationException("wenrgise.common.datashouldbesaved", "M"); 
    String sFormName = form.getClass().getName();
    BaseQueryVO oBaseQueryVO = getQueryVO(form);
    BaseBD oBaseBD = getHeaderBusinessDelegate(form, request);
    BeanUtils.copyProperties(oBaseQueryVO, form);
    oBaseQueryVO.setMaxHeaderSize(ParamUtil.getHeaderSize(sFormName));
    UserSession oUser = getUserSessionBean(request);
    BaseHeaderVO oBaseHeaderVO = oBaseBD.getHeaderRecord(oBaseQueryVO, sFormName, oUser);
    BeanUtils.copyProperties(form, oBaseHeaderVO.getHeaderRecord(1));
    BeanUtils.copyProperties(form, oBaseHeaderVO);
    resetDetailPageData(oBaseForm);
    if (oBaseHeaderVO.getTotalCount() > 0L)
      oBaseForm.setScreenMode("U"); 
    enableAll(oBaseForm);
    enableDisable(oBaseForm, executeClicked(), "D");
    controlHeaderNavigation(oBaseForm);
    SecurityBD oSecBD = new SecurityBD();
    Map oSecMap = oSecBD.getDisabledFields(String.valueOf(oBaseForm.getScreenName()).concat(String.valueOf(".jsp")), "HEADER", 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(1);
    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"); 
    } 
    onLoad(form, request, 1);
    oUser.setForwardedPage(sFormName, "success");
    return mapping.findForward("success");
  }
}
