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

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.StringTokenizer;
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.businessdelegate.LOVBD;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.UserInfo;
import wenrgise.common.vo.LovQueryVO;
import wenrgise.common.vo.LovVO;
import wenrgise.common.webtier.action.BaseAction;
import wenrgise.common.webtier.form.BaseLOVForm;
import wenrgise.common.xml.vo.LOVInfo;
import wenrgise.ejb.common.session.UserSession;
import wenrgise.ejb.common.utility.LOVManager;

public class LOVAction extends BaseAction {
  public ActionForward executeImpl(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    BaseLOVForm oBaseLOVForm = (BaseLOVForm)form;
    System.out.println(oBaseLOVForm.getLovKey());
    UserSession oUser = getUserSessionBean(request);
    LOVInfo oLOVInfo = LOVManager.getInstance().getCachedObject(oBaseLOVForm.getLovKey());
    if (oLOVInfo != null)
      if (EnrgiseUtil.checkString(oLOVInfo.getRecursiveFlag())) {
        if (oLOVInfo.getRecursiveFlag().equalsIgnoreCase("Y")) {
          processRecursiveLov(oBaseLOVForm, oLOVInfo, mapping);
          oBaseLOVForm.setRecursiveFlag("Y");
        } else {
          processSimpleLov(oBaseLOVForm, oLOVInfo, mapping, oUser.getUserInfo());
          oBaseLOVForm.setRecursiveFlag("N");
        } 
      } else {
        processSimpleLov(oBaseLOVForm, oLOVInfo, mapping, oUser.getUserInfo());
        oBaseLOVForm.setRecursiveFlag("N");
      }  
    return mapping.findForward("success");
  }
  
  private ActionForward processSimpleLov(BaseLOVForm oBaseLOVForm, LOVInfo oLOVInfo, ActionMapping mapping, UserInfo oUserInfo) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    if (oBaseLOVForm.getTxtHidAction().equals("FirstTime")) {
      oBaseLOVForm.setTxtHidTotCols(-1);
      oBaseLOVForm.setTxtHidSubmitFirst("true");
      oBaseLOVForm.setLevel(0);
      oBaseLOVForm.setFinalLevel(0);
      return mapping.findForward("success");
    } 
    if (oBaseLOVForm.getTxtHidAction().equals("SearchRecords")) {
      LOVBD oLOVBD = new LOVBD();
      LovVO oLovVO = new LovVO();
      LovQueryVO oLOVQueryVO = new LovQueryVO();
      StringTokenizer stParameters = new StringTokenizer(oBaseLOVForm.getQueryParam(), ",");
      while (stParameters.hasMoreTokens()) {
        String[] sKeyValuePair = stParameters.nextToken().split("=");
        oLOVQueryVO.setProperty(sKeyValuePair[0], sKeyValuePair[1]);
      } 
      oLOVQueryVO.setProperty("LoginSiteId", oUserInfo.getSiteId());
      oLOVQueryVO.setProperty("ListSiteId", String.valueOf(String.valueOf("(").concat(String.valueOf(oUserInfo.getSiteId()))).concat(String.valueOf(")")));
      oLOVQueryVO.setSearchField1(oBaseLOVForm.getSearchField1());
      oLOVQueryVO.setSearchField2(oBaseLOVForm.getSearchField2());
      oLOVQueryVO.setSearchField2(oBaseLOVForm.getSearchField2());
      oLOVQueryVO.setSearchField3(oBaseLOVForm.getSearchField3());
      oLOVQueryVO.setSearchField4(oBaseLOVForm.getSearchField4());
      oLOVQueryVO.setSearchField5(oBaseLOVForm.getSearchField5());
      oLOVQueryVO.setSearchField6(oBaseLOVForm.getSearchField6());
      oLOVQueryVO.setLevel(0);
      oBaseLOVForm.setFinalLevel(-1);
      oLovVO = oLOVBD.getLOVData(oLOVInfo, oLOVQueryVO);
      if (oBaseLOVForm.getLevel() == 0)
        oBaseLOVForm.setLevel(oLovVO.getFinalLevel()); 
      if (oLovVO.getDetailList().size() > 0) {
        BeanUtils.copyProperties(oBaseLOVForm, oLovVO);
        oBaseLOVForm.setTxtHidTotRows(oLovVO.getDetailList().size());
        oBaseLOVForm.setTxtHidTotCols(oLovVO.getHeaderList().size());
      } else {
        oBaseLOVForm.setTxtHidTotRows(0);
        oBaseLOVForm.setTxtHidTotCols(-1);
      } 
      oBaseLOVForm.setTxtHidSubmitFirst("false");
      oBaseLOVForm.setInsertFlag(oLOVInfo.getInsertFlag());
    } 
    return null;
  }
  
  private ActionForward processRecursiveLov(BaseLOVForm oBaseLOVForm, LOVInfo oLOVInfo, ActionMapping mapping) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    if (oBaseLOVForm.getTxtHidAction().equals("FirstTime")) {
      oBaseLOVForm.setTxtHidTotCols(-1);
      oBaseLOVForm.setTxtHidSubmitFirst("true");
      oBaseLOVForm.setLevel(-999);
      return mapping.findForward("success");
    } 
    if (oBaseLOVForm.getTxtHidAction().equals("SearchRecords")) {
      LOVBD oLOVBD = new LOVBD();
      LovVO oLovVO = new LovVO();
      LovQueryVO oLOVQueryVO = new LovQueryVO();
      StringTokenizer stParameters = new StringTokenizer(oBaseLOVForm.getQueryParam(), ",");
      while (stParameters.hasMoreTokens()) {
        String[] sKeyValuePair = stParameters.nextToken().split("=");
        oLOVQueryVO.setProperty(sKeyValuePair[0], sKeyValuePair[1]);
      } 
      oLOVQueryVO.setSearchField1(oBaseLOVForm.getSearchField1());
      oLOVQueryVO.setSearchField2(oBaseLOVForm.getSearchField2());
      oLOVQueryVO.setLevel(oBaseLOVForm.getLevel());
      int iLevel = oBaseLOVForm.getLevel();
      int iFinalLevel = oBaseLOVForm.getFinalLevel();
      int iInitialLevel = oBaseLOVForm.getInitialLevel();
      oLOVQueryVO.setFinalLevel(iFinalLevel);
      oLovVO = oLOVBD.getLOVData(oLOVInfo, oLOVQueryVO);
      if (oLovVO.getDetailList().size() > 0) {
        BeanUtils.copyProperties(oBaseLOVForm, oLovVO);
        oBaseLOVForm.setTxtHidTotRows(oLovVO.getDetailList().size());
        oBaseLOVForm.setTxtHidTotCols(oLovVO.getHeaderList().size());
      } else {
        oBaseLOVForm.setTxtHidTotRows(0);
        oBaseLOVForm.setTxtHidTotCols(-1);
      } 
      oBaseLOVForm.setTxtHidSubmitFirst("false");
      if (oLOVQueryVO.getLevel() == -999) {
        oBaseLOVForm.setLevel(oLovVO.getInitialLevel());
        oBaseLOVForm.setFinalLevel(oLovVO.getFinalLevel());
        oBaseLOVForm.setInitialLevel(oLovVO.getInitialLevel());
      } else {
        oBaseLOVForm.setLevel(iLevel);
        oBaseLOVForm.setFinalLevel(iFinalLevel);
        oBaseLOVForm.setInitialLevel(iInitialLevel);
      } 
      oBaseLOVForm.setInsertFlag(oLOVInfo.getInsertFlag());
    } 
    return null;
  }
}
