1 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 39 public class CurrentDateMemberUdf implements UserDefinedFunction { 40 41 public Object execute(Evaluator evaluator, Argument[] arguments) { 42 43 Object formatArg = arguments[1].evaluateScalar(evaluator); 45 46 final Locale locale = Locale.getDefault(); 47 final Format format = new Format((String ) formatArg, locale); 48 Date currDate = new Date(); 49 String currDateStr = format.format(currDate); 50 51 MatchType matchType; 53 if (arguments.length == 3) { 54 String matchStr = arguments[2].evaluateScalar(evaluator).toString(); 55 matchType = Enum.valueOf(MatchType.class, matchStr); 56 } else { 57 matchType = MatchType.EXACT; 58 } 59 60 String [] uniqueNames = Util.explode(currDateStr); 61 Object retDate = 62 evaluator.getSchemaReader().getMemberByUniqueName( 63 uniqueNames, false, matchType); 64 if (retDate != null) { 65 return retDate; 66 } 67 68 Object 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 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 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 [] getReservedWords() { 95 return new String [] { 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 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 | Popular Tags |