KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > mdx > ParameterExpr


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/mdx/ParameterExpr.java#3 $
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) 2006-2006 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.mdx;
11
12 import mondrian.olap.*;
13 import mondrian.olap.type.Type;
14 import mondrian.olap.type.TypeUtil;
15 import mondrian.calc.Calc;
16 import mondrian.calc.ExpCompiler;
17 import mondrian.calc.ParameterCompilable;
18
19 import java.io.PrintWriter JavaDoc;
20
21 /**
22  * MDX expression which is a usage of a {@link mondrian.olap.Parameter}.
23  *
24  * @author jhyde
25  * @version $Id: //open/mondrian/src/main/mondrian/mdx/ParameterExpr.java#3 $
26  */

27 public class ParameterExpr extends ExpBase {
28
29     private Parameter parameter;
30
31     public ParameterExpr(Parameter parameter)
32     {
33         this.parameter = parameter;
34     }
35
36     public Type getType() {
37         return parameter.getType();
38     }
39
40     public int getCategory() {
41         return TypeUtil.typeToCategory(parameter.getType());
42     }
43
44     public Exp accept(Validator validator) {
45         // There must be some Parameter with this name registered with the
46
// Query. After clone(), there will be many copies of the same
47
// parameter, and we rely on this method to bring them down to one.
48
// So if this object is not the registered vesion, that's fine, go with
49
// the other one. The registered one will be resolved after everything
50
// else in the query has been resolved.
51
String JavaDoc parameterName = parameter.getName();
52         Parameter p = validator.getQuery().getSchemaReader(false).getParameter(
53             parameterName);
54         if (p == null) {
55           this.parameter = validator.createOrLookupParam(
56               true,
57               parameter.getName(),
58               parameter.getType(),
59               parameter.getDefaultExp(),
60               parameter.getDescription());
61         }
62         else {
63           this.parameter = p;
64         }
65         return this;
66     }
67
68     public Calc accept(ExpCompiler compiler) {
69         return ((ParameterCompilable) parameter).compile(compiler);
70     }
71
72     public Object JavaDoc accept(MdxVisitor visitor) {
73         return visitor.visit(this);
74     }
75
76     public ParameterExpr clone() {
77         return new ParameterExpr(parameter);
78     }
79
80     /**
81      * Unparses the definition of this Parameter.
82      *
83      * <p>The first usage of a parameter in a query becomes a call to the
84      * <code>Parameter(paramName, description, defaultValue)</code>
85      * function, and subsequent usages become calls to
86      * <code>ParamRef(paramName)</code>
87      *
88      * @param pw PrintWriter
89      */

90     public void unparse(PrintWriter JavaDoc pw) {
91         // Is this the first time we've seen a statement parameter? If so,
92
// we will generate a call to the Parameter() function, to define
93
// the parameter.
94
final boolean def;
95         if (pw instanceof QueryPrintWriter &&
96             parameter.getScope() == Parameter.Scope.Statement) {
97             def = ((QueryPrintWriter) pw).parameters.add(parameter);
98         } else {
99             def = false;
100         }
101         final String JavaDoc name = parameter.getName();
102         final Type type = parameter.getType();
103         final int category = TypeUtil.typeToCategory(type);
104         if (def) {
105             pw.print("Parameter(" + Util.quoteForMdx(name) + ", ");
106             switch (category) {
107             case Category.String:
108             case Category.Numeric:
109                 pw.print(Category.instance.getName(category).toUpperCase());
110                 break;
111             case Category.Member:
112                 String JavaDoc memberName =
113                         type.getLevel() != null ?
114                         type.getLevel().getUniqueName() :
115                         type.getHierarchy() != null ?
116                         type.getHierarchy().getUniqueName() :
117                         type.getDimension().getUniqueName();
118                 pw.print(memberName);
119                 break;
120             default:
121                 throw Category.instance.badValue(category);
122             }
123             pw.print(", ");
124             final Object JavaDoc value = parameter.getValue();
125             if (value == null) {
126                 parameter.getDefaultExp().unparse(pw);
127             } else if (value instanceof String JavaDoc) {
128                 String JavaDoc s = (String JavaDoc) value;
129                 pw.print(Util.quoteForMdx(s));
130             } else {
131                 pw.print(value);
132             }
133             final String JavaDoc description = parameter.getDescription();
134             if (description != null) {
135                 pw.print(", " + Util.quoteForMdx(description));
136             }
137             pw.print(")");
138         } else {
139             pw.print("ParamRef(" + Util.quoteForMdx(name) + ")");
140         }
141     }
142
143     // For the purposes of type inference and expression substitution, a
144
// parameter is atomic; therefore, we ignore the child member, if any.
145
public Object JavaDoc[] getChildren() {
146         return null;
147     }
148
149     /**
150      * Returns whether this parameter is equal to another, based upon name,
151      * type and value
152      */

153     public boolean equals(Object JavaDoc other) {
154         if (!(other instanceof ParameterExpr)) {
155             return false;
156         }
157         ParameterExpr that = (ParameterExpr) other;
158         return this.parameter == that.parameter;
159     }
160
161     /**
162      * Returns whether the parameter can be modified.
163      */

164     public boolean isModifiable() {
165         return true;
166     }
167
168     /**
169      * Returns the parameter used by this expression.
170      */

171     public Parameter getParameter() {
172         return parameter;
173     }
174 }
175
176 // End ParameterExpr.java
177
Popular Tags