package wenrgise.common.webtier.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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.ThisPageVO;
import wenrgise.common.webtier.form.BaseForm;
import wenrgise.ejb.common.utility.ParamUtil;

public class QueryModeAction 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();
    if (oBaseForm.isHeaderDataChanged() || oBaseForm.isDetailDataChanged())
      throw new EnrgiseApplicationException("wenrgise.common.datashouldbesaved", "M"); 
    changeMode(oBaseForm, request);
    oBaseForm.setQueried(true);
    oBaseForm.setScreenMode("Q");
    BaseBD oBaseBD = getHeaderBusinessDelegate((ActionForm)oBaseForm, request);
    setComboValues(oBaseBD.getComboDetails(), request.getSession());
    setComboValues(oBaseBD.getDetailComboDetails(), request.getSession());
    enableAll(oBaseForm);
    enableDisable(oBaseForm, queryClicked(), "D");
    SecurityBD oSecBD = new SecurityBD();
    HashMap oMap = oSecBD.getDisabledFields(String.valueOf(oBaseForm.getScreenName()).concat(String.valueOf(".jsp")), "HEADER", oBaseForm.getScreenMode(), null);
    if (null != oMap)
      enableDisable(oBaseForm, (ArrayList)oMap.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(10);
    Map oBDMap = oBaseBD.getDisabledFields(oThisPageVO);
    if (oBDMap != null) {
      if (oBDMap.containsKey("D"))
        enableDisable(oBaseForm, (ArrayList)oBDMap.get("D"), "D"); 
      if (oBDMap.containsKey("E"))
        enableDisable(oBaseForm, (ArrayList)oBDMap.get("E"), "E"); 
    } 
    if (oThisPageVO.getODetailList() != null) {
      oBaseForm.setThisPageDetailCount(oThisPageVO.getODetailList().size());
      BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oThisPageVO.getODetailList());
    } 
    onLoad(form, request, 10);
    return mapping.findForward("success");
  }
  
  private void setComboValues(HashMap oComboMap, HttpSession session) {
    if (null == oComboMap)
      return; 
    if (oComboMap.size() <= 0)
      return; 
    Set oKeySet = oComboMap.keySet();
    Iterator oIt = oKeySet.iterator();
    while (oIt.hasNext()) {
      String sKey = oIt.next();
      session.setAttribute(sKey, oComboMap.get(sKey));
    } 
    oKeySet.clear();
    oComboMap.clear();
  }
}
