<commit_before>package org.intermine.web.logic.pathqueryresult;

/*
 * Copyright (C) 2002-2008 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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.intermine.metadata.AttributeDescriptor;
import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.Model;
import org.intermine.model.InterMineObject;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.objectstore.query.ConstraintOp;
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.QuerySelectable;
import org.intermine.objectstore.query.Results;
import org.intermine.path.Path;
import org.intermine.util.CollectionUtil;
import org.intermine.util.DynamicUtil;
import org.intermine.util.TypeUtil;
import org.intermine.web.logic.WebUtil;
import org.intermine.web.logic.bag.BagQueryConfig;
import org.intermine.web.logic.bag.InterMineBag;
import org.intermine.web.logic.config.FieldConfig;
import org.intermine.web.logic.config.FieldConfigHelper;
import org.intermine.web.logic.config.WebConfig;
import org.intermine.web.logic.profile.Profile;
import org.intermine.web.logic.query.Constraint;
import org.intermine.web.logic.query.MainHelper;
import org.intermine.web.logic.query.Node;
import org.intermine.web.logic.query.PathQuery;
import org.intermine.web.logic.results.TableHelper;
import org.intermine.web.logic.results.WebResults;

import javax.servlet.ServletContext;

/**
 * Helper for everything related to PathQueryResults
 *
 * @author "<NAME>"
 */
public class PathQueryResultHelper
{
    /**
     * For a given type, return the default view as configured in webconfig-model.xml
     * as a List of Path objects
     *
     * @param type the type for which to get the view
     * @param model the Model
     * @param webConfig the WebConfig
     * @param prefix non-mandatory. Useful when getting a view for a reference or collection
     * @param excludeNonAttributes a boolean
     * @return a List of Paths representing the view
     */
    public static List<Path> getDefaultView(String type, Model model, WebConfig webConfig,
                          String prefix, boolean excludeNonAttributes) {
        List<Path> view = new ArrayList<Path>();
        Set<ClassDescriptor> classDescriptors = model.getClassDescriptorsForClass(
            TypeUtil.instantiate(model.getPackageName() + "." + type));
        for (ClassDescriptor classDescriptor : classDescriptors) {
            List<FieldConfig> fieldConfigs = FieldConfigHelper.getClassFieldConfigs(webConfig,
                classDescriptor);
            for (FieldConfig fieldConfig : fieldConfigs) {
                if (fieldConfig.getShowInResults()) {
                    String expr = fieldConfig.getFieldExpr();
                    if (prefix == null || prefix.length() <= 0) {
                        prefix = type;
                    }
                    String prePath = "." + expr;
                    StringBuffer pathStringBuffer = new StringBuffer();
                    int lastIndex = prePath.lastIndexOf(".");
                    int index = prePath.indexOf(".");
                    int prevIndex = 0;
                    while (index < lastIndex) {
                        pathStringBuffer.append(prePath.substring(prevIndex, index))
                            .append(":");
                        prevIndex = index + 1;
                        index = prePath.indexOf(".", prevIndex);
                    }
                    pathStringBuffer.append(prePath.substring(prevIndex));
                    String pathString = prefix + "[" + type + "]" + pathStringBuffer.toString();
                    Path path = new Path(model, pathString);
                    // Path path = MainHelper.makePath(model, pathQuery, pathString);
                    // TODO remove isOnlyAttribute when outer joins
                    if (!view.contains(path)
                                    && ((excludeNonAttributes && path.isOnlyAttribute())
                                    || (!excludeNonAttributes))) {
                        view.add(path);
                    }
                }
            }
            if (view.size() == 0) {
                Set<AttributeDescriptor> attrDesc = classDescriptor.getAttributeDescriptors();
                for (AttributeDescriptor attributeDescriptor : attrDesc) {
                    Map<String, String> classToPath = new HashMap<String, String>();
                    classToPath.put(prefix, classDescriptor.getUnqualifiedName());
                    Path path = new Path(model, prefix + "." + attributeDescriptor.getName(),
                                    classToPath);
                    if (!view.contains(path)) {
                        view.add(path);
                    }
                }
            }
        }
        return view;
    }

