/*
 * Created on Sep 6, 2005
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package edu.uprm.ece.icom5016.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import edu.uprm.ece.icom5016.bean.Customer;

/**
 * @author elliot
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class LogInChecker {
	
	public static String getLoginLink(HttpServletRequest request){
		HttpSession session = request.getSession(false);
		String display = "<A HREF=\"/icom5016/login.jsp\">Login</A>";;
		//Check if session is null
		//if(session != null){
			/*
			 * Session is not null
			 * Check if a Cheker exist in the session and
			 * if the checker id equals the session id
			 */
			//Customer ch = (Customer)session.getAttribute("customer");
			//if((ch != null) && (ch.getSession() == session.getId()))
				//This user is already log on
			if(checkLogin(session))
				display = "<A HREF=\"/icom5016/servlet/LogoutServlet\">Logout</A>";
		//}
		return display;
	}
	
	/*public static void addCustomerToSession(HttpServletRequest request){
		HttpSession session = request.getSession(true);
		//This should check if the client exists in the Database
		String id = "gambo_" + Math.random() * 1000;
		session.setAttribute("customer", new Customer(id));
	}*/
	
	/*public LogInCheker getNewCheker(String id){
		return new LogInCheker(id);
	}*/
	
	public synchronized static boolean checkLogin(HttpSession session){
		if(session != null){
			Customer c = (Customer) session.getAttribute("customer");
			if(c != null)
				return true;				
		}
		return false;
	}
}
