/*
 * CompanyListListener.java
 *
 * Created on 10 de agosto de 2004, 18:19
 */

package com.fenalco.servlets.sysmanager;

import java.io.IOException;

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.ShopKeeper;
import com.fenalco.entities.Supplier;
import com.fenalco.entities.Company;
import com.fenalco.entities.SupplierList;
import com.fenalco.utilities.MessageBuilder;

/**
 * 
 * @author andres
 * @version
 */
public class CompanyListListener extends HttpServlet
{

	/**
	 * Inicializa el servlet.
	 */
	public void init( ServletConfig config ) throws ServletException
	{
		super.init(config);

	}

	/**
	 * Destruye el servlet.
	 */
	public void destroy( )
	{

	}

	/**
	 * Procesa las peticiones de los mètodos HTTP <code>GET</code> y
	 * <code>POST</code>.
	 * 
	 * Recibe del request el parámtero operationType que indica el tipo de
	 * operación a ejecutar
	 * 
	 * 1)"changeStatus" o "delete" -Redirecciona a administrarempresas
	 * 
	 * 2)"setStores" y recibe el id de un tipo de proveedor (supplierId)
	 * -Redirecciona a minoristasexclusivos
	 * 
	 * @param request
	 *            peticiòn al servlet
	 * @param response
	 *            respuesta del servlet
	 */
	protected void processRequest( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException
	{

            String url = "";
            String message = "";
            if(request.getAttribute("message")!=null){
                message = (String)request.getAttribute("message");
            }

            HttpSession session = request.getSession(false);
            
	    try{
	        if( session == null || ( User )session.getAttribute( "user" ) == null || ( ( User )session.getAttribute( "user" ) ).getRoleId() != 1 ){
                    url = "/index.jsp";
                    message = MessageBuilder.buildMessage(15);
                }else{
                    String operationType = request.getParameter("operationType");

                    if( operationType.equals("changeStatus") || operationType.equals("delete") ){
                        url = "/administrador/administrarempresas";
                        
                    }else if( operationType.equals("setStores") ){
                        url = "/administrador/minoristasexclusivos";
                    
                    }else if( operationType.equals("updateSupplier") ){
                        int id = Integer.parseInt(request.getParameter("key"));
                        Supplier supplier = new Supplier(id);
                        request.setAttribute("supplier", supplier);
                        url = "/administrador/actualizarproveedor.jsp";
                    
                    }else if( operationType.equals("updateShopKeeper") ){
                        int id = Integer.parseInt(request.getParameter("key"));
                        ShopKeeper shopKeeper = new ShopKeeper(id);
                        request.setAttribute("shopKeeper", shopKeeper);
                        url = "/administrador/actualizarminorista.jsp";
                    
                    }else{
                        String companyType = request.getParameter("companyType");
                        url = "/administrador/listaempresas?companyType=" + companyType;
                    }
                }
	    }catch( Exception e ){
                System.out.println("Error-> "+e);
	        url="/logout";
	        message = MessageBuilder.buildMessage(15);
	    }

            request.setAttribute("message", message);
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);
	}

	/**
	 * Maneja el mètodo HTTP <code>GET</code>
	 * 
	 * @param request
	 *            peticiòn al servlet
	 * @param response
	 *            respuesta del servlet
	 */
	protected void doGet( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException
	{
		processRequest(request, response);
	}

	/**
	 * Maneja el mètodo HTTP <code>POST</code>
	 * 
	 * @param request
	 *            peticiòn al servlet
	 * @param response
	 *            respuesta del servlet
	 */
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException
	{
		processRequest(request, response);
	}

	/**
	 * Retorna una corta descripciòn del servlet.
	 */
	public String getServletInfo( )
	{
		return "Short description";
	}

}