001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/MemberLevelFunDef.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.olap.type.Type;
013    import mondrian.olap.type.LevelType;
014    import mondrian.olap.*;
015    import mondrian.calc.Calc;
016    import mondrian.calc.ExpCompiler;
017    import mondrian.calc.MemberCalc;
018    import mondrian.calc.impl.AbstractLevelCalc;
019    import mondrian.mdx.ResolvedFunCall;
020    
021    /**
022     * Definition of the <code>&lt;Member&gt;.Level</code> MDX builtin function.
023     *
024     * @author jhyde
025     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/MemberLevelFunDef.java#3 $
026     * @since Mar 23, 2006
027     */
028    public class MemberLevelFunDef extends FunDefBase {
029        static final MemberLevelFunDef instance = new MemberLevelFunDef();
030    
031        private MemberLevelFunDef() {
032            super("Level", "Returns a member's level.", "plm");
033        }
034    
035        public Type getResultType(Validator validator, Exp[] args) {
036            final Type argType = args[0].getType();
037            return LevelType.forType(argType);
038        }
039    
040        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
041            final MemberCalc memberCalc =
042                    compiler.compileMember(call.getArg(0));
043            return new CalcImpl(call, memberCalc);
044        }
045    
046        public static class CalcImpl extends AbstractLevelCalc {
047            private final MemberCalc memberCalc;
048    
049            public CalcImpl(Exp exp, MemberCalc memberCalc) {
050                super(exp, new Calc[] {memberCalc});
051                this.memberCalc = memberCalc;
052            }
053    
054            public Level evaluateLevel(Evaluator evaluator) {
055                Member member = memberCalc.evaluateMember(evaluator);
056                return member.getLevel();
057            }
058        }
059    }
060    
061    // End MemberLevelFunDef.java