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