/*
 * ScaleView.java
 *
 * Created on 25 de septiembre de 2004, 15:54
 */

package com.fenalco.servlets.shopkeeper;

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.Community;
import com.fenalco.entities.PriceScale;
import com.fenalco.entities.Store;
import com.fenalco.entities.Supplier;
import com.fenalco.entities.SupplierProduct;
import com.fenalco.entities.User;
import com.fenalco.utilities.MessageBuilder;

/**
 *
 * @author  andres
 * @version
 */
public class ScaleView extends HttpServlet
{
    
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
    }
    
    /** Destroys the servlet.
     */
    public void destroy() {
        
    }
    
    /** 
     * Carga la escala de precios de un producto dado y la pasa al jsp para que
     * la muestre
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        HttpSession session = request.getSession(false);
        String url = "";
        String message = "";
        
	    try
	    {
	        if( session == null || ( User )session.getAttribute( "user" ) == null || ( ( User )session.getAttribute( "user" ) ).getRoleId() != 3 )
	        {
	            url="/index.jsp";
	            message = MessageBuilder.buildMessage(15);
	        }
	        else
	        {
	
	            int prodId = Integer.parseInt( request.getParameter( "prodid" ) );
	            Store store = ( Store )session.getAttribute( "store" );
	            int supplier = SupplierProduct.getSupplierFromProduct( prodId );
	            int commId = Community.getComunityOfShopKeeper( store.getId(), supplier );
	            
	            PriceScale scale = new PriceScale( prodId, commId );
	            
	            // voy a buscar el nombre del producto
	            String prodName = SupplierProduct.getProductName(prodId);
	            
	            // voy a buscar el nombre del proveedor
	            String suppName = Supplier.getName( supplier );
	            
	            request.setAttribute( "scale", scale );
	            request.setAttribute( "prodName", prodName );
	            request.setAttribute( "suppName", suppName );
	            url = "/minorista/escalaproducto.jsp";
	        }
	    }
	    catch( Exception e )
	    {
	        url="/logout";
	        message = MessageBuilder.buildMessage(15);
	    }
        
        RequestDispatcher rd = getServletContext().getRequestDispatcher( url );
        rd.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";
    }
    
}
