001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2002-2005 Julian Hyde
008// Copyright (C) 2005-2010 Pentaho and others
009// All Rights Reserved.
010*/
011package mondrian.web.taglib;
012
013import javax.servlet.jsp.JspException;
014import javax.servlet.jsp.tagext.BodyTagSupport;
015
016/**
017 * A <code>QueryTag</code> creates a {@link ResultCache} object and initializes
018 * it with the MDX query. Example:<blockquote>
019 *
020 * <pre>&lt;query name="query1" resultCache="true"&gt;
021 * select
022 *   {[Measures].[Unit Sales], [Measures].[Store Cost]} on columns,
023 *   CrossJoin(
024 *     { [Promotion Media].[Radio],
025 *       [Promotion Media].[TV],
026 *       [Promotion Media].[Sunday Paper],
027 *       [Promotion Media].[Street Handout] },
028 *     [Product].[Drink].children) on rows
029 * from Sales
030 * where ([Time].[1997])
031 * &lt;/query&gt;</pre>
032 *
033 * </blockquote>
034 *
035 * Attributes are
036 * {@link #setName name},
037 * {@link #setResultCache resultCache}.
038 *
039 * @author Andreas Voss, 22 March, 2002
040 */
041public class QueryTag extends BodyTagSupport {
042
043    public QueryTag() {
044    }
045
046    public int doAfterBody() throws JspException {
047        try {
048            ApplResources ar =
049                ApplResources.getInstance(pageContext.getServletContext());
050            ResultCache rc =
051                ResultCache.getInstance(
052                    pageContext.getSession(),
053                    pageContext.getServletContext(),
054                    name);
055            // if this is the first call, we have to parse the mdx query
056            if (!resultCache || rc.getQuery() == null) {
057                String mdx = getBodyContent().getString();
058                rc.parse(mdx);
059            }
060            return SKIP_BODY;
061        } catch (Exception e) {
062            e.printStackTrace();
063            throw new JspException(e);
064        }
065    }
066
067    /** Sets string attribute <code>name</code>, which identifies this query
068     * within its page. The {@link TransformTag#setQuery &lt;transform
069     * query&gt;} attribute uses this. */
070    public void setName(String newName) {
071        name = newName;
072    }
073    public String getName() {
074        return name;
075    }
076    /** Sets boolean attribute <code>resultCache</code>; if true, the query is
077     * parsed, executed, and converted to an XML document at most once. This
078     * improves performance and consistency, but the results may become out of
079     * date. We also need a way to prevent the cache using too much memory. */
080    public void setResultCache(boolean newResultCache) {
081        resultCache = newResultCache;
082    }
083    public boolean isResultCache() {
084        return resultCache;
085    }
086    private String name;
087    private boolean resultCache;
088}
089
090// End QueryTag.java