KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > fun > HierarchyCurrentMemberFunDef


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/HierarchyCurrentMemberFunDef.java#1 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2006-2006 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.olap.fun;
11
12 import mondrian.calc.Calc;
13 import mondrian.calc.ExpCompiler;
14 import mondrian.calc.HierarchyCalc;
15 import mondrian.calc.impl.AbstractMemberCalc;
16 import mondrian.mdx.ResolvedFunCall;
17 import mondrian.olap.*;
18
19 /**
20  * Definition of the <code>&lt;Hierarchy&gt;.CurrentMember</code> MDX builtin function.
21  *
22  * @author jhyde
23  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/HierarchyCurrentMemberFunDef.java#1 $
24  * @since Mar 23, 2006
25  */

26 public class HierarchyCurrentMemberFunDef extends FunDefBase {
27     static final HierarchyCurrentMemberFunDef instance =
28             new HierarchyCurrentMemberFunDef();
29
30     private HierarchyCurrentMemberFunDef() {
31         super("CurrentMember", "<Hierarchy>.CurrentMember",
32                 "Returns the current member along a hierarchy during an iteration.",
33                 "pmh");
34     }
35
36     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
37         final HierarchyCalc hierarchyCalc =
38                 compiler.compileHierarchy(call.getArg(0));
39         return new CalcImpl(call, hierarchyCalc);
40     }
41
42     public static class CalcImpl extends AbstractMemberCalc {
43         private final HierarchyCalc hierarchyCalc;
44
45         public CalcImpl(Exp exp, HierarchyCalc hierarchyCalc) {
46             super(exp, new Calc[] {hierarchyCalc});
47             this.hierarchyCalc = hierarchyCalc;
48         }
49
50         protected String JavaDoc getName() {
51             return "CurrentMember";
52         }
53
54         public Member evaluateMember(Evaluator evaluator) {
55             Hierarchy hierarchy =
56                     hierarchyCalc.evaluateHierarchy(evaluator);
57             Member member = evaluator.getContext(hierarchy.getDimension());
58             // If the dimension has multiple hierarchies, and the current
59
// member belongs to a different hierarchy, then this hierarchy
60
// reverts to its default member.
61
//
62
// For example, if the current member of the [Time] dimension
63
// is [Time.Weekly].[2003].[Week 4], then the current member
64
// of the [Time.Monthly] hierarchy is its default member,
65
// [Time.Monthy].[All].
66
if (member.getHierarchy() != hierarchy) {
67                 member = hierarchy.getDefaultMember();
68             }
69             return member;
70         }
71
72         public boolean dependsOn(Dimension dimension) {
73             return hierarchyCalc.getType().usesDimension(dimension, true);
74         }
75     }
76 }
77
78 // End HierarchyCurrentMemberFunDef.java
79
Popular Tags