KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/XtdFunDef.java#11 $
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) 2005-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.olap.*;
13 import mondrian.olap.type.Type;
14 import mondrian.olap.type.SetType;
15 import mondrian.olap.type.MemberType;
16 import mondrian.resource.MondrianResource;
17 import mondrian.calc.*;
18 import mondrian.calc.impl.AbstractListCalc;
19 import mondrian.mdx.ResolvedFunCall;
20
21 import java.util.List JavaDoc;
22
23 /**
24  * Definition of <code>Ytd</code>, <code>Qtd</code>, <code>Mtd</code>,
25  * and <code>Wtd</code> MDX builtin functions.
26  *
27  * @author jhyde
28  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/XtdFunDef.java#11 $
29  * @since Mar 23, 2006
30  */

31 class XtdFunDef extends FunDefBase {
32     private final LevelType levelType;
33
34     static final ResolverImpl MtdResolver = new ResolverImpl(
35             "Mtd",
36             "Mtd([<Member>])",
37             "A shortcut function for the PeriodsToDate function that specifies the level to be Month.",
38             new String JavaDoc[]{"fx", "fxm"},
39             LevelType.TimeMonths);
40
41     static final ResolverImpl QtdResolver = new ResolverImpl(
42             "Qtd",
43             "Qtd([<Member>])",
44             "A shortcut function for the PeriodsToDate function that specifies the level to be Quarter.",
45             new String JavaDoc[]{"fx", "fxm"},
46             LevelType.TimeQuarters);
47
48     static final ResolverImpl WtdResolver = new ResolverImpl(
49             "Wtd",
50             "Wtd([<Member>])",
51             "A shortcut function for the PeriodsToDate function that specifies the level to be Week.",
52             new String JavaDoc[]{"fx", "fxm"},
53             LevelType.TimeWeeks);
54
55     static final ResolverImpl YtdResolver = new ResolverImpl(
56             "Ytd",
57             "Ytd([<Member>])",
58             "A shortcut function for the PeriodsToDate function that specifies the level to be Year.",
59             new String JavaDoc[]{"fx", "fxm"},
60             LevelType.TimeYears);
61
62     public XtdFunDef(FunDef dummyFunDef, LevelType levelType) {
63         super(dummyFunDef);
64         this.levelType = levelType;
65     }
66
67     public Type getResultType(Validator validator, Exp[] args) {
68         if (args.length == 0) {
69             // With no args, the default implementation cannot
70
// guess the hierarchy.
71
Hierarchy hierarchy = validator.getQuery()
72                     .getCube().getTimeDimension()
73                     .getHierarchy();
74             return new SetType(MemberType.forHierarchy(hierarchy));
75         }
76         final Type type = args[0].getType();
77         if (type.getHierarchy().getDimension()
78                 .getDimensionType() !=
79                 DimensionType.TimeDimension) {
80             throw MondrianResource.instance().TimeArgNeeded.ex(getName());
81         }
82         return super.getResultType(validator, args);
83     }
84
85     private Level getLevel(Evaluator evaluator) {
86         switch (levelType) {
87         case TimeYears:
88             return evaluator.getCube().getYearLevel();
89         case TimeQuarters:
90             return evaluator.getCube().getQuarterLevel();
91         case TimeMonths:
92             return evaluator.getCube().getMonthLevel();
93         case TimeWeeks:
94             return evaluator.getCube().getWeekLevel();
95         case TimeDays:
96             return evaluator.getCube().getWeekLevel();
97         default:
98             throw Util.badValue(levelType);
99         }
100     }
101
102     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
103         final Level level = getLevel(compiler.getEvaluator());
104         switch (call.getArgCount()) {
105         case 0:
106             return new AbstractListCalc(call, new Calc[0]) {
107                 public List JavaDoc evaluateList(Evaluator evaluator) {
108                     return periodsToDate(evaluator, level, null);
109                 }
110
111                 public boolean dependsOn(Dimension dimension) {
112                     return dimension.getDimensionType() ==
113                             mondrian.olap.DimensionType.TimeDimension;
114                 }
115             };
116         default:
117             final MemberCalc memberCalc =
118                     compiler.compileMember(call.getArg(0));
119             return new AbstractListCalc(call, new Calc[] {memberCalc}) {
120                 public List JavaDoc evaluateList(Evaluator evaluator) {
121                     return periodsToDate(evaluator, level,
122                             memberCalc.evaluateMember(evaluator));
123                 }
124             };
125         }
126     }
127
128     private static class ResolverImpl extends MultiResolver {
129         private final LevelType levelType;
130
131         public ResolverImpl(
132                 String JavaDoc name,
133                 String JavaDoc signature,
134                 String JavaDoc description,
135                 String JavaDoc[] signatures,
136                 LevelType levelType) {
137             super(name, signature, description, signatures);
138             this.levelType = levelType;
139         }
140
141         protected FunDef createFunDef(Exp[] args, FunDef dummyFunDef) {
142             return new XtdFunDef(dummyFunDef, levelType);
143         }
144     };
145 }
146
147 // End XtdFunDef.java
148
Popular Tags