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