KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > udf > CurrentDateMemberUdf


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/udf/CurrentDateMemberUdf.java#6 $
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 and others
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.udf;
11
12 import mondrian.olap.*;
13 import mondrian.olap.type.*;
14 import mondrian.spi.UserDefinedFunction;
15 import mondrian.util.*;
16
17 import java.util.*;
18
19 /**
20  * User-defined function <code>CurrentDateMember</code>. Arguments to the
21  * function are as follows:
22  *
23  * <blockquote>
24  * <code>
25  * CurrentDataMember(&lt;Hierarchy&gt;, &lt;FormatString&gt;[, &lt;Find&gt;)
26  * returns &lt;Member&gt;
27  * </code>
28  * </blockquote>
29  *
30  * The function returns the member from the specified hierarchy that matches
31  * the current date, to the granularity specified by the &lt;FormatString&gt;.
32  *
33  * The format string conforms to the format string implemented by
34  * {@link Format}.
35  *
36  * @author Zelaine Fong
37  * @version $Id: //open/mondrian/src/main/mondrian/udf/CurrentDateMemberUdf.java#6 $
38  */

39 public class CurrentDateMemberUdf implements UserDefinedFunction {
40
41     public Object JavaDoc execute(Evaluator evaluator, Argument[] arguments) {
42
43         // determine the current date
44
Object JavaDoc formatArg = arguments[1].evaluateScalar(evaluator);
45
46         final Locale locale = Locale.getDefault();
47         final Format format = new Format((String JavaDoc) formatArg, locale);
48         Date currDate = new Date();
49         String JavaDoc currDateStr = format.format(currDate);
50
51         // determine the match type
52
MatchType matchType;
53         if (arguments.length == 3) {
54             String JavaDoc matchStr = arguments[2].evaluateScalar(evaluator).toString();
55             matchType = Enum.valueOf(MatchType.class, matchStr);
56         } else {
57             matchType = MatchType.EXACT;
58         }
59
60         String JavaDoc[] uniqueNames = Util.explode(currDateStr);
61         Object JavaDoc retDate =
62             evaluator.getSchemaReader().getMemberByUniqueName(
63                 uniqueNames, false, matchType);
64         if (retDate != null) {
65             return retDate;
66         }
67
68         // if there is no matching member, return the null member for
69
// the specified dimension/hierarchy
70
Object JavaDoc arg0 = arguments[0].evaluate(evaluator);
71         if (arg0 instanceof Hierarchy) {
72             return ((Hierarchy) arg0).getNullMember();
73         } else {
74             return ((Dimension) arg0).getHierarchy().getNullMember();
75         }
76     }
77
78     public String JavaDoc getDescription() {
79         return "Returns the closest or exact member within the specified dimension corresponding to the current date, in the format specified by the format parameter.";
80     }
81
82     public String JavaDoc getName() {
83         return "CurrentDateMember";
84     }
85
86     public Type[] getParameterTypes() {
87         return new Type[] {
88             new HierarchyType(null, null),
89             new StringType(),
90             new SymbolType()
91         };
92     }
93
94     public String JavaDoc[] getReservedWords() {
95         return new String JavaDoc[] {
96             "EXACT",
97             "BEFORE",
98             "AFTER"
99         };
100     }
101
102     public Type getReturnType(Type[] parameterTypes) {
103         return MemberType.Unknown;
104     }
105
106     public Syntax getSyntax() {
107         return Syntax.Function;
108     }
109
110     private MatchType mapMatchStrToType(String JavaDoc matchStr)
111     {
112         if (matchStr.equals("EXACT")) {
113             return MatchType.EXACT;
114         } else if (matchStr.equals("BEFORE")) {
115             return MatchType.BEFORE;
116         } else {
117             return MatchType.AFTER;
118         }
119     }
120 }
121
122 // End CurrentDateMemberUdf.java
123
Popular Tags