KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.log4j.Logger;
22
23 import com.tonbeller.jpivot.olap.model.OlapException;
24
25 /**
26  * this is the result of parsing an MDX
27  */

28 public class ParsedQuery implements Exp {
29
30   static Logger logger = Logger.getLogger(ParsedQuery.class);
31
32   List JavaDoc formulas = new ArrayList JavaDoc();
33   Map JavaDoc paraMap = new Hashtable JavaDoc();
34   //List paramRefs = new ArrayList();
35

36   ArrayList JavaDoc axisDef = null;
37   String JavaDoc cube;
38   List JavaDoc cellProps = new ArrayList JavaDoc();
39
40   Exp slicer = null;
41
42   private QueryAxis[] axes = new QueryAxis[0];
43
44   /**
45    * called after parse to make things easier
46    *
47    */

48   public void afterParse() throws OlapException {
49     if (axisDef != null) {
50       axes = (QueryAxis[]) axisDef.toArray(new QueryAxis[0]);
51       axisDef = null;
52     }
53     // collect Parameters
54
collectParams();
55   }
56
57   /**
58    *
59    * @return QueryAxis[]
60    */

61   public QueryAxis[] getAxes() {
62     return this.axes;
63   }
64
65   /**
66    *
67    * @param axes
68    */

69   public void setAxes(QueryAxis[] axes) {
70     this.axes = axes;
71   }
72
73   /**
74    * Returns the cube.
75    * @return String
76    */

77   public String JavaDoc getCube() {
78     // remove enclosing brackets, if there
79
if (cube.charAt(0) == '[' && cube.charAt(cube.length() - 1) == ']')
80       return cube.substring(1, cube.length() - 1);
81     else
82       return cube;
83   }
84
85   /**
86    * Sets the cube.
87    * @param cube The cube to set
88    */

89   public void setCube(String JavaDoc cube) {
90     this.cube = cube;
91   }
92
93   /**
94    * get the formulas of this query
95    * @return Formula[]
96    */

97   public Formula[] getFormulas() {
98     return (Formula[]) formulas.toArray(new Formula[0]);
99   }
100
101   public String JavaDoc toMdx() {
102     StringBuffer JavaDoc mdx = new StringBuffer JavaDoc();
103     boolean isFollow;
104     if (formulas.size() > 0) {
105       mdx.append("WITH ");
106       for (Iterator JavaDoc iter = formulas.iterator(); iter.hasNext();) {
107         mdx.append(' ');
108         Formula form = (Formula) iter.next();
109         mdx.append(form.toMdx());
110       }
111       mdx.append(' ');
112     }
113     mdx.append("SELECT ");
114     isFollow = false;
115     for (int i = 0; i < axes.length; i++) {
116       QueryAxis qa = axes[i];
117       if (isFollow)
118         mdx.append(", ");
119       isFollow = true;
120       mdx.append(qa.toMdx());
121     }
122     mdx.append(" FROM ");
123     mdx.append(cube);
124
125     if (slicer != null) {
126       mdx.append(" WHERE ");
127       mdx.append(slicer.toMdx());
128     }
129
130     // add CELL PROPERTIES VALUE, FORMATTED_VALUE, ...
131
if (cellProps.size() > 0) {
132       mdx.append(" CELL PROPERTIES VALUE, FORMATTED_VALUE");
133       for (Iterator JavaDoc iter = cellProps.iterator(); iter.hasNext();) {
134         CompoundId cid = (CompoundId) iter.next();
135         String JavaDoc str = cid.toMdx();
136         if (str.equalsIgnoreCase("VALUE"))
137           continue; // default
138
if (str.equalsIgnoreCase("FORMATTED_VALUE"))
139           continue; // default
140
mdx.append(" ,");
141         mdx.append(str);
142       }
143     }
144     return mdx.toString();
145   }
146
147
148   public String JavaDoc toDrillMdx() {
149     StringBuffer JavaDoc mdx = new StringBuffer JavaDoc();
150     boolean isFollow;
151     if (formulas.size() > 0) {
152       mdx.append("WITH ");
153       for (Iterator JavaDoc iter = formulas.iterator(); iter.hasNext();) {
154         mdx.append(' ');
155         Formula form = (Formula) iter.next();
156         mdx.append(form.toMdx());
157       }
158       mdx.append(' ');
159     }
160     // dsf
161
mdx.append("DRILLTHROUGH SELECT ");
162
163     isFollow = false;
164     for (int i = 0; i < axes.length; i++) {
165       QueryAxis qa = axes[i];
166       if (isFollow)
167         mdx.append(", ");
168       isFollow = true;
169       mdx.append(qa.toMdx());
170     }
171     mdx.append(" FROM ");
172     mdx.append(cube);
173
174     if (slicer != null) {
175       mdx.append(" WHERE ");
176       mdx.append(slicer.toMdx());
177     }
178
179     // add CELL PROPERTIES VALUE, FORMATTED_VALUE, ...
180
if (cellProps.size() > 0) {
181       mdx.append(" CELL PROPERTIES VALUE, FORMATTED_VALUE");
182       for (Iterator JavaDoc iter = cellProps.iterator(); iter.hasNext();) {
183         CompoundId cid = (CompoundId) iter.next();
184         String JavaDoc str = cid.toMdx();
185         if (str.equalsIgnoreCase("VALUE"))
186           continue; // default
187
if (str.equalsIgnoreCase("FORMATTED_VALUE"))
188           continue; // default
189
mdx.append(" ,");
190         mdx.append(str);
191       }
192     }
193     return mdx.toString();
194   }
195
196   
197   
198   
199   /**
200    *
201    * @see java.lang.Object#clone()
202    */

203   public Object JavaDoc clone() {
204     ParsedQuery cloned = new ParsedQuery();
205     if (formulas.size() > 0) {
206       ArrayList JavaDoc clonedFormulas = new ArrayList JavaDoc();
207       for (Iterator JavaDoc iter = formulas.iterator(); iter.hasNext();) {
208         Formula form = (Formula) iter.next();
209         clonedFormulas.add(form.clone());
210       }
211       cloned.formulas = clonedFormulas;
212     }
213     if (axes.length > 0) {
214       QueryAxis[] clonedAxes = new QueryAxis[axes.length];
215       for (int i = 0; i < clonedAxes.length; i++) {
216         clonedAxes[i] = (QueryAxis) axes[i].clone();
217       }
218       cloned.setAxes(clonedAxes);
219     }
220     if (slicer != null)
221       cloned.slicer = (Exp) this.slicer.clone();
222     
223     // you need to wrap the cube in backets again!
224
// Why does getCube remove the brackets?
225
cloned.setCube("[" + this.getCube() + "]");
226     return cloned;
227   }
228
229   /**
230    * @return sliecer exp
231    */

232   public Exp getSlicer() {
233     return slicer;
234   }
235
236   /**
237    * set the slicer exp
238    * @param exp
239    */

240   public void setSlicer(Exp exp) {
241     slicer = exp;
242   }
243
244   /**
245    * Collect Parameters and Parameter references
246    */

247   private void collectParams() throws OlapException {
248
249     // walk Formulas
250
int iAxis = -2;
251     for (Iterator JavaDoc iter = formulas.iterator(); iter.hasNext();) {
252       Formula formula = (Formula) iter.next();
253       walkTreeForParams(formula.getExp(), iAxis);
254     }
255
256     // walk axes
257
for (int i = 0; i < axes.length; i++) {
258       Exp exp = axes[i].getExp();
259       iAxis = i;
260       walkTreeForParams(exp, iAxis);
261     }
262
263   }
264
265   /**
266    * Collect Parameters and Parameter references
267    * in a subtree below (and including) exp
268    * visitor ???
269    */

270   private void walkTreeForParams(Exp exp, int iAxis) throws OlapException {
271     if (!(exp instanceof FunCall))
272       return;
273     FunCall f = (FunCall) exp;
274     if (f.isCallTo("Parameter")) {
275       int nArgs = f.getArgs().length;
276       if (nArgs != 3 && nArgs != 4) {
277         throw new OlapException("Syntax Error in Parameter: wrong number of arguments");
278       }
279       if (!(f.getArgs()[0] instanceof Literal)) {
280         throw new OlapException("Syntax Error in Parameter definition - 1.argument");
281       }
282
283       Literal eName = (Literal) f.getArgs()[0];
284       String JavaDoc paraName = eName.stringValue();
285       // Found a parameter
286
// if it is already there, throw an error
287
if (paraMap.containsKey(paraName.toUpperCase())) {
288         // already there, error
289
String JavaDoc err = "Parameter defined more than once:" + paraName;
290         logger.error(err);
291         throw new OlapException(err);
292       } else {
293         // analyze and add the Parameter
294
int type;
295         Object JavaDoc value = null;
296         f.pQuery = this;
297         // second Parameter must be ID, either a hierarchy or NUMERIC or STRING
298
CompoundId id = (CompoundId) f.getArgs()[1];
299         String JavaDoc[] ids = id.toStringArray();
300         if (ids.length > 1) {
301           // error, must be NUMERIC or STRING or name of an hierarchy
302
throw new OlapException("Syntax Error in Parameter definition - 2.argument");
303         }
304         if (ids[0].equalsIgnoreCase("NUMERIC")) {
305           type = Parameter.TYPE_NUMERIC;
306           // 3.argument (value) is literal
307
if (!(f.getArgs()[0] instanceof Literal)) {
308             throw new OlapException("Syntax Error in Parameter definition - 3.argument");
309           }
310           Literal val = (Literal) f.getArgs()[2];
311           value = val.getValueObject();
312
313         } else if (ids[0].equalsIgnoreCase("STRING")) {
314           type = Parameter.TYPE_STRING;
315           Literal val = (Literal) f.getArgs()[2];
316           value = val.getValueObject();
317         } else {
318           // hierarchy expected
319
type = Parameter.TYPE_MEMBER;
320           CompoundId val = (CompoundId) f.getArgs()[2];
321           value = val.toMdx(); // member String
322
}
323         Parameter par = new Parameter(paraName, type, iAxis);
324         par.setOValue(value);
325         if (nArgs == 4) {
326           // set description
327
Literal desc = (Literal) f.getArgs()[3];
328           String JavaDoc description = (String JavaDoc) desc.getValueObject();
329           par.setDescription(description);
330         }
331         paraMap.put(paraName.toUpperCase(), par);
332       }
333     } else if (f.isCallTo("ParamRef")) {
334       f.pQuery = this;
335     }
336
337     // recurse down on params
338
for (int i = 0; i < f.getArgs().length; i++) {
339       walkTreeForParams(f.getArgs()[i], iAxis);
340     }
341
342   }
343
344   /**
345    * @return parameter map
346    */

347   public Map JavaDoc getParaMap() {
348     return paraMap;
349   }
350
351   /**
352    * add a formula for a member
353    */

354   public void addFormula(String JavaDoc[] names, Exp exp, MemberProperty[] memberProperties) {
355     Formula newFormula = new Formula(names, exp, memberProperties);
356     formulas.add(newFormula);
357   }
358
359   /**
360    * add a formula for a set
361    */

362   public void addFormula(String JavaDoc[] names, Exp exp) {
363     Formula newFormula = new Formula(names, exp);
364     formulas.add(newFormula);
365   }
366
367   /**
368    * remove a formula
369    */

370   public void removeFormula(String JavaDoc uniqueName) {
371     for (Iterator JavaDoc iter = formulas.iterator(); iter.hasNext();) {
372       Formula formula = (Formula) iter.next();
373       if (uniqueName.equals(formula.getUniqeName()))
374         iter.remove();
375     }
376   }
377
378   /**
379    * @see com.tonbeller.jpivot.olap.mdxparse.Exp#accept
380    */

381   public void accept(ExpVisitor visitor) {
382     visitor.visitParsedQuery(this);
383   }
384
385   /**
386    * @return
387    */

388   public List JavaDoc getCellProps() {
389     return cellProps;
390   }
391
392 } // End ParsedQuery
393
Popular Tags