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