/*
 * PaymentMethodListCommunity.java
 *
 * Created on 26 de enero de 2005, 07:49 AM
 */

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 com.fenalco.entities.User;
import com.fenalco.entities.Community;
import com.fenalco.entities.PaymentMethodList;

import com.fenalco.utilities.MessageBuilder;

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

                            /*
                            // criterios de paginación
                            int pagina = 1;
                            int resultadosPagina = 20;
                            int paginas; // numero total de páginas
                            int inicio = 0; // numero del primero registro que se va a mostrar
                            int fin = 9; // numero del último registro que se va a mostrar
                            int total; // numero total de resultados

                            String temp = request.getParameter("page");
                            if( temp == null ){

                                pagina = 1;

                            }else{

                                pagina = Integer.parseInt( temp );

                            }

                            pagina = (pagina < 1)? 1 : pagina;
                            temp = request.getParameter( "numResults" );

                            if( temp == null ){

                                resultadosPagina = 20;

                            }else{

                                resultadosPagina = Integer.parseInt( temp );

                            }

                            resultadosPagina = (resultadosPagina < 1)? 20 : resultadosPagina;
                            */


                            User supplier = (User) session.getAttribute("user");
                            int key = Integer.parseInt( request.getParameter( "key" ) );
                            
                            Community community = new Community( key );
                            PaymentMethodList communityIds = new PaymentMethodList( supplier.getCompanyId(), key, 2 );
                            PaymentMethodList paymentMethods = new PaymentMethodList( supplier.getCompanyId() );
                            
                            
                            /*
                            Vector results = new Vector();
                            total = billingList.getBills().size();  

                            paginas = (int) Math.ceil((double) total / (double) resultadosPagina);

                            if( pagina > paginas ){
                                    pagina = paginas;
                            }

                            inicio = (pagina - 1) * resultadosPagina;
                            fin = pagina * resultadosPagina;
                            inicio = (inicio < 0) ? 0 : inicio;

                            if( fin > total ){
                                    fin = total;
                            }

                            for( int i = inicio; i < fin; i++ ){
                                    results.addElement( billingList.getBills().elementAt(i) );
                            }

                            billingList.setBills( results ); 
                            */

                            //BillingListViewSupplier view = new BillingListViewSupplier(billingList);

                            if( communityIds == null || paymentMethods == null || community ==null )
                            {
                                    
                                    url = "/proveedor/formaspagocomunidad.jsp";
                                    message = MessageBuilder.buildMessage(9);

                            }
                            else
                            {
                                    //Todo OK
                                    url = "/proveedor/formaspagocomunidad.jsp";
                                    message = "";
                                    
                                    request.setAttribute( "community", community );
                                    request.setAttribute( "communityIds" , communityIds );
                                    request.setAttribute( "paymentMethods", paymentMethods );
                            }

			    /*	
                            /// información de la paginacion
                             inicio++; // para no arrancar desde 0
                             inicio = (fin == 0) ? 0 : inicio;
                             request.setAttribute("inicio", inicio + "");
                             request.setAttribute("fin", fin + "");
                             request.setAttribute("total", total + "");
                             request.setAttribute("pagina", pagina + "");
                             request.setAttribute("paginas", paginas + "");
                             request.setAttribute("cantidad", resultadosPagina + "");
			     */	
                    }
        }
        catch( Exception e )
        {
            e.printStackTrace();
            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";
    }
    
}
