package com.console.web;

import com.console.core.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.Logger;

public class  FrontSessionListener implements HttpSessionListener{
	
	private static org.apache.log4j.Logger log = Logger.getLogger(FrontSessionListener.class);

	public void sessionCreated(HttpSessionEvent hse){
		
		if(log.isDebugEnabled()){
			log.debug("New Session Created");
		}
		
		HttpSession mySession = hse.getSession();
		
		ServletContext mySContext = hse.getSession().getServletContext();
		
		UserBean usr = new UserBean();
		mySession.setAttribute("userBean",usr);
		if(log.isDebugEnabled()){
			log.debug("UserBean added to session");
		}
		
		LoginBean loginBean = new LoginBean();
		mySession.setAttribute("loginBean", loginBean);
		if(log.isDebugEnabled()){
			log.debug("LoginBean added to session");
		}
		
		RegisterBean registerBean = new RegisterBean();
		mySession.setAttribute("registerBean", registerBean);
		if(log.isDebugEnabled()){
			log.debug("RegisterBean added to session");
		}
		
		ApplicationBean appBean = (ApplicationBean) mySContext.getAttribute("ApplicationBean");
				
		appBean.addActiveGuest(mySession.getId(),usr.getUsr().getF_name());
		
	}
	public void sessionDestroyed(HttpSessionEvent hse){
		
		HttpSession mySession = hse.getSession();
		
		ServletContext mySContext = hse.getSession().getServletContext();
		
		if(log.isDebugEnabled()){
			log.debug("Session Destroyed");
		}

		mySession.removeAttribute("UserBean");
		if(log.isDebugEnabled()){
			log.debug("UserBean removed from session");
		}		
		
		mySession.removeAttribute("LoginBean");
		if(log.isDebugEnabled()){
			log.debug("LoginBean removed from session");
		}		
		
		mySession.removeAttribute("RegisterBean");
		if(log.isDebugEnabled()){
			log.debug("RegisterBean removed from session");
		}	
		
	}

}
