001 /* 002 // $Id: //open/mondrian/src/main/mondrian/olap/fun/HierarchyCurrentMemberFunDef.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.AbstractMemberCalc; 016 import mondrian.mdx.ResolvedFunCall; 017 import mondrian.olap.*; 018 019 /** 020 * Definition of the <code><Hierarchy>.CurrentMember</code> MDX builtin function. 021 * 022 * @author jhyde 023 * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/HierarchyCurrentMemberFunDef.java#3 $ 024 * @since Mar 23, 2006 025 */ 026 public class HierarchyCurrentMemberFunDef extends FunDefBase { 027 static final HierarchyCurrentMemberFunDef instance = 028 new HierarchyCurrentMemberFunDef(); 029 030 private HierarchyCurrentMemberFunDef() { 031 super( 032 "CurrentMember", 033 "Returns the current member along a hierarchy during an iteration.", 034 "pmh"); 035 } 036 037 public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) { 038 final HierarchyCalc hierarchyCalc = 039 compiler.compileHierarchy(call.getArg(0)); 040 return new CalcImpl(call, hierarchyCalc); 041 } 042 043 public static class CalcImpl extends AbstractMemberCalc { 044 private final HierarchyCalc hierarchyCalc; 045 046 public CalcImpl(Exp exp, HierarchyCalc hierarchyCalc) { 047 super(exp, new Calc[] {hierarchyCalc}); 048 this.hierarchyCalc = hierarchyCalc; 049 } 050 051 protected String getName() { 052 return "CurrentMember"; 053 } 054 055 public Member evaluateMember(Evaluator evaluator) { 056 Hierarchy hierarchy = 057 hierarchyCalc.evaluateHierarchy(evaluator); 058 Member member = evaluator.getContext(hierarchy.getDimension()); 059 // If the dimension has multiple hierarchies, and the current 060 // member belongs to a different hierarchy, then this hierarchy 061 // reverts to its default member. 062 // 063 // For example, if the current member of the [Time] dimension 064 // is [Time.Weekly].[2003].[Week 4], then the current member 065 // of the [Time.Monthly] hierarchy is its default member, 066 // [Time.Monthy].[All]. 067 if (member.getHierarchy() != hierarchy) { 068 member = hierarchy.getDefaultMember(); 069 } 070 return member; 071 } 072 073 public boolean dependsOn(Dimension dimension) { 074 return hierarchyCalc.getType().usesDimension(dimension, true); 075 } 076 } 077 } 078 079 // End HierarchyCurrentMemberFunDef.java