1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.util.*; 25 import oracle.toplink.essentials.expressions.*; 26 import oracle.toplink.essentials.internal.expressions.*; 27 import oracle.toplink.essentials.internal.queryframework.*; 28 import oracle.toplink.essentials.exceptions.*; 29 import oracle.toplink.essentials.internal.helper.*; 30 import oracle.toplink.essentials.mappings.*; 31 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 32 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 33 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 34 import oracle.toplink.essentials.internal.sessions.AbstractSession; 35 import oracle.toplink.essentials.descriptors.ClassDescriptor; 36 37 57 public class ReportQuery extends ReadAllQuery { 58 59 60 public static final int ShouldReturnSingleResult = 1; 61 62 63 public static final int ShouldReturnSingleValue = 2; 64 65 67 public static final int ShouldReturnSingleAttribute = 3; 68 69 70 public static final int ShouldReturnWithoutReportQueryResult = 4; 71 72 73 public static final int FULL_PRIMARY_KEY = 2; 74 public static final int FIRST_PRIMARY_KEY = 1; 75 public static final int NO_PRIMARY_KEY = 0; 76 77 protected static final Boolean RESULT_IGNORED = Boolean.valueOf(true); 79 81 82 protected int shouldRetrievePrimaryKeys; 83 84 85 protected Vector names; 86 87 88 protected Vector items; 89 90 91 protected Vector groupByExpressions; 92 93 94 protected Expression havingExpression; 95 96 99 protected int returnChoice; 100 101 102 protected boolean addToConstructorItem; 103 protected Class resultConstructorClass; 104 protected Class [] constructorArgTypes; 105 protected List constructorMappings; 106 107 110 protected HashSet returnedKeys; 111 112 116 public ReportQuery() { 117 this.queryMechanism = new ExpressionQueryMechanism(this); 118 this.items = new Vector(); 119 this.shouldRetrievePrimaryKeys = NO_PRIMARY_KEY; 120 this.groupByExpressions = new Vector(3); 121 this.havingExpression=null; 122 this.addToConstructorItem = false; 123 124 this.setLockMode(ObjectBuildingQuery.NO_LOCK); 127 } 128 129 public ReportQuery(Class javaClass, Expression expression) { 130 this(); 131 this.defaultBuilder = expression.getBuilder(); 132 setReferenceClass(javaClass); 133 setSelectionCriteria(expression); 134 } 135 136 141 public ReportQuery(Class javaClass, ExpressionBuilder builder) { 142 this(); 143 this.defaultBuilder = builder; 144 setReferenceClass(javaClass); 145 } 146 147 152 public ReportQuery(ExpressionBuilder builder) { 153 this(); 154 this.defaultBuilder = builder; 155 } 156 157 162 public void addAttribute(String itemName) { 163 addItem(itemName, getExpressionBuilder().get(itemName)); 164 } 165 166 171 public void addAttribute(String itemName, Expression attributeExpression) { 172 addItem(itemName, attributeExpression); 173 } 174 175 180 public void addAttribute(String itemName, Expression attributeExpression, Class type) { 181 addItem(itemName, attributeExpression, type); 182 } 183 184 190 public void addAverage(String itemName) { 191 addAverage(itemName, getExpressionBuilder().get(itemName)); 192 } 193 194 201 public void addAverage(String itemName, Class resultType) { 202 addAverage(itemName, getExpressionBuilder().get(itemName), resultType); 203 } 204 205 211 public void addAverage(String itemName, Expression attributeExpression) { 212 addItem(itemName, attributeExpression.average()); 213 } 214 215 222 public void addAverage(String itemName, Expression attributeExpression, Class resultType) { 223 addItem(itemName, attributeExpression.average(), resultType); 224 } 225 226 232 public void addConstructorReportItem(ConstructorReportItem item){ 233 addItem(item); 234 } 235 236 247 public void addCount() { 248 addCount("COUNT", getExpressionBuilder()); 249 } 250 251 263 public void addCount(String attributeName) { 264 addCount(attributeName, getExpressionBuilder().get(attributeName)); 265 } 266 267 280 public void addCount(String attributeName, Class resultType) { 281 addCount(attributeName, getExpressionBuilder().get(attributeName), resultType); 282 } 283 284 303 public void addCount(String itemName, Expression attributeExpression) { 304 addItem(itemName, attributeExpression.count()); 305 } 306 307 327 public void addCount(String itemName, Expression attributeExpression, Class resultType) { 328 addItem(itemName, attributeExpression.count(), resultType); 329 } 330 331 337 public void addFunctionItem(String itemName, Expression attributeExpression, String functionName) { 338 Expression functionExpression = attributeExpression; 339 functionExpression = attributeExpression.getFunction(functionName); 340 341 ReportItem item = new ReportItem(itemName, functionExpression); 342 addItem(item); 343 344 } 345 346 352 public void addGrouping(String attributeName) { 353 addGrouping(getExpressionBuilder().get(attributeName)); 354 } 355 356 362 public void addGrouping(Expression expression) { 363 getGroupByExpressions().addElement(expression); 364 setIsPrepared(false); 366 } 367 368 374 public void setHavingExpression(Expression expression) { 375 havingExpression = expression; 376 setIsPrepared(false); 377 } 378 379 383 private void addItem(ReportItem item){ 384 if (addToConstructorItem && (getItems().size()>0) &&(((ReportItem)getItems().lastElement()).isContructorItem() )){ 385 ((ConstructorReportItem)getItems().lastElement()).addItem(item); 386 }else{ 387 getItems().addElement(item); 388 } 389 setIsPrepared(false); 391 } 392 393 398 public void addItem(String itemName, Expression attributeExpression) { 399 ReportItem item = new ReportItem(itemName, attributeExpression); 400 addItem(item); 401 } 402 403 408 public void addItem(String itemName, Expression attributeExpression, List joinedExpressions) { 409 ReportItem item = new ReportItem(itemName, attributeExpression); 410 item.getJoinedAttributeManager().setJoinedAttributeExpressions_(joinedExpressions); 411 addItem(item); 412 } 413 414 421 protected void addItem(String itemName, Expression attributeExpression, Class resultType) { 422 ReportItem item = new ReportItem(itemName, attributeExpression); 423 item.setResultType(resultType); 424 addItem(item); 425 } 426 427 433 public void addMaximum(String itemName) { 434 addMaximum(itemName, getExpressionBuilder().get(itemName)); 435 } 436 437 443 public void addMaximum(String itemName, Expression attributeExpression) { 444 addItem(itemName, attributeExpression.maximum()); 445 } 446 447 453 public void addMinimum(String itemName) { 454 addMinimum(itemName, getExpressionBuilder().get(itemName)); 455 } 456 457 463 public void addMinimum(String itemName, Expression attributeExpression) { 464 addItem(itemName, attributeExpression.minimum()); 465 } 466 467 473 public void addStandardDeviation(String itemName) { 474 addStandardDeviation(itemName, getExpressionBuilder().get(itemName)); 475 } 476 477 483 public void addStandardDeviation(String itemName, Expression attributeExpression) { 484 addItem(itemName, attributeExpression.standardDeviation()); 485 } 486 487 493 public void addSum(String itemName) { 494 addSum(itemName, getExpressionBuilder().get(itemName)); 495 } 496 497 504 public void addSum(String itemName, Class resultType) { 505 addSum(itemName, getExpressionBuilder().get(itemName), resultType); 506 } 507 508 514 public void addSum(String itemName, Expression attributeExpression) { 515 addItem(itemName, attributeExpression.sum()); 516 } 517 518 525 public void addSum(String itemName, Expression attributeExpression, Class resultType) { 526 addItem(itemName, attributeExpression.sum(), resultType); 527 } 528 529 535 public void addVariance(String itemName) { 536 addVariance(itemName, getExpressionBuilder().get(itemName)); 537 } 538 539 545 public void addVariance(String itemName, Expression attributeExpression) { 546 addItem(itemName, attributeExpression.variance()); 547 } 548 549 553 public ConstructorReportItem beginAddingConstructorArguments(Class constructorClass){ 554 ConstructorReportItem citem = new ConstructorReportItem(constructorClass.getName()); 555 citem.setResultType(constructorClass); 556 getItems().add(citem); 558 setIsPrepared(false); 560 this.addToConstructorItem=true; 561 return citem; 562 } 563 568 public ConstructorReportItem beginAddingConstructorArguments(Class constructorClass, Class [] constructorArgTypes){ 569 ConstructorReportItem citem =beginAddingConstructorArguments(constructorClass); 570 citem.setConstructorArgTypes(constructorArgTypes); 571 return citem; 572 } 573 574 578 public Object buildObject(AbstractRecord row, Vector toManyJoinData) { 579 ReportQueryResult reportQueryResult = new ReportQueryResult(this, row, toManyJoinData); 580 if (this.returnedKeys != null){ 582 if (this.returnedKeys.contains(reportQueryResult.getResultKey())){ 583 return RESULT_IGNORED; } else { 585 this.returnedKeys.add(reportQueryResult.getResultKey()); 586 } 587 } 588 if (this.shouldReturnSingleAttribute()) { 590 return reportQueryResult.getResults().firstElement(); 591 } 592 if (this.shouldReturnWithoutReportQueryResult()){ 593 if (reportQueryResult.getResults().size() == 1){ 594 return reportQueryResult.getResults().firstElement(); 595 } 596 return reportQueryResult.toArray(); 597 } 598 return reportQueryResult; 599 } 600 601 606 public Object buildObjects(Vector rows) { 607 if (shouldReturnSingleResult() || shouldReturnSingleValue()) { 608 if (rows.isEmpty()) { 609 return null; 610 } 611 ReportQueryResult result = (ReportQueryResult)buildObject((AbstractRecord)rows.firstElement(), rows); 612 if (shouldReturnSingleValue()) { 613 return result.elements().nextElement(); 614 } 615 return result; 616 } 617 618 ContainerPolicy containerPolicy = getContainerPolicy(); 619 Object reportResults = containerPolicy.containerInstance(rows.size()); 620 if (shouldDistinctBeUsed()){ 622 this.returnedKeys = new HashSet(); 623 } 624 for (Enumeration rowsEnum = rows.elements(); rowsEnum.hasMoreElements();) { 627 Object result = buildObject((AbstractRecord)rowsEnum.nextElement(), rows); 629 if (result != RESULT_IGNORED){ 630 containerPolicy.addInto(result, reportResults, getSession()); 631 } 632 } 634 return reportResults; 635 } 636 637 641 protected Object checkEarlyReturnImpl(AbstractSession session, AbstractRecord translationRow) { 642 if (shouldCheckCacheOnly()) { 644 throw QueryException.cannotSetShouldCheckCacheOnlyOnReportQuery(); 645 } else { 646 return null; 647 } 648 } 649 650 659 public void copyReportItems(Dictionary alreadyDone) { 660 items = (Vector)items.clone(); 661 for (int i = items.size() - 1; i >= 0; i--) { 662 ReportItem item = (ReportItem)items.elementAt(i); 663 Expression expression = item.getAttributeExpression(); 664 if ((expression != null) && (alreadyDone.get(expression.getBuilder()) != null)) { 665 expression = expression.copiedVersionFrom(alreadyDone); 666 } 667 items.set(i, new ReportItem(item.getName(), expression)); 668 } 669 if (groupByExpressions != null) { 670 groupByExpressions = (Vector)groupByExpressions.clone(); 671 for (int i = groupByExpressions.size() - 1; i >= 0; i--) { 672 Expression item = (Expression)groupByExpressions.elementAt(i); 673 if (alreadyDone.get(item.getBuilder()) != null) { 674 groupByExpressions.set(i, item.copiedVersionFrom(alreadyDone)); 675 } 676 } 677 } 678 if (orderByExpressions != null) { 679 for (int i = orderByExpressions.size() - 1; i >= 0; i--) { 680 Expression item = (Expression)orderByExpressions.elementAt(i); 681 if (alreadyDone.get(item.getBuilder()) != null) { 682 orderByExpressions.set(i, item.copiedVersionFrom(alreadyDone)); 683 } 684 } 685 } 686 } 687 688 694 public void dontRetrievePrimaryKeys() { 695 setShouldRetrievePrimaryKeys(false); 696 setIsPrepared(false); 698 } 699 700 704 public void dontReturnSingleAttribute() { 705 if (shouldReturnSingleAttribute()) { 706 returnChoice = 0; 707 } 708 } 709 710 715 public void dontReturnSingleResult() { 716 if (shouldReturnSingleResult()) { 717 returnChoice = 0; 718 } 719 } 720 721 727 public void dontReturnSingleValue() { 728 if (shouldReturnSingleValue()) { 729 returnChoice = 0; 730 } 731 } 732 733 739 public void dontReturnWithoutReportQueryResult() { 740 if (shouldReturnWithoutReportQueryResult()) { 741 returnChoice = 0; 742 } 743 } 744 745 754 public void endAddingToConstructorItem(){ 755 this.addToConstructorItem=false; 756 } 757 758 765 public Object executeDatabaseQuery() throws DatabaseException { 766 if (getContainerPolicy().overridesRead()) { 767 return getContainerPolicy().execute(); 768 } 769 770 if (getQueryId() == 0) { 771 setQueryId(getSession().getNextQueryId()); 772 } 773 774 Vector rows = getQueryMechanism().selectAllReportQueryRows(); 775 return buildObjects(rows); 777 } 778 779 783 public Vector getGroupByExpressions() { 784 return groupByExpressions; 785 } 786 787 791 public Expression getHavingExpression() { 792 return havingExpression; 793 } 794 795 799 public Vector getQueryExpressions() { 800 Vector fieldExpressions = new Vector(getItems().size()); 801 802 if (shouldRetrieveFirstPrimaryKey()) { 804 if (!getDescriptor().getPrimaryKeyFields().isEmpty()) { 805 fieldExpressions.addElement(getDescriptor().getPrimaryKeyFields().get(0)); 806 } 807 } 808 if (shouldRetrievePrimaryKeys()) { 809 fieldExpressions.addAll(getDescriptor().getPrimaryKeyFields()); 810 } 811 812 return fieldExpressions; 813 } 814 815 819 public Vector getItemExpressions() { 820 Vector fieldExpressions = new Vector(getItems().size()); 821 822 if (shouldRetrieveFirstPrimaryKey()) { 824 if (!getDescriptor().getPrimaryKeyFields().isEmpty()) { 825 fieldExpressions.addElement(getDescriptor().getPrimaryKeyFields().get(0)); 826 } 827 } 828 if (shouldRetrievePrimaryKeys()) { 829 fieldExpressions.addAll(getDescriptor().getPrimaryKeyFields()); 830 } 831 832 for (Enumeration itemsEnum = getItems().elements(); itemsEnum.hasMoreElements();) { 833 ReportItem item = (ReportItem)itemsEnum.nextElement(); 834 Expression fieldExpression = item.getAttributeExpression(); 835 if (fieldExpression != null) { 836 fieldExpressions.addElement(fieldExpression); 837 } 838 } 839 return fieldExpressions; 840 } 841 842 846 public Vector getItems() { 847 return items; 848 } 849 850 854 public void clearItems() { 855 items = new Vector(); 856 setIsPrepared(false); 857 } 858 859 863 public Vector getNames() { 864 if (names == null) { 865 names = new Vector(); 866 for (Enumeration e = getItems().elements(); e.hasMoreElements();) { 867 names.addElement(((ReportItem)e.nextElement()).getName()); 868 } 869 } 870 return names; 871 } 872 873 877 public boolean isReportQuery() { 878 return true; 879 } 880 881 886 protected void prepare() throws QueryException { 887 if (getItems().size() > 0) { 890 try { 891 for (Enumeration itemsEnum = getItems().elements(); itemsEnum.hasMoreElements();) { 892 ((ReportItem)itemsEnum.nextElement()).initialize(this); 893 } 894 } catch (QueryException exception) { 895 exception.setQuery(this); 896 throw exception; 897 } 898 } else { 899 if ((!shouldRetrievePrimaryKeys()) && (!shouldRetrieveFirstPrimaryKey())) { 900 throw QueryException.noAttributesForReportQuery(this); 901 } 902 } 903 904 super.prepare(); 905 906 } 907 908 913 protected void prepareObjectAttributeCount(Dictionary clonedExpressions) { 914 int numOfReportItems = getItems().size(); 915 for (int i =0;i<numOfReportItems; i++){ 917 ReportItem countItem = (ReportItem)getItems().elementAt(i); 918 if ((countItem != null) && (countItem.getAttributeExpression() instanceof FunctionExpression)) { 919 FunctionExpression count = (FunctionExpression)countItem.getAttributeExpression(); 920 if (count.getOperator().getSelector() == ExpressionOperator.Count) { 921 Expression baseExp = count.getBaseExpression(); 922 boolean distinctUsed = false; 923 if (baseExp.isFunctionExpression() && (((FunctionExpression)baseExp).getOperator().getSelector() == ExpressionOperator.Distinct)) { 924 distinctUsed = true; 925 baseExp = ((FunctionExpression)baseExp).getBaseExpression(); 926 } 927 if (baseExp.isQueryKeyExpression()) { 928 QueryKeyExpression countExp = (QueryKeyExpression)baseExp; 929 930 DatabaseMapping mapping = getLeafMappingFor(countExp, getDescriptor()); 932 if ((mapping != null) && !mapping.isDirectToFieldMapping() && (mapping.getReferenceDescriptor() != null)) { 933 ClassDescriptor newDescriptor = mapping.getReferenceDescriptor(); 935 if (distinctUsed) { 936 if (clonedExpressions != null) { 939 if (clonedExpressions.get(countExp.getBuilder()) != null) { 940 countExp = (QueryKeyExpression)countExp.copiedVersionFrom(clonedExpressions); 941 } else { 942 countExp = (QueryKeyExpression)countExp.rebuildOn(getExpressionBuilder()); 943 } 944 } 945 946 ExpressionBuilder countBuilder = countExp.getBuilder(); 949 ExpressionBuilder outerBuilder = new ExpressionBuilder(); 950 951 ReportQuery subSelect = new ReportQuery(getReferenceClass(), countBuilder); 952 subSelect.setShouldRetrieveFirstPrimaryKey(true); 953 954 if (getSelectionCriteria() != null) { 957 outerBuilder.setQueryClass(newDescriptor.getJavaClass()); 958 subSelect.setSelectionCriteria(getSelectionCriteria().and(outerBuilder.equal(countExp))); 959 } else { 960 subSelect.setSelectionCriteria(countExp.equal(outerBuilder)); 961 } 962 setSelectionCriteria(outerBuilder.exists(subSelect)); 963 count.setBaseExpression(outerBuilder); 964 count.getChildren().setElementAt( outerBuilder, 0); 965 setReferenceClass(newDescriptor.getJavaClass()); 966 changeDescriptor(getSession()); 967 } else { 968 Expression newCountEx = countExp.prefixSQL("*"); 970 count.setBaseExpression(newCountEx); 972 count.getChildren().setElementAt( newCountEx, 0); 973 } 974 } 975 } 976 } 977 } 978 } 979 } 980 981 985 protected void prepareSelectAllRows() { 986 prepareObjectAttributeCount(null); 987 988 getQueryMechanism().prepareReportQuerySelectAllRows(); 989 } 990 991 996 public synchronized void prepareSubSelect(AbstractSession session, AbstractRecord translationRow, Dictionary clonedExpressions) throws QueryException { 997 if (isPrepared()) { 998 return; 999 } 1000 1001 setIsPrepared(true); 1002 setSession(session); 1003 setTranslationRow(translationRow); 1004 1005 checkDescriptor(getSession()); 1006 1007 if (descriptor.isAggregateDescriptor()) { 1008 throw QueryException.aggregateObjectCannotBeDeletedOrWritten(descriptor, this); 1010 } 1011 1012 try { 1013 for (Enumeration itemsEnum = getItems().elements(); itemsEnum.hasMoreElements();) { 1014 ((ReportItem)itemsEnum.nextElement()).initialize(this); 1015 } 1016 } catch (QueryException exception) { 1017 exception.setQuery(this); 1018 throw exception; 1019 } 1020 1021 prepareObjectAttributeCount(clonedExpressions); 1022 1023 getQueryMechanism().prepareReportQuerySubSelect(); 1024 1025 setSession(null); 1026 setTranslationRow(null); 1027 } 1028 1029 1035 public void retrievePrimaryKeys() { 1036 setShouldRetrievePrimaryKeys(true); 1037 setIsPrepared(false); 1039 } 1040 1041 1045 public void returnSingleAttribute() { 1046 returnChoice = ShouldReturnSingleAttribute; 1047 } 1048 1049 1054 public void returnSingleResult() { 1055 returnChoice = ShouldReturnSingleResult; 1056 } 1057 1058 1064 public void returnSingleValue() { 1065 returnChoice = ShouldReturnSingleValue; 1066 } 1067 1068 1074 public void returnWithoutReportQueryResult() { 1075 this.returnChoice = ShouldReturnWithoutReportQueryResult; 1076 } 1077 1078 1084 public void setShouldRetrievePrimaryKeys(boolean shouldRetrievePrimaryKeys) { 1085 this.shouldRetrievePrimaryKeys = (shouldRetrievePrimaryKeys ? FULL_PRIMARY_KEY : NO_PRIMARY_KEY); 1086 } 1087 1088 1096 public void setShouldRetrieveFirstPrimaryKey(boolean shouldRetrieveFirstPrimaryKey) { 1097 this.shouldRetrievePrimaryKeys = (shouldRetrieveFirstPrimaryKey ? FIRST_PRIMARY_KEY : NO_PRIMARY_KEY); 1098 } 1099 1100 1105 public void setShouldReturnSingleAttribute(boolean newChoice) { 1106 if (newChoice) { 1107 returnSingleAttribute(); 1108 } else { 1109 dontReturnSingleAttribute(); 1110 } 1111 } 1112 1113 1118 public void setShouldReturnSingleResult(boolean newChoice) { 1119 if (newChoice) { 1120 returnSingleResult(); 1121 } else { 1122 dontReturnSingleResult(); 1123 } 1124 } 1125 1126 1132 public void setShouldReturnSingleValue(boolean newChoice) { 1133 if (newChoice) { 1134 returnSingleValue(); 1135 } else { 1136 dontReturnSingleValue(); 1137 } 1138 } 1139 1140 1145 public void setShouldReturnWithoutReportQueryResult(boolean newChoice) { 1146 if (newChoice) { 1147 returnWithoutReportQueryResult(); 1148 } else { 1149 dontReturnWithoutReportQueryResult(); 1150 } 1151 } 1152 1153 1158 public boolean shouldRetrievePrimaryKeys() { 1159 return (shouldRetrievePrimaryKeys == FULL_PRIMARY_KEY); 1160 } 1161 1162 1168 public boolean shouldRetrieveFirstPrimaryKey() { 1169 return (shouldRetrievePrimaryKeys == FIRST_PRIMARY_KEY); 1170 } 1171 1172 1177 public boolean shouldReturnSingleAttribute() { 1178 return returnChoice == ShouldReturnSingleAttribute; 1179 } 1180 1181 1186 public boolean shouldReturnSingleResult() { 1187 return returnChoice == ShouldReturnSingleResult; 1188 } 1189 1190 1196 public boolean shouldReturnSingleValue() { 1197 return returnChoice == ShouldReturnSingleValue; 1198 } 1199 1200 1205 public boolean shouldReturnWithoutReportQueryResult() { 1206 return returnChoice == ShouldReturnWithoutReportQueryResult; 1207 } 1208 1209 1210} 1211 | Popular Tags |