package wenrgise.common.webtier.action;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Logger;
import javax.ejb.CreateException;
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.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import wenrgise.common.bean.AccessBean;
import wenrgise.common.bean.BaseDetailBean;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.bean.EmpInfoBean;
import wenrgise.common.businessdelegate.BaseBD;
import wenrgise.common.businessdelegate.SecurityBD;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseMessageKeyException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.DebugHelper;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.MessageKey;
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.form.BaseForm;
import wenrgise.common.xml.vo.DetailScreen;
import wenrgise.ejb.common.session.UserSession;
import wenrgise.ejb.common.session.UserSessionHome;
import wenrgise.ejb.common.utility.ParamUtil;
import wenrgise.ejb.common.utility.ServiceLocator;
import wenrgise.hrms.ejb.facade.HrmFacade;
import wenrgise.hrms.ejb.facade.HrmFacadeHome;
import wenrgise.hrms.ejb.facade.HrmSecondFacade;
import wenrgise.hrms.ejb.facade.HrmSecondFacadeHome;
import wenrgise.hrms.ejb.facade.HrmThirdFacade;
import wenrgise.hrms.ejb.facade.HrmThirdFacadeHome;

public abstract class BaseAction extends Action {
  static final Logger log = Logger.getLogger("wenrgise.common.webtier.action.BaseAction");
  
