<commit_before>/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2002-2002 Kana Software, Inc.
// Copyright (C) 2002-2008 <NAME> and others
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
// <NAME>, 13 February, 2002, 10:25 PM
*/

package mondrian.web.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mondrian.olap.*;
import mondrian.spi.CatalogLocator;
import mondrian.spi.impl.ServletContextCatalogLocator;
import mondrian.web.taglib.ResultCache;

import org.eigenbase.xom.StringEscaper;

/**
 * <code>MDXQueryServlet</code> is a servlet which receives MDX queries,
 * executes them, and formats the results in an HTML table.
 *
 * @author  <NAME>
 * @since 13 February, 2002
 * @version $Id$
 */
public class MDXQueryServlet extends HttpServlet {
    private String connectString;
    private CatalogLocator locator;

    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        connectString = config.getInitParameter("connectString");
        Enumeration initParameterNames = config.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String name = (String) initParameterNames.nextElement();
            String value = config.getInitParameter(name);
            MondrianProperties.instance().setProperty(name, value);
        }
        locator = new ServletContextCatalogLocator(config.getServletContext());
    }

    /** Destroys the servlet.
     */
    public void destroy() {

    }

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
        String queryName = request.getParameter("query");
        request.setAttribute("query", queryName);
        if (queryName != null) {
            processTransform(request,response);
            return;
        }
        String queryString = request.getParameter("queryString");
        request.setAttribute("queryString", queryString);
        mondrian.olap.Connection mdxConnection = null;
        StringBuffer html = new StringBuffer();

        // execute the query
        try {
            mdxConnection = DriverManager.getConnection(connectString, locator);
            Query q = mdxConnection.parseQuery(queryString);
            Result result = mdxConnection.execute(q);
            List<Position> slicers = result.getSlicerAxis().getPositions();
            html.append("<table class='resulttable' cellspacing=1 border=0>");
            html.append(Util.nl);

            List<Position> columns = result.getAxes()[0].getPositions();
            List<Position> rows = null;
            if (result.getAxes().length == 2) {
                rows = result.getAxes()[1].getPositions();
            }
            int columnWidth = columns.get(0).size();
            int rowWidth = 0;
            if (result.getAxes().length == 2) {
                rowWidth = result.getAxes()[1].getPositions().get(0).size();
            }
            for (int j = 0; j < columnWidth; j++) {
                html.append("<tr>");

                // if it has more than 1 dimension
                if (j == 0 && result.getAxes().length > 1) {
                    // Print the top-left cell, and fill it with slicer members.
                    html.append("<td nowrap class='slicer' rowspan='").append(
                        columnWidth).append("' colspan='").append(rowWidth)
                        .append("'>");
                    for (Position position : slicers) {
                        int k = 0;
                        for (Member member : position) {
                            if (k > 0) {
                                html.append("<br/>");
                            }
                            html.append(member.getUniqueName());
                            k++;
                        }
/*
                        for (int k = 0; k < position.size(); k++) {
                            if (k > 0) {
                                html.append("<br/>");
                            }
                            Member member = position.getMember(k);
                            html.append(member.getUniqueName());
                        }
*/
                    }
                    html.append("&nbsp;</td>").append(Util.nl);
                }

                // Print the column headings.
                for (int i = 0; i < columns.size(); i++) {
                    Position position = columns.get(i);
                    //Member member = columns[i].getMember(j);
                    Member member = position.get(j);
                    int width = 1;
                    while ((i + 1) < columns.size() &&
                            columns.get(i + 1).get(j) == member) {
                        i++;
                        width++;
                    }
                    html.append("<td nowrap class='columnheading' colspan='")
                        .append(width).append("'>")
                        .append(member.getUniqueName()).append("</td>");
                }
                html.append("</tr>").append(Util.nl);
            }
            //if is two axes, show
            if (result.getAxes().length > 1) {
                for (int i = 0; i < rows.size(); i++) {
                    html.append("<tr>");
                    final Position row = rows.get(i);
                    for (Member member : row) {
                        html.append("<td nowrap class='rowheading'>").append(
                            member.getUniqueName()).append("</td>");
                    }
                    for (int j = 0; j < columns.size(); j++) {
                        showCell(html,result.getCell(new int[]{j,i}));
                    }
                    html.append("</tr>");
                }
            } else {
                html.append("<tr>");
                for (int i = 0; i < columns.size(); i++) {
                    showCell(html,result.getCell(new int[]{i}));
                }
                html.append("</tr>");
            }
            html.append("</table>");
        } catch (Throwable e) {
            final String[] strings = Util.convertStackToString(e);
            html.append("Error:<pre><blockquote>");
            for (String string : strings) {
                StringEscaper.htmlEscaper.appendEscapedString(string, html);
            }
            html.append("</blockquote></pre>");
        } finally {
            if (mdxConnection != null) {
                mdxConnection.close();
            }
        }

        request.setAttribute("result", html.toString());
        response.setHeader("Content-Type", "text/html");
        getServletContext().getRequestDispatcher("/adhoc.jsp").include(request, response);
    }

    private void showCell(StringBuffer out, Cell cell) {
        out.append("<td class='cell'>").append(cell.getFormattedValue()).append(
            "</td>");
    }
    private void processTransform(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String queryName = request.getParameter("query");
        ResultCache rc = ResultCache.getInstance(request.getSession(), getServletContext(), queryName);
        Query query = rc.getQuery();
        query = query.safeClone();
        rc.setDirty();
        String operation = request.getParameter("operation");
        if (operation.equals("expand")) {
            String memberName = request.getParameter("member");<commit_msg>MONDRIAN: Match XOM API change. UTIL/RESGEN: Oops! Fix error introduced when converting Vector to ArrayList.<commit_after>/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2002-2002 Kana Software, Inc.
// Copyright (C) 2002-2008 <NAME> and others
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
// <NAME>, 13 February, 2002, 10:25 PM
*/

package mondrian.web.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mondrian.olap.*;
import mondrian.spi.CatalogLocator;
import mondrian.spi.impl.ServletContextCatalogLocator;
import mondrian.web.taglib.ResultCache;

import org.eigenbase.xom.StringEscaper;

/**
 * <code>MDXQueryServlet</code> is a servlet which receives MDX queries,
 * executes them, and formats the results in an HTML table.
 *
 * @author  <NAME>
 * @since 13 February, 2002
 * @version $Id$
 */
public class MDXQueryServlet extends HttpServlet {
    private String connectString;
    private CatalogLocator locator;

    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        connectString = config.getInitParameter("connectString");
        Enumeration initParameterNames = config.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String name = (String) initParameterNames.nextElement();
            String value = config.getInitParameter(name);
            MondrianProperties.instance().setProperty(name, value);
        }
        locator = new ServletContextCatalogLocator(config.getServletContext());
    }

    /** Destroys the servlet.
     */
    public void destroy() {

    }

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
        String queryName = request.getParameter("query");
        request.setAttribute("query", queryName);
        if (queryName != null) {
            processTransform(request,response);
            return;
        }
        String queryString = request.getParameter("queryString");
        request.setAttribute("queryString", queryString);
        mondrian.olap.Connection mdxConnection = null;
        StringBuilder html = new StringBuilder();

        // execute the query
        try {
            mdxConnection = DriverManager.getConnection(connectString, locator);
            Query q = mdxConnection.parseQuery(queryString);
            Result result = mdxConnection.execute(q);
            List<Position> slicers = result.getSlicerAxis().getPositions();
            html.append("<table class='resulttable' cellspacing=1 border=0>");
            html.append(Util.nl);

            List<Position> columns = result.getAxes()[0].getPositions();
            List<Position> rows = null;
            if (result.getAxes().length == 2) {
                rows = result.getAxes()[1].getPositions();
            }
            int columnWidth = columns.get(0).size();
            int rowWidth = 0;
            if (result.getAxes().length == 2) {
                rowWidth = result.getAxes()[1].getPositions().get(0).size();
            }
            for (int j = 0; j < columnWidth; j++) {
                html.append("<tr>");

                // if it has more than 1 dimension
                if (j == 0 && result.getAxes().length > 1) {
                    // Print the top-left cell, and fill it with slicer members.
                    html.append("<td nowrap class='slicer' rowspan='").append(
                        columnWidth).append("' colspan='").append(rowWidth)
                        .append("'>");
                    for (Position position : slicers) {
                        int k = 0;
                        for (Member member : position) {
                            if (k > 0) {
                                html.append("<br/>");
                            }
                            html.append(member.getUniqueName());
                            k++;
                        }
/*
                        for (int k = 0; k < position.size(); k++) {
                            if (k > 0) {
                                html.append("<br/>");
                            }
                            Member member = position.getMember(k);
                            html.append(member.getUniqueName());
                        }
*/
                    }
                    html.append("&nbsp;</td>").append(Util.nl);
                }

                // Print the column headings.
                for (int i = 0; i < columns.size(); i++) {
                    Position position = columns.get(i);
                    //Member member = columns[i].getMember(j);
                    Member member = position.get(j);
                    int width = 1;
                    while ((i + 1) < columns.size() &&
                            columns.get(i + 1).get(j) == member) {
                        i++;
                        width++;
                    }
                    html.append("<td nowrap class='columnheading' colspan='")
                        .append(width).append("'>")
                        .append(member.getUniqueName()).append("</td>");
                }
                html.append("</tr>").append(Util.nl);
            }
            //if is two axes, show
            if (result.getAxes().length > 1) {
                for (int i = 0; i < rows.size(); i++) {
                    html.append("<tr>");
                    final Position row = rows.get(i);
                    for (Member member : row) {
                        html.append("<td nowrap class='rowheading'>").append(
                            member.getUniqueName()).append("</td>");
                    }
                    for (int j = 0; j < columns.size(); j++) {
                        showCell(html, result.getCell(new int[] {j, i}));
                    }
                    html.append("</tr>");
                }
            } else {
                html.append("<tr>");
                for (int i = 0; i < columns.size(); i++) {
                    showCell(html,result.getCell(new int[]{i}));
                }
                html.append("</tr>");
            }
            html.append("</table>");
        } catch (Throwable e) {
            final String[] strings = Util.convertStackToString(e);
            html.append("Error:<pre><blockquote>");
            for (String string : strings) {
                StringEscaper.htmlEscaper.appendEscapedString(string, html);
            }
            html.append("</blockquote></pre>");
        } finally {
            if (mdxConnection != null) {
                mdxConnection.close();
            }
        }

        request.setAttribute("result", html.toString());
        response.setHeader("Content-Type", "text/html");
        getServletContext().getRequestDispatcher("/adhoc.jsp").include(request, response);
    }

    private void showCell(StringBuilder out, Cell cell) {
        out.append("<td class='cell'>").append(cell.getFormattedValue()).append(
            "</td>");
    }

    private void processTransform(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String queryName = request.getParameter("query");
        ResultCache rc = ResultCache.getInstance(request.getSession(), getServletContext(), queryName);
        Query query = rc.getQuery();
        query = query.safeClone();
        rc.setDirty();
        String operation = request.getParameter("operation");
        if (operation.equals("expand")) {
            String memberName = request.getParameter("member");
{'fraction_non_alphanumeric': 0.12454212454212454, 'fraction_numerical': 0.007081807081807082, 'mean_word_length': 1.7283477681545636, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3220611', 'n_tokens_mistral': 4282, 'n_tokens_neox': 4053, 'n_words': 958}