<commit_before>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 java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Date;

import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.FieldDescriptor;
import org.intermine.metadata.Model;
import org.intermine.metadata.ReferenceDescriptor;
import org.intermine.objectstore.query.ConstraintOp;
import org.intermine.objectstore.query.ConstraintSet;
import org.intermine.objectstore.query.ContainsConstraint;
import org.intermine.objectstore.query.Query;
import org.intermine.objectstore.query.QueryClass;
import org.intermine.objectstore.query.QueryCollectionReference;
import org.intermine.objectstore.query.QueryField;
import org.intermine.objectstore.query.QueryNode;
import org.intermine.objectstore.query.QueryObjectReference;
import org.intermine.objectstore.query.QueryReference;
import org.intermine.objectstore.query.QueryValue;
import org.intermine.objectstore.query.SimpleConstraint;
import org.intermine.objectstore.query.BagConstraint;
import org.intermine.util.TypeUtil;

/**
 * Helper methods for main controller and main action
 * @author <NAME>
 */
public class MainHelper
{
    /**
     * Given a path, render a set of metadata Nodes to the relevant depth
     * @param path of form Gene.organism.name
     * @param model the model used to resolve class names
     * @return an ordered Set of nodes
     */
    public static Collection makeNodes(String path, Model model) {
        String className, subPath;
        if (path.indexOf(".") == -1) {
            className = path;
            subPath = "";
        } else {
            className = path.substring(0, path.indexOf("."));
            subPath = path.substring(path.indexOf(".") + 1);
        }
        Map nodes = new TreeMap();
        nodes.put(className, new MetadataNode(className));
        makeNodes(getClassDescriptor(className, model), subPath, className, nodes);
        return nodes.values();
    }
    
    /**
     * Recursive method used to add nodes to a set representing a path from a given ClassDescriptor
     * @param cld the root ClassDescriptor
     * @param path current path prefix (eg Gene)
     * @param currentPath current path suffix (eg organism.name)
     * @param nodes the current Node set
     */
    protected static void makeNodes(ClassDescriptor cld, String path, String currentPath,
                                    Map nodes) {
        for (Iterator i = cld.getAllFieldDescriptors().iterator(); i.hasNext();) {
            FieldDescriptor fd = (FieldDescriptor) i.next();
            String fieldName = fd.getName();

            if (fieldName.equals("id")) {
                return;
            }

            String head, tail;
            if (path.indexOf(".") != -1) {
                head = path.substring(0, path.indexOf("."));
                tail = path.substring(path.indexOf(".") + 1);
            } else {
                head = path;
                tail = "";
            }

            String button;
            if (fieldName.equals(head)) {
                button = "-";
            } else if (fd.isReference() || fd.isCollection()) {
                button = "+";
            } else {
                button = " ";
            }

            MetadataNode parent = (MetadataNode) nodes.get(currentPath);
            MetadataNode node = new MetadataNode(parent, fieldName, cld.getModel(), button);
            nodes.put(node.getPath(), node);
            if (fieldName.equals(head)) {
                ClassDescriptor refCld = ((ReferenceDescriptor) fd).getReferencedClassDescriptor();
                makeNodes(refCld, tail, currentPath + "." + head, nodes);
            }
        }
    }

