<commit_before>package org.intermine.web.struts;

/*
 * Copyright (C) 2002-2007 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 java.io.InputStream;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

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

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.intermine.cache.InterMineCache;
import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.Model;
import org.intermine.model.InterMineObject;
import org.intermine.model.userprofile.Tag;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.objectstore.ObjectStoreFactory;
import org.intermine.objectstore.ObjectStoreSummary;
import org.intermine.objectstore.ObjectStoreWriter;
import org.intermine.objectstore.ObjectStoreWriterFactory;
import org.intermine.util.TypeUtil;
import org.intermine.web.logic.ClassKeyHelper;
import org.intermine.web.logic.Constants;
import org.intermine.web.logic.aspects.AspectBinding;
import org.intermine.web.logic.bag.BagQueryConfig;
import org.intermine.web.logic.bag.BagQueryHelper;
import org.intermine.web.logic.config.WebConfig;
import org.intermine.web.logic.profile.Profile;
import org.intermine.web.logic.profile.ProfileManager;
import org.intermine.web.logic.query.MainHelper;
import org.intermine.web.logic.results.DisplayObject;
import org.intermine.web.logic.search.SearchRepository;
import org.intermine.web.logic.search.WebSearchable;
import org.intermine.web.logic.session.SessionMethods;
import org.intermine.web.logic.tagging.TagNames;
import org.intermine.web.logic.tagging.TagTypes;
import org.intermine.web.logic.template.TemplateHelper;

/**
 * Initialiser for the InterMine web application.
 * Anything that needs global initialisation goes here.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class InitialiserPlugin implements PlugIn
{
    private static final Logger LOG = Logger.getLogger(InitialiserPlugin.class);

    ProfileManager profileManager;
    
    private static final List<String> PUBLIC_TAG_LIST =
        Arrays.asList(new String[] {TagNames.IM_PUBLIC});
    
    /**
     * 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 {
        final ServletContext servletContext = servlet.getServletContext();
 
        System.setProperty("java.awt.headless", "true");
        
        loadWebProperties(servletContext);
        
        ObjectStore os = null;
        Properties props = (Properties) servletContext.getAttribute(Constants.WEB_PROPERTIES);
        String osAlias = (String) props.get("webapp.os.alias");
        try {
            os = ObjectStoreFactory.getObjectStore(osAlias);
        } catch (Exception e) {
            e.getCause().printStackTrace();
            throw new ServletException("Unable to instantiate ObjectStore " + osAlias, e);
        }
        servletContext.setAttribute(Constants.OBJECTSTORE, os); 
        
        loadWebConfig(servletContext, os);
        loadAspectsConfig(servletContext, os);

        //loadClassCategories(servletContext, os);
        loadClassDescriptions(servletContext, os);
        
        summarizeObjectStore(servletContext, os);
        // load class keys
        loadClassKeys(servletContext, os);
        final ProfileManager pm = createProfileManager(servletContext, os);
        // Loading shared template queries requires profile manager
        loadSuperUserDetails(servletContext);
        
        // index global webSearchables
        SearchRepository searchRepository = new SearchRepository();
        servletContext.setAttribute(Constants.GLOBAL_SEARCH_REPOSITORY, searchRepository);
        final Profile superProfile = SessionMethods.getSuperUserProfile(servletContext);
        
        AbstractMap<String, WebSearchable> templateSearchableMap =
            new AbstractMap<String, WebSearchable>() {
                @Override
                public Set<Map.Entry<String, WebSearchable>> entrySet() {
                    return pm.filterByTags(superProfile.getSavedTemplates(), PUBLIC_TAG_LIST,
                                           TagTypes.TEMPLATE,
                                           superProfile.getUsername()).entrySet();
                }
            };
        searchRepository.addWebSearchables(TagTypes.TEMPLATE, templateSearchableMap);
        
        AbstractMap<String, WebSearchable> bagSearchableMap =
            new AbstractMap<String, WebSearchable>() {
                @Override
                public Set<Map.Entry<String, WebSearchable>> entrySet() {
                    return pm.filterByTags(superProfile.getSavedBags(), PUBLIC_TAG_LIST,
                                           TagTypes.BAG,
                                           superProfile.getUsername()).entrySet();
                }
            };
        searchRepository.addWebSearchables(TagTypes.BAG, bagSearchableMap);
 
        servletContext.setAttribute(Constants.GRAPH_CACHE, new HashMap());

        // load custom bag queries
        loadBagQueries(servletContext, os);
        makeCache(servletContext, os);
        
        cleanTags(pm);
    }

    /**
     * Load the Aspects configuration from aspects.xml
     * @param servletContext the servlet cnotext
     * @param os the main objectstore<commit_msg>Fixed exception in PathQuery.checkValidity() when called from InitialiserPlugin.<commit_after>package org.intermine.web.struts;

/*
 * Copyright (C) 2002-2007 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 java.io.InputStream;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

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

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.intermine.cache.InterMineCache;
import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.Model;
import org.intermine.model.InterMineObject;
import org.intermine.model.userprofile.Tag;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.objectstore.ObjectStoreFactory;
import org.intermine.objectstore.ObjectStoreSummary;
import org.intermine.objectstore.ObjectStoreWriter;
import org.intermine.objectstore.ObjectStoreWriterFactory;
import org.intermine.util.TypeUtil;
import org.intermine.web.logic.ClassKeyHelper;
import org.intermine.web.logic.Constants;
import org.intermine.web.logic.aspects.AspectBinding;
import org.intermine.web.logic.bag.BagQueryConfig;
import org.intermine.web.logic.bag.BagQueryHelper;
import org.intermine.web.logic.config.WebConfig;
import org.intermine.web.logic.profile.Profile;
import org.intermine.web.logic.profile.ProfileManager;
import org.intermine.web.logic.query.MainHelper;
import org.intermine.web.logic.results.DisplayObject;
import org.intermine.web.logic.search.SearchRepository;
import org.intermine.web.logic.search.WebSearchable;
import org.intermine.web.logic.session.SessionMethods;
import org.intermine.web.logic.tagging.TagNames;
import org.intermine.web.logic.tagging.TagTypes;
import org.intermine.web.logic.template.TemplateHelper;

/**
 * Initialiser for the InterMine web application.
 * Anything that needs global initialisation goes here.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class InitialiserPlugin implements PlugIn
{
    private static final Logger LOG = Logger.getLogger(InitialiserPlugin.class);

    ProfileManager profileManager;
    
    private static final List<String> PUBLIC_TAG_LIST =
        Arrays.asList(new String[] {TagNames.IM_PUBLIC});
    
    /**
     * 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 {
        final ServletContext servletContext = servlet.getServletContext();
 
        System.setProperty("java.awt.headless", "true");
        
        loadWebProperties(servletContext);
        
        ObjectStore os = null;
        Properties props = (Properties) servletContext.getAttribute(Constants.WEB_PROPERTIES);
        String osAlias = (String) props.get("webapp.os.alias");
        try {
            os = ObjectStoreFactory.getObjectStore(osAlias);
        } catch (Exception e) {
            e.getCause().printStackTrace();
            throw new ServletException("Unable to instantiate ObjectStore " + osAlias, e);
        }
        servletContext.setAttribute(Constants.OBJECTSTORE, os); 
        
        loadWebConfig(servletContext, os);
        loadAspectsConfig(servletContext, os);

        //loadClassCategories(servletContext, os);
        loadClassDescriptions(servletContext, os);
        
        summarizeObjectStore(servletContext, os);

        // load class keys
        loadClassKeys(servletContext, os);

        // load custom bag queries
        loadBagQueries(servletContext, os);
        
        final ProfileManager pm = createProfileManager(servletContext, os);
        // Loading shared template queries requires profile manager
        loadSuperUserDetails(servletContext);

        // index global webSearchables
        SearchRepository searchRepository = new SearchRepository();
        servletContext.setAttribute(Constants.GLOBAL_SEARCH_REPOSITORY, searchRepository);
        final Profile superProfile = SessionMethods.getSuperUserProfile(servletContext);
        
        AbstractMap<String, WebSearchable> templateSearchableMap =
            new AbstractMap<String, WebSearchable>() {
                @Override
                public Set<Map.Entry<String, WebSearchable>> entrySet() {
                    return pm.filterByTags(superProfile.getSavedTemplates(), PUBLIC_TAG_LIST,
                                           TagTypes.TEMPLATE,
                                           superProfile.getUsername()).entrySet();
                }
            };
        searchRepository.addWebSearchables(TagTypes.TEMPLATE, templateSearchableMap);
        
        AbstractMap<String, WebSearchable> bagSearchableMap =
            new AbstractMap<String, WebSearchable>() {
                @Override
                public Set<Map.Entry<String, WebSearchable>> entrySet() {
                    return pm.filterByTags(superProfile.getSavedBags(), PUBLIC_TAG_LIST,
                                           TagTypes.BAG,
                                           superProfile.getUsername()).entrySet();
                }
            };
        searchRepository.addWebSearchables(TagTypes.BAG, bagSearchableMap);
 
        servletContext.setAttribute(Constants.GRAPH_CACHE, new HashMap());

        makeCache(servletContext, os);
        
        cleanTags(pm);
    }

    /**
     * Load the Aspects configuration from aspects.xml
     * @param servletContext the servlet cnotext
     * @param os the main objectstore
{'fraction_non_alphanumeric': 0.08777673838478328, 'fraction_numerical': 0.001403180542563143, 'mean_word_length': 3.155814706835115, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3110697', 'n_tokens_mistral': 3443, 'n_tokens_neox': 3341, 'n_words': 736}