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

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.HashMap;
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.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.ServiceLocator;
import wenrgise.common.utility.UserInfo;
import wenrgise.common.webtier.form.BaseForm;
import wenrgise.ejb.common.session.UserSession;
import wenrgise.ejb.common.session.UserSessionHome;
import wenrgise.ejb.common.utility.ParamUtil;
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;
import wenrgise.workflow.bean.WflWorkListInfoBean;
import wenrgise.workflow.utility.WflDocParamUtil;

public class HrmGenWflAction extends Action {
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    String sWorkListId = request.getParameter("workListId");
    System.out.println(sWorkListId);
    WflWorkListInfoBean wflWorkListInfoBean = getWorkListInfo(sWorkListId);
    if (WflDocParamUtil.getFormName(wflWorkListInfoBean.getDocTypeCode()) != null && WflDocParamUtil.getFormBeanName(wflWorkListInfoBean.getDocTypeCode()) != null) {
      BaseForm oForm = (BaseForm)Class.forName(WflDocParamUtil.getFormName(wflWorkListInfoBean.getDocTypeCode())).newInstance();
      oForm.setScreenMode("Q");
      oForm.setHeaderPrimaryKey(wflWorkListInfoBean.getDocId());
      oForm.setHidWorkListId(sWorkListId);
      oForm.setFirstLoad("false");
      oForm.setScreenName(WflDocParamUtil.getScreenName(wflWorkListInfoBean.getDocTypeCode()));
      HttpSession oSes = request.getSession();
      oSes.setAttribute(WflDocParamUtil.getFormBeanName(wflWorkListInfoBean.getDocTypeCode()), oForm);
      System.out.println("Populating User Info:-");
      populateUserInfo(wflWorkListInfoBean.getRequestorEmpId(), null, oSes);
      System.out.println("User Info Populated:-");
      System.out.println(String.valueOf("The type is ").concat(String.valueOf(wflWorkListInfoBean.getDocTypeCode())));
      return mapping.findForward(WflDocParamUtil.getForwardName(wflWorkListInfoBean.getDocTypeCode()));
    } 
    return mapping.findForward("success");
  }
  
  private UserInfo populateUserInfo(String sEmpId, String sSiteId, HttpSession session) throws EnrgiseSystemException {
    UserInfo oUserInfo = new UserInfo();
    try {
      UserSession oUser = (UserSession)session.getAttribute(ParamUtil.getSessionBeanName());
      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);
      session.setAttribute("userId", sEmpId);
    } catch (RemoteException oRmt) {
      oRmt.printStackTrace();
      throw new EnrgiseSystemException();
    } catch (CreateException oCrt) {
      oCrt.printStackTrace();
      throw new EnrgiseSystemException();
    } catch (Exception oExCc) {
      oExCc.printStackTrace();
      throw new EnrgiseSystemException();
    } 
    return oUserInfo;
  }
  
  private void populate(String sEmpId, HttpSession session) throws EnrgiseSystemException {
    try {
      UserSession oUser = (UserSession)session.getAttribute(ParamUtil.getSessionBeanName());
      if (oUser == null) {
        UserSessionHome oUserHome = (UserSessionHome)ServiceLocator.getLocator().getService("HrmUserSession");
        oUser = oUserHome.create();
        String sUserId = getUserId(sEmpId);
        UserInfo oUserInfo = populateUserInfo(sUserId);
        oUser.setUserInfo(oUserInfo);
        HrmFacadeHome oHome = (HrmFacadeHome)ServiceLocator.getLocator().getService("HrmFacade");
        HrmFacade oHrmFacade = oHome.create();
        HashMap oMap = new HashMap();
        oMap.put(ParamUtil.getModuleName(), oHrmFacade);
        oUser.setModuleFacade(oMap);
        session.setAttribute(ParamUtil.getSessionBeanName(), oUser);
      } 
    } catch (RemoteException oRmt) {
      oRmt.printStackTrace();
      throw new EnrgiseSystemException();
    } catch (CreateException oCrt) {
      oCrt.printStackTrace();
      throw new EnrgiseSystemException();
    } catch (Exception oExCc) {
      oExCc.printStackTrace();
      throw new EnrgiseSystemException();
    } 
  }
  
  private UserInfo populateUserInfo(String sUserId) {
    return null;
  }
  
  private String getUserId(String sEmpId) {
    return null;
  }
  
  private WflWorkListInfoBean getWorkListInfo(String sWorkListId) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      HrmFacadeHome oHome = (HrmFacadeHome)ServiceLocator.getLocator().getService("HrmFacade");
      HrmFacade oHrmFacade = oHome.create();
      return oHrmFacade.getWorkListInfo(sWorkListId);
    } catch (CreateException oCrtExcep) {
      oCrtExcep.printStackTrace();
      throw new EnrgiseSystemException(oCrtExcep);
    } catch (RemoteException oRt) {
      oRt.printStackTrace();
      throw new EnrgiseSystemException(oRt);
    } 
  }
}
