/*
 * Logout.java
 *
 * Created on 30 de agosto de 2004, 10:34
 */

package com.fenalco.servlets;

import java.io.IOException;
import java.util.Enumeration;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fenalco.entities.User;
import com.fenalco.entities.Log;

import com.fenalco.utilities.database.Transaction;
/**
 * Este servlet es el encargado de cerrar la sesión y redireccionar al flujo a la
 * página logout.jsp
 * @author  Mario Linares Vásquez
 * 
 */
public class Logout extends HttpServlet {
    
    /**
     * Inicializa el servlet.
     * @param config
     * @throws ServletException
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
    }
    
    /** Método destructor.
     */
    public void destroy() {
        
    }
    
    /**
     * Procesa solicitudes de los métodos HTTP <code>GET</code> y <code>POST</code>.
     * Este método cierra la sesión usando el método <CODE>invalidate</CODE> y luego redirecciona
     * el flujo a logout.jsp
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException
     * @throws IOException
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        String url = new String("/index.jsp");
        String message = "Ha finalizado su sesión correctamente";
        Enumeration en = request.getSession().getAttributeNames();
        
        HttpSession session = request.getSession(false);
	User callcenter = ((User)session.getAttribute("callcenter"));
        
        
        Transaction t = new Transaction( );
        Log l = new Log();
        long endTime = new java.util.Date().getTime();
        long diffTime =  endTime - request.getSession().getCreationTime();
        java.sql.Time time = new java.sql.Time(diffTime);
        String description = "Duracion de la sesion-->"+ time;
        
        if((User)request.getSession().getAttribute("user")!=null){
            
            l.saveData( ((User)request.getSession().getAttribute("user")).getLogin(), ((User)request.getSession().getAttribute("user")).getIdentificationNumber(), 2, description, (callcenter==null?"":callcenter.getLogin()), t);
            t.execute();
            
        }
        
        
        while ( en.hasMoreElements() )
        {
            request.getSession().removeAttribute( ( String )en.nextElement() );
        }
        
        request.setAttribute( "message", message );

        request.getSession().invalidate();
        
        
        String flag = request.getParameter("flag");
        if(callcenter!=null && flag==null){
            request.getSession().setAttribute("callcenter", callcenter);
            url="/callcenter/callcenter";
        }
        
        
        //RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( url );
        //dispatcher.forward(request, response);
        //response.sendRedirect("/index.jsp?message="+message);
        response.sendRedirect(url + "?message="+message);
        
    }
    
    /**
     * Procesa el método HTTP <code>GET</code>.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException
     * @throws IOException
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /**
     * Procesa el método HTTP <code>POST</code>.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException
     * @throws IOException
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /**
     * Retorna una breve descripción de el servlet.
     * @return
     */
    public String getServletInfo() {
        return "Short description";
    }
    
}
