1 13 package com.tonbeller.jpivot.olap.mdxparse; 14 15 import java.util.ArrayList ; 16 import java.util.Hashtable ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.apache.log4j.Logger; 22 23 import com.tonbeller.jpivot.olap.model.OlapException; 24 25 28 public class ParsedQuery implements Exp { 29 30 static Logger logger = Logger.getLogger(ParsedQuery.class); 31 32 List formulas = new ArrayList (); 33 Map paraMap = new Hashtable (); 34 36 ArrayList axisDef = null; 37 String cube; 38 List cellProps = new ArrayList (); 39 40 Exp slicer = null; 41 42 private QueryAxis[] axes = new QueryAxis[0]; 43 44 48 public void afterParse() throws OlapException { 49 if (axisDef != null) { 50 axes = (QueryAxis[]) axisDef.toArray(new QueryAxis[0]); 51 axisDef = null; 52 } 53 collectParams(); 55 } 56 57 61 public QueryAxis[] getAxes() { 62 return this.axes; 63 } 64 65 69 public void setAxes(QueryAxis[] axes) { 70 this.axes = axes; 71 } 72 73 77 public String getCube() { 78 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 89 public void setCube(String cube) { 90 this.cube = cube; 91 } 92 93 97 public Formula[] getFormulas() { 98 return (Formula[]) formulas.toArray(new Formula[0]); 99 } 100 101 public String toMdx() { 102 StringBuffer mdx = new StringBuffer (); 103 boolean isFollow; 104 if (formulas.size() > 0) { 105 mdx.append("WITH "); 106 for (Iterator 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 if (cellProps.size() > 0) { 132 mdx.append(" CELL PROPERTIES VALUE, FORMATTED_VALUE"); 133 for (Iterator iter = cellProps.iterator(); iter.hasNext();) { 134 CompoundId cid = (CompoundId) iter.next(); 135 String str = cid.toMdx(); 136 if (str.equalsIgnoreCase("VALUE")) 137 continue; if (str.equalsIgnoreCase("FORMATTED_VALUE")) 139 continue; mdx.append(" ,"); 141 mdx.append(str); 142 } 143 } 144 return mdx.toString(); 145 } 146 147 148 public String toDrillMdx() { 149 StringBuffer mdx = new StringBuffer (); 150 boolean isFollow; 151 if (formulas.size() > 0) { 152 mdx.append("WITH "); 153 for (Iterator 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 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 if (cellProps.size() > 0) { 181 mdx.append(" CELL PROPERTIES VALUE, FORMATTED_VALUE"); 182 for (Iterator iter = cellProps.iterator(); iter.hasNext();) { 183 CompoundId cid = (CompoundId) iter.next(); 184 String str = cid.toMdx(); 185 if (str.equalsIgnoreCase("VALUE")) 186 continue; if (str.equalsIgnoreCase("FORMATTED_VALUE")) 188 continue; mdx.append(" ,"); 190 mdx.append(str); 191 } 192 } 193 return mdx.toString(); 194 } 195 196 197 198 199 203 public Object clone() { 204 ParsedQuery cloned = new ParsedQuery(); 205 if (formulas.size() > 0) { 206 ArrayList clonedFormulas = new ArrayList (); 207 for (Iterator 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 cloned.setCube("[" + this.getCube() + "]"); 226 return cloned; 227 } 228 229 232 public Exp getSlicer() { 233 return slicer; 234 } 235 236 240 public void setSlicer(Exp exp) { 241 slicer = exp; 242 } 243 244 247 private void collectParams() throws OlapException { 248 249 int iAxis = -2; 251 for (Iterator iter = formulas.iterator(); iter.hasNext();) { 252 Formula formula = (Formula) iter.next(); 253 walkTreeForParams(formula.getExp(), iAxis); 254 } 255 256 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 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 paraName = eName.stringValue(); 285 if (paraMap.containsKey(paraName.toUpperCase())) { 288 String err = "Parameter defined more than once:" + paraName; 290 logger.error(err); 291 throw new OlapException(err); 292 } else { 293 int type; 295 Object value = null; 296 f.pQuery = this; 297 CompoundId id = (CompoundId) f.getArgs()[1]; 299 String [] ids = id.toStringArray(); 300 if (ids.length > 1) { 301 throw new OlapException("Syntax Error in Parameter definition - 2.argument"); 303 } 304 if (ids[0].equalsIgnoreCase("NUMERIC")) { 305 type = Parameter.TYPE_NUMERIC; 306 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 type = Parameter.TYPE_MEMBER; 320 CompoundId val = (CompoundId) f.getArgs()[2]; 321 value = val.toMdx(); } 323 Parameter par = new Parameter(paraName, type, iAxis); 324 par.setOValue(value); 325 if (nArgs == 4) { 326 Literal desc = (Literal) f.getArgs()[3]; 328 String description = (String ) 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 for (int i = 0; i < f.getArgs().length; i++) { 339 walkTreeForParams(f.getArgs()[i], iAxis); 340 } 341 342 } 343 344 347 public Map getParaMap() { 348 return paraMap; 349 } 350 351 354 public void addFormula(String [] names, Exp exp, MemberProperty[] memberProperties) { 355 Formula newFormula = new Formula(names, exp, memberProperties); 356 formulas.add(newFormula); 357 } 358 359 362 public void addFormula(String [] names, Exp exp) { 363 Formula newFormula = new Formula(names, exp); 364 formulas.add(newFormula); 365 } 366 367 370 public void removeFormula(String uniqueName) { 371 for (Iterator iter = formulas.iterator(); iter.hasNext();) { 372 Formula formula = (Formula) iter.next(); 373 if (uniqueName.equals(formula.getUniqeName())) 374 iter.remove(); 375 } 376 } 377 378 381 public void accept(ExpVisitor visitor) { 382 visitor.visitParsedQuery(this); 383 } 384 385 388 public List getCellProps() { 389 return cellProps; 390 } 391 392 } | Popular Tags |