/*
 * PaymentMethodListHandlerShopkeeper.java
 *
 * Created on 25 de enero de 2005, 12:20 PM
 */

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 java.util.Vector;
import java.util.Enumeration;

import com.fenalco.entities.User;
import com.fenalco.entities.ShopKeeper;
import com.fenalco.entities.PaymentMethod;
import com.fenalco.entities.PaymentMethodList;

import com.fenalco.utilities.MessageBuilder;
import com.fenalco.utilities.database.Transaction;


/**
 *
 * @author  Néstor Romero León
 * @version
 */
public class PaymentMethodListHandlerShopkeeper 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;


            //Validación de sesión
        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 )request.getSession().getAttribute( "user" );
                            Transaction t = new Transaction();
                            
                            PaymentMethod pm;
                            int shopKeeperId = Integer.parseInt( request.getParameter( "key" ) );
                            PaymentMethodList list = new PaymentMethodList( supplier.getCompanyId(), shopKeeperId, 1 );
                            Enumeration en = list.getPaymentMethods().elements();
                            
                            int key;
                            
                            //Eliminar Viejas Formas de Pago
                            while( en.hasMoreElements() ){
                            
                                pm = ( PaymentMethod )en.nextElement();
                                pm.removePaymentMethod( shopKeeperId, 1, t );
                                
                            }
                            
                            //Agregar Nuevas Formas de Pago
                            if( request.getParameterValues( "item" ) != null ){
                                
                                String[] items = request.getParameterValues( "item" );
                                for( int i=0; i<items.length; i++ ){

                                    key = Integer.parseInt( items[i] );
                                    pm = new PaymentMethod( key );
                                    pm.addPaymentMethod( shopKeeperId, 1, t );

                                }
                            }
                            
                            
                            if( !t.execute() )
                            {
                                    //Fallo al actualizar
                                    url = "/proveedor/clientesproveedor";
                                    message = MessageBuilder.buildMessage(7);

                            }
                            else
                            {
                                    //Todo OK
                                    url = "/proveedor/clientesproveedor";
                                    message = MessageBuilder.buildMessage( 4 );
                                    
                            }

			    
                    }
        }
        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";
    }
    
}
