package org.intermine.web;

/*
 * Copyright (C) 2002-2004 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import javax.servlet.ServletException;
import javax.servlet.ServletContext;

import java.util.Properties;
import java.io.InputStream;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

import org.intermine.metadata.Model;
import org.intermine.web.config.WebConfig;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreFactory;

/**
 * Initialiser for the InterMine web application
 * Anything that needs global initialisation goes here.
 *
 * @author Andrew Varley
 */

public class InitialiserPlugin implements PlugIn
{
    /**
     * Init method called at Servlet initialisation
     *
     * @param servlet ActionServlet that is managing all the modules
     * in this web application
     * @param config ModuleConfig for the module with which this
     * plug-in is associated
     *
     * @throws ServletException if this <code>PlugIn</code> cannot
     * be successfully initialized
     */
    public void init(ActionServlet servlet, ModuleConfig config)
        throws ServletException {

        ServletContext servletContext = servlet.getServletContext();

        try {
            WebConfig wc =
                WebConfig.parse(servletContext.getResourceAsStream("/WEB-INF/webconfig-model.xml"));
            ObjectStore os = ObjectStoreFactory.getObjectStore();
            Model model = os.getModel();

            servletContext.setAttribute(Constants.OBJECTSTORE, os);
            servletContext.setAttribute(Constants.MODEL, model);
            servletContext.setAttribute("webconfig", wc);

            Properties webProperties = new Properties();

            InputStream globalPropertiesStream =
                servletContext.getResourceAsStream("/WEB-INF/global.web.properties");

            webProperties.load(globalPropertiesStream);

            InputStream modelPropertiesStream =
                servletContext.getResourceAsStream("/WEB-INF/web.properties");

            webProperties.load(modelPropertiesStream);

            servletContext.setAttribute(Constants.WEB_PROPERTIES, webProperties);
        } catch (Exception e) {
            throw new ServletException("there was a problem while initialising", e);
        }
    }

    /**
     * Destroy method called at Servlet destroy
     */
    public void destroy() {
    }
}

@@ -425,16 +425,123 @@
 erties;%0A
+import java.util.Map;%0Aimport java.util.Iterator;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.TreeSet;%0A
 import j
@@ -560,16 +560,16 @@
 Stream;%0A
-
 %0Aimport 
@@ -695,16 +695,52 @@
 onfig;%0A%0A
+import org.intermine.util.TypeUtil;%0A
 import o
@@ -857,16 +857,69 @@
 tStore;%0A
+import org.intermine.objectstore.ObjectStoreSummary;%0A
 import o
@@ -1951,24 +1951,90 @@
 etModel();%0A%0A
+            ObjectStoreSummary oss = new ObjectStoreSummary(os);%0A%0A
             
@@ -2640,32 +2640,32 @@
 ertiesStream);%0A%0A
-
             serv
@@ -2729,16 +2729,571 @@
 erties);
+%0A%0A            Map classes = new LinkedHashMap();%0A            Map classCounts = new LinkedHashMap();%0A            for (Iterator i = new TreeSet(model.getClassNames()).iterator(); i.hasNext();) %7B%0A                String className = (String) i.next();%0A                classes.put(className, TypeUtil.unqualifiedName(className));%0A                classCounts.put(className, new Integer(oss.getClassCount(className)));%0A            %7D%0A%0A            servletContext.setAttribute(%22classes%22, classes);%0A            servletContext.setAttribute(%22classCounts%22, classCounts);
 %0A       

Set classCounts Map.