/*
 * ShopKeepersView.java
 *
 * Created on 10 de agosto de 2004, 18:12
 */

package com.fenalco.servlets.supplier;

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.ShopKeeperList;
import com.fenalco.entities.User;
import com.fenalco.utilities.MessageBuilder;
import com.fenalco.utilities.lists.NonClientShopKeeperListViewSupplier;

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

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

	}

	/**
	 * Destroys the servlet.
	 */
	public void destroy( )
	{

	}

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void processRequest( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException
	{
		HttpSession session = request.getSession(false);
		RequestDispatcher dispatcher;
		String url;
		String message = "";

	    try
	    {
	        if( session == null || ( User )session.getAttribute( "user" ) == null || ( ( User )session.getAttribute( "user" ) ).getRoleId() != 2 )
			{
				url = "/logout";
				//message = "Sesión no iniciada";
				message = MessageBuilder.buildMessage(15);
			}
			else
			{
				User supplier = (User) session.getAttribute("user");
				ShopKeeperList shopKeeperList = new ShopKeeperList();
				shopKeeperList.nonClientList(supplier.getCompanyId(), -1);
	
				NonClientShopKeeperListViewSupplier shopKeeperListView = new NonClientShopKeeperListViewSupplier(
						shopKeeperList);
	
				if( shopKeeperListView == null )
				{
					//Fallo en la creación del listado de minoristas
					//message = "Fallo en la creación del listado de minoristas";
					message = MessageBuilder.buildMessage(9);
					url = "/proveedor/verminoristas.jsp";
				}
				else
				{
					//Todo OK
					//message = "El listado de minoristas se creó correctamente";
					//message = MessageBuilder.buildMessage(3);
					message = "";
					url = "/proveedor/verminoristas.jsp";
	
					request.setAttribute("view", shopKeeperListView);
				}
			}
	    }
	    catch( Exception e )
	    {
	        url="/logout";
	        message = MessageBuilder.buildMessage(15);
	    }
		request.setAttribute("message", message);
		dispatcher = getServletContext().getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}

	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doGet( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException
	{
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException
	{
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 */
	public String getServletInfo( )
	{
		return "Short description";
	}

}