/*
 * StoreHandler.java
 *
 * Created on 5 de julio de 2005, 08:56 PM
 */

package com.fenalco.servlets.sysmanager;

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

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

import com.fenalco.entities.DataAccess;
import com.fenalco.entities.Store;
import com.fenalco.entities.City;
import com.fenalco.entities.Province;
import com.fenalco.entities.Zone;
import com.fenalco.entities.Neighbourhood;

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


import java.util.Vector;
import java.util.Enumeration;

/**
 *
 * @author  Juan Ernesto Peņa Kaltekis
 * @version
 */
public class StoreView 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 = "";
        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 id = request.getParameter("key"); 
                Store store = new Store(Integer.parseInt(id));
                
                
                Vector countries = DataAccess.getCountries();
                Vector provinces = new Vector();
                Vector cities = new Vector();
                Vector zones = new Vector();
                Vector neighbourhoods = new Vector();

                int countryId = 1;
                int provinceId = 1;
                int cityId = 1;
                int zoneId = 1;
                String combo = request.getParameter("combo");

                if( combo != null ){
                    
                        //Carga los datos del formulario en el objeto
                        store.setName(request.getParameter("name"));
                        store.setAddress(request.getParameter("address"));
                        try{
                            store.setStreet(Integer.parseInt(request.getParameter("street")));
                        }catch(NumberFormatException nfe){
                        }
                        try{
                            store.setAvenue(Integer.parseInt(request.getParameter("avenue")));
                        }catch(NumberFormatException nfe){
                        }
                        store.setStreetOrientation(request.getParameter("streetOrientation"));
                        store.setAvenueOrientation(request.getParameter("avenueOrientation"));
                        store.setContact1(request.getParameter("contact1"));
                        store.setPhoneNumber1(request.getParameter("phoneNumber1"));
                        store.setGln(request.getParameter("gln"));

                    
                        //Parte de los combos
                        countryId = (new Integer(request.getParameter("country"))).intValue();

                        if( request.getParameter("province") != null ){
                                provinceId = (new Integer(request.getParameter("province"))).intValue();
                        }
                        if( request.getParameter("city") != null ){
                                cityId = (new Integer(request.getParameter("city"))).intValue();
                        }
                        if( request.getParameter("zone") != null ){
                                zoneId = (new Integer(request.getParameter("zone"))).intValue();
                        }

                        if( combo.equals("country") ){
                                provinces = DataAccess.getProvinces(countryId);
                                Enumeration en = provinces.elements();
                                if( en.hasMoreElements() ){
                                        provinceId = ((Province) en.nextElement()).getId();
                                        cities = DataAccess.getCities(provinceId);
                                        en = cities.elements();
                                        if( en.hasMoreElements() ){
                                                cityId = ((City) en.nextElement()).getId();
                                                zones = DataAccess.getZones(cityId);
                                                en = zones.elements();
                                                if( en.hasMoreElements() ){
                                                        zoneId = ((Zone) en.nextElement()).getId();
                                                        neighbourhoods = DataAccess.getNeighbourhoodsZone(zoneId);
                                                }
                                        }
                                }

                        }else if( combo.equals("province") ){
                                        provinces = DataAccess.getProvinces(countryId);
                                        cities = DataAccess.getCities(provinceId);
                                        Enumeration en = cities.elements();
                                        if( en.hasMoreElements() ){
                                                cityId = ((City) en.nextElement()).getId();
                                                zones = DataAccess.getZones(cityId);
                                                en = zones.elements();
                                                if( en.hasMoreElements() ){
                                                        zoneId = ((Zone) en.nextElement()).getId();
                                                        neighbourhoods = DataAccess.getNeighbourhoodsZone(zoneId);
                                                }
                                        }
                        }else if( combo.equals("city") ){
                                                provinces = DataAccess.getProvinces(countryId);
                                                cities = DataAccess.getCities(provinceId);
                                                zones = DataAccess.getZones(cityId);
                                                Enumeration en = zones.elements();
                                                if( en.hasMoreElements() ){
                                                        zoneId = ((Zone) en.nextElement()).getId();
                                                        neighbourhoods = DataAccess.getNeighbourhoodsZone(zoneId);
                                                }
                        }else{
                                                provinces = DataAccess.getProvinces(countryId);
                                                cities = DataAccess.getCities(provinceId);
                                                zones = DataAccess.getZones(cityId);
                                                neighbourhoods = DataAccess.getNeighbourhoodsZone(zoneId);
                        }
                }else{
                        Neighbourhood neighbourhood = new Neighbourhood(store.getNeighbourhood());
                        zoneId = neighbourhood.getZone();
                        neighbourhoods = DataAccess.getNeighbourhoodsZone(zoneId);
                        Zone zone = new Zone(zoneId);
                        cityId = zone.getCity();
                        zones = DataAccess.getZones(cityId);
                        City city = new City(cityId);
                        provinceId = city.getProvince();
                        cities = DataAccess.getCities(provinceId);
                        Province province = new Province(provinceId);
                        countryId = province.getCountry();
                        provinces = DataAccess.getProvinces(provinceId);
                        
                        
                        /*provinces = DataAccess.getProvinces(countryId);
                        Enumeration en = provinces.elements();
                        if( en.hasMoreElements() ){
                                provinceId = ((Province) en.nextElement()).getId();
                                cities = DataAccess.getCities(provinceId);
                                en = cities.elements();
                                if( en.hasMoreElements() ){
                                        cityId = ((City) en.nextElement()).getId();
                                        zones = DataAccess.getZones(cityId);
                                        en = zones.elements();
                                        if( en.hasMoreElements() ){
                                                zoneId = ((Zone) en.nextElement()).getId();
                                                neighbourhoods = DataAccess.getNeighbourhoodsZone(zoneId);
                                        }
                                }
                        }*/
                }

                request.setAttribute("countries", countries);
                request.setAttribute("countryId", new Integer(countryId));
                request.setAttribute("provinces", provinces);
                request.setAttribute("provinceId", new Integer(provinceId));
                request.setAttribute("cities", cities);
                request.setAttribute("cityId", new Integer(cityId));
                request.setAttribute("zones", zones);
                request.setAttribute("zoneId", new Integer(zoneId));
                request.setAttribute("neighbourhoods", neighbourhoods);
                
                
                
                request.setAttribute("store", store);
                
                url = "/administrador/actualizarestablecimiento.jsp";
            }
        }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";
    }
    
}
