1 2 38 39 package jspPhoneBook.data; 40 41 import org.enhydra.dods.DODS; 42 import com.lutris.dods.builder.generator.query.*; 43 import com.lutris.appserver.server.sql.*; 44 import org.enhydra.dods.cache.LRUCache; 45 import org.enhydra.dods.cache.DataStructCache; 46 import org.enhydra.dods.cache.QueryCache; 47 import org.enhydra.dods.cache.QueryCacheItem; 48 import org.enhydra.dods.cache.QueryResult; 49 import org.enhydra.dods.cache.DataStructShell; 50 import org.enhydra.dods.cache.DOShell; 51 import org.enhydra.dods.cache.Condition; 52 import org.enhydra.dods.statistics.Statistics; 53 import org.enhydra.dods.cache.CacheConstants; 54 import org.enhydra.dods.statistics.*; 55 import com.lutris.logging.LogChannel; 56 import com.lutris.logging.Logger; 57 import com.lutris.appserver.server.sql.standard.StandardDBTransaction; 58 59 import java.sql.*; 60 import java.util.*; 61 import java.math.*; 62 import java.util.Date ; 64 122 public class PersonQuery implements Query { 123 124 private QueryBuilder builder; 125 126 129 private String logicalDatabase; 130 131 private ResultSet resultSet = null; 132 private boolean uniqueInstance = false; 133 private boolean loadData = false; 134 private PersonDO[] DOs = null; 135 private int arrayIndex = -1; 136 private boolean needToRun = true; 137 private LinkedHashMap cacheHits = null; 138 private boolean isQueryByOId = false; 139 private boolean hasNonOidCond = false; 140 private boolean hitDb = false; 141 private boolean userHitDb = false; 142 private int maxDBrows = 0; 143 private boolean orderRelevant = true; private QueryCacheItem queryItem = null; 145 private String currentHandle = null; 146 private HashMap refs = new HashMap(); 147 private int iCurrentFetchSize = -1; 148 private int iCurrentQueryTimeout = 0; 149 private DBTransaction transaction = null; 150 151 156 public PersonQuery() { 157 158 builder = new QueryBuilder( "person", "person.*" ); 159 160 String dbName = PersonDO.get_logicalDBName(); 161 try { 162 String vendor = DODS.getDatabaseManager().logicalDatabaseType(dbName); 163 if (vendor != null) { 164 builder.setDatabaseVendor(vendor); 165 logicalDatabase = dbName; 166 } else { 167 builder.setDatabaseVendor(); 168 logicalDatabase = DODS.getDatabaseManager().getDefaultDB(); 169 } 170 } catch (Exception e) { 171 builder.setDatabaseVendor(); 172 logicalDatabase = DODS.getDatabaseManager().getDefaultDB(); 173 } 174 builder.setUserStringAppendWildcard( false ); 175 builder.setUserStringTrim( false ); 176 reset(); 177 } 178 179 184 public PersonQuery(DBTransaction dbTrans) { 185 186 builder = new QueryBuilder( "person", "person.*" ); 187 188 String dbName = null; 189 if(dbTrans!=null) 190 dbName = dbTrans.getDatabaseName(); 191 else 192 dbName = PersonDO.get_logicalDBName(); 193 try { 194 transaction = dbTrans; 195 String vendor = DODS.getDatabaseManager().logicalDatabaseType(dbName); 196 if (vendor != null) { 197 builder.setDatabaseVendor(vendor); 198 logicalDatabase = dbName; 199 } else { 200 builder.setDatabaseVendor(); 201 logicalDatabase = DODS.getDatabaseManager().getDefaultDB(); 202 } 203 } catch (Exception e) { 204 builder.setDatabaseVendor(); 205 logicalDatabase = DODS.getDatabaseManager().getDefaultDB(); 206 } 207 builder.setUserStringAppendWildcard( false ); 208 builder.setUserStringTrim( false ); 209 reset(); 210 } 211 212 213 214 220 public String getLogicalDatabase() { 221 return logicalDatabase; 222 } 223 224 230 public void setLogicalDatabase(String dbName) throws SQLException, DatabaseManagerException { 231 String vendor = DODS.getDatabaseManager().logicalDatabaseType(dbName); 232 if (vendor != null) { 233 builder.setDatabaseVendor(vendor); 234 logicalDatabase = dbName; 235 } else { 236 builder.setDatabaseVendor(); 237 logicalDatabase = DODS.getDatabaseManager().getDefaultDB(); 238 } 239 reset(); 240 } 241 242 243 244 private boolean unique = false; 247 248 256 public void setUnique(boolean newUnique) { unique = newUnique; } 257 258 266 public boolean getUnique() { return unique; } 267 268 private int readSkip = 0; 271 272 280 public void setReadSkip(int newReadSkip) { 281 readSkip = newReadSkip; 282 } 283 284 292 public int getReadSkip() { return readSkip; } 293 294 private int databaseLimit = 0; 297 298 306 public void setDatabaseLimit(int newLimit) { 307 databaseLimit = newLimit; 308 } 309 310 318 public int getDatabaseLimit() { return databaseLimit; } 319 320 private boolean databaseLimitExceeded = false; 321 322 330 public boolean getDatabaseLimitExceeded() { return databaseLimitExceeded; } 331 333 336 public void hitDatabase() { userHitDb = true; } 337 338 341 348 public void setUserStringWildcard(String newUserStringWildcard) { 349 builder.setUserStringWildcard( newUserStringWildcard ); 350 } 351 352 359 public void setUserStringSingleWildcard(String newUserStringSingleWildcard) { 360 builder.setUserStringSingleWildcard( newUserStringSingleWildcard ); 361 } 362 363 370 public void setUserStringSingleWildcardEscape(String newUserStringSingleWildcardEscape) { 371 builder.setUserStringSingleWildcardEscape( newUserStringSingleWildcardEscape ); 372 } 373 374 381 public void setUserStringWildcardEscape(String newUserStringWildcardEscape) { 382 builder.setUserStringWildcardEscape( newUserStringWildcardEscape ); 383 } 384 385 392 public void setUserStringAppendWildcard(boolean userStringAppendWildcard ) { 393 builder.setUserStringAppendWildcard( userStringAppendWildcard ); 394 } 395 396 403 public void setUserStringTrim(boolean userStringTrim ) { 404 builder.setUserStringTrim( userStringTrim ); 405 } 406 407 414 public String getUserStringWildcard() { 415 return builder.getUserStringWildcard(); 416 } 417 418 425 public String getUserStringSingleWildcard() { 426 return builder.getUserStringSingleWildcard(); 427 } 428 429 436 public String getUserStringSingleWildcardEscape() { 437 return builder.getUserStringSingleWildcardEscape(); 438 } 439 440 447 public String getUserStringWildcardEscape() { 448 return builder.getUserStringWildcardEscape(); 449 } 450 451 458 public boolean getUserStringAppendWildcard() { 459 return builder.getUserStringAppendWildcard(); 460 } 461 462 469 public boolean getUserStringTrim() { 470 return builder.getUserStringTrim(); 471 } 472 474 481 private void getQueryByOIds(Vector DOs) 482 throws DataObjectException { 483 if (DOs.size() == 0) 484 return; 485 PersonDO DO = null; 486 DOShell shell = null; 487 PersonQuery tmpQuery = null; 488 Date startQueryTime = new Date (); 489 long queryTime = 0; 490 for (int i=0; i<DOs.size(); i++) { 491 shell = (DOShell)DOs.elementAt(i); 492 tmpQuery = new PersonQuery(transaction); 493 try { 494 tmpQuery.setQueryHandle( shell.handle ); 495 tmpQuery.requireUniqueInstance(); 496 497 DO = tmpQuery.getNextDO(); 498 if ( null == DO ) 499 throw new DataObjectException("PersonDO DO not found for id=" + shell.handle ); 500 } catch ( Exception e ) { 501 throw new DataObjectException( "Duplicate ObjectId" ); 502 } 503 shell.dataObject = DO; 504 } 505 Date stopQueryTime = new Date (); 506 queryTime = stopQueryTime.getTime() - startQueryTime.getTime(); 507 PersonDO.statistics.updateQueryByOIdAverageTime((new Long (queryTime)).intValue(),DOs.size()); 508 } 509 510 511 512 519 private void runQuery() 520 throws DataObjectException, NonUniqueQueryException { 521 needToRun = false; 522 arrayIndex = -1; 523 DBQuery dbQuery = null; 524 Date startQueryTime = new Date (); 525 long queryTime = 0; 526 boolean readDOs = false; 527 boolean canUseQueryCache = true; 528 CacheStatistics stat = null; 529 boolean resultsFromQCache = false; 530 QueryCacheItem queryCachedItem = null; 531 532 if ((transaction!=null) && 533 (transaction instanceof com.lutris.appserver.server.sql.standard.StandardDBTransaction)) { 534 if(((com.lutris.appserver.server.sql.standard.StandardDBTransaction)transaction).getAutoWrite()) try { 535 transaction.write(); 536 } catch (SQLException sqle) { 537 sqle.printStackTrace(); 538 throw new DataObjectException("Couldn't write transaction: "+sqle); 539 } 540 ((com.lutris.appserver.server.sql.standard.StandardDBTransaction)transaction).dontAggregateDOModifications(); 541 } 542 543 try { 544 QueryResult results = null; 545 DOShell shell = null; 546 if (isQueryByOId && !hasNonOidCond) { results = new QueryResult(); 548 if (currentHandle != null) { 549 if(transaction!=null && _tr_(transaction).getTransactionCache()!=null && !loadData) { 550 PersonDO DO= (PersonDO)_tr_(transaction).getTransactionCache().getDOByHandle(currentHandle); 551 if(DO!=null){ 552 shell = new DOShell(DO); 553 results.DOs.add(shell); 554 resultsFromQCache = true; 555 } 556 else 557 resultsFromQCache = false; 558 } 559 if(!resultsFromQCache) { PersonDataStruct DS = (PersonDataStruct)PersonDO.cache.getDataStructByHandle(currentHandle); 561 if (DS != null) { PersonDO DO = (PersonDO)PersonDO.createExisting(DS.get_OId(), transaction); 563 shell = new DOShell(DO); 564 results.DOs.add(shell); 565 resultsFromQCache = true; 566 }else{ if (PersonDO.cache.isFull()) 568 resultsFromQCache = false; 569 } 570 } 571 } } 573 else { if ((PersonDO.cache.isFull()) && (!hitDb)) { 575 resultsFromQCache = true; 576 } 577 else { 578 if (PersonDO.cache.getLevelOfCaching() == CacheConstants.QUERY_CACHING) { 579 if (hitDb) { stat = null; 581 stat = PersonDO.statistics.getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE); 582 if (stat!= null){ 583 stat.incrementCacheAccessNum(1); 584 } 585 }else{ stat = null; 587 stat = PersonDO.statistics.getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE); 588 if (stat!= null){ 589 stat.incrementCacheAccessNum(1); 590 } 591 } 592 } 593 if(transaction != null) 594 canUseQueryCache = !transaction.preventCacheQueries(); 595 if ((PersonDO.cache.getLevelOfCaching() == CacheConstants.QUERY_CACHING) && canUseQueryCache) { 596 String queryID = builder.getSQLwithParms(); int resNum = 0; 598 int evaluateNo = 0; 599 if (hitDb) 600 queryCachedItem = ((QueryCache)PersonDO.cache).getComplexQueryCacheItem(logicalDatabase, queryID); 601 else 602 queryCachedItem = ((QueryCache)PersonDO.cache).getSimpleQueryCacheItem(logicalDatabase, queryID); 603 queryItem.setQueryId(queryID); if (queryCachedItem == null) { if ((!builder.isMultiTableJoin()) || PersonDO.isAllReadOnly()) { 606 if (hitDb) 607 ((QueryCache)PersonDO.cache).addComplexQuery(queryItem); else 609 ((QueryCache)PersonDO.cache).addSimpleQuery(queryItem); } 611 }else{ if ( !(isOrderRelevant() && queryCachedItem.isModifiedQuery()) ) { 613 if (hitDb) { stat = null; 615 stat = PersonDO.statistics.getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE); 616 if (stat!= null){ 617 stat.incrementCacheHitsNum(1); 618 } 619 }else{ stat = null; 621 stat = PersonDO.statistics.getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE); 622 if (stat!= null){ 623 stat.incrementCacheHitsNum(1); 624 } 625 } 626 627 int limitOfRes; 628 if (databaseLimit == 0) 629 limitOfRes = 0; 630 else 631 limitOfRes = readSkip+databaseLimit+1; 632 if (! unique) { 633 if (hitDb) 634 results = ((QueryCache)PersonDO.cache).getComplexQueryResults(logicalDatabase, queryID, limitOfRes, maxDBrows); 635 else 636 results = ((QueryCache)PersonDO.cache).getSimpleQueryResults(logicalDatabase, queryID, limitOfRes, maxDBrows); 637 }else{ if (hitDb) 639 results = ((QueryCache)PersonDO.cache).getComplexQueryResults(logicalDatabase, queryID, limitOfRes, maxDBrows, true); 640 else 641 results = ((QueryCache)PersonDO.cache).getSimpleQueryResults(logicalDatabase, queryID, limitOfRes, maxDBrows, true); 642 } if (results != null) { 644 results = getCachedResults(results); 646 resNum = results.DOs.size(); 647 if (databaseLimit != 0) { if (resNum == readSkip+databaseLimit+1) { 649 resNum--; 650 databaseLimitExceeded = true; 651 results.DOs.remove(readSkip+databaseLimit); 652 }else{ 653 if ( (resNum == readSkip+databaseLimit) && (!queryCachedItem.isCompleteResult()) ) 654 databaseLimitExceeded = true; 655 } 656 } if ( (databaseLimit!=0 &&(resNum == (readSkip+databaseLimit))) || (maxDBrows!=0 && (resNum + results.skippedUnique) == maxDBrows) || (queryCachedItem.isCompleteResult()) ) { 658 int lazyTime = PersonDO.statistics.getQueryByOIdAverageTime()*results.lazy.size(); 659 if (lazyTime <= queryCachedItem.getTime()) { 660 resultsFromQCache = true; 661 getQueryByOIds(results.lazy); }else 663 databaseLimitExceeded = false; 664 }else 665 databaseLimitExceeded = false; 666 } 668 } } } } } if (( userHitDb) || (!resultsFromQCache)) { 675 dbQuery = PersonDO.createQuery(transaction); 676 677 results = new QueryResult(); 678 int resultCount=0; 679 boolean bHasMoreResults = false; 680 if (( (PersonDO.getConfigurationAdministration().getTableConfiguration().isLazyLoading()) || isCaching()) && (!builder.getPreventPrimaryKeySelect()) && !loadData) { 681 builder.resetSelectedFields(); 682 builder.setSelectClause("person."+PersonDO.get_OIdColumnName()+", person."+PersonDO.get_versionColumnName()); 683 } 684 else 685 builder.setSelectClause( "person.*"); 686 dbQuery.query( this ); 688 689 if (! unique) { 690 while ( (bHasMoreResults = resultSet.next()) && (databaseLimit==0 || (results.DOs.size()<databaseLimit)) ) { 691 PersonDO newDO; 692 PersonDataStruct newDS; 693 694 if (( (PersonDO.getConfigurationAdministration().getTableConfiguration().isLazyLoading()) || isCaching()) && (!builder.getPreventPrimaryKeySelect()) && !loadData) { 695 newDO = PersonDO.createExisting ( new ObjectId(resultSet.getBigDecimal(CoreDO.get_OIdColumnName())) , refs, transaction); 696 newDO.set_Version(resultSet.getInt(PersonDO.get_versionColumnName())); 697 } else 698 newDO = PersonDO.createExisting ( resultSet , refs, transaction); 699 if(transaction==null) { 700 if(newDO!=null && newDO.isTransactionCheck()) { 701 String trace=""; 702 StackTraceElement [] traceElements= (new Throwable ()).getStackTrace(); 703 for(int i=0; i < traceElements.length; i++) 704 trace+=traceElements[i].toString()+"\n"; 705 DODS.getLogChannel().write(Logger.WARNING, "DO without transaction context is created : Database: "+newDO.get_OriginDatabase()+" PersonDO class, oid: "+newDO.get_OId()+", version: "+newDO.get_Version()+" \n"+trace); 706 } 707 } 708 709 if (queryItem != null) { 710 queryItem.add((PersonDataStruct)newDO.originalData_get()); 711 } 712 if (( (PersonDO.getConfigurationAdministration().getTableConfiguration().isLazyLoading()) || isCaching()) && (!builder.getPreventPrimaryKeySelect()) && !loadData) 713 {} 714 else 715 newDS =PersonDO.addToCache((PersonDataStruct)newDO.originalData_get()); 716 717 if (resultCount >= readSkip) { 718 shell = new DOShell(newDO); 719 results.DOs.add(shell); 720 } 721 resultCount++; 722 723 } } 726 else { HashSet hsResult = new HashSet(readSkip+databaseLimit); 728 while((bHasMoreResults = resultSet.next()) && (databaseLimit==0 || (results.DOs.size()<databaseLimit)) ) { 729 PersonDO newDO; 730 PersonDataStruct newDS; 731 732 if (( (PersonDO.getConfigurationAdministration().getTableConfiguration().isLazyLoading()) || isCaching()) && (!builder.getPreventPrimaryKeySelect()) && !loadData) { 733 newDO = PersonDO.createExisting ( new ObjectId(resultSet.getBigDecimal(CoreDO.get_OIdColumnName())) , refs, transaction); 734 newDO.set_Version(resultSet.getInt(PersonDO.get_versionColumnName())); 735 } else 736 newDO = PersonDO.createExisting ( resultSet , refs , transaction); 737 if(transaction==null) { 738 if(newDO!=null && newDO.isTransactionCheck()) { 739 String trace=""; 740 StackTraceElement [] traceElements= (new Throwable ()).getStackTrace(); 741 for(int i=0; i < traceElements.length; i++) 742 trace+=traceElements[i].toString()+"\n"; 743 DODS.getLogChannel().write(Logger.WARNING, "DO without transaction context is created : Database: "+newDO.get_OriginDatabase()+" PersonDO class, oid: "+newDO.get_OId()+", version: "+newDO.get_Version()+" \n"+trace); 744 } 745 } 746 747 if (queryItem != null) { 748 queryItem.add((PersonDataStruct)newDO.originalData_get()); 749 } 750 if (( (PersonDO.getConfigurationAdministration().getTableConfiguration().isLazyLoading()) || isCaching()) && (!builder.getPreventPrimaryKeySelect()) && !loadData) 751 { 752 } else 753 newDS = PersonDO.addToCache((PersonDataStruct)newDO.originalData_get()); 754 755 if (!hsResult.contains(newDO.get_Handle())) { 756 hsResult.add(newDO.get_Handle()); 757 if (resultCount >= readSkip) { 758 759 shell = new DOShell(newDO); 760 results.DOs.add(shell); 761 } 762 resultCount++; 763 } 764 765 } } 768 if ((results.DOs.size()==databaseLimit)&& bHasMoreResults) { 769 resultSet.close(); 770 databaseLimitExceeded = true; 771 } 772 if (maxDBrows > 0) { 773 if (!bHasMoreResults) { 774 if ((databaseLimit > 0) && databaseLimit < maxDBrows ) 775 queryItem.setCompleteResult(true); 776 } 777 } 778 else { 779 if (!bHasMoreResults) 780 queryItem.setCompleteResult(true); 781 } 782 Date stopQueryTime = new Date (); 783 queryTime = stopQueryTime.getTime() - startQueryTime.getTime(); 784 if (queryItem != null) { 785 queryItem.setTime((new Long (queryTime)).intValue()); 786 if (queryCachedItem != null) { 787 if ( queryItem.isCompleteResult() || (queryCachedItem.isModifiedQuery() && isOrderRelevant()) || (queryCachedItem.getResultNum() < queryItem.getResultNum()) ) { 788 if ((!builder.isMultiTableJoin()) || PersonDO.isAllReadOnly() ) { 789 if (hitDb) { 790 ((QueryCache)PersonDO.cache).addComplexQuery(queryItem); 791 } 792 else { 793 ((QueryCache)PersonDO.cache).addSimpleQuery(queryItem); 794 } 795 } 797 } 798 else { 799 if ( (queryCachedItem.getResultNum() < (readSkip + databaseLimit) ) && (queryCachedItem.getResultNum() < maxDBrows) ) { 800 queryCachedItem.setCompleteResult(true); 801 } 802 } 803 if ( (queryItem.getResultNum() < (readSkip + databaseLimit) ) && (queryItem.getResultNum() < maxDBrows) ) 804 queryItem.setCompleteResult(true); 805 } } int maxExecuteTime = PersonDO.cache.getTableConfiguration().getMaxExecuteTime(); 808 if (maxExecuteTime > 0 && queryTime > maxExecuteTime) 809 DODS.getLogChannel().write(Logger.WARNING, "sql = " + builder.getSQLwithParms()+" execute time = "+queryTime + "max table execute time = "+maxExecuteTime); 810 } 811 else { if (PersonDO.cache.isFull()&& (!hitDb)) { 813 results = new QueryResult(); 814 if (readSkip<cacheHits.size()) { 815 Vector vect = new Vector(cacheHits.values()); 816 results.DOs = new Vector(); 817 PersonDO DO = null; 818 PersonDataStruct DS = null; 819 String cachePrefix = getLogicalDatabase()+"."; 820 int i = readSkip; 821 while (i < vect.size() && ((databaseLimit==0) || (i<=readSkip+databaseLimit))) { 822 boolean findInTransactionCache = false; 823 DS = (PersonDataStruct)vect.get(i); 824 if(transaction!=null && _tr_(transaction).getTransactionCache()!=null && !loadData) { 825 DO = (PersonDO)_tr_(transaction).getTransactionCache().getDOByHandle(cachePrefix+DS.get_OId().toString()); 826 if(DO != null) 827 findInTransactionCache = true; 828 } 829 if(!findInTransactionCache){ 830 DO = (PersonDO)PersonDO.createExisting(DS.get_OId(), transaction); 831 } 832 833 results.DOs.add(DO); 834 835 i++; 836 } 837 readDOs = true; 838 } 839 840 if ((databaseLimit != 0) && (results.DOs.size() == databaseLimit+1)) { 841 databaseLimitExceeded = true; 842 results.DOs.remove(databaseLimit); 843 } 844 845 } 847 } if ( results.DOs.size() > 1 && uniqueInstance ) 850 throw new NonUniqueQueryException("Too many rows returned from database" ); 851 DOs = new PersonDO [ results.DOs.size() ]; 852 PersonDataStruct orig; 853 if (readDOs) { 854 for ( int i = 0; i < results.DOs.size(); i++ ) { 855 DOs[i] = (PersonDO)results.DOs.elementAt(i); 856 } 857 } 858 else { 859 for ( int i = 0; i < results.DOs.size(); i++ ) { 860 DOs[i] = (PersonDO)((DOShell)results.DOs.elementAt(i)).dataObject; 861 } 862 } 863 arrayIndex = 0; 864 if (isQueryByOId && !hasNonOidCond) { 865 PersonDO.statistics.incrementQueryByOIdNum(); 866 PersonDO.statistics.updateQueryByOIdAverageTime((new Long (queryTime)).intValue(),1); 867 } 868 else { 869 PersonDO.statistics.incrementQueryNum(); 870 PersonDO.statistics.updateQueryAverageTime((new Long (queryTime)).intValue()); 871 } 872 } catch ( SQLException se ) { 873 if (null == se.getSQLState() ) { 874 throw new DataObjectException("Unknown SQLException", se ); 875 } 876 if ( se.getSQLState().startsWith("02") && se.getErrorCode() == 100 ) { 877 throw new DataObjectException("Update or delete DO is out of synch", se ); 878 } else if ( se.getSQLState().equals("S1000") && se.getErrorCode() == -268) { 879 throw new DataObjectException("Integrity constraint violation", se ); 880 } else { 881 throw new DataObjectException( "Data Object Error", se ); 882 } 883 } catch ( ObjectIdException oe ) { 884 throw new DataObjectException( "Object ID Error", oe ); 885 } catch ( DatabaseManagerException de ) { 886 throw new DataObjectException( "Database connection Error", de ); 887 } 888 finally { 889 if (( null != dbQuery )&&(null== transaction)) 890 dbQuery.release(); 891 } 892 } 893 894 910 public void setMaxRows( int maxRows ) 911 throws DataObjectException, NonUniqueQueryException { 912 913 maxDBrows = maxRows; 914 builder.setMaxRows( maxRows ); 915 } 916 917 922 public int getMaxRows() { 923 return maxDBrows; 924 } 925 930 public boolean isOrderRelevant() { 931 return orderRelevant; 932 } 933 934 939 public void setOrderRelevant(boolean newOrderRelevant) { 940 orderRelevant = newOrderRelevant; 941 } 942 943 944 951 public QueryResult getCachedResults(QueryResult result) throws DataObjectException { 952 Vector tempVec = result.DOs; 953 if (tempVec == null) 954 return null; 955 result.DOs = new Vector(); 956 result.lazy = new Vector(); 957 DOShell shell = null; 958 PersonDO cacheDO = null; 959 PersonDataStruct cacheDS = null; 960 String handle = ""; 961 String cachePrefix=getLogicalDatabase()+"."; 962 963 for(int i=0; i < tempVec.size(); i++) { 964 965 if(tempVec.get(i)!=null) { 966 cacheDO = null; 967 cacheDS = null; 968 handle=(String )tempVec.get(i); 969 shell = new DOShell(handle); 970 if(transaction!=null && _tr_(transaction).getTransactionCache()!=null && !loadData) { 971 try { 972 cacheDO = (PersonDO)_tr_(transaction).getTransactionCache().getDOByHandle(cachePrefix+handle); 973 } catch (Exception e) { 974 } 975 } 976 if (cacheDO == null){ 977 cacheDS = (PersonDataStruct)PersonDO.cache.getDataStructByHandle(cachePrefix+handle); 978 if(cacheDS!=null) { 979 try { 980 cacheDO = (PersonDO)PersonDO.createExisting(cacheDS.get_OId(), transaction); 981 } catch (Exception e) { 982 } 983 } 984 } 985 if (cacheDO == null){ 986 result.lazy.add(shell); 987 } 988 else { 989 shell.dataObject = cacheDO; 990 } 991 result.DOs.add(shell); 992 } 993 } return result; 995 } 996 997 998 999 1007 public PersonDO[] getDOArray() 1008 throws DataObjectException, NonUniqueQueryException { 1009 if ( needToRun ) 1010 runQuery(); 1011 return DOs; 1012 } 1013 1014 1022 public PersonDO getNextDO() 1023 throws DataObjectException, NonUniqueQueryException { 1024 if ( needToRun ) 1025 runQuery(); 1026 if ( null == DOs ) { 1027 1031 return null; 1032 } 1033 if ( arrayIndex < DOs.length ) 1034 return DOs[ arrayIndex++ ]; 1035 return null; 1036 } 1037 1038 1039 1047 public void setQueryOId(ObjectId oid) { 1048 String handle = getLogicalDatabase()+ "." +oid.toString(); 1051 if (PersonDO.cache.isFull()) { 1052 PersonDataStruct DS = (PersonDataStruct)cacheHits.get(handle); 1053 cacheHits = new LinkedHashMap(); 1054 if (DS != null) { 1055 cacheHits.put(handle,DS); 1056 } 1057 } 1058 if (isQueryByOId) { 1060 hasNonOidCond = true; 1062 1063 } else { currentHandle = handle; 1065 } 1066 isQueryByOId = true; 1067 try { 1068 Condition cond = new Condition(PersonDataStruct.COLUMN_OID,handle,"="); 1069 queryItem.addCond(cond); 1070 } 1071 catch (Exception e){ 1072 } 1073 builder.addWhere( PersonDO.PrimaryKey, oid.toBigDecimal(), QueryBuilder.EQUAL ); 1076 } 1077 1078 1084 public void setQueryHandle(String handle) 1085 throws ObjectIdException { 1086 ObjectId oid = new ObjectId( handle ); 1087 setQueryOId( oid ); 1088 } 1089 1090 1091 1096 public void requireUniqueInstance() { 1097 uniqueInstance = true; 1098 } 1099 1100 1104 public void setLoadData(boolean newValue) { 1105 loadData = newValue; 1106 } 1107 1108 1112 public boolean getLoadData() { 1113 if(loadData) 1114 return true; 1115 else 1116 return ((PersonDO.getConfigurationAdministration().getTableConfiguration().isLazyLoading()) || isCaching()); 1117 } 1118 1119 1122 public void reset() { 1123 if (PersonDO.cache.isFull()) { 1124 cacheHits = new LinkedHashMap(PersonDO.cache.getCacheContent()); 1125 } 1126 DOs = null; 1127 uniqueInstance = false; 1128 needToRun = true; 1129 isQueryByOId = false; 1130 hasNonOidCond = false; 1131 loadData=false; 1132 builder.reset(); 1133 if (PersonDO.cache.getLevelOfCaching() == CacheConstants.QUERY_CACHING) { 1134 queryItem = ((QueryCache)PersonDO.cache).newQueryCacheItemInstance(logicalDatabase); 1135 } 1136 } 1137 1138 1145 private boolean exactFlag( boolean exact ) { 1146 return exact ? QueryBuilder.EXACT_MATCH : QueryBuilder.NOT_EXACT; 1147 } 1148 1149 1153 1166 public ResultSet executeQuery(DBConnection conn) 1167 throws SQLException { 1168 builder.setCurrentFetchSize(iCurrentFetchSize); 1169 builder.setCurrentQueryTimeout(iCurrentQueryTimeout); 1170 resultSet = builder.executeQuery( conn ); 1171 return resultSet; 1172 } 1173 1174 1192 public Object next(ResultSet rs) 1193 throws SQLException, ObjectIdException { 1194 throw new ObjectIdException("next() should not be used. Use getNextDO() instead." ); 1198 } 1200 1201 1202 1214 public String convertUserSearchValue( String userSearchValue ) { 1215 return builder.convertUserSearchValue( userSearchValue ); 1216 } 1217 1218 1228 public boolean containsWildcards( String dbSearchValue ) { 1229 return builder.containsWildcards( dbSearchValue ); 1230 } 1231 1233 1244 public int getCount() 1245 throws NonUniqueQueryException, DataObjectException, SQLException, DatabaseManagerException { 1246 int rowCount=0; 1247 if (needToRun && databaseLimit==0) { 1248 rowCount = selectCount(); 1249 } else { 1250 if (needToRun) runQuery(); 1251 rowCount = DOs.length; 1252 } 1253 return rowCount; 1254 } 1255 1256 1260 protected void setRefs(HashMap queryRefs) { 1261 refs = queryRefs; 1262 } 1263 1264 1268 public void set_FetchSize (int iCurrentFetchSizeIn) { 1269 iCurrentFetchSize = iCurrentFetchSizeIn; 1270 } 1271 1272 1276 public int get_FetchSize() { 1277 return (iCurrentFetchSize < 0)? builder.getDefaultFetchSize() : iCurrentFetchSize; 1278 } 1279 1280 1284 public int get_QueryTimeout() { 1285 return iCurrentQueryTimeout; 1286 } 1287 1288 1292 public void set_QueryTimeout(int iQueryTimeoutIn) { 1293 iCurrentQueryTimeout = (iCurrentQueryTimeout < 0)? builder.getDefaultQueryTimeout() : iCurrentQueryTimeout; 1294 } 1295 1296 1297 1305 public int selectCount() 1306 throws SQLException, DatabaseManagerException { 1307 int rowCount=0; 1308 String tempClause = builder.getSelectClause(); 1309 builder.setSelectClause(" count(*) as \"counter\" "); 1310 DBQuery dbQuery; 1311 1312 dbQuery = PersonDO.createQuery(transaction); 1313 dbQuery.query(this); 1314 resultSet.next(); 1315 rowCount=resultSet.getInt("counter"); 1316 resultSet.close(); 1317 if (transaction == null) 1318 dbQuery.release(); 1319 builder.close(); 1320 resultSet = null; 1321 builder.setSelectClause(tempClause); 1322 return rowCount; 1323 } 1324 1326 1330 1331 private boolean isCaching() { 1332 double cachePercentage = PersonDO.cache.getCachePercentage(); 1333 double usedPercentage = 0; 1334 if(cachePercentage == -1) 1335 return false; 1336 else if(cachePercentage == 0) 1337 return true; 1338 else { 1339 try { 1340 usedPercentage = PersonDO.getConfigurationAdministration().getStatistics().getCacheStatistics(CacheConstants.DATA_CACHE).getUsedPercents(); 1341 } catch (Exception ex) { 1342 return false; 1343 } 1344 if(usedPercentage > PersonDO.cache.getCachePercentage()*100) 1345 return true; 1346 else 1347 return false; 1348 } 1349 } 1350 1351 private static StandardDBTransaction _tr_(DBTransaction dbt) { 1352 return (StandardDBTransaction)dbt; 1353 } 1354 1355 1356 1366 1367 1368 public void setQueryFirstName(String x, String cmp_op ) 1369 throws DataObjectException, QueryException { 1370 1371 String cachePrefix = getLogicalDatabase()+"."; 1372 1373 hasNonOidCond = true; 1374 Condition cond = new Condition(PersonDataStruct.COLUMN_FIRSTNAME,x,cmp_op); 1375 queryItem.addCond(cond); 1376 if (cmp_op.equals(QueryBuilder.CASE_INSENSITIVE_MATCH) || cmp_op.equals(QueryBuilder.CASE_SENSITIVE_MATCH) || cmp_op.equals(QueryBuilder.USER_CASE_SENSITIVE_MATCH) || cmp_op.equals(QueryBuilder.USER_CASE_INSENSITIVE_MATCH)) { 1379 hitDb = true; 1380 } else { 1381 1383 if (PersonDO.cache.isFull()) { 1384 PersonDO DO = null; 1387 PersonDataStruct DS = null; 1388 for ( Iterator iter = (new HashSet(cacheHits.values())).iterator(); iter.hasNext();) { 1389 try { 1390 boolean findInTransactionCache = false; 1391 DS = (PersonDataStruct)iter.next(); 1392 if(transaction!=null && _tr_(transaction).getTransactionCache()!=null) { 1393 DO = (PersonDO)_tr_(transaction).getTransactionCache().getDOByHandle(cachePrefix+DS.get_OId().toString()); 1394 if(DO != null) 1395 findInTransactionCache = true; 1396 } 1397 if(!findInTransactionCache){ 1398 DO = (PersonDO)PersonDO.createExisting(DS.get_OId(), transaction); 1399 } 1400 }catch (Exception ex) { 1401 System.out.println("Error in query member stuff"); 1402 } 1403 1404 String m = DO.getFirstName(); 1405 if(!QueryBuilder.compare( m, x, cmp_op )) { 1406 try { 1407 String cacheHandle = DO.get_CacheHandle(); 1408 cacheHits.remove(cacheHandle); 1409 } catch (DatabaseManagerException e) { 1410 throw new DataObjectException("Error in loading data object's handle."); 1411 } 1412 } 1413 } 1414 } 1415 } 1416 builder.addWhere( PersonDO.FirstName, x, cmp_op ); 1423 } 1424 1425 1434 public void setQueryFirstName(String x) 1435 throws DataObjectException, QueryException { 1436 setQueryFirstName(x, QueryBuilder.EQUAL); 1437 } 1438 1439 1446 public void addOrderByFirstName(boolean direction_flag) { 1447 hitDb = true; 1448 builder.addOrderByColumn("firstName", (direction_flag) ? "ASC" : "DESC"); 1449 } 1450 1451 1452 1458 public void addOrderByFirstName() { 1459 hitDb = true; 1460 builder.addOrderByColumn("firstName","ASC"); 1461 } 1462 1463 1464 1465 1468 1480 public void setUserMatchFirstName( String x ) 1481 throws DataObjectException, QueryException { 1482 String y = convertUserSearchValue( x ); 1483 setDBMatchFirstName( y ); 1484 } 1485 1486 1487 1488 1500 public void setDBMatchFirstName(String x ) 1501 throws DataObjectException, QueryException { 1502 if (containsWildcards(x) || builder.getUserStringAppendWildcard()) { 1503 builder.addMatchClause( PersonDO.FirstName, x ); 1504 hitDb = true; 1505 } else 1506 setQueryFirstName( x, QueryBuilder.EQUAL ); 1507 } 1508 1509 1510 1512 1513 1523 1524 1525 public void setQueryLastName(String x, String cmp_op ) 1526 throws DataObjectException, QueryException { 1527 1528 String cachePrefix = getLogicalDatabase()+"."; 1529 1530 hasNonOidCond = true; 1531 Condition cond = new Condition(PersonDataStruct.COLUMN_LASTNAME,x,cmp_op); 1532 queryItem.addCond(cond); 1533 if (cmp_op.equals(QueryBuilder.CASE_INSENSITIVE_MATCH) || cmp_op.equals(QueryBuilder.CASE_SENSITIVE_MATCH) || cmp_op.equals(QueryBuilder.USER_CASE_SENSITIVE_MATCH) || cmp_op.equals(QueryBuilder.USER_CASE_INSENSITIVE_MATCH)) { 1536 hitDb = true; 1537 } else { 1538 1540 if (PersonDO.cache.isFull()) { 1541 PersonDO DO = null; 1544 PersonDataStruct DS = null; 1545 for ( Iterator iter = (new HashSet(cacheHits.values())).iterator(); iter.hasNext();) { 1546 try { 1547 boolean findInTransactionCache = false; 1548 DS = (PersonDataStruct)iter.next(); 1549 if(transaction!=null && _tr_(transaction).getTransactionCache()!=null) { 1550 DO = (PersonDO)_tr_(transaction).getTransactionCache().getDOByHandle(cachePrefix+DS.get_OId().toString()); 1551 if(DO != null) 1552 findInTransactionCache = true; 1553 } 1554 if(!findInTransactionCache){ 1555 DO = (PersonDO)PersonDO.createExisting(DS.get_OId(), transaction); 1556 } 1557 }catch (Exception ex) { 1558 System.out.println("Error in query member stuff"); 1559 } 1560 1561 String m = DO.getLastName(); 1562 if(!QueryBuilder.compare( m, x, cmp_op )) { 1563 try { 1564 String cacheHandle = DO.get_CacheHandle(); 1565 cacheHits.remove(cacheHandle); 1566 } catch (DatabaseManagerException e) { 1567 throw new DataObjectException("Error in loading data object's handle."); 1568 } 1569 } 1570 } 1571 } 1572 } 1573 builder.addWhere( PersonDO.LastName, x, cmp_op ); 1580 } 1581 1582 1591 public void setQueryLastName(String x) 1592 throws DataObjectException, QueryException { 1593 setQueryLastName(x, QueryBuilder.EQUAL); 1594 } 1595 1596 1603 public void addOrderByLastName(boolean direction_flag) { 1604 hitDb = true; 1605 builder.addOrderByColumn("lastName", (direction_flag) ? "ASC" : "DESC"); 1606 } 1607 1608 1609 1615 public void addOrderByLastName() { 1616 hitDb = true; 1617 builder.addOrderByColumn("lastName","ASC"); 1618 } 1619 1620 1621 1622 1625 1637 public void setUserMatchLastName( String x ) 1638 throws DataObjectException, QueryException { 1639 String y = convertUserSearchValue( x ); 1640 setDBMatchLastName( y ); 1641 } 1642 1643 1644 1645 1657 public void setDBMatchLastName(String x ) 1658 throws DataObjectException, QueryException { 1659 if (containsWildcards(x) || builder.getUserStringAppendWildcard()) { 1660 builder.addMatchClause( PersonDO.LastName, x ); 1661 hitDb = true; 1662 } else 1663 setQueryLastName( x, QueryBuilder.EQUAL ); 1664 } 1665 1666 1667 1669 1670 1680 1681 1682 public void setQueryPhoneNumber(String x, String cmp_op ) 1683 throws DataObjectException, QueryException { 1684 1685 String cachePrefix = getLogicalDatabase()+"."; 1686 1687 hasNonOidCond = true; 1688 Condition cond = new Condition(PersonDataStruct.COLUMN_PHONENUMBER,x,cmp_op); 1689 queryItem.addCond(cond); 1690 if (cmp_op.equals(QueryBuilder.CASE_INSENSITIVE_MATCH) || cmp_op.equals(QueryBuilder.CASE_SENSITIVE_MATCH) || cmp_op.equals(QueryBuilder.USER_CASE_SENSITIVE_MATCH) || cmp_op.equals(QueryBuilder.USER_CASE_INSENSITIVE_MATCH)) { 1693 hitDb = true; 1694 } else { 1695 1697 if (PersonDO.cache.isFull()) { 1698 PersonDO DO = null; 1701 PersonDataStruct DS = null; 1702 for ( Iterator iter = (new HashSet(cacheHits.values())).iterator(); iter.hasNext();) { 1703 try { 1704 boolean findInTransactionCache = false; 1705 DS = (PersonDataStruct)iter.next(); 1706 if(transaction!=null && _tr_(transaction).getTransactionCache()!=null) { 1707 DO = (PersonDO)_tr_(transaction).getTransactionCache().getDOByHandle(cachePrefix+DS.get_OId().toString()); 1708 if(DO != null) 1709 findInTransactionCache = true; 1710 } 1711 if(!findInTransactionCache){ 1712 DO = (PersonDO)PersonDO.createExisting(DS.get_OId(), transaction); 1713 } 1714 }catch (Exception ex) { 1715 System.out.println("Error in query member stuff"); 1716 } 1717 1718 String m = DO.getPhoneNumber(); 1719 if(!QueryBuilder.compare( m, x, cmp_op )) { 1720 try { 1721 String cacheHandle = DO.get_CacheHandle(); 1722 cacheHits.remove(cacheHandle); 1723 } catch (DatabaseManagerException e) { 1724 throw new DataObjectException("Error in loading data object's handle."); 1725 } 1726 } 1727 } 1728 } 1729 } 1730 builder.addWhere( PersonDO.PhoneNumber, x, cmp_op ); 1737 } 1738 1739 1748 public void setQueryPhoneNumber(String x) 1749 throws DataObjectException, QueryException { 1750 setQueryPhoneNumber(x, QueryBuilder.EQUAL); 1751 } 1752 1753 1760 public void addOrderByPhoneNumber(boolean direction_flag) { 1761 hitDb = true; 1762 builder.addOrderByColumn("phoneNumber", (direction_flag) ? "ASC" : "DESC"); 1763 } 1764 1765 1766 1772 public void addOrderByPhoneNumber() { 1773 hitDb = true; 1774 builder.addOrderByColumn("phoneNumber","ASC"); 1775 } 1776 1777 1778 1779 1782 1794 public void setUserMatchPhoneNumber( String x ) 1795 throws DataObjectException, QueryException { 1796 String y = convertUserSearchValue( x ); 1797 setDBMatchPhoneNumber( y ); 1798 } 1799 1800 1801 1802 1814 public void setDBMatchPhoneNumber(String x ) 1815 throws DataObjectException, QueryException { 1816 if (containsWildcards(x) || builder.getUserStringAppendWildcard()) { 1817 builder.addMatchClause( PersonDO.PhoneNumber, x ); 1818 hitDb = true; 1819 } else 1820 setQueryPhoneNumber( x, QueryBuilder.EQUAL ); 1821 } 1822 1823 1824 1826 1851 public QueryBuilder getQueryBuilder() { 1852 hitDb = true; 1853 return builder; 1854 } 1855 1856 1884 public void or() { 1885 hitDb = true; 1886 builder.addWhereOr(); 1887 } 1888 1889 1915 public void openParen() { 1916 hitDb = true; 1917 builder.addWhereOpenParen(); 1918 } 1919 1920 1931 public void closeParen() { 1932 hitDb = true; 1933 builder.addWhereCloseParen(); 1934 } 1935} 1936 | Popular Tags |