package WEB-INF.classes.wenrgise.common.webtier.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
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.utility.EnrgiseUtil;
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.hrms.webtier.form.HrmBaseForm;

public class OnLoadAction 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();
    HrmBaseForm oHrmBaseForm = (HrmBaseForm)oBaseForm;
    HttpSession oSes = request.getSession();
    String sSesId = oSes.getId();
    if (null == sSesId)
      throw new EnrgiseApplicationException("wenrgise.common.session.expire", "E"); 
    String sUserId = (String)oSes.getAttribute("userId");
    if (null == sUserId) {
      oBaseForm.setFirstLoad("false");
      throw new EnrgiseApplicationException("wenrgise.common.session.expire", "E");
    } 
    oHrmBaseForm.setEmpId(sUserId);
    UserSession oUser = getUserSessionBean(request);
    String sForwardedPage = oUser.getForwardedPage(sFormName);
    if (EnrgiseUtil.checkString(oUser.getForwardedPage(sFormName))) {
      sForwardedPage = oUser.getForwardedPage(sFormName);
      oBaseForm.setScreenName(sForwardedPage);
      return mapping.findForward(sForwardedPage);
    } 
    sScreenName = request.getParameter("screenName");
    if (sScreenName.equalsIgnoreCase("HrmLvAppln")) {
      changeMode(oBaseForm, request);
      oBaseForm.setQueried(true);
      oBaseForm.setScreenMode("N");
      oBaseForm.setFirstLoad("false");
      enableAll(oBaseForm);
      enableDisable(oBaseForm, insertClicked(), "D");
    } else {
      changeMode(oBaseForm, request);
      oBaseForm.setQueried(true);
      oBaseForm.setScreenMode("Q");
      oBaseForm.setFirstLoad("false");
      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"); 
    BaseBD oBaseBD = getHeaderBusinessDelegate(form, request);
    BaseHeaderBean oBaseHeaderBean = getBaseHeaderBean(sFormName);
    BeanUtils.copyProperties(oBaseHeaderBean, form);
    ThisPageVO oThisPageVO = new ThisPageVO();
    oThisPageVO.setOHeaderBean(oBaseHeaderBean);
    oThisPageVO.setScreenMode(oBaseForm.getScreenMode());
    oThisPageVO.setScreenName(oBaseForm.getScreenName());
    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"); 
    } 
    onLoad(form, request, 13);
    return mapping.findForward("success");
  }
}
