001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/DimensionCurrentMemberFunDef.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.DimensionCalc;
015    import mondrian.calc.impl.AbstractMemberCalc;
016    import mondrian.mdx.ResolvedFunCall;
017    import mondrian.olap.Exp;
018    import mondrian.olap.Member;
019    import mondrian.olap.Evaluator;
020    import mondrian.olap.Dimension;
021    
022    /**
023     * Definition of the <code>&lt;Dimension&gt;.CurrentMember</code> MDX builtin
024     * function.
025     *
026     * <p>Syntax:
027     * <blockquote><code>&lt;Dimension&gt;.CurrentMember</code></blockquote>
028     *
029     * @author jhyde
030     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/DimensionCurrentMemberFunDef.java#3 $
031     * @since Mar 23, 2006
032     */
033    public class DimensionCurrentMemberFunDef extends FunDefBase {
034        static final DimensionCurrentMemberFunDef instance =
035                new DimensionCurrentMemberFunDef();
036    
037        private DimensionCurrentMemberFunDef() {
038            super("CurrentMember",
039                    "Returns the current member along a dimension during an iteration.",
040                    "pmd");
041        }
042    
043        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
044            final DimensionCalc dimensionCalc =
045                    compiler.compileDimension(call.getArg(0));
046            return new CalcImpl(call, dimensionCalc);
047        }
048    
049        public static class CalcImpl extends AbstractMemberCalc {
050            private final DimensionCalc dimensionCalc;
051    
052            public CalcImpl(Exp exp, DimensionCalc dimensionCalc) {
053                super(exp, new Calc[] {dimensionCalc});
054                this.dimensionCalc = dimensionCalc;
055            }
056    
057            protected String getName() {
058                return "CurrentMember";
059            }
060    
061            public Member evaluateMember(Evaluator evaluator) {
062                Dimension dimension =
063                        dimensionCalc.evaluateDimension(evaluator);
064                return evaluator.getContext(dimension);
065            }
066    
067            public boolean dependsOn(Dimension dimension) {
068                return dimensionCalc.getType().usesDimension(dimension, true) ;
069            }
070        }
071    }
072    
073    // End DimensionCurrentMemberFunDef.java