    /**
     * Make an InterMine query from a path query
     * @param query the PathQuery
     * @param savedBags the current saved bags map
     * @return an InterMine Query
     */
    public static Query makeQuery(PathQuery query, Map savedBags) {
        PathQuery query2 = (PathQuery) query.clone();
        Map qNodes = query.getNodes();
        List view = query.getView();
        Model model = query.getModel();

        //first merge the query and the view
        for (Iterator i = view.iterator(); i.hasNext();) {
            String path = (String) i.next();
            if (path.indexOf(".") != -1 && !qNodes.containsKey(path)) {
                query2.addNode(path);
            }
        }

        //create the real query
        Query q = new Query();
        ConstraintSet cs = new ConstraintSet(ConstraintOp.AND);
        q.setConstraint(cs);

        Map queryBits = new HashMap();

        //build the FROM and WHERE clauses
        for (Iterator i = query2.getNodes().values().iterator(); i.hasNext();) {
            PathNode node = (PathNode) i.next();
            String path = node.getPath();
            
            if (path.indexOf(".") == -1) {
                QueryClass qc = new QueryClass(getClass(node.getType(), model));
                q.addFrom(qc);<commit_msg>Don't exclude root nodes from being added to query<commit_after>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 java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Date;

import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.FieldDescriptor;
import org.intermine.metadata.Model;
import org.intermine.metadata.ReferenceDescriptor;
import org.intermine.objectstore.query.ConstraintOp;
import org.intermine.objectstore.query.ConstraintSet;
import org.intermine.objectstore.query.ContainsConstraint;
import org.intermine.objectstore.query.Query;
import org.intermine.objectstore.query.QueryClass;
import org.intermine.objectstore.query.QueryCollectionReference;
import org.intermine.objectstore.query.QueryField;
import org.intermine.objectstore.query.QueryNode;
import org.intermine.objectstore.query.QueryObjectReference;
import org.intermine.objectstore.query.QueryReference;
import org.intermine.objectstore.query.QueryValue;
import org.intermine.objectstore.query.SimpleConstraint;
import org.intermine.objectstore.query.BagConstraint;
import org.intermine.util.TypeUtil;

/**
 * Helper methods for main controller and main action
 * @author <NAME>
 */
public class MainHelper
{
    /**
     * Given a path, render a set of metadata Nodes to the relevant depth
     * @param path of form Gene.organism.name
     * @param model the model used to resolve class names
     * @return an ordered Set of nodes
     */
    public static Collection makeNodes(String path, Model model) {
        String className, subPath;
        if (path.indexOf(".") == -1) {
            className = path;
            subPath = "";
        } else {
            className = path.substring(0, path.indexOf("."));
            subPath = path.substring(path.indexOf(".") + 1);
        }
        Map nodes = new TreeMap();
        nodes.put(className, new MetadataNode(className));
        makeNodes(getClassDescriptor(className, model), subPath, className, nodes);
        return nodes.values();
    }
    
    /**
     * Recursive method used to add nodes to a set representing a path from a given ClassDescriptor
     * @param cld the root ClassDescriptor
     * @param path current path prefix (eg Gene)
     * @param currentPath current path suffix (eg organism.name)
     * @param nodes the current Node set
     */
    protected static void makeNodes(ClassDescriptor cld, String path, String currentPath,
                                    Map nodes) {
        for (Iterator i = cld.getAllFieldDescriptors().iterator(); i.hasNext();) {
            FieldDescriptor fd = (FieldDescriptor) i.next();
            String fieldName = fd.getName();

            if (fieldName.equals("id")) {
                return;
            }

            String head, tail;
            if (path.indexOf(".") != -1) {
                head = path.substring(0, path.indexOf("."));
                tail = path.substring(path.indexOf(".") + 1);
            } else {
                head = path;
                tail = "";
            }

            String button;
            if (fieldName.equals(head)) {
                button = "-";
            } else if (fd.isReference() || fd.isCollection()) {
                button = "+";
            } else {
                button = " ";
            }

            MetadataNode parent = (MetadataNode) nodes.get(currentPath);
            MetadataNode node = new MetadataNode(parent, fieldName, cld.getModel(), button);
            nodes.put(node.getPath(), node);
            if (fieldName.equals(head)) {
                ClassDescriptor refCld = ((ReferenceDescriptor) fd).getReferencedClassDescriptor();
                makeNodes(refCld, tail, currentPath + "." + head, nodes);
            }
        }
    }

    /**
     * Make an InterMine query from a path query
     * @param query the PathQuery
     * @param savedBags the current saved bags map
     * @return an InterMine Query
     */
    public static Query makeQuery(PathQuery query, Map savedBags) {
        PathQuery query2 = (PathQuery) query.clone();
        Map qNodes = query.getNodes();
        List view = query.getView();
        Model model = query.getModel();

        //first merge the query and the view
        for (Iterator i = view.iterator(); i.hasNext();) {
            String path = (String) i.next();
            if (!qNodes.containsKey(path)) {
                query2.addNode(path);
            }
        }

        //create the real query
        Query q = new Query();
        ConstraintSet cs = new ConstraintSet(ConstraintOp.AND);
        q.setConstraint(cs);

        Map queryBits = new HashMap();

        //build the FROM and WHERE clauses
        for (Iterator i = query2.getNodes().values().iterator(); i.hasNext();) {
            PathNode node = (PathNode) i.next();
            String path = node.getPath();
            
            if (path.indexOf(".") == -1) {
                QueryClass qc = new QueryClass(getClass(node.getType(), model));
                q.addFrom(qc);
{'fraction_non_alphanumeric': 0.1100646352723915, 'fraction_numerical': 0.0034164358264081257, 'mean_word_length': 2.3449660284126, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '130940', 'n_tokens_mistral': 2929, 'n_tokens_neox': 2857, 'n_words': 809}