001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/HierarchyDimensionFunDef.java#3 $
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-2007 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.olap.fun;
011    
012    import mondrian.calc.Calc;
013    import mondrian.calc.ExpCompiler;
014    import mondrian.calc.HierarchyCalc;
015    import mondrian.calc.impl.AbstractDimensionCalc;
016    import mondrian.mdx.ResolvedFunCall;
017    import mondrian.olap.Exp;
018    import mondrian.olap.Dimension;
019    import mondrian.olap.Evaluator;
020    import mondrian.olap.Hierarchy;
021    
022    /**
023     * Definition of the <code>&lt;Hierarchy&gt;.Dimension</code> MDX builtin function.
024     *
025     * @author jhyde
026     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/HierarchyDimensionFunDef.java#3 $
027     * @since Mar 23, 2006
028     */
029    public class HierarchyDimensionFunDef extends FunDefBase {
030        static final HierarchyDimensionFunDef instance = new HierarchyDimensionFunDef();
031    
032        private HierarchyDimensionFunDef() {
033            super(
034                "Dimension",
035                "Returns the dimension that contains a specified hierarchy.",
036                "pdh");
037        }
038    
039        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
040            final HierarchyCalc hierarchyCalc =
041                    compiler.compileHierarchy(call.getArg(0));
042            return new CalcImpl(call, hierarchyCalc);
043        }
044    
045        public static class CalcImpl extends AbstractDimensionCalc {
046            private final HierarchyCalc hierarchyCalc;
047    
048            public CalcImpl(Exp exp, HierarchyCalc hierarchyCalc) {
049                super(exp, new Calc[] {hierarchyCalc});
050                this.hierarchyCalc = hierarchyCalc;
051            }
052    
053            public Dimension evaluateDimension(Evaluator evaluator) {
054                Hierarchy hierarchy =
055                        hierarchyCalc.evaluateHierarchy(evaluator);
056                return hierarchy.getDimension();
057            }
058        }
059    }
060    
061    // End HierarchyDimensionFunDef.java