1 27 28 package org.objectweb.speedo.query.lib; 29 30 import org.objectweb.speedo.query.api.CompiledQuery; 31 import org.objectweb.speedo.query.api.QueryDefinition; 32 import org.objectweb.speedo.query.api.QueryManager; 33 import org.objectweb.speedo.query.lib.QueryResultList; 34 import org.objectweb.speedo.pm.api.ProxyManager; 35 import org.objectweb.util.monolog.api.BasicLevel; 36 import org.objectweb.util.monolog.api.Logger; 37 38 import javax.jdo.Extent; 39 import javax.jdo.FetchPlan; 40 import javax.jdo.JDOFatalUserException; 41 import javax.jdo.JDOUserException; 42 import javax.jdo.PersistenceManager; 43 import javax.jdo.Query; 44 import javax.jdo.JDOException; 45 import java.util.Collection ; 46 import java.util.HashMap ; 47 import java.util.Map ; 48 import java.util.StringTokenizer ; 49 import java.util.Vector ; 50 import java.util.List ; 51 import java.util.ArrayList ; 52 53 60 public class SpeedoQuery extends QueryDefinitionImpl implements Query { 61 62 63 67 private QueryManager queryManager = null; 68 69 72 private Logger logger = null; 73 74 private boolean hasChanged = true; 75 76 79 private ProxyManager pm; 80 81 private List results; 82 83 87 private CompiledQuery qc = null; 88 89 96 private FetchPlan fetchPlan; 97 98 public SpeedoQuery() { 99 super(); 100 results = new ArrayList (1); 101 } 102 103 public void setLogger(Logger logger) { 104 this.logger = logger; 105 } 106 107 public QueryManager getQueryManager() { 108 return queryManager; 109 } 110 111 public void setQueryManager(QueryManager _qm) { 112 queryManager = _qm; 113 } 114 115 public CompiledQuery getQueryCompiler() { 116 return qc; 117 } 118 119 public void setQueryCompiler(CompiledQuery _qc) { 120 qc = _qc; 121 } 122 123 public void setProxyManager(ProxyManager _pm) { 124 pm = _pm; 125 } 126 127 130 public void defineWith(QueryDefinition qd) { 131 super.defineWith(qd); 132 hasChanged = true; 133 } 134 142 public void setClass(Class cls) { 143 assertPMIsOpen(); 144 assertModifiable(); 145 if (candidateClass != null) hasChanged = true; 146 candidateClass = cls; 147 assertCandidateClass(); 148 } 149 150 154 public void setCandidates(Extent pcs) { 155 assertPMIsOpen(); 156 if (extentClass != null) hasChanged = true; 157 extentClass = pcs; 158 assertExtentClass(); 159 } 160 161 165 public void setCandidates(Collection pcs) { 166 assertPMIsOpen(); 167 assertModifiable(); 168 if (candidateInstances != null) hasChanged = true; 169 candidateInstances = pcs; 170 assertCandidateInstances(); 171 } 172 173 223 public void setFilter(String f) { 224 assertPMIsOpen(); 225 assertModifiable(); 226 if (f == null || f.length() == 0) { 227 filter = "true"; 228 } else { 229 filter = "(" + f + ")"; 230 hasChanged = true; 231 } 232 } 233 234 245 public void declareImports(String imports) { 246 assertPMIsOpen(); 247 assertModifiable(); 248 if (importStatements != null) hasChanged = true; 249 StringTokenizer tok = new StringTokenizer (imports, ";"); 250 importStatements = new Vector (); 251 while (tok.hasMoreTokens()) 252 importStatements.add(tok.nextToken().trim()); 253 } 254 255 265 public void declareParameters(String parameters) { 266 assertPMIsOpen(); 267 assertModifiable(); 268 if (this.parameters != null) { 269 hasChanged = true; 270 } 271 this.parameters = parameters; 272 assertParameters(); 273 } 274 275 284 public void declareVariables(String variables) { 285 assertPMIsOpen(); 286 assertModifiable(); 287 if (this.variables != null) hasChanged = true; 288 this.variables = variables; 289 assertVariables(); 290 } 291 292 308 public void setOrdering(String ordering) { 309 assertPMIsOpen(); 310 assertModifiable(); 311 if (order != null) hasChanged = true; 312 StringTokenizer tok = new StringTokenizer (ordering, ","); 313 order = new ArrayList (); 314 while (tok.hasMoreTokens()) 315 order.add(tok.nextToken().trim()); 316 } 317 318 330 public void setIgnoreCache(boolean ignoreCache) { 331 assertPMIsOpen(); 332 assertModifiable(); 333 this.ignoreCache = ignoreCache; 334 } 335 336 341 public boolean getIgnoreCache() { 342 assertPMIsOpen(); 343 return ignoreCache; 344 } 345 346 public void setIncludeSubClasses(boolean val) { 347 assertModifiable(); 348 includeSubClasses = val; 349 } 350 351 355 public void compile() { 356 qc = queryManager.getQueryCompiler(this); 359 360 try { 362 qc.compile(); 363 } catch (Exception e) { 364 throw new JDOException("Impossible to compile a query ", e); 365 } 366 hasChanged = false; 367 } 368 369 private void treatSpeedoHints(Map parameterValues) { 370 String speedoHints = (String ) parameterValues.get("SPEEDO_HINTS"); 371 if (speedoHints != null) { 372 treatSpeedoHints(speedoHints); 373 } 374 } 375 376 private void treatSpeedoHints(Object [] parameterValues) { 377 StringTokenizer st = new StringTokenizer (parameters, ",", false); 378 int speedoHintsIndex = -1; 379 int tokenIndex = 0; 380 while(speedoHintsIndex < 0 && st.hasMoreTokens()) { 381 String token = st.nextToken(); 382 if (parameters.indexOf("SPEEDO_HINTS") > -1) { 383 speedoHintsIndex = tokenIndex; 384 } 385 tokenIndex++; 386 } 387 if (speedoHintsIndex >= 0) { 388 treatSpeedoHints((String ) parameterValues[speedoHintsIndex]); 389 } 390 } 391 392 private void treatSpeedoHints(String speedoHints) { 393 StringTokenizer st = new StringTokenizer (speedoHints, ",", false); 394 while(st.hasMoreTokens()) { 395 String token = st.nextToken(); 396 if (token.equalsIgnoreCase("prefetch")) { 397 if (!withPrefetch) { 398 withPrefetch = true; 399 hasChanged = true; 400 } 401 } else if (token.equalsIgnoreCase("noprefetch")) { 402 if (withPrefetch) { 403 withPrefetch = false; 404 hasChanged = true; 405 } 406 408 } 409 } 410 411 } 412 413 418 public Object execute() { 419 Object [] params = new Object [0]; 420 beforeExecute(params); 421 try { 422 Object res = qc.execute(params, pm, this); 423 if (!unique) { 424 results.add(res); 425 } 426 return res; 427 } catch (JDOException e) { 428 throw e; 429 } catch (Exception e) { 430 logger.log(BasicLevel.ERROR, "execute failed", e); 431 throw new JDOException("Impossible to execute a query ", e); 432 } 433 } 434 435 441 public Object execute(Object p1) { 442 Object [] params = new Object []{p1}; 443 beforeExecute(params); 444 try { 445 Object res = qc.execute(params, pm, this); 446 if (!unique) { 447 results.add(res); 448 } 449 return res; 450 } catch (JDOException e) { 451 throw e; 452 } catch (Exception e) { 453 logger.log(BasicLevel.ERROR, "execute failed", e); 454 throw new JDOException("Impossible to execute a query ", e); 455 } 456 } 457 458 465 public Object execute(Object p1, Object p2) { 466 Object [] params = new Object []{p1, p2}; 467 beforeExecute(params); 468 try { 469 Object res = qc.execute(params, pm, this); 470 if (!unique) { 471 results.add(res); 472 } 473 return res; 474 } catch (JDOException e) { 475 throw e; 476 } catch (Exception e) { 477 logger.log(BasicLevel.ERROR, "execute failed", e); 478 throw new JDOException("Impossible to execute a query ", e); 479 } 480 } 481 482 490 public Object execute(Object p1, Object p2, Object p3) { 491 Object [] params = new Object []{p1, p2, p3}; 492 beforeExecute(params); 493 try { 494 Object res = qc.execute(params, pm, this); 495 if (!unique) { 496 results.add(res); 497 } 498 return res; 499 } catch (JDOException e) { 500 throw e; 501 } catch (Exception e) { 502 logger.log(BasicLevel.ERROR, "execute failed", e); 503 throw new JDOException("Impossible to execute a query ", e); 504 } 505 } 506 507 518 public Object executeWithMap(Map amap) { 519 beforeExecute(amap); 520 try { 521 Object res = qc.execute(amap, pm, this); 522 if (!unique) { 523 results.add(res); 524 } 525 return res; 526 } catch (JDOException e) { 527 throw e; 528 } catch (Exception e) { 529 logger.log(BasicLevel.ERROR, "execute failed", e); 530 throw new JDOException("Impossible to execute a query ", e); 531 } 532 } 533 534 554 public Object executeWithArray(Object [] anarray) { 555 beforeExecute(anarray); 556 try { 557 Object res = qc.execute(anarray, pm, this); 558 if (!unique) { 559 results.add(res); 560 } 561 return res; 562 } catch (JDOException e) { 563 throw e; 564 } catch (Exception e) { 565 logger.log(BasicLevel.ERROR, "execute failed", e); 566 throw new JDOException("Impossible to execute a query ", e); 567 } 568 } 569 570 576 public PersistenceManager getPersistenceManager() { 577 return pm; 578 } 579 580 593 public void close(Object queryResult) { 594 if (queryResult instanceof QueryResultList) { 595 ((QueryResultList) queryResult).close(); 596 int i=0; 597 while(i < results.size() && results.get(i) != queryResult) { 598 i++; 599 } 600 if (i < results.size()) { 601 results.remove(i); 602 } 603 } 604 } 605 606 613 public void closeAll() { 614 for(int i=0; i<results.size(); i++) { 615 Object queryResult = results.get(i); 616 if (queryResult instanceof QueryResultList) { 617 ((QueryResultList) queryResult).close(); 618 } 619 } 620 results.clear(); 621 } 622 623 public FetchPlan getFetchPlan(){ 624 return fetchPlan; 625 } 626 627 public void setFetchPlan(FetchPlan fp){ 628 assertModifiable(); 629 fetchPlan = fp; 630 } 631 632 public long deletePersistentAll() { 633 throw new JDOException("delete by query are not supported currently"); 635 } 636 637 public long deletePersistentAll(Map arg0) { 638 throw new JDOException("delete by query are not supported currently"); 640 } 641 642 public long deletePersistentAll(Object [] arg0) { 643 throw new JDOException("delete by query are not supported currently"); 645 } 646 647 public void setGrouping(String arg0) { 648 assertModifiable(); 649 this.grouping = arg0; 650 } 651 652 public void setRange(long first, long last) { 653 assertModifiable(); 654 indexFirst = first; 655 indexLast = last; 656 } 657 658 public void setUnique(boolean unique) { 659 assertModifiable(); 660 this.unique = unique; 661 } 662 663 public void setResult(String result) { 664 assertModifiable(); 665 this.result = result; 666 } 667 668 public void setResultClass(Class resultClass) { 669 assertModifiable(); 670 this.resultClass = resultClass; 671 } 672 673 public Map extensions = null; 674 675 public void setExtensions(Map exts) { 676 assertModifiable(); 677 this.extensions = exts; 678 } 679 public void addExtension(String key, Object value) { 680 assertModifiable(); 681 if (extensions == null) { 682 if (value == null) { 683 return; 684 } 685 extensions = new HashMap (); 686 } 687 if (value == null) { 688 extensions.remove(key); 689 } else { 690 extensions.put(key, value); 691 } 692 } 693 694 boolean modifiable = true; 695 696 public boolean isUnmodifiable() { 697 return modifiable; 698 } 699 public void setUnmodifiable() { 700 modifiable = false; 701 } 702 703 706 private void beforeExecute(Object [] parameterValues) { 707 if (parameters != null && parameterValues.length == 0 && parameters.length()>0) 708 throw new JDOUserException( 709 "Parameters in not null, and you try to execute without parameters"); 710 if (pm.isClosed()) 711 throw new JDOFatalUserException( 712 "Impossible to use the query: the persistent manager is closed."); 713 if (parameterValues != null && parameters != null) { 714 treatSpeedoHints(parameterValues); 715 } 716 if (hasChanged) { 717 compile(); 718 } 719 } 720 721 private void beforeExecute(Map parameterValues) { 722 if (parameters != null && parameterValues.size() == 0 && parameters.length()>0) 723 throw new JDOUserException( 724 "Parameters in not null, and you try to execute without parameters"); 725 if (pm.isClosed()) 726 throw new JDOFatalUserException( 727 "Impossible to use the query: the persistent manager is closed."); 728 if (parameterValues != null) { 729 treatSpeedoHints(parameterValues); 730 } 731 if (hasChanged) { 732 compile(); 733 } 734 } 735 736 private void assertPMIsOpen() { 737 if (pm.isClosed()) 738 throw new JDOUserException("The persistence manager is closed."); 739 } 740 741 private void assertModifiable() { 742 if (!modifiable) 743 throw new JDOUserException("The query has been marked as not modifiable."); 744 } 745 746 private void assertParameters() { 747 if (parameters == null) 748 throw new JDOUserException("Parameters string can not be null"); 749 } 750 751 private void assertVariables() { 752 if (variables == null) 753 throw new JDOUserException("variables string can not be null"); 754 } 755 756 private void assertCandidateClass() { 757 if (candidateClass == null) 758 throw new JDOUserException("candidate class can not be null"); 759 } 760 761 private void assertExtentClass() { 762 if (extentClass == null) 763 throw new JDOUserException("Extent class can not be null"); 764 } 765 766 private void assertCandidateInstances() { 767 if (candidateInstances == null) 768 throw new JDOUserException("candidate Collection can not be null"); 769 } 770 } 771 | Popular Tags |