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.BaseDetailVO;
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 GetDetailPageAction extends BaseAction {
  public ActionForward executeImpl(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, 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();
    UserSession oUser = getUserSessionBean(request);
    BaseBD oBaseBD = getDetailBusinessDelegate(form, request);
    String sScreenName = oBaseForm.getScreenName();
    checkDetailNavigation(oBaseForm, oUser.getBaseDetailVO(sFormName, sScreenName));
    BaseDetailVO oBaseDetailVO = getDetailData(oBaseForm, sFormName, sScreenName, oBaseForm.getPageRequested(), request, oBaseBD, false, oUser);
    long totDetRecord = oBaseDetailVO.getTotalDetailRecord();
    oBaseForm.setTotalPageCount(calculateTotalDetailPage(sFormName, oBaseForm.getScreenName(), totDetRecord));
    ArrayList oClicked = getDetailClicked();
    ArrayList oEnableList = new ArrayList();
    oEnableList.add("butAddRow");
    if (totDetRecord == 0L) {
      oClicked.add("butDelRow");
    } else {
      oEnableList.add("butDelRow");
      oEnableList.add("butSave");
    } 
    enableDisable(oBaseForm, oClicked, "D");
    enableDisable(oBaseForm, oEnableList, "E");
    controlDetailNavigation(oBaseForm);
    SecurityBD oSecBD = new SecurityBD();
    Map oSecMap = oSecBD.getDisabledFields(String.valueOf(oBaseForm.getScreenName()).concat(String.valueOf(".jsp")), "DETAIL", oBaseForm.getScreenMode(), oBaseForm.getHeaderStatus());
    if (null != oSecMap) {
      if (null != oSecMap.get("D"))
        enableDisable(oBaseForm, (ArrayList)oSecMap.get("D"), "D"); 
      if (null != oSecMap.get("E"))
        enableDisable(oBaseForm, (ArrayList)oSecMap.get("E"), "E"); 
    } 
    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(6);
    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, 6);
    return mapping.findForward("success");
  }
}