    /**
     * Create a PathQuery to get the contents of an InterMineBag
     *
     * @param imBag the bag
     * @param webConfig the WebConfig
     * @param model the Model
     * @return a PathQuery
     */
    public static PathQuery makePathQueryForBag(InterMineBag imBag, WebConfig webConfig,
                                                Model model) {
        PathQuery pathQuery = new PathQuery(model);

        List<Path> view = PathQueryResultHelper.getDefaultView(imBag.getType(), model, webConfig,
            null, true);
<commit_msg>'Show in table' links for collections no longer use config for superclasses.<commit_after>package org.intermine.web.logic.pathqueryresult;

/*
 * Copyright (C) 2002-2008 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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.intermine.metadata.AttributeDescriptor;
import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.Model;
import org.intermine.model.InterMineObject;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.objectstore.query.ConstraintOp;
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.QuerySelectable;
import org.intermine.objectstore.query.Results;
import org.intermine.path.Path;
import org.intermine.util.CollectionUtil;
import org.intermine.util.DynamicUtil;
import org.intermine.util.TypeUtil;
import org.intermine.web.logic.WebUtil;
import org.intermine.web.logic.bag.BagQueryConfig;
import org.intermine.web.logic.bag.InterMineBag;
import org.intermine.web.logic.config.FieldConfig;
import org.intermine.web.logic.config.FieldConfigHelper;
import org.intermine.web.logic.config.WebConfig;
import org.intermine.web.logic.profile.Profile;
import org.intermine.web.logic.query.Constraint;
import org.intermine.web.logic.query.MainHelper;
import org.intermine.web.logic.query.Node;
import org.intermine.web.logic.query.PathQuery;
import org.intermine.web.logic.results.TableHelper;
import org.intermine.web.logic.results.WebResults;

import javax.servlet.ServletContext;

/**
 * Helper for everything related to PathQueryResults
 *
 * @author "<NAME>"
 */
public class PathQueryResultHelper
{
    /**
     * For a given type, return the default view as configured in webconfig-model.xml
     * as a List of Path objects
     *
     * @param type the type for which to get the view
     * @param model the Model
     * @param webConfig the WebConfig
     * @param prefix non-mandatory. Useful when getting a view for a reference or collection
     * @param excludeNonAttributes a boolean
     * @return a List of Paths representing the view
     */
    public static List<Path> getDefaultView(String type, Model model, WebConfig webConfig,
                          String prefix, boolean excludeNonAttributes) {
        List<Path> view = new ArrayList<Path>();
        ClassDescriptor classDescriptor = model.getClassDescriptorByName(type);
            List<FieldConfig> fieldConfigs = FieldConfigHelper.getClassFieldConfigs(webConfig,
                classDescriptor);
            for (FieldConfig fieldConfig : fieldConfigs) {
                if (fieldConfig.getShowInResults()) {
                    String expr = fieldConfig.getFieldExpr();
                    if (prefix == null || prefix.length() <= 0) {
                        prefix = type;
                    }
                    String prePath = "." + expr;
                    StringBuffer pathStringBuffer = new StringBuffer();
                    int lastIndex = prePath.lastIndexOf(".");
                    int index = prePath.indexOf(".");
                    int prevIndex = 0;
                    while (index < lastIndex) {
                        pathStringBuffer.append(prePath.substring(prevIndex, index))
                            .append(":");
                        prevIndex = index + 1;
                        index = prePath.indexOf(".", prevIndex);
                    }
                    pathStringBuffer.append(prePath.substring(prevIndex));
                    String pathString = prefix + "[" + type + "]" + pathStringBuffer.toString();
                    Path path = new Path(model, pathString);
                    // Path path = MainHelper.makePath(model, pathQuery, pathString);
                    // TODO remove isOnlyAttribute when outer joins
                    if (!view.contains(path)
                                    && ((excludeNonAttributes && path.isOnlyAttribute())
                                    || (!excludeNonAttributes))) {
                        view.add(path);
                    }
                }
            if (view.size() == 0) {
                Set<AttributeDescriptor> attrDesc = classDescriptor.getAttributeDescriptors();
                for (AttributeDescriptor attributeDescriptor : attrDesc) {
                    Map<String, String> classToPath = new HashMap<String, String>();
                    classToPath.put(prefix, classDescriptor.getUnqualifiedName());
                    Path path = new Path(model, prefix + "." + attributeDescriptor.getName(),
                                    classToPath);
                    if (!view.contains(path)) {
                        view.add(path);
                    }
                }
            }
        }
        return view;
    }

    /**
     * Create a PathQuery to get the contents of an InterMineBag
     *
     * @param imBag the bag
     * @param webConfig the WebConfig
     * @param model the Model
     * @return a PathQuery
     */
    public static PathQuery makePathQueryForBag(InterMineBag imBag, WebConfig webConfig,
                                                Model model) {
        PathQuery pathQuery = new PathQuery(model);

        List<Path> view = PathQueryResultHelper.getDefaultView(imBag.getType(), model, webConfig,
            null, true);

{'fraction_non_alphanumeric': 0.09265660568248883, 'fraction_numerical': 0.0020234381586712756, 'mean_word_length': 2.2786069651741294, 'pattern_counts': {'":': 2, '<': 26, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6965547', 'n_tokens_mistral': 3078, 'n_tokens_neox': 2949, 'n_words': 731}