  MessageResources oMsgRes = null;
  
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ActionErrors oErrorTable = null;
    ActionMessages oMessageTable = null;
    BaseForm oBaseForm = (BaseForm)form;
    this.oMsgRes = getResources(request);
    try {
      HttpSession oSes = request.getSession();
      String sUserId = (String)oSes.getAttribute("userId");
      System.out.println(String.valueOf("I am in Base Action and UserId: --->").concat(String.valueOf(sUserId)));
      if (null == sUserId) {
        System.out.println("I am in Base Action And session expired");
        System.out.println("Session has expired");
        return mapping.findForward("failure");
      } 
      DebugHelper.createUserEjb(request.getSession());
      return executeImpl(mapping, form, request, response);
    } catch (ClassNotFoundException oClassEx) {
      processSystemException(oClassEx, oErrorTable);
    } catch (InstantiationException oInSt) {
      processSystemException(oInSt, oErrorTable);
    } catch (InvocationTargetException oInvTar) {
      processSystemException(oInvTar, oErrorTable);
    } catch (IllegalAccessException oIlAcc) {
      processSystemException(oIlAcc, oErrorTable);
    } catch (EnrgiseSystemException oSys) {
      if (oErrorTable == null)
        oErrorTable = new ActionErrors(); 
      String sKey = (null != oSys.getKey()) ? oSys.getKey() : "wenrgise.common.system";
      ActionError oSysError = new ActionError(sKey);
      oErrorTable.add("org.apache.struts.action.GLOBAL_ERROR", oSysError);
    } catch (EnrgiseApplicationException oApp) {
      if (oErrorTable == null)
        oErrorTable = new ActionErrors(); 
      if (oMessageTable == null)
        oMessageTable = new ActionMessages(); 
      processException(oErrorTable, oMessageTable, oApp);
      if (oApp.getList() != null) {
        Iterator oIt = oApp.getList().iterator();
        while (oIt.hasNext()) {
          EnrgiseApplicationException oAppErr = oIt.next();
          processException(oErrorTable, oMessageTable, oAppErr);
        } 
      } 
    } finally {
      if (oErrorTable != null) {
        System.out.println("Save errors");
        if (oErrorTable.size() > 0) {
          saveErrors(request, oErrorTable);
          return mapping.findForward("success");
        } 
      } 
      if (oMessageTable != null)
        if (oMessageTable.size() > 0) {
          System.out.println("Save messages");
          saveMessages(request, oMessageTable);
          return mapping.findForward("success");
        }  
    } 
    return mapping.findForward("failure");
  }
  
  private void processSystemException(Exception oEc, ActionErrors oErrorTable) {
    if (oErrorTable == null)
      oErrorTable = new ActionErrors(); 
    EnrgiseSystemException oSys = new EnrgiseSystemException("wenrgise.common.system", oEc);
    ActionError oSysError = new ActionError(oSys.getKey());
    oErrorTable.add("org.apache.struts.action.GLOBAL_ERROR", oSysError);
  }
  
  private void processException(ActionErrors oErrorTable, ActionMessages oMessageTable, EnrgiseApplicationException oApp) {
    if (oApp.getArguments() == null) {
      if (oApp.getErrorType().equals("E")) {
        oErrorTable.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError(oApp.getKey()));
      } else {
        oMessageTable.add("org.apache.struts.action.GLOBAL_MESSAGE", new ActionMessage(oApp.getKey()));
      } 
    } else if (oApp instanceof EnrgiseMessageKeyException) {
      processMessageKeyException((EnrgiseMessageKeyException)oApp, oErrorTable, oMessageTable);
    } else if (oApp.getErrorType().equals("E")) {
      oErrorTable.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError(oApp.getKey(), oApp.getArguments().toArray()));
    } else {
      oMessageTable.add("org.apache.struts.action.GLOBAL_MESSAGE", new ActionMessage(oApp.getKey(), oApp.getArguments().toArray()));
    } 
  }
  
  private void processMessageKeyException(EnrgiseMessageKeyException oApp, ActionErrors oErrorTable, ActionMessages oMessageTable) {
    ArrayList oList = oApp.getArguments();
    if (null == oList) {
      if (oApp.getErrorType().equals("E")) {
        oErrorTable.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError(oApp.getKey()));
      } else {
        oMessageTable.add("org.apache.struts.action.GLOBAL_MESSAGE", new ActionMessage(oApp.getKey()));
      } 
    } else {
      ArrayList oNewList = new ArrayList();
      Iterator oIt = oList.iterator();
      while (oIt.hasNext()) {
        Object obj = oIt.next();
        if (obj instanceof MessageKey) {
          MessageKey oMsgKey = (MessageKey)obj;
          oNewList.add(this.oMsgRes.getMessage(oMsgKey.getKey()));
          continue;
        } 
        oNewList.add(obj);
      } 
      if (oApp.getErrorType().equals("E")) {
        oErrorTable.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError(oApp.getKey(), oNewList.toArray()));
      } else {
        oMessageTable.add("org.apache.struts.action.GLOBAL_MESSAGE", new ActionMessage(oApp.getKey(), oNewList.toArray()));
      } 
    } 
  }
  
  protected BaseQueryVO getQueryVO(ActionForm form) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
    BaseForm oBaseForm = (BaseForm)form;
    String sFormName = form.getClass().getName();
    String sScreenName = oBaseForm.getScreenName();
    String sQueryVO = ParamUtil.getQueryVO(sFormName);
    BaseQueryVO oBaseQueryVO = (BaseQueryVO)Class.forName(sQueryVO).newInstance();
    oBaseQueryVO.setMaxHeaderSize(ParamUtil.getHeaderSize(sFormName));
    return oBaseQueryVO;
  }
  
  protected BaseDetailBean getDetailBean(ActionForm form) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
    BaseForm oBaseForm = (BaseForm)form;
    String sFormName = form.getClass().getName();
    String sScreenName = oBaseForm.getScreenName();
    String sDetailBean = ParamUtil.getDetailBeanName(sFormName, sScreenName);
    BaseDetailBean oBaseDetailBean = (BaseDetailBean)Class.forName(sDetailBean).newInstance();
    return oBaseDetailBean;
  }
  
  protected BaseBD getHeaderBusinessDelegate(ActionForm form, HttpServletRequest request) throws EnrgiseApplicationException, EnrgiseSystemException, RemoteException, IllegalAccessException, InstantiationException, ClassNotFoundException {
    BaseForm oBaseForm = (BaseForm)form;
    String sFormName = form.getClass().getName();
    String sScreenName = oBaseForm.getScreenName();
    String sBusinessDelegate = ParamUtil.getHeaderBD(sFormName);
    try {
      BaseBD oBaseBD = (BaseBD)Class.forName(sBusinessDelegate).newInstance();
      UserSession oUser = getUserSessionBean(request);
      oBaseBD.setModuleName(ParamUtil.getModuleName());
      oBaseBD.setModuleFacade(oUser.getModuleFacade());
      oBaseBD.setOUserInfo(oUser.getUserInfo());
      return oBaseBD;
    } catch (Exception Ex) {
      Ex.printStackTrace();
      return null;
    } 
  }
  
  protected BaseBD getDetailBusinessDelegate(ActionForm form, HttpServletRequest request) throws EnrgiseApplicationException, EnrgiseSystemException, RemoteException, IllegalAccessException, InstantiationException, ClassNotFoundException {
    BaseForm oBaseForm = (BaseForm)form;
    String sFormName = form.getClass().getName();
    String sScreenName = oBaseForm.getScreenName();
    String sBusinessDelegate = ParamUtil.getDetailBD(sFormName, sScreenName);
    BaseBD oBaseBD = (BaseBD)Class.forName(sBusinessDelegate).newInstance();
    UserSession oUser = getUserSessionBean(request);
    oBaseBD.setModuleName(ParamUtil.getModuleName());
    oBaseBD.setModuleFacade(oUser.getModuleFacade());
    oBaseBD.setOUserInfo(oUser.getUserInfo());
    return oBaseBD;
  }
  
  protected BaseHeaderBean getBaseHeaderBean(String sFormName) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
    String sBaseHeaderBean = ParamUtil.getHeaderBean(sFormName);
    try {
      BaseHeaderBean oBaseHeaderBean = (BaseHeaderBean)Class.forName(sBaseHeaderBean).newInstance();
      String pseudoHeader = ParamUtil.getPseudoHeaderFlag(sFormName);
      oBaseHeaderBean.setPseudoHeader(pseudoHeader);
      return oBaseHeaderBean;
    } catch (Exception ex) {
      ex.printStackTrace();
      return null;
    } 
  }
  
  protected ArrayList getDetailArrayList(BaseForm oBaseForm, String sFormName, String sScreenName) throws EnrgiseSystemException, IllegalAccessException, InstantiationException, ClassNotFoundException {
    String sDetailArrayList = ParamUtil.getDetailArrayName(sFormName, sScreenName);
    ArrayList oList = (ArrayList)EnrgiseUtil.getFieldValue(oBaseForm, sDetailArrayList);
    return oList;
  }
  
  protected UserSession getUserSessionBean(HttpServletRequest request) throws EnrgiseApplicationException, RemoteException, EnrgiseSystemException {
    UserInfo oUserInfo = new UserInfo();
    HttpSession session = request.getSession();
    UserSession oUser = (UserSession)session.getAttribute(ParamUtil.getSessionBeanName());
    if (oUser == null)
      try {
        String sEmpId = (String)session.getAttribute("empId");
        String sSiteId = (String)session.getAttribute("siteId");
        UserSessionHome oUserHome = (UserSessionHome)ServiceLocator.getLocator().getService("HrmUserSession");
        oUser = oUserHome.create();
        HrmFacadeHome oHome = (HrmFacadeHome)ServiceLocator.getLocator().getService("HrmFacade");
        HrmFacade oHrmFacade = oHome.create();
        oUserInfo = oHrmFacade.getLoginUserInfo(sEmpId, sSiteId);
        oUser.setUserInfo(oUserInfo);
        HrmSecondFacadeHome oSecHome = (HrmSecondFacadeHome)ServiceLocator.getLocator().getService("HrmSecondFacade");
        HrmSecondFacade oSecHrmFacade = oSecHome.create();
        HrmThirdFacadeHome oThirdHome = (HrmThirdFacadeHome)ServiceLocator.getLocator().getService("HrmThirdFacade");
        HrmThirdFacade oThirdFacade = oThirdHome.create();
        HashMap oMap = new HashMap();
        oMap.put(ParamUtil.getModuleName(), oHrmFacade);
        oMap.put("SecHRMS", oSecHrmFacade);
        oMap.put("ThirdHRMS", oThirdFacade);
        oUser.setModuleFacade(oMap);
        session.setAttribute(ParamUtil.getSessionBeanName(), oUser);
        EmpInfoBean oEmpInfoBean = new EmpInfoBean();
        oEmpInfoBean.setModuleId("8");
        oEmpInfoBean.setEmpId(oUserInfo.getUserTypeId());
        SecurityBD oSecurityBD = new SecurityBD();
        ArrayList menu = oSecurityBD.getTreeSet(oEmpInfoBean);
        HashMap secMap = oSecurityBD.getAccessInfo(oEmpInfoBean);
        oUser.setAccessInfo(secMap);
        session.setAttribute("menuList", menu);
        session.setAttribute("userId", sEmpId);
      } catch (CreateException ex) {
        ex.printStackTrace();
      }  
    return oUser;
  }
  
  protected void clearDetailLists(BaseForm form, ArrayList oDetailList) throws IllegalAccessException, InvocationTargetException {
    Iterator oIt = oDetailList.iterator();
    while (oIt.hasNext()) {
      DetailScreen oDetailScreen = oIt.next();
      BeanUtils.copyProperty(form, oDetailScreen.get_DetailArrayName(), new ArrayList());
    } 
  }
  
  protected ArrayList getDetailArray(BaseForm oBaseForm) throws EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException {
    String sFormName = oBaseForm.getClass().getName();
    String sScreenName = oBaseForm.getScreenName();
    ArrayList oDetailArray = new ArrayList();
    String sDetailBean = ParamUtil.getDetailBeanName(sFormName, sScreenName);
    BaseDetailBean oBaseDetailBean = (BaseDetailBean)Class.forName(sDetailBean).newInstance();
    ArrayList oFieldList = new ArrayList();
    oFieldList = getFieldList(oFieldList, oBaseDetailBean.getClass());
    ArrayList oList = (ArrayList)EnrgiseUtil.getFieldValue(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName));
    for (int iRecord = 0; iRecord < oBaseForm.getThisPageDetailCount(); iRecord++) {
      if (null == oList)
        break; 
      oBaseDetailBean = oList.get(iRecord);
      if (null == oBaseDetailBean)
        break; 
      Iterator oIt = oFieldList.iterator();
      while (oIt.hasNext()) {
        try {
          Field ob = oIt.next();
          String sFieldName = ob.getName();
          if (sFieldName.startsWith("disab"))
            continue; 
          if (ob.getType().getName().equals("java.lang.String")) {
            String sValue = BeanUtils.getIndexedProperty(oBaseForm, sFieldName, iRecord);
            BeanUtils.setProperty(oBaseDetailBean, sFieldName, sValue);
          } 
        } catch (NoSuchMethodException noSuchMethodException) {}
      } 
      oDetailArray.add(oBaseDetailBean);
    } 
    return oDetailArray;
  }
  
  private ArrayList getFieldList(ArrayList oFieldList, Class oClass) {
    if (oClass == null)
      return oFieldList; 
    Field[] oFields = oClass.getDeclaredFields();
    EnrgiseUtil.addToList(oFieldList, (Object[])oFields);
    return getFieldList(oFieldList, oClass.getSuperclass());
  }
  
  protected void changeMode(BaseForm oBaseForm, HttpServletRequest request) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    String sFormName = oBaseForm.getClass().getName();
    String sScreenName = oBaseForm.getScreenName();
    BaseHeaderBean oBaseHeaderBean = getBaseHeaderBean(sFormName);
    BeanUtils.copyProperties(oBaseForm, oBaseHeaderBean);
    clearDetailLists(oBaseForm, ParamUtil.getDetailList(sFormName));
    UserSession oUser = getUserSessionBean(request);
    oUser.putBaseHeaderVO(sFormName, null);
    oUser.removeAllDetailVO(sFormName);
    resetForm(oBaseForm);
  }
  
  protected void checkHeaderNavigation(BaseForm oBaseForm, BaseHeaderVO oBaseHeaderVO) throws EnrgiseApplicationException {
    long lPositionRequested = Long.parseLong(oBaseForm.getUserPositionRequested());
    if (null != oBaseHeaderVO) {
      if (lPositionRequested <= 0L || lPositionRequested > oBaseHeaderVO.getTotalCount())
        throw new EnrgiseApplicationException("wenrgise.common.headerNavigation", "E"); 
      oBaseForm.setPositionRequested(lPositionRequested);
    } else {
      throw new EnrgiseApplicationException("wenrgise.common.headerNavigation", "E");
    } 
  }
  
  private void resetForm(BaseForm oBaseForm) {
    oBaseForm.setPageRequested(0L);
    oBaseForm.setPositionRequested(0L);
    oBaseForm.setTotalCount(0L);
    oBaseForm.setTotalDetailRecord(0L);
    oBaseForm.setTotalHeaderRecord(0L);
    oBaseForm.setHeaderPrimaryKey(null);
    oBaseForm.setUserPageRequested(null);
    oBaseForm.setDetailId(null);
    oBaseForm.setStatus(null);
    oBaseForm.setDetailDataChanged(false);
    oBaseForm.setHeaderDataChanged(false);
    oBaseForm.setChecked(null);
    oBaseForm.setUserPositionRequested(null);
    oBaseForm.setTotalCount(0L);
    oBaseForm.setNewPageRequested(null);
    oBaseForm.setItemChecked(null);
    oBaseForm.setLovKey(null);
    oBaseForm.setTxtSearchFields(null);
    oBaseForm.setTxtDisplayFields(null);
    oBaseForm.setTxtIndex(null);
    oBaseForm.setThisPageDetailCount(0);
    oBaseForm.setTotalPageCount(0L);
    oBaseForm.setHeaderStatus(null);
    oBaseForm.setButtonClicked(null);
    oBaseForm.setButtonName(null);
    oBaseForm.setDetailStartPage(0);
  }
  
  protected void checkDetailNavigation(BaseForm oBaseForm, BaseDetailVO oBaseDetailVO) throws EnrgiseApplicationException {
    boolean flag = false;
    long lPageRequested = 0L;
    if (EnrgiseUtil.checkString(oBaseForm.getUserPageRequested())) {
      lPageRequested = Long.parseLong(oBaseForm.getUserPageRequested());
    } else {
      flag = true;
    } 
    if (null != oBaseDetailVO) {
      if (flag == false) {
        if (lPageRequested <= 0L || oBaseDetailVO.getTotalDetailRecord() <= oBaseDetailVO.getRecordsPerPage() * (lPageRequested - 1L))
          throw new EnrgiseApplicationException("wenrgise.common.detailNavigation", "E"); 
        oBaseForm.setPageRequested(lPageRequested);
      } else if (oBaseForm.getDetailList() != null) {
        oBaseForm.setPageRequested(1L);
      } else {
        oBaseForm.setPageRequested(0L);
      } 
    } else {
      throw new EnrgiseApplicationException("wenrgise.common.detailNavigation", "E");
    } 
  }
  
  protected void enableDisable(BaseForm oBaseForm, ArrayList arylstFields, String enableFlag) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    if (null != arylstFields) {
      Iterator iterFields = arylstFields.iterator();
      while (iterFields.hasNext()) {
        StringBuffer sField = new StringBuffer(iterFields.next());
        String sProperty = String.valueOf("setDisab").concat(String.valueOf(sField.toString()));
        try {
          Class[] oCls = { Class.forName("java.lang.String") };
          Method oMethod = oBaseForm.getClass().getMethod(sProperty, oCls);
          String sFlag = enableFlag.equals("D") ? "true" : "false";
          Object[] obj = { sFlag };
          oMethod.invoke(oBaseForm, obj);
        } catch (NoSuchMethodException nse) {
          System.out.println(String.valueOf(String.valueOf(String.valueOf("The problem is ").concat(String.valueOf(nse.getMessage()))).concat(String.valueOf(" "))).concat(String.valueOf(sProperty)));
        } 
      } 
    } 
  }
  
  protected void enableAll(BaseForm oBaseForm) {
    try {
      ArrayList arylstDisFields = new ArrayList();
      ArrayList arylstFields = new ArrayList();
      arylstFields = getFieldList(arylstFields, oBaseForm.getClass());
      Iterator iterFields = arylstFields.iterator();
      while (iterFields.hasNext()) {
        Field oField = iterFields.next();
        if (oField.getName().startsWith("disab"))
          arylstDisFields.add(oField.getName().replaceFirst("disab", "")); 
      } 
      enableDisable(oBaseForm, arylstDisFields, "E");
    } catch (Exception exception) {}
  }
  
  protected void disableAll(BaseForm oBaseForm) {
    try {
      ArrayList arylstDisFields = new ArrayList();
      ArrayList arylstFields = new ArrayList();
      arylstFields = getFieldList(arylstFields, oBaseForm.getClass());
      Iterator iterFields = arylstFields.iterator();
      while (iterFields.hasNext()) {
        Field oField = iterFields.next();
        if (oField.getName().startsWith("disab"))
          arylstDisFields.add(oField.getName().replaceFirst("disab", "")); 
      } 
      enableDisable(oBaseForm, arylstDisFields, "D");
    } catch (Exception exception) {}
  }
  
  protected ArrayList queryClicked() {
    ArrayList arylstFields = new ArrayList();
    arylstFields.add("butQuery");
    arylstFields.add("butSave");
    arylstFields.add("butDelete");
    arylstFields.add("butPrint");
    arylstFields.add("butGetDetail");
    arylstFields.add("butAddRow");
    arylstFields.add("butDelRow");
    arylstFields.add("butNextHeader");
    arylstFields.add("butPrevHeader");
    arylstFields.add("newPositionRequested");
    arylstFields.add("butJumpHeader");
    arylstFields.add("butNextDetail");
    arylstFields.add("butPrevDetail");
    arylstFields.add("newPageRequested");
    arylstFields.add("butJumpDetail");
    return arylstFields;
  }
  
  protected ArrayList insertClicked() {
    ArrayList arylstFields = new ArrayList();
    arylstFields.add("butInsert");
    arylstFields.add("butExecute");
    arylstFields.add("butDelete");
    arylstFields.add("butPrint");
    arylstFields.add("butGetDetail");
    arylstFields.add("butAddRow");
    arylstFields.add("butDelRow");
    arylstFields.add("butNextHeader");
    arylstFields.add("butPrevHeader");
    arylstFields.add("newPositionRequested");
    arylstFields.add("butJumpHeader");
    arylstFields.add("butNextDetail");
    arylstFields.add("butPrevDetail");
    arylstFields.add("newPageRequested");
    arylstFields.add("butJumpDetail");
    return arylstFields;
  }
  
  protected ArrayList executeClicked() {
    ArrayList arylstFields = new ArrayList();
    arylstFields.add("butExecute");
    arylstFields.add("butAddRow");
    arylstFields.add("butDelRow");
    arylstFields.add("butExecute");
    arylstFields.add("butNextDetail");
    arylstFields.add("butPrevDetail");
    arylstFields.add("butJumpDetail");
    arylstFields.add("newPageRequested");
    return arylstFields;
  }
  
  protected ArrayList deleteClicked() {
    ArrayList arylstFields = new ArrayList();
    arylstFields.add("butQuery");
    arylstFields.add("butExecute");
    arylstFields.add("butDelete");
    return arylstFields;
  }
  
  protected ArrayList saveClicked() {
    ArrayList arylstFields = new ArrayList();
    arylstFields.add("butExecute");
    arylstFields.add("butGetDetail");
    return arylstFields;
  }
  
  protected ArrayList refreshClicked() {
    ArrayList arylstFields = new ArrayList();
    return arylstFields;
  }
  
  protected ArrayList nextHeaderClicked() {
    ArrayList arylstFields = new ArrayList();
    arylstFields.add("butExecute");
    arylstFields.add("butAddRow");
    arylstFields.add("butDelRow");
    arylstFields.add("butNextDetail");
    arylstFields.add("butPrevDetail");
    arylstFields.add("butJumpDetail");
    arylstFields.add("newPageRequested");
    return arylstFields;
  }
  
  protected ArrayList prevHeaderClicked() {
    ArrayList arylstFields = new ArrayList();
    arylstFields.add("butExecute");
    arylstFields.add("butAddRow");
    arylstFields.add("butDelRow");
    arylstFields.add("butNextDetail");
    arylstFields.add("butPrevDetail");
    arylstFields.add("butJumpDetail");
    arylstFields.add("newPageRequested");
    return arylstFields;
  }
  
  protected ArrayList getDetailClicked() {
    ArrayList arylstFields = new ArrayList();
    arylstFields.add("butGetDetail");
    return arylstFields;
  }
  
  protected ArrayList addRowClicked() {
    ArrayList arylstFields = new ArrayList();
    arylstFields.add("butGetDetail");
    return arylstFields;
  }
  
  protected ArrayList delRowClicked() {
    ArrayList arylstFields = new ArrayList();
    arylstFields.add("butDelRow");
    arylstFields.add("butGetDetail");
    return arylstFields;
  }
  
  protected void controlHeaderNavigation(BaseForm oBaseForm) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    ArrayList arylstFields = new ArrayList();
    long lTotalRecord = oBaseForm.getTotalCount();
    long lRecordNum = oBaseForm.getPositionRequested();
    if (lTotalRecord == 0L || lTotalRecord == 1L) {
      arylstFields.add("butNextHeader");
      arylstFields.add("butPrevHeader");
      arylstFields.add("newPositionRequested");
      arylstFields.add("butJumpHeader");
    } else {
      if (lRecordNum == lTotalRecord)
        arylstFields.add("butNextHeader"); 
      if (lRecordNum == 1L)
        arylstFields.add("butPrevHeader"); 
    } 
    enableDisable(oBaseForm, arylstFields, "D");
  }
  
  protected void controlDetailNavigation(BaseForm oBaseForm) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    ArrayList oEnableList = new ArrayList();
    ArrayList oDisableList = new ArrayList();
    long lTotalRecord = oBaseForm.getTotalPageCount();
    long lRecordNum = oBaseForm.getPageRequested();
    if (lTotalRecord == 0L || lTotalRecord == 1L) {
      oDisableList.add("butNextDetail");
      oDisableList.add("butPrevDetail");
      oDisableList.add("newPageRequested");
      oDisableList.add("butJumpDetail");
    } else if (lRecordNum == lTotalRecord) {
      oEnableList.add("butPrevDetail");
      oEnableList.add("newPageRequested");
      oEnableList.add("butJumpDetail");
      oDisableList.add("butNextDetail");
    } else if (lRecordNum == 1L) {
      oEnableList.add("newPageRequested");
      oEnableList.add("butJumpDetail");
      oEnableList.add("butNextDetail");
      oDisableList.add("butPrevDetail");
    } else {
      oEnableList.add("newPageRequested");
      oEnableList.add("butJumpDetail");
      oEnableList.add("butNextDetail");
      oEnableList.add("butPrevDetail");
    } 
    enableDisable(oBaseForm, oEnableList, "E");
    enableDisable(oBaseForm, oDisableList, "D");
  }
  
  protected void resetDetailPageData(BaseForm oBaseForm) {
    oBaseForm.setPageRequested(0L);
    oBaseForm.setTotalPageCount(0L);
  }
  
  protected long calculateTotalDetailPage(String sFormName, String sScreenName, long lTotDetRecord) {
    long totRecPerPage = ParamUtil.getDetailRecordPerPage(sFormName, sScreenName);
    if (lTotDetRecord % totRecPerPage != 0L)
      return lTotDetRecord / totRecPerPage + 1L; 
    return lTotDetRecord / totRecPerPage;
  }
  
  protected BaseDetailVO getDetailData(BaseForm oBaseForm, String sFormName, String sScreenName, long lPageRequested, HttpServletRequest request, BaseBD oBaseBD, boolean bForce, UserSession oUser) throws RemoteException, ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, EnrgiseApplicationException, EnrgiseSystemException {
    String sHeaderPrimaryKey = oBaseForm.getHeaderPrimaryKey();
    String pseudoHeader = ParamUtil.getPseudoHeaderFlag(sFormName);
    DetailSizeValues oDetailSizeValues = new DetailSizeValues();
    oDetailSizeValues.setDetailRecordPerPage(ParamUtil.getDetailRecordPerPage(sFormName, sScreenName));
    oDetailSizeValues.setMaxPages(ParamUtil.getMaxDetailPages(sFormName, sScreenName));
    BaseDetailVO oBaseDetailVO = null;
    oBaseDetailVO = oUser.getBaseDetailVO(sFormName, sScreenName);
    if (!EnrgiseUtil.checkString(pseudoHeader)) {
      oBaseDetailVO = oBaseBD.getDetailRecord(sFormName, sScreenName, sHeaderPrimaryKey, lPageRequested, oDetailSizeValues, oBaseDetailVO, bForce, oUser);
    } else {
      BaseQueryVO oBaseQueryVO = getQueryVO((ActionForm)oBaseForm);
      BeanUtils.copyProperties(oBaseQueryVO, oBaseForm);
      oBaseDetailVO = oBaseBD.getDetailRecord(sFormName, sScreenName, oBaseQueryVO, lPageRequested, oDetailSizeValues, oBaseDetailVO, bForce, oUser);
    } 
    if (oBaseDetailVO.getOThisPageData() != null)
      BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oBaseDetailVO.getOThisPageData()); 
    oBaseForm.setPageRequested(lPageRequested);
    oBaseForm.setThisPageDetailCount((null != oBaseDetailVO.getOThisPageData()) ? oBaseDetailVO.getOThisPageData().size() : 0);
    BeanUtils.copyProperties(oBaseForm, oBaseDetailVO);
    oBaseForm.setNewPageRequested(null);
    oBaseForm.setPageRequested(lPageRequested);
    oUser.putBaseDetailVO(sFormName, sScreenName, oBaseDetailVO);
    return oBaseDetailVO;
  }
  
  protected void onLoad(ActionForm form, HttpServletRequest request, int actionName) throws RemoteException, ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, EnrgiseApplicationException, EnrgiseSystemException {
    BaseForm oBaseForm = (BaseForm)form;
    String sFormName = form.getClass().getName();
    String sScreenName = oBaseForm.getScreenName();
    BaseDetailVO oBaseDetailVO = new BaseDetailVO();
    UserSession oUser = getUserSessionBean(request);
    BaseBD oBaseBD = getHeaderBusinessDelegate(form, request);
    BaseHeaderBean oBaseHeaderBean = getBaseHeaderBean(sFormName);
    BeanUtils.copyProperties(oBaseHeaderBean, form);
    ThisPageVO oThisPageVO = new ThisPageVO();
    oThisPageVO.setActionName(actionName);
    oThisPageVO.setOHeaderBean(oBaseHeaderBean);
    oThisPageVO.setScreenMode(oBaseForm.getScreenMode());
    oThisPageVO.setScreenName(oBaseForm.getScreenName());
    oBaseDetailVO = oUser.getBaseDetailVO(sFormName, sScreenName);
    if (null != oBaseDetailVO) {
      ArrayList oDetailList = getDetailArrayList(oBaseForm, sFormName, sScreenName);
      oThisPageVO.setODetailList(oDetailList);
    } 
    oBaseBD.onLoadAction(oThisPageVO);
    if (oThisPageVO != null)
      if (oThisPageVO.getOHeaderBean() != null)
        BeanUtils.copyProperties(form, oThisPageVO.getOHeaderBean());  
    if (oThisPageVO.getODetailList() != null) {
      oBaseForm.setThisPageDetailCount(oThisPageVO.getODetailList().size());
      BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oThisPageVO.getODetailList());
    } 
  }
  
  protected void checkAccessInfo(UserSession oUserSession, String sComponentName, String sScreenMode, String sButtonName, int iActionName, String sWorkListId) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    HashMap oSecMap = oUserSession.getAccessInfo();
    AccessBean objBean = (AccessBean)oSecMap.get(sComponentName);
    if (!EnrgiseUtil.checkString(sWorkListId)) {
      if (iActionName == 10 || iActionName == 14 || iActionName == 23) {
        String s = null;
        System.out.println("I am in AccessInfo");
        System.out.println("Insert Flag: ");
        try {
          if (!objBean.getInsertFlag().equalsIgnoreCase("Y")) {
            System.out.println("after Insert Flag: ");
            ArrayList oParam = new ArrayList();
            MessageKey oMessageKey = new MessageKey("right.for.insert");
            oParam.add(oMessageKey);
            throw new EnrgiseMessageKeyException("wenrgise.authorisation.error", oParam, "M");
          } 
        } catch (Exception e) {
          e.printStackTrace();
        } 
      } 
      if (iActionName == 8)
        if (sScreenMode.equalsIgnoreCase("N")) {
          if (!objBean.getInsertFlag().equalsIgnoreCase("Y")) {
            ArrayList oParam = new ArrayList();
            MessageKey oMessageKey = new MessageKey("right.for.insert");
            oParam.add(oMessageKey);
            throw new EnrgiseMessageKeyException("wenrgise.authorisation.error", oParam, "M");
          } 
        } else if (sScreenMode.equalsIgnoreCase("U")) {
          if (!objBean.getUpdateFlag().equalsIgnoreCase("Y")) {
            ArrayList oParam = new ArrayList();
            MessageKey oMessageKey = new MessageKey("right.for.update");
            oParam.add(oMessageKey);
            throw new EnrgiseMessageKeyException("wenrgise.authorisation.error", oParam, "M");
          } 
        }  
      if (iActionName == 17 || iActionName == 15)
        if (!objBean.getDeleteFlag().equalsIgnoreCase("Y")) {
          ArrayList oParam = new ArrayList();
          MessageKey oMessageKey = new MessageKey("right.for.delete");
          oParam.add(oMessageKey);
          throw new EnrgiseMessageKeyException("wenrgise.authorisation.error", oParam, "M");
        }  
    } 
  }
  
  protected String getComponentName(String sFormName) {
    String sModFormName = sFormName.replace('.', ',');
    int index = 0;
    char g = ',';
    char[] arrayModeFormName = sModFormName.toCharArray();
    int k;
    for (k = arrayModeFormName.length - 1; k > 0; k--) {
      if (arrayModeFormName[k] == g) {
        index = k + 1;
        break;
      } 
    } 
    String sComponentName = sModFormName.substring(index, sModFormName.length());
    return sComponentName;
  }
  
  public abstract ActionForward executeImpl(ActionMapping paramActionMapping, ActionForm paramActionForm, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException;
}
