KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > rolap > RolapSchemaParameter


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/rolap/RolapSchemaParameter.java#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) 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.rolap;
11
12 import mondrian.olap.*;
13 import mondrian.olap.type.Type;
14 import mondrian.calc.ParameterCompilable;
15 import mondrian.calc.Calc;
16 import mondrian.calc.ExpCompiler;
17 import mondrian.calc.impl.GenericCalc;
18 import mondrian.resource.MondrianResource;
19
20 /**
21  * Parameter which is defined in a schema.
22  *
23  * @author jhyde
24  * @version $Id: //open/mondrian/src/main/mondrian/rolap/RolapSchemaParameter.java#2 $
25  * @since Jul 20, 2006
26  */

27 public class RolapSchemaParameter implements Parameter, ParameterCompilable {
28     private final RolapSchema schema;
29     private final String JavaDoc name;
30     private String JavaDoc description;
31     private String JavaDoc defaultExpString;
32     private Type type;
33     private final boolean modifiable;
34     private Object JavaDoc value;
35     private Object JavaDoc cachedDefaultValue;
36
37     RolapSchemaParameter(
38         RolapSchema schema,
39         String JavaDoc name,
40         String JavaDoc defaultExpString,
41         String JavaDoc description,
42         Type type,
43         boolean modifiable)
44     {
45         assert defaultExpString != null;
46         assert name != null;
47         assert schema != null;
48         assert type != null;
49         this.schema = schema;
50         this.name = name;
51         this.defaultExpString = defaultExpString;
52         this.description = description;
53         this.type = type;
54         this.modifiable = modifiable;
55         schema.parameterList.add(this);
56     }
57
58     RolapSchema getSchema() {
59         return schema;
60     }
61
62     public boolean isModifiable() {
63         return modifiable;
64     }
65
66     public Scope getScope() {
67         return Scope.Schema;
68     }
69
70     public Type getType() {
71         return type;
72     }
73
74     public Exp getDefaultExp() {
75         throw new UnsupportedOperationException JavaDoc();
76     }
77
78     public String JavaDoc getName() {
79         return name;
80     }
81
82     public String JavaDoc getDescription() {
83         return description;
84     }
85
86     public Object JavaDoc getValue() {
87         return value;
88     }
89
90     public void setValue(Object JavaDoc value) {
91         if (!modifiable) {
92             throw MondrianResource.instance().ParameterIsNotModifiable.ex(
93                 getName(), getScope().name());
94         }
95         this.value = value;
96     }
97
98     public Calc compile(ExpCompiler compiler) {
99         // Parse and compile the expression for the default value.
100
Exp defaultExp = compiler.getValidator()
101             .getQuery()
102             .getConnection()
103             .parseExpression(defaultExpString);
104         defaultExp = compiler.getValidator().validate(defaultExp, true);
105         final Calc defaultCalc = defaultExp.accept(compiler);
106
107         // Generate a program which looks at the assigned value first,
108
// and if it is not set, returns the default expression.
109
return new GenericCalc(defaultExp) {
110             public Calc[] getCalcs() {
111                 return new Calc[] {defaultCalc};
112             }
113
114             public Object JavaDoc evaluate(Evaluator evaluator) {
115                 if (value != null) {
116                     return value;
117                 }
118                 if (cachedDefaultValue == null) {
119                     cachedDefaultValue = defaultCalc.evaluate(evaluator);
120                 }
121                 return cachedDefaultValue;
122             }
123         };
124     }
125 }
126
127 // End RolapSchemaParameter.java
128
Popular Tags