KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > param > SetParameterTag


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.param;
14
15 import javax.servlet.jsp.JspException JavaDoc;
16 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
17
18 import com.tonbeller.jpivot.olap.model.Expression;
19 import com.tonbeller.jpivot.olap.model.OlapModel;
20 import com.tonbeller.jpivot.olap.navi.ExpressionParser;
21 import com.tonbeller.jpivot.olap.navi.SetParameter;
22 import com.tonbeller.jpivot.olap.navi.ExpressionParser.InvalidSyntaxException;
23 import com.tonbeller.wcf.expr.ExprUtils;
24 import com.tonbeller.wcf.param.SessionParam;
25 import com.tonbeller.wcf.param.SessionParamPool;
26
27 /**
28  * looks for a http parameter with a certain name. If the parameter is
29  * present, the parameter is parsed and set into the MDX Query.
30  * <p/>
31  * If a sessionParam is specified, its value is set into the MDX query.
32  * @author av
33  */

34 public class SetParameterTag extends TagSupport JavaDoc {
35   String JavaDoc httpParam;
36   String JavaDoc mdxParam;
37   String JavaDoc sessionParam;
38   String JavaDoc query;
39
40   public int doStartTag() throws JspException JavaDoc {
41     if ((httpParam == null) == (sessionParam == null))
42       throw new JspException JavaDoc("either httpParam or sessionParam required");
43
44     if (httpParam != null)
45       return doStartTagHttp();
46     return doStartTagSession();
47   }
48
49   public int doStartTagSession() throws JspException JavaDoc {
50     return SKIP_BODY;
51   }
52
53   public int doStartTagHttp() throws JspException JavaDoc {
54     String JavaDoc value = pageContext.getRequest().getParameter(httpParam);
55     if (value != null)
56       return EVAL_BODY_INCLUDE;
57     return SKIP_BODY;
58   }
59
60   public int doEndTag() throws JspException JavaDoc {
61     if (httpParam != null)
62       return doEndTagHttp();
63     return doEndTagSession();
64   }
65
66   public int doEndTagSession() throws JspException JavaDoc {
67     SessionParamPool pool = SessionParamPool.instance(pageContext.getSession());
68     SessionParam p = pool.getParam(sessionParam);
69     if (p != null)
70       setQueryParam(p.getMdxValue());
71     return super.doEndTag();
72   }
73
74   public int doEndTagHttp() throws JspException JavaDoc {
75     String JavaDoc value = pageContext.getRequest().getParameter(httpParam);
76     if (value != null)
77       setQueryParam(value);
78     return super.doEndTag();
79   }
80
81   private void setQueryParam(String JavaDoc value) throws JspException JavaDoc {
82     // RequestContext context = RequestContextFactoryFinder.createContext(pageContext);
83
OlapModel model = (OlapModel) ExprUtils.getModelReference(pageContext, query);
84     if (model == null)
85       throw new JspException JavaDoc("OlapModel/Query " + query + " not found");
86     SetParameter setter = (SetParameter) model.getExtension(SetParameter.ID);
87     if (setter == null)
88       throw new JspException JavaDoc("SetParameter not supported");
89     ExpressionParser parser = (ExpressionParser) model.getExtension(ExpressionParser.ID);
90     if (parser == null)
91       throw new JspException JavaDoc("ExpressionParser not supported");
92     try {
93       Expression expr = parser.parse(value);
94       setter.setParameter(mdxParam, expr);
95     } catch (InvalidSyntaxException e) {
96       throw new JspException JavaDoc(e);
97     }
98   }
99
100   public void setHttpParam(String JavaDoc string) {
101     httpParam = string;
102   }
103
104   public void setMdxParam(String JavaDoc string) {
105     mdxParam = string;
106   }
107
108   public void setQuery(String JavaDoc string) {
109     query = string;
110   }
111
112   public void setSessionParam(String JavaDoc sessionParam) {
113     this.sessionParam = sessionParam;
114   }
115 }
Popular Tags