KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > mdxparse > FunCall


1 /*
2  * ====================================================================
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) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.olap.mdxparse;
14
15 import org.apache.log4j.Logger;
16
17 /**
18  * @author hh
19  *
20  */

21 public class FunCall implements Exp {
22
23   static Logger logger = Logger.getLogger(FunCall.class);
24
25   // Syntactip types
26
public static final int TypeFunction = 0;
27   public static final int TypeProperty = 1; // ???
28
public static final int TypeMethod = 2;
29   public static final int TypeInfix = 3;
30   public static final int TypePrefix = 4;
31   public static final int TypeBraces = 5;
32   public static final int TypeParentheses = 6;
33   public static final int TypeCase = 7;
34   public static final int TypeMask = 0xFF;
35   public static final int TypePropertyQuoted = TypeProperty | 0x100;
36   public static final int TypePropertyAmpQuoted = TypeProperty | 0x200;
37
38   private int syntacticType;
39   private String JavaDoc function;
40   private Exp[] args;
41
42   ParsedQuery pQuery = null; // needed by Parameter FunCall
43

44   public FunCall(String JavaDoc fun, Exp[] args) {
45     this(fun, args, TypeFunction);
46   }
47
48   public FunCall(String JavaDoc fun, Exp[] args, int syntacticType) {
49     this.function = fun;
50     this.args = args;
51     this.syntacticType = syntacticType;
52   }
53
54   /**
55    * format to MDX
56    */

57   public String JavaDoc toMdx() {
58     if (this.isCallTo("Parameter") || this.isCallTo("ParamRef")) {
59       // parameters are evaluated to MDX
60
return evaluateParameter();
61     }
62
63     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
64
65     // "+" instead of Union yields much better readable MDX
66
// however
67
// - does not work with SAP
68
// - is not compatible with Mondrian
69
/* sorry
70     if (this.isCallTo("Union")) {
71       // render Union as "+"
72       sb.append(args[0].toMdx());
73       sb.append(" + ");
74       sb.append(args[1].toMdx());
75       return sb.toString();
76     }
77     */

78     
79     boolean isFollow = false;
80
81     switch (syntacticType) {
82       case TypeFunction : // f(a, b, c)
83
sb.append(function);
84         sb.append("(");
85         for (int i = 0; i < args.length; i++) {
86           if (isFollow)
87             sb.append(", ");
88           isFollow = true;
89           sb.append(args[i].toMdx());
90         }
91         sb.append(")");
92         break;
93
94       case TypeBraces : // { a, b, c }
95
sb.append("{");
96         for (int i = 0; i < args.length; i++) {
97           if (isFollow)
98             sb.append(", ");
99           isFollow = true;
100           sb.append(args[i].toMdx());
101         }
102         sb.append("}");
103         break;
104
105       case TypeParentheses : // (a, b, c)
106
sb.append("(");
107         for (int i = 0; i < args.length; i++) {
108           if (isFollow)
109             sb.append(", ");
110           isFollow = true;
111           sb.append(args[i].toMdx());
112         }
113         sb.append(")");
114         break;
115
116       case TypePrefix : // NOT a
117
sb.append(function);
118         sb.append(" ");
119         sb.append(args[0].toMdx());
120         break;
121
122       case TypeInfix : // a + b
123
sb.append(args[0].toMdx());
124         sb.append(" ");
125         sb.append(function);
126         sb.append(" ");
127         sb.append(args[1].toMdx());
128         break;
129
130       case TypeProperty : // a.b
131
case TypePropertyQuoted :
132       case TypePropertyAmpQuoted :
133         sb.append(args[0].toMdx());
134         sb.append(".");
135         sb.append(function);
136         break;
137
138       default :
139         System.out.println("unexpected FunCall syntatic type");
140
141     }
142     return sb.toString();
143   }
144
145   /**
146    *
147    * @see java.lang.Object#clone()
148    */

149   public Object JavaDoc clone() {
150     Exp[] cloneArgs = new Exp[args.length];
151     for (int i = 0; i < cloneArgs.length; i++) {
152       cloneArgs[i] = (Exp) args[i].clone();
153     }
154     return new FunCall(function, cloneArgs, syntacticType);
155   }
156
157   /**
158    * compare function name ignoring case
159    * @param fName
160    * @return boolean
161    */

162   public boolean isCallTo(String JavaDoc fName) {
163     return (fName.compareToIgnoreCase(function) == 0);
164   }
165
166   /**
167    * Returns the args.
168    * @return Exp[]
169    */

170   public Exp[] getArgs() {
171     return args;
172   }
173
174   /**
175    * @return
176    */

177   public String JavaDoc getFunction() {
178     return function;
179   }
180
181   /**
182    * put parameter value to MDX
183    */

184   private String JavaDoc evaluateParameter() {
185     Literal eName = (Literal) args[0];
186     String JavaDoc paraName = eName.stringValue();
187     Parameter param = (Parameter) pQuery.paraMap.get(paraName.toUpperCase());
188     if (param == null) {
189       // should not occur
190
logger.error("could not find parameter " + paraName);
191       return ("Parameter( \"" + paraName + "\" )"); // MDX parse will fail here
192
}
193     int type = param.getType();
194     if (type == Parameter.TYPE_NUMERIC) {
195       Object JavaDoc value = param.getOValue();
196       return value.toString(); // Integer or double
197
} else if (type == Parameter.TYPE_STRING) {
198       String JavaDoc str = (String JavaDoc) param.getOValue();
199       return "\"" + str + "\"";
200     } else {
201       // member assumed
202
String JavaDoc str = (String JavaDoc) param.getOValue();
203       return str;
204
205     }
206
207   }
208
209   /**
210    * @see com.tonbeller.jpivot.olap.mdxparse.Exp#accept
211    */

212   public void accept(ExpVisitor visitor) {
213     visitor.visitFunCall(this);
214   }
215
216 } // End FunCall
217
Popular Tags