1 25 package org.objectweb.jonas_ejb.lib; 26 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Stack ; 32 33 import org.objectweb.jonas_ejb.container.TraceEjb; 34 import org.objectweb.jonas_ejb.deployment.api.DeploymentDescEjb2; 35 import org.objectweb.jonas_ejb.deployment.ejbql.ASTAbstractSchemaName; 36 import org.objectweb.jonas_ejb.deployment.ejbql.ASTCollectionMemberDeclaration; 37 import org.objectweb.jonas_ejb.deployment.ejbql.ASTCollectionValuedPathExpression; 38 import org.objectweb.jonas_ejb.deployment.ejbql.ASTEJBQL; 39 import org.objectweb.jonas_ejb.deployment.ejbql.ASTFromClause; 40 import org.objectweb.jonas_ejb.deployment.ejbql.ASTIdentifier; 41 import org.objectweb.jonas_ejb.deployment.ejbql.ASTPath; 42 import org.objectweb.jonas_ejb.deployment.ejbql.ASTRangeVariableDeclaration; 43 import org.objectweb.jonas_ejb.deployment.ejbql.ParseException; 44 import org.objectweb.jonas_ejb.deployment.ejbql.SimpleNode; 45 import org.objectweb.jonas_ejb.deployment.ejbql.ASTSelectClause; 46 import org.objectweb.jonas_ejb.deployment.ejbql.ASTSelectExpression; 47 import org.objectweb.jonas_ejb.deployment.ejbql.ASTAggregateSelectExpression; 48 import org.objectweb.jonas_ejb.deployment.ejbql.ASTIdentificationVariable; 49 import org.objectweb.jonas_ejb.deployment.ejbql.ASTCmpPathExpression; 50 import org.objectweb.jonas_ejb.deployment.ejbql.ASTSingleValuedCmrPathExpression; 51 import org.objectweb.jonas_ejb.deployment.ejbql.ASTSingleValuedPathExpression; 52 import org.objectweb.jonas_ejb.deployment.ejbql.ASTWhereClause; 53 import org.objectweb.jonas_ejb.deployment.ejbql.ASTConditionalExpression; 54 import org.objectweb.jonas_ejb.deployment.ejbql.ASTConditionalTerm; 55 import org.objectweb.jonas_ejb.deployment.ejbql.ASTConditionalFactor; 56 import org.objectweb.jonas_ejb.deployment.ejbql.ASTBetweenExpression; 57 import org.objectweb.jonas_ejb.deployment.ejbql.ASTInExpression; 58 import org.objectweb.jonas_ejb.deployment.ejbql.ASTLikeExpression; 59 import org.objectweb.jonas_ejb.deployment.ejbql.ASTNullComparisonExpression; 60 import org.objectweb.jonas_ejb.deployment.ejbql.ASTEmptyCollectionComparisonExpression; 61 import org.objectweb.jonas_ejb.deployment.ejbql.ASTCollectionMemberExpression; 62 import org.objectweb.jonas_ejb.deployment.ejbql.ASTComparisonExpression; 63 import org.objectweb.jonas_ejb.deployment.ejbql.ASTArithmeticExpression; 64 import org.objectweb.jonas_ejb.deployment.ejbql.ASTArithmeticTerm; 65 import org.objectweb.jonas_ejb.deployment.ejbql.ASTArithmeticFactor; 66 import org.objectweb.jonas_ejb.deployment.ejbql.ASTStringExpression; 67 import org.objectweb.jonas_ejb.deployment.ejbql.ASTDatetimeExpression; 68 import org.objectweb.jonas_ejb.deployment.ejbql.ASTBooleanExpression; 69 import org.objectweb.jonas_ejb.deployment.ejbql.ASTEntityBeanExpression; 70 import org.objectweb.jonas_ejb.deployment.ejbql.ASTFunctionsReturningStrings; 71 import org.objectweb.jonas_ejb.deployment.ejbql.ASTFunctionsReturningNumerics; 72 import org.objectweb.jonas_ejb.deployment.ejbql.ASTOrderByClause; 73 import org.objectweb.jonas_ejb.deployment.ejbql.ASTOrderByItem; 74 import org.objectweb.jorm.metainfo.api.Class; 75 import org.objectweb.jorm.metainfo.api.Manager; 76 import org.objectweb.medor.api.Field; 77 import org.objectweb.medor.api.MedorException; 78 import org.objectweb.medor.query.api.QueryTree; 79 import org.objectweb.medor.query.api.QueryTreeField; 80 import org.objectweb.medor.query.jorm.lib.ClassExtent; 81 import org.objectweb.medor.query.jorm.lib.PNameField; 82 import org.objectweb.medor.query.jorm.lib.QueryBuilder; 83 import org.objectweb.medor.query.lib.QueryTreePrinter; 84 import org.objectweb.medor.filter.lib.BasicFieldOperand; 85 import org.objectweb.util.monolog.api.BasicLevel; 86 87 110 public class EjbqlVariableVisitor extends EjbqlAbstractVisitor { 111 112 115 private Manager manager; 116 117 120 private DeploymentDescEjb2 dd; 121 122 125 private HashMap ids = new HashMap (); 126 127 130 private HashMap fields = new HashMap (); 131 132 private QueryBuilder qb = new QueryBuilder(); 133 134 137 private HashSet collections = new HashSet (); 138 139 146 147 public EjbqlVariableVisitor(ASTEJBQL ejbql, DeploymentDescEjb2 dd, QueryBuilder qb) throws Exception { 148 this.dd = dd; 149 this.qb = qb; 150 manager = dd.getJormManager(); 151 152 visit(ejbql); 154 155 for (Iterator it = ids.keySet().iterator(); it.hasNext(); ) { 156 String id = (String ) it.next(); 157 IdValue idv = (IdValue) ids.get(id); 158 fields.put(id + "." + Field.PNAMENAME, qb.project(define(id))); 159 for (int i = 0; i < idv.getDeclaredPathLength(); i++) { 160 String path = idv.getMergedPath(i); 161 if (!collections.contains(path)) { 162 fields.put(path, qb.project(define(path))); 163 } 164 } 165 } 166 } 167 168 private QueryTreeField define(String id) throws ParseException, MedorException { 169 String [] path = splitPath(id); 170 171 if (!qb.contains(path[0])) { 172 IdValue idv = (IdValue) ids.get(path[0]); 173 String [] name = idv.getName(); 174 175 PNameField pnf; 176 if (name.length == 1) { 177 pnf = extent(name[0], path[0]); 178 } else { 179 pnf = (PNameField) define(mergePath(name)); 180 } 181 qb.define(path[0], pnf); 182 } 183 184 return qb.navigate(path); 185 } 186 187 private PNameField extent(String schema, String alias) throws ParseException, MedorException { 188 if (schema == null) { throw new NullPointerException ("schema"); } 189 String cn = dd.asn2BeanDesc(schema).getJormClassName(); 190 Class theClass = manager.getClass(cn); 191 if (theClass == null) { 192 throw new ParseException 193 ("Abstract schema name \"" + schema + 194 "\" has not been declared in the jorm meta information"); 195 } 196 ClassExtent ext = new ClassExtent(theClass, alias, Field.PNAMENAME, false); 197 return (PNameField) ext.getField(ext.getPNameFieldName()); 198 } 199 200 206 public Map getFields() { 207 return fields; 208 } 209 210 214 public void traceIds(HashMap idsM) { 215 if (TraceEjb.isDebugQuery()) { 216 TraceEjb.query.log(BasicLevel.DEBUG, "Begin of IDS structure:"); 217 for (Iterator i = idsM.keySet().iterator(); i.hasNext();) { 219 String id = (String ) i.next(); 220 IdValue idv = (IdValue) idsM.get(id); 221 TraceEjb.query.log(BasicLevel.DEBUG, "ids[" + id + "]=" + idv); 222 if (idv.getQueryTree() != null) { 223 QueryTreePrinter.printQueryTree(idv.getQueryTree(), TraceEjb.query, BasicLevel.DEBUG); 224 } 225 } 226 TraceEjb.query.log(BasicLevel.DEBUG, "End of IDS structure:"); 227 } 228 } 229 230 236 public Object visit(ASTFromClause node, Object data) { 237 return visit((SimpleNode) node, data); 238 } 239 240 246 public Object visit(ASTCollectionMemberDeclaration node, Object data) { 247 return visit((SimpleNode) node, data); 248 } 249 250 256 public Object visit(ASTRangeVariableDeclaration node, Object data) { 257 return visit((SimpleNode) node, data); 258 } 259 260 266 public Object visit(ASTCollectionValuedPathExpression node, Object data) { 267 return visit((SimpleNode) node, data); 268 } 269 270 276 public Object visit(ASTAbstractSchemaName node, Object data) { 277 ((Stack ) data).push(node.value); 278 return null; 279 } 280 281 287 public Object visit(ASTIdentifier node, Object data) { 288 String id = (String ) node.value; 289 String name = (String ) ((Stack ) data).pop(); 290 IdValue iv = (IdValue) ids.get(id); 291 if (iv == null) { 292 iv = new IdValue(name); 293 ids.put(id, iv); 294 } else { 295 iv.setName(splitPath(name)); 296 } 297 return null; 298 } 299 300 306 public Object visit(ASTPath node, Object data) { 307 ((Stack ) data).push(node.value); 308 return null; 309 } 310 311 315 private void processVariable(String path) { 316 String id = splitPath(path)[0]; 318 IdValue iv = (IdValue) ids.get(id); 319 if (iv == null) { 320 iv = new IdValue(); 321 ids.put(id, iv); 322 } 323 iv.addPath(path); 324 } 325 326 333 public Object visit(ASTSelectClause node, Object data) { 334 visit((SimpleNode) node, data); 335 Stack st = (Stack ) data; 336 if (!st.empty()) { 337 processVariable((String ) st.pop()); 338 } 339 return null; 340 } 341 342 348 public Object visit(ASTSelectExpression node, Object data) { 349 return visit((SimpleNode) node, data); 350 } 351 352 358 public Object visit(ASTAggregateSelectExpression node, Object data) { 359 return visit((SimpleNode) node, data); 360 } 361 362 368 public Object visit(ASTIdentificationVariable node, Object data) { 369 return visit((SimpleNode) node, data); 370 } 371 372 378 public Object visit(ASTSingleValuedPathExpression node, Object data) { 379 return visit((SimpleNode) node, data); 380 } 381 382 388 public Object visit(ASTSingleValuedCmrPathExpression node, Object data) { 389 return visit((SimpleNode) node, data); 390 } 391 392 398 public Object visit(ASTCmpPathExpression node, Object data) { 399 return visit((SimpleNode) node, data); 400 } 401 402 409 public Object visit(ASTWhereClause node, Object data) { 410 visit((SimpleNode) node, data); 411 Stack st = (Stack ) data; 412 while (!st.empty()) { 413 processVariable((String ) st.pop()); 414 } 415 return null; 416 } 417 418 424 public Object visit(ASTConditionalExpression node, Object data) { 425 return visit((SimpleNode) node, data); 426 } 427 428 434 public Object visit(ASTConditionalTerm node, Object data) { 435 return visit((SimpleNode) node, data); 436 } 437 438 444 public Object visit(ASTConditionalFactor node, Object data) { 445 return visit((SimpleNode) node, data); 446 } 447 448 454 public Object visit(ASTBetweenExpression node, Object data) { 455 return visit((SimpleNode) node, data); 456 } 457 458 464 public Object visit(ASTInExpression node, Object data) { 465 return visit((SimpleNode) node, data); 466 } 467 468 474 public Object visit(ASTLikeExpression node, Object data) { 475 return visit((SimpleNode) node, data); 476 } 477 478 484 public Object visit(ASTNullComparisonExpression node, Object data) { 485 return visit((SimpleNode) node, data); 486 } 487 488 494 public Object visit(ASTEmptyCollectionComparisonExpression node, Object data) { 495 visit((SimpleNode) node, data); 496 Stack st = (Stack ) data; 497 String path = (String ) st.peek(); 498 collections.add(path); 499 return null; 500 } 501 502 508 public Object visit(ASTCollectionMemberExpression node, Object data) { 509 visit((SimpleNode) node, data); 510 Stack st = (Stack ) data; 511 String path = (String ) st.peek(); 512 collections.add(path); 513 return null; 514 } 515 516 522 public Object visit(ASTComparisonExpression node, Object data) { 523 return visit((SimpleNode) node, data); 524 } 525 526 532 public Object visit(ASTArithmeticExpression node, Object data) { 533 return visit((SimpleNode) node, data); 534 } 535 536 542 public Object visit(ASTArithmeticTerm node, Object data) { 543 return visit((SimpleNode) node, data); 544 } 545 546 552 public Object visit(ASTArithmeticFactor node, Object data) { 553 return visit((SimpleNode) node, data); 554 } 555 556 562 public Object visit(ASTStringExpression node, Object data) { 563 return visit((SimpleNode) node, data); 564 } 565 566 572 public Object visit(ASTDatetimeExpression node, Object data) { 573 return visit((SimpleNode) node, data); 574 } 575 576 582 public Object visit(ASTBooleanExpression node, Object data) { 583 return visit((SimpleNode) node, data); 584 } 585 586 592 public Object visit(ASTEntityBeanExpression node, Object data) { 593 return visit((SimpleNode) node, data); 594 } 595 596 602 public Object visit(ASTFunctionsReturningStrings node, Object data) { 603 return visit((SimpleNode) node, data); 604 } 605 606 612 public Object visit(ASTFunctionsReturningNumerics node, Object data) { 613 return visit((SimpleNode) node, data); 614 } 615 616 623 public Object visit(ASTOrderByClause node, Object data) { 624 visit((SimpleNode) node, data); 625 Stack st = (Stack ) data; 626 while (!st.empty()) { 627 processVariable((String ) st.pop()); 628 } 629 return null; 630 } 631 632 638 public Object visit(ASTOrderByItem node, Object data) { 639 return visit((SimpleNode) node, data); 640 } 641 642 } 643 | Popular Tags |