package wenrgise.hrms.webtier.action;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.ejb.CreateException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
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.bean.EmpInfoBean;
import wenrgise.common.businessdelegate.SecurityBD;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.ServiceLocator;
import wenrgise.common.utility.UserInfo;
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.hrms.webtier.form.HrmBaseForm;

public class HrmGenLoginAction extends Action {
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Cookie[] cookies = request.getCookies();
    String sEmpId = null;
    String sSiteId = null;
    if (null != cookies)
      for (int i = 0; i < cookies.length; i++) {
        if ("EmpId".equals(cookies[i].getName()))
          sEmpId = cookies[i].getValue(); 
        if ("SiteId".equals(cookies[i].getName()))
          sSiteId = cookies[i].getValue(); 
      }  
    HttpSession oSes = request.getSession();
    HrmBaseForm oBaseForm = new HrmBaseForm();
    oBaseForm.setEmpId(sEmpId);
    try {
      populateUserInfo(sEmpId, sSiteId, oSes);
    } catch (EnrgiseSystemException enrgiseSystemException) {}
    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());
      if (oUser == null) {
        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 (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 String getUserId(String sEmpId) {
    return null;
  }
}
