package wenrgise.ejb.common.utility;

import java.rmi.RemoteException;
import java.util.Locale;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import wenrgise.common.utility.UserInfo;
import wenrgise.ejb.common.session.UserSession;
import wenrgise.ejb.common.session.UserSessionHome;

public class EnrgiseListener implements HttpSessionListener {
  static final Logger log = Logger.getLogger("wenrgise.common.utility.EnrgiseListener");
  
  public void sessionCreated(HttpSessionEvent sEvent) {
    try {
      System.out.println("Creating Session---->");
      HttpSession session = sEvent.getSession();
      UserSession oUser = (UserSession)session.getAttribute(ParamUtil.getSessionBeanName());
      if (oUser == null) {
        UserSessionHome oUserHome = (UserSessionHome)ServiceLocator.getLocator().getService("UserSession");
        oUser = oUserHome.create();
        UserInfo oUserInfo = new UserInfo();
        oUser.setUserInfo(oUserInfo);
        Locale oLocale = (Locale)session.getAttribute("locale");
        oUser.setUserLocale((Locale)session.getAttribute("locale"));
        session.setAttribute(ParamUtil.getSessionBeanName(), oUser);
      } 
    } catch (RemoteException oRmt) {
      log.severe(oRmt.getMessage());
    } catch (CreateException oCrt) {
      log.severe(oCrt.getMessage());
    } catch (Exception oExCc) {
      log.severe(oExCc.getMessage());
    } 
  }
  
  public void sessionDestroyed(HttpSessionEvent sEvent) {
    try {
      HttpSession session = sEvent.getSession();
      if (session != null) {
        UserSession oUser = (UserSession)session.getAttribute(ParamUtil.getSessionBeanName());
        oUser.remove();
      } 
    } catch (RemoveException removeException) {
    
    } catch (RemoteException remoteException) {}
  }
}
