| 1 21 22 package org.opensubsystems.patterns.listdata.persist.db; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import java.sql.Timestamp ; 29 import java.text.SimpleDateFormat ; 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import org.opensubsystems.core.data.DataConstant; 36 import org.opensubsystems.core.data.DataObject; 37 import org.opensubsystems.core.error.OSSDatabaseAccessException; 38 import org.opensubsystems.core.error.OSSException; 39 import org.opensubsystems.core.error.OSSInvalidDataException; 40 import org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl; 41 import org.opensubsystems.core.persist.db.DatabaseDependentClassManager; 42 import org.opensubsystems.core.persist.db.DatabaseFactoryImpl; 43 import org.opensubsystems.core.persist.db.DatabaseImpl; 44 import org.opensubsystems.core.persist.db.DatabaseSchemaImpl; 45 import org.opensubsystems.core.util.CallContext; 46 import org.opensubsystems.core.util.DatabaseUtils; 47 import org.opensubsystems.core.util.DateUtils; 48 import org.opensubsystems.core.util.GlobalConstants; 49 import org.opensubsystems.core.util.StringUtils; 50 import org.opensubsystems.patterns.listdata.data.DataCondition; 51 import org.opensubsystems.patterns.listdata.data.ListOptions; 52 import org.opensubsystems.patterns.listdata.data.SimpleRule; 53 54 62 public class ListDatabaseUtils 63 { 64 66 70 public static final String LIST_INDEX_PREFIX = "LST_"; 71 72 76 private static final String [] OPERANDS = new String [] {"", 77 "=", 78 "in", 79 "not in", 80 "<>", 81 ">", 82 ">=", 83 "<", 84 "<=", 85 "like", 86 "like", 87 "", 88 "=", 89 "not like", 90 "not like", 91 "like", 92 "like", 93 "not like", 94 "not like", 95 "like", 96 "like", 97 "not like", 98 "not like", 99 "like", 100 "like", 101 "not like", 102 "not like", 103 }; 104 105 107 113 public static ListDatabaseUtils getInstance( 114 ) throws OSSException 115 { 116 return (ListDatabaseUtils)DatabaseDependentClassManager.getInstance( 118 ListDatabaseUtils.class); 119 } 120 121 143 public String getConstructListIndexName( 144 String strTableName, 145 String strColumnName 146 ) 147 { 148 if (GlobalConstants.ERROR_CHECKING) 149 { 150 assert strTableName.length() > 2 : "Table name is too short."; 152 assert strColumnName.length() > 1 : "Column name is too short."; 153 } 154 155 StringBuffer buffer = new StringBuffer (); 156 157 int iTableNameLength = strTableName.length(); 158 int iColumnNameLength = strColumnName.length(); 159 160 buffer.append(LIST_INDEX_PREFIX); 162 163 if (strTableName.toUpperCase().startsWith(DatabaseSchemaImpl.getSchemaPrefix()) 164 && strTableName.length() > 3) 165 { 166 strTableName = strTableName.substring( 168 DatabaseSchemaImpl.getSchemaPrefix().length(), iTableNameLength); 169 } 170 171 if (iTableNameLength < 8) 173 { 174 buffer.append(strTableName); 175 } 176 else 177 { 178 179 strTableName = strTableName.replaceAll("_", ""); 181 iTableNameLength = strTableName.length(); 182 buffer.append(strTableName.substring(0, 3)); 184 buffer.append(strTableName.substring(iTableNameLength - 4, iTableNameLength)); 186 } 187 188 if (iColumnNameLength < 8) 190 { 191 buffer.append(strColumnName); 192 } 193 else 194 { 195 strColumnName = strColumnName.replaceAll("_", ""); 197 iColumnNameLength = strColumnName.length(); 198 buffer.append(strColumnName.substring(0, 3)); 200 buffer.append(strColumnName.substring(iColumnNameLength - 4, iColumnNameLength)); 202 } 203 204 return buffer.toString(); 205 } 206 207 217 public List getObjectList( 218 ListOptions options, 219 String query, 220 ListDatabaseFactory factory, 221 ListDatabaseSchema schema 222 ) throws OSSException 223 { 224 List lstObjects = null; 225 Connection cntConnection = null; 226 ResultSet rsQueryResults = null; 227 PreparedStatement pstmQuery = null; 228 229 String strOriginalQuery = query; 230 232 try 233 { 234 cntConnection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(true); 236 237 try 238 { 239 int iCount = 0; 240 241 if ((!DatabaseImpl.getInstance().hasAbsolutePositioningSupport()) 242 || (DatabaseImpl.getInstance().preferCountToLast())) 243 { 244 250 iCount = getSelectCount(cntConnection, query, schema, options.getDomainId()); 252 253 setOptionPositioning(iCount, options); 255 256 lstObjects = allocateObjectList(options, iCount); 258 259 query = preprocessSelectQueryForCreationDate(query, options, schema); 262 strOriginalQuery = query; 266 267 query = preprocessSelectQuery(query, options, schema); 270 271 if (DatabaseImpl.getInstance().hasSelectListRangeSupport()) 273 { 274 pstmQuery = cntConnection.prepareStatement(query); 275 } 276 else 277 { 278 pstmQuery = cntConnection.prepareStatement(query, 281 ((DatabaseFactoryImpl) factory).getTypeSelectListResultSet(), 282 ((DatabaseFactoryImpl) factory).getConcurrencySelectListResultSet()); 283 } 284 if (schema.isInDomain()) 285 { 286 pstmQuery.setInt(1, options.getDomainId()); 287 } 288 289 291 rsQueryResults = pstmQuery.executeQuery(); 292 293 if (lstObjects != null) 294 { 295 if (DatabaseImpl.getInstance().hasSelectListRangeSupport()) 299 { 300 rsQueryResults.next(); 301 } 302 else 303 { 304 rsQueryResults.absolute(options.getBeginPosition()); 305 } 306 } 309 } 310 else 311 { 312 318 query = preprocessSelectQueryForCreationDate(query, options, schema); 320 strOriginalQuery = query; 324 325 pstmQuery = cntConnection.prepareStatement( 328 query, 329 DatabaseImpl.getInstance().getSelectListResultSetType(), 330 DatabaseImpl.getInstance().getSelectListResultSetConcurrency() 331 ); 332 if (schema.isInDomain()) 333 { 334 pstmQuery.setInt(1, options.getDomainId()); 335 } 336 338 rsQueryResults = pstmQuery.executeQuery(); 340 342 iCount = getTotalRecords(rsQueryResults, options); 345 347 setOptionPositioning(iCount, options); 349 350 lstObjects = allocateObjectList(options, iCount); 352 353 if (lstObjects != null) 354 { 355 rsQueryResults.absolute(options.getBeginPosition()); 357 } 359 } 360 361 363 if (lstObjects != null) 367 { 368 if (GlobalConstants.ERROR_CHECKING) 369 { 370 assert iCount > 0 371 : "List should be null for empty result"; 372 } 373 374 int iHelpCounter = 0; 375 int[] columnCodes = options.getRetrieveColumnCodes(); 377 378 while ((iHelpCounter == 0 || rsQueryResults.next()) 381 && ((iHelpCounter < options.getPageSize()) 382 || (options.getPageSize() == ListOptions.PAGE_SIZE_ALL))) 383 { 384 iHelpCounter++; 385 lstObjects.add(factory.load(rsQueryResults, columnCodes, 1)); 386 } 387 } 389 } 390 finally 391 { 392 DatabaseUtils.closeResultSetAndStatement(rsQueryResults, pstmQuery); 393 } 394 395 397 lstObjects = getObjectListKeepSelected(factory, cntConnection, strOriginalQuery, 400 options, lstObjects, schema); 401 } 402 catch (SQLException sqleExc) 403 { 404 throw new OSSDatabaseAccessException( 405 "Failed to retrieve specified list of data objects" + 406 " from the database.", sqleExc); 407 } 408 finally 409 { 410 DatabaseConnectionFactoryImpl.getInstance().returnConnection(cntConnection); 411 } 412 413 return lstObjects; 414 } 415 416 429 protected List getObjectListKeepSelected( 430 ListDatabaseFactory factory, 431 Connection cntConnection, 432 String query, 433 ListOptions options, 434 List lstObjects, 435 ListDatabaseSchema schema 436 ) throws OSSException, SQLException  437 { 438 if ((options.getIsKeepSelectedChecked()) 441 && (options.getSelectedItemIDs().length() > 0)) 442 { 443 Iterator items = null; 444 int iActualID; 445 int iIndex = 0; 446 447 StringBuffer sbSelectedIDs = new StringBuffer (); 448 StringBuffer sbTemp = new StringBuffer (); 449 450 sbSelectedIDs.append(","); 452 sbSelectedIDs.append(options.getSelectedItemIDs().replaceAll(" ", "")); 453 sbSelectedIDs.append(","); 454 455 items = lstObjects.iterator(); 456 while (items.hasNext()) 457 { 458 iActualID = ((DataObject)items.next()).getId(); 459 sbTemp.append(","); 461 sbTemp.append(iActualID); 462 sbTemp.append(","); 463 464 iIndex = sbSelectedIDs.indexOf(sbTemp.toString()); 468 if (iIndex > -1) 469 { 470 sbSelectedIDs.delete(iIndex, iIndex + sbTemp.length() - 1); 471 } 472 473 sbTemp.delete(0, sbTemp.length()); 474 } 475 iIndex = sbSelectedIDs.indexOf(","); 477 if (iIndex != -1) 478 { 479 sbSelectedIDs.deleteCharAt(iIndex); 480 } 481 iIndex = sbSelectedIDs.lastIndexOf(","); 482 if (iIndex != -1) 483 { 484 sbSelectedIDs.deleteCharAt(iIndex); 485 } 486 487 if (sbSelectedIDs.length() > 0) 491 { 492 int iIndexOrderBy = query.toLowerCase().indexOf(" order by "); 493 if (iIndexOrderBy > -1) 494 { 495 sbTemp.append(query.substring(0, iIndexOrderBy)); 496 sbTemp.append(" and ID in ("); 497 sbTemp.append(sbSelectedIDs); 498 sbTemp.append(") "); 499 sbTemp.append(query.substring(iIndexOrderBy, query.length())); 500 } 501 else 502 { 503 sbTemp.append(query); 504 sbTemp.append(" and ID in ("); 505 sbTemp.append(sbSelectedIDs); 506 sbTemp.append(") "); 507 } 508 509 ResultSet rsQueryResults = null; 510 PreparedStatement pstmQuery = null; 511 512 try 513 { 514 pstmQuery = cntConnection.prepareStatement(sbTemp.toString()); 515 if (schema.isInDomain()) 516 { 517 pstmQuery.setInt(1, options.getDomainId()); 518 } 519 rsQueryResults = pstmQuery.executeQuery(); 520 521 int[] columnCodes = options.getRetrieveColumnCodes(); 525 iIndex = 0; 526 527 while (rsQueryResults.next()) 528 { 529 lstObjects.add(iIndex++, factory.load(rsQueryResults, columnCodes, 1)); 530 } 531 } 532 finally 533 { 534 DatabaseUtils.closeResultSetAndStatement(rsQueryResults, pstmQuery); 535 } 536 } 537 } 538 539 return lstObjects; 540 } 541 542 564 public String getSelectList( 565 String strTableName, 566 ListOptions options, 567 ListDatabaseSchema schema 568 ) throws OSSException 569 { 570 if (GlobalConstants.ERROR_CHECKING) 571 { 572 assert ((strTableName != null) && (strTableName.length() > 0)) 573 : "Table name must be specified"; 574 assert options != null : "List options must be specified."; 575 assert options.getSecurityFilter() != null 576 : "If I don't have right to view, this shouldn't be called"; 577 assert options.getDefinition() != null 578 : "We have to have filter to view something (even if we want to view all)"; 579 } 580 581 586 List filterConditions = null; 587 List securityConditions = null; 588 List extraConditions = null; 589 List allConditions = new ArrayList (); 591 Iterator items; 592 593 598 StringBuffer parentChildAssociationCondition = null; 601 if (options.getParentDataType() != DataConstant.NO_DATA_TYPE) 602 { 603 DataCondition parentChildAssociation = null; 604 605 parentChildAssociationCondition = new StringBuffer (); 606 parentChildAssociation = new DataCondition(options.getParentDataType(), 607 DataCondition.OPERAND_EQUALS, 608 new Integer (options.getParentId()), 609 DataCondition.VALUE_TYPE_ID); 610 allConditions.add(parentChildAssociation); 611 parseCondition(parentChildAssociationCondition, parentChildAssociation, schema); 613 } 614 615 if (options.getSecurityFilter() != null) 617 { 618 securityConditions = options.getSecurityFilter().getConditions(); 619 if (securityConditions != null) 620 { 621 allConditions.addAll(securityConditions); 622 } 623 } 624 625 filterConditions = options.getDefinition().getRule().getConditions(); 627 if (filterConditions != null) 628 { 629 allConditions.addAll(filterConditions); 630 } 631 632 SimpleRule extraFilter; 634 635 extraFilter = options.getExtraFilter(); 636 if (extraFilter != null) 637 { 638 extraConditions = extraFilter.getConditions(); 639 if (extraConditions != null) 640 { 641 allConditions.addAll(extraConditions); 642 } 643 } 644 645 649 String [] otherFromWhere = schema.getJoinFromWhere(allConditions, 650 options.getRetrieveColumnCodes()); 651 652 655 StringBuffer buffer = new StringBuffer (); 656 657 buffer.append("select "); 659 660 if (options.isDistinct()) 661 { 662 buffer.append("distinct "); 663 } 664 665 schema.getColumns(true, options.getRetrieveColumnCodes(), null, null, buffer); 666 buffer.append(" from "); 667 buffer.append(strTableName); 669 670 if (otherFromWhere != null) 673 { 674 buffer.append(otherFromWhere[0]); 675 } 676 677 buffer.append(" where "); 678 680 if (schema.isInDomain()) 681 { 682 buffer.append(strTableName); 683 buffer.append(".DOMAIN_ID = ?"); 684 } 685 else 688 { 689 buffer.append(strTableName); 690 buffer.append(".ID is not null"); 691 } 692 693 if ((otherFromWhere != null) && (otherFromWhere[1].length() > 0)) 696 { 697 buffer.append(" and "); 698 buffer.append(otherFromWhere[1]); 699 } 700 701 705 if ((parentChildAssociationCondition != null) 706 && (parentChildAssociationCondition.length() > 0)) 707 { 708 buffer.append(" and "); 709 buffer.append(parentChildAssociationCondition); 710 } 711 712 StringBuffer sbSecurityBuffer = null; 714 if (securityConditions != null) 715 { 716 sbSecurityBuffer = new StringBuffer (); 717 for (items = securityConditions.iterator(); items.hasNext();) 718 { 719 parseCondition(sbSecurityBuffer, (DataCondition)items.next(), schema); 720 if (items.hasNext()) 721 { 722 sbSecurityBuffer.append(" or "); 726 } 727 } 728 if (sbSecurityBuffer.length() > 0) 729 { 730 buffer.append(" and ("); 731 buffer.append(sbSecurityBuffer); 732 buffer.append(")"); 733 } 734 } 735 736 if (filterConditions != null) 738 { 739 int iFilterType; 740 741 iFilterType = options.getDefinition().getRule().getConditionsOperand(); 742 743 buffer.append(" and ("); 744 for (items = filterConditions.iterator(); items.hasNext();) 745 { 746 parseCondition(buffer, (DataCondition)items.next(), schema); 747 if (items.hasNext()) 748 { 749 switch (iFilterType) 751 { 752 case (SimpleRule.LOGICAL_AND) : 753 { 754 buffer.append(" and "); 755 break; 756 } 757 case (SimpleRule.LOGICAL_OR) : 758 { 759 buffer.append(" or "); 760 break; 761 } 762 default : 763 { 764 assert false : "Invalid filter type " + iFilterType; 765 } 766 } 767 } 768 } 769 buffer.append(")"); 770 } 771 772 if (extraConditions != null) 774 { 775 int iFilterType; 776 777 iFilterType = options.getExtraLogicalOperation(); 778 779 buffer.append(" and ("); 780 for (items = extraConditions.iterator(); items.hasNext();) 781 { 782 parseCondition(buffer, (DataCondition)items.next(), schema); 783 if (items.hasNext()) 784 { 785 switch (iFilterType) 787 { 788 case (SimpleRule.LOGICAL_AND) : 789 { 790 buffer.append(" and "); 791 break; 792 } 793 case (SimpleRule.LOGICAL_OR) : 794 { 795 buffer.append(" or "); 796 break; 797 } 798 default : 799 { 800 assert false : "Invalid filter type " + iFilterType; 801 } 802 } 803 } 804 } 805 buffer.append(")"); 806 } 807 808 if ((options.getIgnoredItemIDs() != null) 810 && (options.getIgnoredItemIDs().length() > 0)) 811 { 812 buffer.append(" and "); 813 buffer.append(strTableName); 815 buffer.append(".ID NOT IN("); 816 buffer.append(options.getIgnoredItemIDs()); 817 buffer.append(")"); 818 } 819 820 int[] sortColumns; 822 823 sortColumns = options.getOrderColumnCodes(); 824 if ((sortColumns != null) && (sortColumns.length > 0)) 825 { 826 buffer.append(" order by "); 827 schema.getColumns(true, sortColumns, null, options.getOrderDirections(), buffer); 828 } 829 830 return buffer.toString(); 831 } 832 833 847 public String getWhereClause( 848 String strTableName, 849 int[] arrIds, 850 SimpleRule securityFilter, 851 ListDatabaseSchema schema 852 ) throws OSSException 853 { 854 if (GlobalConstants.ERROR_CHECKING) 855 { 856 assert securityFilter != null 857 : "If I don't have rights for action, this shouldn't be called"; 858 } 859 860 StringBuffer buffer = new StringBuffer (); 861 862 buffer.append(" where "); 863 864 if (schema.isInDomain()) 865 { 866 buffer.append(strTableName); 869 buffer.append(".DOMAIN_ID = ? "); 870 } 871 872 if ((arrIds != null) && (arrIds.length > 0)) 873 { 874 if (schema.isInDomain()) 877 { 878 buffer.append(" and "); 879 } 880 buffer.append(strTableName); 881 buffer.append(".ID in ("); 882 buffer.append(StringUtils.parseIntArrayToString(arrIds, ",")); 883 buffer.append(")"); 884 } 885 886 892 List securityConditions = null; 895 896 securityConditions = securityFilter.getConditions(); 897 if (securityConditions != null) 898 { 899 String [] otherFromWhere; 903 904 buffer.append(" and "); 906 buffer.append(strTableName); 907 buffer.append(".ID in (select "); 908 buffer.append(strTableName); 909 buffer.append(".ID from "); 910 buffer.append(strTableName); 911 otherFromWhere = schema.getJoinFromWhere(securityConditions, null); 914 if (otherFromWhere != null) 915 { 916 buffer.append(otherFromWhere[0]); 917 } 918 buffer.append(" where "); 920 if (schema.isInDomain()) 921 { 922 buffer.append(strTableName); 923 buffer.append(".DOMAIN_ID="); 924 buffer.append(CallContext.getInstance().getCurrentDomainId()); 925 } 926 else 929 { 930 buffer.append(strTableName); 931 buffer.append(".ID is not null "); 932 } 933 934 if ((otherFromWhere != null) && (otherFromWhere[1].length() > 0)) 935 { 936 buffer.append(" and "); 937 buffer.append(otherFromWhere[1]); 938 } 939 buffer.append(" and ("); 940 941 for (int iCount = 0; iCount < securityConditions.size(); iCount++) 943 { 944 if (iCount > 0) 945 { 946 buffer.append(" or "); 947 } 948 parseCondition(buffer, (DataCondition) securityConditions.get(iCount), 949 schema); 950 } 951 buffer.append("))"); 952 } 953 954 return buffer.toString(); 955 } 956 957 974 public String getWhereClause( 975 String strTableName, 976 int[] arrIds, 977 int[] arrDomainIds, 978 SimpleRule securityFilter, 979 ListDatabaseSchema schema 980 ) throws OSSException 981 { 982 if (GlobalConstants.ERROR_CHECKING) 983 { 984 assert securityFilter != null 985 : "If I don't have rights for action, this shouldn't be called"; 986 } 987 988 StringBuffer buffer = new StringBuffer (); 989 990 if (arrDomainIds != null && arrDomainIds.length > 0) 991 { 992 String strDomainIDs; 993 994 strDomainIDs = StringUtils.parseIntArrayToString(arrDomainIds, ","); 995 buffer.append(" where "); 996 997 if (schema.isInDomain()) 998 { 999 buffer.append(strTableName); 1002 buffer.append(".DOMAIN_ID IN ("); 1003 buffer.append(strDomainIDs); 1004 buffer.append(") "); 1005 } 1006 1007 if ((arrIds != null) && (arrIds.length > 0)) 1008 { 1009 if (schema.isInDomain()) 1012 { 1013 buffer.append(" and "); 1014 } 1015 buffer.append(strTableName); 1016 buffer.append(".ID in ("); 1017 buffer.append(StringUtils.parseIntArrayToString(arrIds, ",")); 1018 buffer.append(")"); 1019 } 1020 1021 List securityConditions = null; 1024 1025 securityConditions = securityFilter.getConditions(); 1026 if (securityConditions != null) 1027 { 1028 String [] otherFromWhere; 1032 1033 buffer.append(" and "); 1035 buffer.append(strTableName); 1036 buffer.append(".ID in (select "); 1037 buffer.append(strTableName); 1038 buffer.append(".ID from "); 1039 buffer.append(strTableName); 1040 otherFromWhere = schema.getJoinFromWhere(securityConditions, null); 1043 if (otherFromWhere != null) 1044 { 1045 buffer.append(otherFromWhere[0]); 1046 } 1047 buffer.append(" where "); 1049 if (schema.isInDomain()) 1050 { 1051 buffer.append(strTableName); 1052 buffer.append(".DOMAIN_ID IN ("); 1053 buffer.append(strDomainIDs); 1054 buffer.append(") "); 1055 } 1056 else 1059 { 1060 buffer.append(strTableName); 1061 buffer.append(".ID is not null "); 1062 } 1063 1064 if ((otherFromWhere != null) && (otherFromWhere[1].length() > 0)) 1065 { 1066 buffer.append(" and "); 1067 buffer.append(otherFromWhere[1]); 1068 } 1069 buffer.append(" and ("); 1070 1071 for (int iCount = 0; iCount < securityConditions.size(); iCount++) 1073 { 1074 if (iCount > 0) 1075 { 1076 buffer.append(" or "); 1077 } 1078 parseCondition(buffer, (DataCondition) securityConditions.get(iCount), 1079 schema); 1080 } 1081 buffer.append("))"); 1082 } 1083 } 1084 else 1085 { 1086 getWhereClause(strTableName, arrIds, securityFilter, schema); 1088 } 1089 1090 1091 return buffer.toString(); 1092 } 1093 1094 1096 1108 protected String preprocessSelectQuery( 1109 String inputQuery, 1110 ListOptions options, 1111 ListDatabaseSchema schema 1112 ) throws OSSException 1113 { 1114 return inputQuery; 1116 } 1117 1118 1130 protected String preprocessSelectQueryForCreationDate( 1131 String inputQuery, 1132 ListOptions options, 1133 ListDatabaseSchema schema 1134 ) throws OSSException 1135 { 1136 return inputQuery; 1138 } 1139 1140 1150 protected void parseCondition( 1151 StringBuffer strBuffer, 1152 DataCondition condition, 1153 ListDatabaseSchema schema 1154 ) throws OSSException 1155 { 1156 Object objValue; 1157 1158 objValue = condition.getValue(); 1159 if ((condition.getAttribute() != DataCondition.NO_ATTRIBUTE) || (objValue != null)) 1160 { 1161 if (condition.getOperation() == DataCondition.OPERAND_EQUALS_OR_NULL) 1163 { 1164 strBuffer.append("("); 1165 schema.getColumns(true, new int[] {condition.getAttribute()}, null, null, strBuffer); 1166 strBuffer.append(getSQLOperation(condition)); 1167 strBuffer.append(convertValueToSQL(condition.getValue(), condition.getValueType())); 1168 strBuffer.append(" or "); 1169 schema.getColumns(true, new int[] {condition.getAttribute()}, null, null, strBuffer); 1170 strBuffer.append(" is null ) "); 1171 } 1172 else if (condition.getOperation() != DataCondition.OPERAND_SQL_QUERY) 1173 { 1174 boolean caseUnsensitive 1175 = condition.getOperation() == DataCondition.OPERAND_LIKE_CASEUNSENSITIVE 1176 || condition.getOperation() == DataCondition.OPERAND_NOT_LIKE_CASEUNSENSITIVE 1177 || condition.getOperation() == DataCondition.OPERAND_STARTS_CASEUNSENSITIVE 1178 || condition.getOperation() == DataCondition.OPERAND_NOT_STARTS_CASEUNSENSITIVE 1179 || condition.getOperation() == DataCondition.OPERAND_ENDS_CASEUNSENSITIVE 1180 || condition.getOperation() == DataCondition.OPERAND_NOT_ENDS_CASEUNSENSITIVE 1181 || condition.getOperation() == DataCondition.OPERAND_EQUALS_CASEUNSENSITIVE 1182 || condition.getOperation() == DataCondition.OPERAND_NOT_EQUALS_CASEUNSENSITIVE; 1183 1184 if (caseUnsensitive) 1185 { 1186 strBuffer.append("UPPER("); 1187 } 1188 schema.getColumns(true, new int[] {condition.getAttribute()}, null, null, strBuffer); 1189 if (caseUnsensitive) 1190 { 1191 strBuffer.append(")"); 1192 } 1193 1194 strBuffer.append(" "); 1195 strBuffer.append(getSQLOperation(condition)); 1196 strBuffer.append(" "); 1197 } 1198 1199 if (condition.getOperation() != DataCondition.OPERAND_EQUALS_OR_NULL) 1202 { 1203 strBuffer.append(getSQLValue(condition)); 1204 } 1205 } 1206 } 1207 1208 1215 protected String getSQLOperation( 1216 DataCondition condition 1217 ) throws OSSInvalidDataException 1218 { 1219 String strReturn; 1220 int iOperation; 1221 Object objValue; 1222 1223 iOperation = condition.getOperation(); 1224 objValue = condition.getValue(); 1225 if ((iOperation != DataCondition.OPERAND_IN) 1226 && (iOperation != DataCondition.OPERAND_NOT_IN) 1227 && ("null".equalsIgnoreCase(convertValueToSQL(objValue, 1228 condition.getValueType())))) 1229 { 1230 switch (iOperation) 1231 { 1232 case (DataCondition.OPERAND_EQUALS) : 1233 { 1234 strReturn = "is"; 1235 break; 1236 } 1237 case (DataCondition.OPERAND_NOT_EQUALS) : 1238 { 1239 strReturn = "is not"; 1240 break; 1241 } 1242 default: 1243 { 1244 strReturn = OPERANDS[iOperation]; 1245 break; 1246 } 1247 } 1248 } 1249 else 1250 { 1251 strReturn = OPERANDS[iOperation]; 1252 } 1253 1254 return strReturn; 1255 } 1256 1257 1266 protected String convertValueToSQL( 1267 Object objValue, 1268 int iValueType 1269 ) throws OSSInvalidDataException 1270 { 1271 StringBuffer strReturn = new StringBuffer (); 1272 int iHelp; 1273 1274 if (objValue == null) 1275 { 1276 strReturn.append("null"); 1277 } 1278 else 1279 { 1280 switch (iValueType) 1281 { 1282 case (DataCondition.VALUE_TYPE_ID) : 1283 { 1284 iHelp = ((Integer ) objValue).intValue(); 1285 if (iHelp == DataObject.NEW_ID) 1286 { 1287 strReturn.append("null"); 1288 } 1289 else 1290 { 1291 strReturn.append(Integer.toString(iHelp)); 1292 } 1293 break; 1294 } 1295 case (DataCondition.VALUE_TYPE_BOOLEAN) : 1296 { 1297 String strValue = objValue.toString(); 1298 if (strValue.equals("0") || strValue.equals("1")) 1299 { 1300 strReturn.append(strValue); 1301 } 1302 else 1303 { 1304 strReturn.append(((Boolean ) objValue).booleanValue() ? "1" : "0"); 1306 } 1307 break; 1308 } 1309 case (DataCondition.VALUE_TYPE_INTEGER) : 1310 { 1311 strReturn.append(Integer.toString(((Integer ) objValue).intValue())); 1312 break; 1313 } 1314 case (DataCondition.VALUE_TYPE_DOUBLE) : 1315 { 1316 strReturn.append(Double.toString(((Double ) objValue).doubleValue())); 1317 break; 1318 } 1319 case (DataCondition.VALUE_TYPE_STRING) : 1320 { 1321 strReturn.append("'"); 1322 strReturn.append((String ) objValue); 1323 strReturn.append("'"); 1324 break; 1325 } 1326 case (DataCondition.VALUE_TYPE_TIMESTAMP) : 1327 { 1328 Timestamp tmstp = DateUtils.parseDateTime( 1329 objValue.toString(), 1330 DateUtils.DATE_TYPE_DATETIME, true); 1331 SimpleDateFormat sdfSQLFormat = new SimpleDateFormat ("''yyyy-MM-dd HH:mm:ss.SSS''"); 1332 strReturn.append(sdfSQLFormat.format(tmstp)); 1333 break; 1334 } 1335 default : 1336 { 1337 assert false : "Not valid value type for data condition."; 1338 } 1339 } 1340 } 1341 return strReturn.toString(); 1342 } 1343 1344 1351 protected String getSQLValue( 1352 DataCondition condition 1353 ) throws OSSInvalidDataException 1354 { 1355 String strValue; 1356 int iOperation; 1357 Object objValue; 1358 1359 iOperation = condition.getOperation(); 1360 objValue = condition.getValue(); 1361 if (objValue == null) 1362 { 1363 strValue = "null"; 1364 } 1365 else 1366 { 1367 if ((iOperation == DataCondition.OPERAND_IN) 1370 || (iOperation == DataCondition.OPERAND_NOT_IN)) 1371 { 1372 StringBuffer buffer = new StringBuffer (); 1373 String strTest; 1374 1375 buffer.append("("); 1376 if (objValue instanceof Object []) 1377 { 1378 Object [] helpArray = (Object [])objValue; 1379 1380 if (GlobalConstants.ERROR_CHECKING) 1381 { 1382 assert helpArray.length > 0 : "IN operator has NO values in array"; 1383 } 1384 1385 for (int iCount = 0; iCount < helpArray.length; iCount++) 1386 { 1387 if (iCount > 0) 1388 { 1389 buffer.append(","); 1390 } 1391 strTest = convertValueToSQL(helpArray[iCount], condition.getValueType()); 1392 buffer.append(strTest); 1393 } 1394 } 1395 else if (objValue instanceof Collection ) 1396 { 1397 Collection helpCol = (Collection )objValue; 1398 boolean bFirst = true; 1399 1400 if (GlobalConstants.ERROR_CHECKING) 1401 { 1402 assert helpCol.size() > 0 : "IN operator has NO values in collection"; 1403 } 1404 1405 for (Iterator items = helpCol.iterator(); items.hasNext();) 1406 { 1407 if (!bFirst) 1408 { 1409 buffer.append(","); 1410 } 1411 strTest = convertValueToSQL(items.next(), condition.getValueType()); 1412 buffer.append(strTest); 1413 bFirst = false; 1414 } 1415 } 1416 else 1417 { 1418 buffer.append(objValue.toString()); 1419 } 1420 buffer.append(")"); 1421 strValue = buffer.toString(); 1422 } 1423 else if (iOperation == DataCondition.OPERAND_LIKE_CASEUNSENSITIVE 1424 || iOperation == DataCondition.OPERAND_NOT_LIKE_CASEUNSENSITIVE) 1425 { 1426 StringBuffer buffer = new StringBuffer (); 1427 buffer.append("'%"); 1428 buffer.append(((String ) objValue).toUpperCase()); 1429 buffer.append("%'"); 1430 strValue = buffer.toString(); 1431 } 1432 else if (iOperation == DataCondition.OPERAND_LIKE_CASESENSITIVE 1433 || iOperation == DataCondition.OPERAND_NOT_LIKE_CASESENSITIVE) 1434 { 1435 StringBuffer buffer = new StringBuffer (); 1436 buffer.append("'%"); 1437 buffer.append((String ) objValue); 1438 buffer.append("%'"); 1439 strValue = buffer.toString(); 1440 } 1441 else if (iOperation == DataCondition.OPERAND_EQUALS_CASEUNSENSITIVE 1442 || iOperation == DataCondition.OPERAND_NOT_EQUALS_CASEUNSENSITIVE) 1443 { 1444 StringBuffer buffer = new StringBuffer (); 1445 buffer.append("'"); 1446 buffer.append(((String ) objValue).toUpperCase()); 1447 buffer.append("'"); 1448 strValue = buffer.toString(); 1449 } 1450 else if (iOperation == DataCondition.OPERAND_EQUALS_CASESENSITIVE 1451 || iOperation == DataCondition.OPERAND_NOT_EQUALS_CASESENSITIVE) 1452 { 1453 StringBuffer buffer = new StringBuffer (); 1454 buffer.append("'"); 1455 buffer.append((String ) objValue); 1456 buffer.append("'"); 1457 strValue = buffer.toString(); 1458 } 1459 else if (iOperation == DataCondition.OPERAND_STARTS_CASESENSITIVE 1460 || iOperation == DataCondition.OPERAND_NOT_STARTS_CASESENSITIVE) 1461 { 1462 StringBuffer buffer = new StringBuffer (); 1463 buffer.append("'"); 1464 buffer.append((String ) objValue); 1465 buffer.append("%'"); 1466 strValue = buffer.toString(); 1467 } 1468 else if (iOperation == DataCondition.OPERAND_STARTS_CASEUNSENSITIVE 1469 || iOperation == DataCondition.OPERAND_NOT_STARTS_CASEUNSENSITIVE) 1470 { 1471 StringBuffer buffer = new StringBuffer (); 1472 buffer.append("'"); 1473 buffer.append(((String ) objValue).toUpperCase()); 1474 buffer.append("%'"); 1475 strValue = buffer.toString(); 1476 } 1477 else if (iOperation == DataCondition.OPERAND_ENDS_CASESENSITIVE 1478 || iOperation == DataCondition.OPERAND_NOT_ENDS_CASESENSITIVE) 1479 { 1480 StringBuffer buffer = new StringBuffer (); 1481 buffer.append("'%"); 1482 buffer.append((String ) objValue); 1483 buffer.append("'"); 1484 strValue = buffer.toString(); 1485 } 1486 else if (iOperation == DataCondition.OPERAND_ENDS_CASEUNSENSITIVE 1487 || iOperation == DataCondition.OPERAND_NOT_ENDS_CASEUNSENSITIVE) 1488 { 1489 StringBuffer buffer = new StringBuffer (); 1490 buffer.append("'%"); 1491 buffer.append(((String ) objValue).toUpperCase()); 1492 buffer.append("'"); 1493 strValue = buffer.toString(); 1494 } 1495 else if (iOperation == DataCondition.OPERAND_SQL_QUERY) 1496 { 1497 strValue = (String ) objValue; 1498 } 1499 else 1500 { 1501 strValue = convertValueToSQL(objValue, condition.getValueType()); 1503 } 1504 } 1505 return strValue; 1506 } 1507 1508 1516 protected ListOptions setOptionPositioning( 1517 int iCount, 1518 ListOptions options 1519 ) 1520 { 1521 if (iCount == 0) 1522 { 1523 options.setBeginPosition(0); 1524 options.setEndPosition(0); 1525 options.setActualPage(0); 1526 } 1527 else 1528 { 1529 if ((options.getBeginPosition() > iCount) 1530 && (options.getPageSize() != ListOptions.PAGE_SIZE_ALL)) 1531 { 1532 options.setBeginPosition( 1533 (((iCount - 1) / options.getPageSize()) 1534 * options.getPageSize()) + 1 1535 ); 1536 } 1537 if (options.getPageSize() >= iCount) 1538 { 1539 options.setBeginPosition(1); 1540 } 1541 1542 if ((options.getBeginPosition() + options.getPageSize() - 1) 1543 > iCount) 1544 { 1545 options.setEndPosition(iCount); 1546 } 1547 else 1548 { 1549 options.setEndPosition(options.getBeginPosition() 1550 + options.getPageSize() - 1); 1551 } 1552 1553 if (options.getPageSize() == ListOptions.PAGE_SIZE_ALL) 1554 { 1555 options.setEndPosition(iCount); 1556 } 1557 else 1558 { 1559 options.setActualPage( 1560 ((options.getBeginPosition() + options.getPageSize() - 2) 1561 / 1562 options.getPageSize()) + 1 1563 ); 1564 } 1565 } 1566 1567 options.setActualListSize(iCount); 1568 1569 return options; 1570 } 1571 1572 1585 protected int getSelectCount( 1586 Connection cntConnection, 1587 String query, 1588 ListDatabaseSchema schema, 1589 int iDomainId 1590 ) throws OSSException, 1591 SQLException  1592 { 1593 PreparedStatement pstmQuery = null; 1594 ResultSet rsQueryResults = null; 1595 1596 int iOutputCount = 0; 1597 1598 StringBuffer buffer = new StringBuffer (); 1600 1601 String strQueryLowerCase = query.toLowerCase(); 1606 1607 int iOrderBy; 1608 1609 try 1610 { 1611 iOrderBy = strQueryLowerCase.indexOf("order by"); 1614 buffer.append("select "); 1615 buffer.append(DatabaseImpl.getInstance().getSQLCountFunctionCall()); 1616 if (iOrderBy != -1) 1617 { 1618 buffer.append(query.substring(strQueryLowerCase.indexOf(" from"), iOrderBy)); 1619 } 1620 else 1621 { 1622 buffer.append(query.substring(strQueryLowerCase.indexOf(" from"))); 1623 } 1624 1625 pstmQuery = cntConnection.prepareStatement(buffer.toString()); 1629 if (schema.isInDomain()) 1630 { 1631 pstmQuery.setInt(1, iDomainId); 1632 } 1633 1635 rsQueryResults = pstmQuery.executeQuery(); 1637 1639 if (rsQueryResults.next()) 1640 { 1641 iOutputCount = rsQueryResults.getInt(1); 1642 } 1643 } 1644 finally 1645 { 1646 DatabaseUtils.closeResultSetAndStatement(rsQueryResults, pstmQuery); 1647 } 1648 1649 return iOutputCount; 1650 } 1651 1652 1661 protected List allocateObjectList( 1662 ListOptions options, 1663 int iCount 1664 ) throws OSSException 1665 { 1666 List lstObjects = null; 1667 int iSelectedIDs = 0; 1668 1669 if ((options.getIsKeepSelectedChecked()) 1670 && (options.getSelectedItemIDs().length() > 0)) 1671 { 1672 iSelectedIDs = StringUtils.parseStringToIntArray( 1681 options.getSelectedItemIDs(), ",").length; 1682 } 1683 1684 if ((options != null) && (iCount > 0)) 1685 { 1686 if (options.getPageSize() != ListOptions.PAGE_SIZE_ALL) 1687 { 1688 lstObjects = new ArrayList (options.getPageSize() + iSelectedIDs); 1689 } 1690 else 1691 { 1692 lstObjects = new ArrayList (iCount); 1693 } 1694 } 1695 1696 return lstObjects; 1697 } 1698 1699 1709 protected int getTotalRecords( 1710 ResultSet rsQueryResults, 1711 ListOptions options 1712 ) throws OSSException, 1713 SQLException  1714 { 1715 int iOutputCount = 0; 1716 1717 if (rsQueryResults.last()) 1722 { 1723 iOutputCount = rsQueryResults.getRow(); 1726 } 1728 else 1729 { 1730 } 1732 1733 return iOutputCount; 1734 } 1735} 1736 | Popular Tags |