001/* 002// This software is subject to the terms of the Eclipse Public License v1.0 003// Agreement, available at the following URL: 004// http://www.eclipse.org/legal/epl-v10.html. 005// You must accept the terms of that agreement to use this software. 006// 007// Copyright (C) 2006-2010 Pentaho 008// All Rights Reserved. 009*/ 010package mondrian.rolap; 011 012import mondrian.calc.*; 013import mondrian.calc.impl.GenericCalc; 014import mondrian.olap.*; 015import mondrian.olap.type.Type; 016import mondrian.resource.MondrianResource; 017 018/** 019 * Parameter that is defined in a schema. 020 * 021 * @author jhyde 022 * @since Jul 20, 2006 023 */ 024public class RolapSchemaParameter implements Parameter, ParameterCompilable { 025 private final RolapSchema schema; 026 private final String name; 027 private String description; 028 private String defaultExpString; 029 private Type type; 030 private final boolean modifiable; 031 private Object value; 032 private boolean assigned; 033 private Object cachedDefaultValue; 034 035 RolapSchemaParameter( 036 RolapSchema schema, 037 String name, 038 String defaultExpString, 039 String description, 040 Type type, 041 boolean modifiable) 042 { 043 assert defaultExpString != null; 044 assert name != null; 045 assert schema != null; 046 assert type != null; 047 this.schema = schema; 048 this.name = name; 049 this.defaultExpString = defaultExpString; 050 this.description = description; 051 this.type = type; 052 this.modifiable = modifiable; 053 schema.parameterList.add(this); 054 } 055 056 RolapSchema getSchema() { 057 return schema; 058 } 059 060 public boolean isModifiable() { 061 return modifiable; 062 } 063 064 public Scope getScope() { 065 return Scope.Schema; 066 } 067 068 public Type getType() { 069 return type; 070 } 071 072 public Exp getDefaultExp() { 073 throw new UnsupportedOperationException(); 074 } 075 076 public String getName() { 077 return name; 078 } 079 080 public String getDescription() { 081 return description; 082 } 083 084 public Object getValue() { 085 return value; 086 } 087 088 public void setValue(Object value) { 089 if (!modifiable) { 090 throw MondrianResource.instance().ParameterIsNotModifiable.ex( 091 getName(), getScope().name()); 092 } 093 this.assigned = true; 094 this.value = value; 095 } 096 097 public boolean isSet() { 098 return assigned; 099 } 100 101 public void unsetValue() { 102 if (!modifiable) { 103 throw MondrianResource.instance().ParameterIsNotModifiable.ex( 104 getName(), getScope().name()); 105 } 106 assigned = false; 107 value = null; 108 } 109 110 public Calc compile(ExpCompiler compiler) { 111 // Parse and compile the expression for the default value. 112 Exp defaultExp = compiler.getValidator() 113 .getQuery() 114 .getConnection() 115 .parseExpression(defaultExpString); 116 defaultExp = compiler.getValidator().validate(defaultExp, true); 117 final Calc defaultCalc = defaultExp.accept(compiler); 118 119 // Generate a program which looks at the assigned value first, 120 // and if it is not set, returns the default expression. 121 return new GenericCalc(defaultExp) { 122 public Calc[] getCalcs() { 123 return new Calc[] {defaultCalc}; 124 } 125 126 public Object evaluate(Evaluator evaluator) { 127 if (value != null) { 128 return value; 129 } 130 if (cachedDefaultValue == null) { 131 cachedDefaultValue = defaultCalc.evaluate(evaluator); 132 } 133 return cachedDefaultValue; 134 } 135 }; 136 } 137} 138 139// End RolapSchemaParameter.java