001    /*
002    // $Id: //open/mondrian/src/main/mondrian/rolap/RolapSchemaParameter.java#2 $
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) 2006-2006 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.rolap;
011    
012    import mondrian.olap.*;
013    import mondrian.olap.type.Type;
014    import mondrian.calc.ParameterCompilable;
015    import mondrian.calc.Calc;
016    import mondrian.calc.ExpCompiler;
017    import mondrian.calc.impl.GenericCalc;
018    import mondrian.resource.MondrianResource;
019    
020    /**
021     * Parameter which is defined in a schema.
022     *
023     * @author jhyde
024     * @version $Id: //open/mondrian/src/main/mondrian/rolap/RolapSchemaParameter.java#2 $
025     * @since Jul 20, 2006
026     */
027    public class RolapSchemaParameter implements Parameter, ParameterCompilable {
028        private final RolapSchema schema;
029        private final String name;
030        private String description;
031        private String defaultExpString;
032        private Type type;
033        private final boolean modifiable;
034        private Object value;
035        private Object cachedDefaultValue;
036    
037        RolapSchemaParameter(
038            RolapSchema schema,
039            String name,
040            String defaultExpString,
041            String description,
042            Type type,
043            boolean modifiable)
044        {
045            assert defaultExpString != null;
046            assert name != null;
047            assert schema != null;
048            assert type != null;
049            this.schema = schema;
050            this.name = name;
051            this.defaultExpString = defaultExpString;
052            this.description = description;
053            this.type = type;
054            this.modifiable = modifiable;
055            schema.parameterList.add(this);
056        }
057    
058        RolapSchema getSchema() {
059            return schema;
060        }
061    
062        public boolean isModifiable() {
063            return modifiable;
064        }
065    
066        public Scope getScope() {
067            return Scope.Schema;
068        }
069    
070        public Type getType() {
071            return type;
072        }
073    
074        public Exp getDefaultExp() {
075            throw new UnsupportedOperationException();
076        }
077    
078        public String getName() {
079            return name;
080        }
081    
082        public String getDescription() {
083            return description;
084        }
085    
086        public Object getValue() {
087            return value;
088        }
089    
090        public void setValue(Object value) {
091            if (!modifiable) {
092                throw MondrianResource.instance().ParameterIsNotModifiable.ex(
093                    getName(), getScope().name());
094            }
095            this.value = value;
096        }
097    
098        public Calc compile(ExpCompiler compiler) {
099            // 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 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