/*
 * NewCompanyListener.java
 *
 * Created on 18 de julio de 2005, 07:50 PM
 */

package com.fenalco.servlets.sysmanager;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.fenalco.entities.Company;
import com.fenalco.entities.SupplierList;
import com.fenalco.entities.User;
import com.fenalco.utilities.MessageBuilder;

/**
 *
 * @author  Juan Ernesto Peña Kaltekis
 * @version
 */
public class NewCompanyListener 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 {

            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") ){

                        url = "/administrador/administrarnuevasempresas";
                        
                        int id = Integer.parseInt(request.getParameter("key"));
                        Company company = new Company(id);

                        //Si la empresas a activar es minorista se dirige a la pantalla de asignación como cliente exclusivo
                        if(company.getType()==1){
                            SupplierList suppliers = new SupplierList();
                            request.setAttribute("key", new Integer(id));
                            request.setAttribute("suppliers", suppliers.getSuppliers());
                            url = "/administrador/asignarminoristaexclusivo.jsp";
                        }
                        
                    }else if(operationType.equals("delete")){
                        url = "/administrador/administrarnuevasempresas";
                    }else{
                        String companyType = request.getParameter("companyType");
                        url = "/administrador/administrador";
                    }
                }
	    }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);        

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