<commit_before>package org.intermine.webservice.server.template;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.intermine.api.InterMineAPI;
import org.intermine.api.profile.Profile;
import org.intermine.api.template.TemplateManager;
import org.intermine.api.template.TemplatePopulator;
import org.intermine.api.template.TemplatePopulatorException;
import org.intermine.api.template.TemplateQuery;
import org.intermine.api.template.TemplateValue;
import org.intermine.pathquery.Path;
import org.intermine.pathquery.PathException;
import org.intermine.pathquery.PathQuery;
import org.intermine.web.logic.session.SessionMethods;
import org.intermine.web.logic.template.TemplateHelper;
import org.intermine.web.logic.template.TemplateResultInput;
import org.intermine.webservice.server.exceptions.BadRequestException;
import org.intermine.webservice.server.query.QueryToListService;
import org.intermine.webservice.server.template.result.TemplateResultRequestParser;

public class TemplateToListService extends QueryToListService {

    private static final String TEMPLATE_PARAM = "template";
    private static final String NEW_VIEW_PARAM = "path";

    private final TemplateManager templateManager;

    private static final Logger LOG = Logger.getLogger(TemplateToListService.class);

    /**
     * Constructor
     * @param im API settings bundle
     */
    public TemplateToListService(InterMineAPI im) {
        super(im);
        templateManager = im.getTemplateManager();
    }

    @Override
    protected PathQuery getQuery(HttpServletRequest request) {

        String newViewString = request.getParameter(NEW_VIEW_PARAM);
        if (StringUtils.isEmpty(newViewString)) {
            throw new BadRequestException("new view string is blank");
        }

        Profile profile = SessionMethods.getProfile(request.getSession());

        TemplateResultInput input = new TemplateResultRequestParser(request).getInput();
        TemplateQuery template = templateManager.getUserOrGlobalTemplate(profile, input.getName());

        Path newViewPath;
        try {
            newViewPath = new Path(template.getModel(), newViewString);
        } catch (PathException e) {
            throw new BadRequestException("The new view string is not a valid path", e);
        }

        if (newViewPath.endIsAttribute()) {
            if (!newViewString.endsWith(".id")) {
                throw new BadRequestException(
                        "The new view string refers to an attribute which is not the object id");
            }
        } else {
            newViewString += ".id";
        }


        Map<String, List<TemplateValue>> templateValues = TemplateHelper.getValuesFromInput(
                template, input);
        TemplateQuery populatedTemplate;
        try {
            populatedTemplate =
                TemplatePopulator.getPopulatedTemplate(template, templateValues);
        } catch (TemplatePopulatorException e) {
            e.printStackTrace();
            LOG.error("Error populating template: " + template.getName() + ". " + e);
            throw new BadRequestException("Error in applying constraint values to template: "
                    + template.getName(), e);
        }

        PathQuery pq = populatedTemplate.getQueryToExecute();

        populatedTemplate.clearView();
        populatedTemplate.addView(newViewString);

        return pq;
    }

}
<commit_msg>Fixed template to list bug, which caused the query to always have the wrong number of select elements.<commit_after>package org.intermine.webservice.server.template;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.intermine.api.InterMineAPI;
import org.intermine.api.profile.Profile;
import org.intermine.api.template.TemplateManager;
import org.intermine.api.template.TemplatePopulator;
import org.intermine.api.template.TemplatePopulatorException;
import org.intermine.api.template.TemplateQuery;
import org.intermine.api.template.TemplateValue;
import org.intermine.pathquery.Path;
import org.intermine.pathquery.PathException;
import org.intermine.pathquery.PathQuery;
import org.intermine.web.logic.session.SessionMethods;
import org.intermine.web.logic.template.TemplateHelper;
import org.intermine.web.logic.template.TemplateResultInput;
import org.intermine.webservice.server.exceptions.BadRequestException;
import org.intermine.webservice.server.query.QueryToListService;
import org.intermine.webservice.server.template.result.TemplateResultRequestParser;

public class TemplateToListService extends QueryToListService {

    private static final String TEMPLATE_PARAM = "template";
    private static final String NEW_VIEW_PARAM = "path";

    private final TemplateManager templateManager;

    private static final Logger LOG = Logger.getLogger(TemplateToListService.class);

    /**
     * Constructor
     * @param im API settings bundle
     */
    public TemplateToListService(InterMineAPI im) {
        super(im);
        templateManager = im.getTemplateManager();
    }

    @Override
    protected PathQuery getQuery(HttpServletRequest request) {

        String newViewString = request.getParameter(NEW_VIEW_PARAM);
        if (StringUtils.isEmpty(newViewString)) {
            throw new BadRequestException("new view string is blank");
        }

        Profile profile = SessionMethods.getProfile(request.getSession());

        TemplateResultInput input = new TemplateResultRequestParser(request).getInput();
        TemplateQuery template = templateManager.getUserOrGlobalTemplate(profile, input.getName());

        Path newViewPath;
        try {
            newViewPath = new Path(template.getModel(), newViewString);
        } catch (PathException e) {
            throw new BadRequestException("The new view string is not a valid path", e);
        }

        if (newViewPath.endIsAttribute()) {
            if (!newViewString.endsWith(".id")) {
                throw new BadRequestException(
                        "The new view string refers to an attribute which is not the object id");
            }
        } else {
            newViewString += ".id";
        }


        Map<String, List<TemplateValue>> templateValues = TemplateHelper.getValuesFromInput(
                template, input);
        TemplateQuery populatedTemplate;
        try {
            populatedTemplate =
                TemplatePopulator.getPopulatedTemplate(template, templateValues);
        } catch (TemplatePopulatorException e) {
            e.printStackTrace();
            LOG.error("Error populating template: " + template.getName() + ". " + e);
            throw new BadRequestException("Error in applying constraint values to template: "
                    + template.getName(), e);
        }

        PathQuery pq = populatedTemplate.getQueryToExecute();

        pq.clearView();
        pq.addView(newViewString);

        return pq;
    }

}

{'fraction_non_alphanumeric': 0.08824765373301582, 'fraction_numerical': 0.0002801512816921137, 'mean_word_length': 3.232365145228216, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7664904', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1790, 'n_words': 409}