/*
 * Q70MenuRepository.java
 *
 * Created on January 29, 2001, 9:51 AM
 */
package es.ejie.frmk.presentacion.iu.taglib.html.menu.base;

import java.io.InputStream;
import java.util.Set;

import javax.servlet.http.HttpServlet;

import org.apache.commons.collections.FastHashMap;
import org.apache.commons.digester.Digester;

import es.ejie.frmk.control.arranque.Q70GestorArranqueConfiguracion;
import es.ejie.frmk.presentacion.iu.taglib.html.menu.displayer.Q70MenuDisplayerMapping;
import es.ejie.frmk.presentacion.iu.taglib.html.menu.util.Q70LoadableResource;
import es.ejie.frmk.presentacion.iu.taglib.html.menu.util.Q70LoadableResourceException;


/**
 *
 * @author  ssayles
 * @version
 */
public class Q70MenuRepository implements Q70LoadableResource {
    //~ Static fields/initializers =============================================

    public static final String MENU_REPOSITORY_KEY =
        "es.ejie.frmk.presentacion.iu.taglib.html.menu.base.MENU_REPOSITORY";


    //~ Instance fields ========================================================

    /**
     * The <code>Q70Log</code> instance for this class.
     */
    protected String config = null;
    protected String name = null;
    protected HttpServlet servlet = null;
    protected FastHashMap menus = new FastHashMap();
	protected FastHashMap displayers = new FastHashMap();
    protected FastHashMap templates = new FastHashMap();
    
    //~ Methods ================================================================
    public Set getMenuNames() {
        return menus.keySet();
    }

    public Q70MenuComponent getMenu(String menuName) {
        return (Q70MenuComponent) menus.get(menuName);
    }

    public Q70MenuDisplayerMapping getMenuDisplayerMapping(String displayerName) {
        Q70MenuDisplayerMapping mapping =
            (Q70MenuDisplayerMapping) displayers.get(displayerName);

        return mapping;
    }

    protected Digester initDigester() {
        Digester digester = new Digester();
        digester.setClassLoader(Thread.currentThread().getContextClassLoader());
        digester.push(this);

        //digester.setDebug(getDebug());
        // 1
        digester.addObjectCreate("MenuConfig/Menus/Menu",
            "es.ejie.frmk.presentacion.iu.taglib.html.menu.base.Q70MenuComponentEstatico", "type");
        digester.addSetProperties("MenuConfig/Menus/Menu");
        digester.addSetNext("MenuConfig/Menus/Menu", "addMenu");
        
        //2
        digester.addObjectCreate("*/Item",
            "es.ejie.frmk.presentacion.iu.taglib.html.menu.base.Q70MenuComponentEstatico", "type");
        digester.addSetProperties("*/Item");
        digester.addSetNext("*/Item", "addMenuComponent",
            "es.ejie.frmk.presentacion.iu.taglib.html.menu.base.Q70MenuComponentEstatico");
        
        
        digester.addObjectCreate("MenuConfig/Displayers/Displayer",
            "es.ejie.frmk.presentacion.iu.taglib.html.menu.displayer.Q70MenuDisplayerMapping", "mapping");
        digester.addSetProperties("MenuConfig/Displayers/Displayer");
        digester.addSetNext("MenuConfig/Displayers/Displayer",
            "addMenuDisplayerMapping",
            "es.ejie.frmk.presentacion.iu.taglib.html.menu.displayer.Q70MenuDisplayerMapping");
        digester.addSetProperty("MenuConfig/Displayers/Displayer/SetProperty",
            "property", "value");
            
        return digester;
    }

    /**
     * Adds a new menu.  This is called when parsing the menu xml definition.
     *
     * @param menu The menu component to add.
     */
    public void addMenu(Q70MenuComponent menu) {
        menus.put(menu.getName(), menu);
    }

    public void addMenuDisplayerMapping(Q70MenuDisplayerMapping displayerMapping) {
        displayers.put(displayerMapping.getName(), displayerMapping);
    }

	//Carga el fichero de configuracion de menus de Geremua situado bajo "config/q70"
	public void frmkLoad() throws Q70LoadableResourceException {
		if (servlet == null) {  	
			throw new Q70LoadableResourceException("no reference to servlet found");
		}

		InputStream input = null;
		Digester digester = initDigester();

		try {
			//input = servlet.getServletContext().getResourceAsStream(config);
			//input = this.getClass().getClassLoader().getResourceAsStream(config);
			//input = Q70GestorArranqueConfiguracion.getResourceAsStream(config);
			input = Q70GestorArranqueConfiguracion.getGlobalResourceAsStream(config);
			
			digester.parse(input);

			menus.setFast(true);
		} catch (Exception e) {
			throw new Q70LoadableResourceException("Error parsing resource file: " +
				config + " nested exception is: " + e.getMessage());
		} finally {
			try {
				input.close();
			} catch (Exception e) {}
		}
	}

	//	Carga el fichero de configuracion de menus de aplicacion situado bajo "WEB-INF"
    public void load() throws Q70LoadableResourceException {
        if (servlet == null) {  	
            throw new Q70LoadableResourceException("no reference to servlet found");
        }

        InputStream input = null;
        Digester digester = initDigester();

        try {
            //input = servlet.getServletContext().getResourceAsStream(config);
			//input = this.getClass().getClassLoader().getResourceAsStream(config);
			input = Q70GestorArranqueConfiguracion.getResourceAsStream(config);
			//input = Q70GestorArranqueConfiguracion.getGlobalResourceAsStream(config);
			
            digester.parse(input);

            menus.setFast(true);
        } catch (Exception e) {
            throw new Q70LoadableResourceException("Error parsing resource file: " +
                config + " nested exception is: " + e.getMessage());
        } finally {
            try {
                input.close();
            } catch (Exception e) {}
        }
    }

    public void reload() throws Q70LoadableResourceException {
        menus.setFast(false);
        menus.clear();
        displayers.setFast(false);
        displayers.clear();
        load();
    }

    public void setLoadParam(String loadParam) {
        config = loadParam;
    }

    public String getLoadParam() {
        return config;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public HttpServlet getServlet() {
        return servlet;
    }

    public void setServlet(HttpServlet servlet) {
        this.servlet = servlet;
    }
   
}
