1 package com.calipso.reportgenerator.common; 2 3 import com.calipso.reportgenerator.reportcalculator.*; 4 import com.calipso.reportgenerator.reportdefinitions.types.*; 5 import com.calipso.reportgenerator.reportdefinitions.*; 6 import com.calipso.reportgenerator.common.LanguageTraslator; 7 import com.calipso.common.DateEx; 8 9 import java.util.*; 10 import java.text.MessageFormat ; 11 import java.io.Serializable ; 12 13 16 public class ReportQuery implements Serializable { 17 private ReportSpec reportSpec; 18 private List dimensions; 19 private List metrics; 20 private List accumulableMetrics; 21 private Map paramValues; 22 private List filterDefinitions; 23 private Map dimensionFilterValues; 24 private boolean visibleTotals; 25 private EnumerationCubeFilter rankingFilter; 26 private EnumerationCubeFilter excludeGroupFilter = null; 27 28 34 public ReportQuery(ReportSpec reportSpec) throws InfoException { 35 this(reportSpec, true); 36 } 37 38 46 public ReportQuery(ReportSpec reportSpec, boolean defaultInitialize) throws InfoException { 47 this.reportSpec = reportSpec; 48 initialize(defaultInitialize); 49 } 50 51 57 public ReportQuery(ReportSpec reportSpec, ReportView reportView) throws InfoException { 58 this(reportSpec, true); 59 overrideQuery(reportView); 60 } 61 62 66 private void overrideQuery(ReportView reportView) throws InfoException { 67 if (reportView != null) { 68 overrideParamValues(reportView); 69 overrideDimensionValues(reportView); 70 overrideMetricsValues(reportView); 71 overrideFiltersValues(reportView); 72 generalOverride(reportView); 73 } 74 } 75 76 private void overrideParamValues(ReportView reportView) throws InfoException { 77 if (reportView.getViewParameterValues()!=null){ 78 Map overrideParams = getParamValuesFromView(reportView.getViewParameterValues(), getReportSpec()); 79 this.paramValues = ReportFilterBuilder.mergeParamValues( getParamValues(), overrideParams); 80 } 81 } 82 83 public static Map getParamValuesFromView(ViewParameterValues viewParameters, ReportSpec reportSpec) throws InfoException { 84 Map result = new HashMap(); 85 ViewParameterValue[] viewParameterValues = viewParameters.getViewParameterValue(); 86 for (int i = 0; i < viewParameterValues.length; i++) { 87 ViewParameterValue viewParameterValue = viewParameterValues[i]; 90 ReportFilterSpec filterSpec = reportSpec.getFilterSpecFromParamName(viewParameterValue.getViewParamName()); 91 int dataType; 92 if(filterSpec.getDataType()!=null){ 93 dataType = filterSpec.getDataType().getType(); 94 }else{ 95 dataType = reportSpec.getDimensionFromName(filterSpec.getDimensionName()).getDataType().getType(); 96 } 97 Object value = SharedData.newFrom(dataType, viewParameterValue.getViewParamValue()); 98 result.put(viewParameterValue.getViewParamName(), value); 99 } 100 return result; 101 } 102 103 private void overrideFiltersValues(ReportView reportView) throws InfoException{ 104 DimensionProperties pr = reportView.getDimensionProperties(); 105 DimensionProperty [] properties = pr.getDimensionProperty(); 106 for(int i = 0 ; i < properties.length ; i++) { 107 Set set = new TreeSet(); 108 DimensionProperty property = properties [i]; 109 ReportDimensionSpec dimensionSpec = reportSpec.getDimensionFromName(property.getDimensionName()); 110 int dimensionIndex = dimensionSpec.getIndex(); 111 ExcludeValue [] values = property.getExcludeValue(); 113 for(int j = 0 ; j < values.length ; j++) { 114 ExcludeValue value = values [j]; 115 switch(dimensionSpec.getDataType().getType()){ 116 case ReportDataType.STRING_TYPE: 117 set.add(value.getValue()); 118 break; 119 case ReportDataType.DATETIME_TYPE: 120 case ReportDataType.DATE_TYPE: 121 try { 122 set.add(SharedDate.newFrom(new DateEx(value.getValue()))); 123 }catch (InfoException e) { 124 throw new InfoException(LanguageTraslator.traslate("262") + value.getValue(),e); 125 } 126 break; 127 case ReportDataType.FLOAT_TYPE: 128 set.add(SharedFloat.newFrom(new Float (value.getValue()))); 129 break; 130 case ReportDataType.INTEGER_TYPE: 131 set.add(SharedInteger.newFrom(new Integer (value.getValue()))); 132 break; 133 case ReportDataType.BOOLEAN_TYPE: 134 set.add(Boolean.valueOf(value.getValue())); 135 break; 136 } 137 } 138 getDimensionFilterValues().put(new Integer (dimensionIndex), set); 139 } 140 } 141 142 146 private void generalOverride(ReportView reportView) { 147 this.visibleTotals = reportView.getVisibleTotals(); 148 } 149 150 155 private void overrideMetricsValues(ReportView reportView) throws InfoException { 156 if (reportView.getMetricProperties() != null) { 157 int count = reportView.getMetricProperties().getMetricPropertyCount(); 158 MetricProperty metricProperty; 159 for (int i = 0; i < count; i++) { 160 metricProperty = reportView.getMetricProperties().getMetricProperty()[i]; 161 setMetricProperties(metricProperty.getMetricName(), metricProperty.getVisible(), i); 162 } 163 } 164 } 165 166 171 private void overrideDimensionValues(ReportView reportView) throws InfoException { 172 int count = reportView.getDimensionProperties().getDimensionPropertyCount(); 173 DimensionProperty dimensionProperty; 174 for (int i = 0; i < count; i++) { 175 dimensionProperty = reportView.getDimensionProperties().getDimensionProperty()[i]; 176 setDimensionProperties(dimensionProperty.getDimensionName(), getLocationTypeFormView(dimensionProperty.getLocation()), getOrderTypeFormView(dimensionProperty.getOrder()), dimensionProperty.getUbication(), dimensionProperty.getRankMetricName()); 177 } 178 } 179 180 185 private DimensionDefinitionLocationType getLocationTypeFormView(DimensionPropertyLocationType locationType) { 186 return DimensionDefinitionLocationType.valueOf(locationType.toString()); 187 } 188 189 194 private DimensionDefinitionOrderType getOrderTypeFormView(DimensionPropertyOrderType orderType) { 195 return DimensionDefinitionOrderType.valueOf(orderType.toString()); 196 } 197 198 202 private void initialize(boolean defaultInitialize) { 203 this.dimensions = new ArrayList(); 204 this.metrics = new ArrayList(); 205 this.accumulableMetrics = new ArrayList(); 206 this.visibleTotals = getReportSpec().isVisibleTotals(); 207 DimensionDefinitionLocationType location; 208 Collection dimensions; 209 210 if ((getReportSpec().getReportType().getType() == ReportDefinitionReportTypeType.CUBE_TYPE) || (getReportSpec().getReportType().getType() == ReportDefinitionReportTypeType.CHARTCUBE_TYPE)) { 211 if (defaultInitialize) { 212 location = DimensionDefinitionLocationType.ROW; 213 dimensions = getReportSpec().getDimensionsByLocation(location); 214 addDimensionsByLocation(dimensions, location); 215 location = DimensionDefinitionLocationType.COLUMN; 216 dimensions = getReportSpec().getDimensionsByLocation(location); 217 addDimensionsByLocation(dimensions, location); 218 location = DimensionDefinitionLocationType.PAGE; 219 dimensions = getReportSpec().getDimensionsByLocation(location); 220 addDimensionsByLocation(dimensions, location); 221 } 222 else { 223 dimensions = getReportSpec().getDimensionSpecs().values(); 224 addDimensionsByLocation(dimensions, DimensionDefinitionLocationType.PAGE); 225 } 226 } 227 else { 228 dimensions = getReportSpec().getDimensionsByGrouping(true); 229 addDimensionsByGrouping(dimensions, true); 230 dimensions = getReportSpec().getDimensionsByGrouping(false); 231 addDimensionsByGrouping(dimensions, false); 232 } 233 234 235 Collection metrics = getReportSpec().getMetricsByIndex(); 236 for (Iterator iterator = metrics.iterator(); iterator.hasNext();) { 237 ReportMetricSpec metricSpec = (ReportMetricSpec) iterator.next(); 238 int index = metricSpec.getReportSourceIndex(); 239 boolean visible; 240 if (defaultInitialize) { 241 visible = metricSpec.getVisible(); 242 } 243 else { 244 visible = false; 245 } 246 getMetrics().add(new QueryMetric(metricSpec.getName(), index, visible, getMetrics().size(), metricSpec.getAccumulable())); 247 } 248 249 Collection accmetrics = getReportSpec().getAccumulableMetricsByIndex(); 250 for (Iterator iterator = accmetrics.iterator(); iterator.hasNext();) { 251 ReportMetricSpec metricSpec = (ReportMetricSpec) iterator.next(); 252 int index = metricSpec.getReportSourceIndex(); 253 boolean visible; 254 if (defaultInitialize) { 255 visible = metricSpec.getVisible(); 256 } 257 else { 258 visible = false; 259 } 260 getAccumulableMetrics().add(new QueryMetric(metricSpec.getName(), index, visible, getMetrics().size(), metricSpec.getAccumulable())); 261 } 262 } 263 264 270 271 private void addDimensionsByLocation(Collection dimensions, DimensionDefinitionLocationType location) { 272 Iterator iter = dimensions.iterator(); 273 int locationOrder = 0; 274 while (iter.hasNext()) { 275 ReportDimensionSpec dimensionSpec = (ReportDimensionSpec) iter.next(); 276 if (dimensionSpec.getInReportDefinition()) { 277 getDimensions().add(new QueryDimension(dimensionSpec.getName(), dimensionSpec.getReportSourceIndex(), location, dimensionSpec.getOrder(), locationOrder, false, dimensionSpec.getRankMetricName())); 278 locationOrder++; 279 } 280 } 281 } 282 283 289 private void addDimensionsByGrouping(Collection dimensions, boolean groups) { 290 Iterator iter = dimensions.iterator(); 291 int locationOrder = 0; 292 while (iter.hasNext()) { 293 ReportDimensionSpec dimensionSpec = (ReportDimensionSpec) iter.next(); 294 if (dimensionSpec.getInReportDefinition()) { 295 getDimensions().add(new QueryDimension(dimensionSpec.getName(), dimensionSpec.getReportSourceIndex(), DimensionDefinitionLocationType.PAGE, dimensionSpec.getOrder(), locationOrder, groups, dimensionSpec.getRankMetricName())); 296 locationOrder++; 297 } 298 } 299 } 300 301 306 public CubeQuery getCubeQuery() throws InfoException { 307 try { 308 CubeQuery query = new CubeQuery(); 309 int[] rows = getCubeRowFields(); 310 int[] cols = getCubeColumnFields(); 311 int[] pages = getCubePageFields(); 312 int[] metrics = getCubeMetricFields(); 313 boolean[] ascending = getDimensionOrder(); 314 int[] dimensioRank = getDimensionRank(); 315 query.setRows(rows); 316 query.setColumns(cols); 317 query.setPages(pages); 318 query.setMetrics(metrics); 319 query.setAscending(ascending); 320 query.setDimensionRank(dimensioRank); 321 query.setFilter(getCubeFilter(getParamValues())); 322 query.setRankingFilter(getRankingFilter()); 323 query.setEnumFilter(getEnumFilter()); 324 query.setExcludeGroupFilter(excludeGroupFilter); 325 return query; 326 } catch (Exception e) { 327 throw new InfoException(LanguageTraslator.traslate("82"), e); 328 } 329 } 330 331 332 335 public List getVisibleMetrics() { 336 List list = new ArrayList(); 337 for (Iterator iterator = metrics.iterator(); iterator.hasNext();) { 338 QueryMetric queryMetric = (QueryMetric) iterator.next(); 339 if (queryMetric.getVisible()) { 340 list.add(queryMetric); 341 } 342 } 343 return list; 344 } 345 346 349 public List getVisibleAccumulableMetrics() { 350 List list = new ArrayList(); 351 for (Iterator iterator = accumulableMetrics.iterator(); iterator.hasNext();) { 352 QueryMetric queryMetric = (QueryMetric) iterator.next(); 353 if (queryMetric.getVisible()) { 354 list.add(queryMetric); 355 } 356 } 357 return list; 358 } 359 360 361 364 private int[] getCubeMetricFields() { 365 List list = getMetrics(); 366 int[] array = new int[list.size()]; 367 int index = 0; 368 for (Iterator iterator = list.iterator(); iterator.hasNext();) { 369 QueryMetric queryMetric = (QueryMetric) iterator.next(); 370 array[index] = queryMetric.getIndex(); 371 index++; 372 } 373 return array; 374 } 375 376 380 381 private int[] getStaticAccumulableMetricFields() { 382 List list = getAccumulableMetrics(); 383 int[] array = new int[list.size()]; 384 int index = 0; 385 for (Iterator iterator = list.iterator(); iterator.hasNext();) { 386 QueryMetric queryMetric = (QueryMetric) iterator.next(); 387 array[index] = queryMetric.getIndex(); 388 index++; 389 } 390 return array; 391 } 392 393 397 398 public int[] getCubeVisibleMetricFields() { 399 List list = getVisibleMetrics(); 400 int[] array = new int[list.size()]; 401 int index = 0; 402 for (Iterator iterator = list.iterator(); iterator.hasNext();) { 403 QueryMetric queryMetric = (QueryMetric) iterator.next(); 404 array[index] = queryMetric.getIndex(); 405 index++; 406 } 407 return array; 408 } 409 410 411 417 private boolean[] getDimensionOrder() { 418 int size = getReportSpec().getDimensionSpecs().size(); 419 boolean[] orders = new boolean[size]; 420 for (int i = 0; i < getDimensions().size(); i++) { 421 QueryDimension queryDimension = (QueryDimension) getDimensions().get(i); 422 orders[queryDimension.getIndex()] = queryDimension.getOrder().getType() == (DimensionDefinitionOrderType.A.getType()); 423 } 424 return orders; 425 } 426 427 428 432 private int[] getDimensionRank() { 433 int size = getReportSpec().getDimensionSpecs().size(); 434 int[] dimensionRank = new int[size]; 435 for (int i = 0; i < getDimensions().size(); i++) { 436 QueryDimension queryDimension = (QueryDimension) getDimensions().get(i); 437 ReportMetricSpec metricSpec = null; 438 if(queryDimension.getRankMetricName() != null){ 439 metricSpec = getReportSpec().getMetricFromName(queryDimension.getRankMetricName()); 440 } 441 int metricIndex; 442 if (metricSpec == null) { 443 metricIndex = -1; 444 } 445 else { 446 metricIndex = metricSpec.getReportSourceIndex(); 447 } 448 dimensionRank[queryDimension.getIndex()] = metricIndex; 449 } 450 return dimensionRank; 451 } 452 453 459 private int[] getDimensionIndexArray(List dimensions) { 460 int size = dimensions.size(); 461 int[] array = new int[size]; 462 for (int i = 0; i < size; i++) { 463 QueryDimension queryDimension = (QueryDimension) dimensions.get(i); 464 array[i] = queryDimension.getIndex(); 465 } 466 return array; 467 } 468 469 473 private int[] getCubeRowFields() { 474 return getDimensionIndexArray(getRowDimensions()); 475 } 476 477 478 482 private int[] getCubeColumnFields() { 483 return getDimensionIndexArray(getColumnDimensions()); 484 } 485 486 490 491 private int[] getCubePageFields() { 492 return getDimensionIndexArray(getPageDimensions()); 493 } 494 495 499 500 public List getDimensions() { 501 return dimensions; 502 } 503 504 509 public List getDimensionsByLocation(DimensionDefinitionLocationType location) { 510 511 List list = new ArrayList(); 512 513 for (Iterator iterator = getDimensions().iterator(); iterator.hasNext();) { 514 QueryDimension queryDimension = (QueryDimension) iterator.next(); 515 if (queryDimension.getLocation().getType() == location.getType()) { 516 (list).add(queryDimension); 517 } 518 } 519 Collections.sort(list, new Comparator() { 520 public int compare(Object o1, Object o2) { 521 QueryDimension dim1 = (QueryDimension) o1; 522 QueryDimension dim2 = (QueryDimension) o2; 523 return dim1.getLocationOrder() - dim2.getLocationOrder(); 524 } 525 }); 526 return list; 527 } 528 529 533 public List getRowDimensions() { 534 return getDimensionsByLocation(DimensionDefinitionLocationType.ROW); 535 } 536 537 541 public List getColumnDimensions() { 542 return getDimensionsByLocation(DimensionDefinitionLocationType.COLUMN); 543 } 544 545 549 public List getPageDimensions() { 550 return getDimensionsByLocation(DimensionDefinitionLocationType.PAGE); 551 } 552 553 557 public List getMetrics() { 558 return metrics; 559 } 560 561 565 public List getAccumulableMetrics() { 566 return accumulableMetrics; 567 } 568 569 570 575 576 public ReportSpec getReportSpec() { 577 return reportSpec; 578 } 579 580 587 public void clearQuery() { 588 for (int i = 0; i < getDimensions().size(); i++) { 589 QueryDimension queryDimension = (QueryDimension) getDimensions().get(i); 590 queryDimension.setProperties(DimensionDefinitionLocationType.PAGE, DimensionDefinitionOrderType.A, 1, ""); 591 } 592 for (int i = 0; i < getMetrics().size(); i++) { 593 QueryMetric queryMetric = (QueryMetric) getMetrics().get(i); 594 queryMetric.setVisible(false); 595 } 596 paramValues = null; 597 dimensionFilterValues = null; 598 } 599 600 606 607 public void setDimensionLocation(String name, DimensionDefinitionLocationType location) throws InfoException { 608 QueryDimension queryDimension = getQueryDimensionFromName(name); 609 queryDimension.setLocation(location); 610 } 611 612 618 public void setDimensionOrder(String name, DimensionDefinitionOrderType order) throws InfoException { 619 QueryDimension queryDimension = getQueryDimensionFromName(name); 620 queryDimension.setOrder(order); 621 } 622 623 624 631 public void setDimensionLocationOrder(String name, int order) throws InfoException { 632 QueryDimension queryDimension = getQueryDimensionFromName(name); 633 queryDimension.setLocationOrder(order); 634 635 } 636 637 645 public void setDimensionLocationOrder(String name, int order, boolean updateOrders) throws InfoException { 646 setDimensionLocationOrder(name, order); 647 if (updateOrders) { 648 locationOrderUpdated(name, order); 649 } 650 } 651 652 657 private void locationOrderUpdated(String name, int locationOrder) throws InfoException { 658 QueryDimension dimension = getQueryDimensionFromName(name); 659 List list = getDimensionsByLocation(dimension.getLocation()); 660 for (Iterator iterator = list.iterator(); iterator.hasNext();) { 661 QueryDimension queryDimension = (QueryDimension) iterator.next(); 662 if (name != queryDimension.getName() && queryDimension.getLocationOrder() >= locationOrder) { 663 queryDimension.setLocationOrder(queryDimension.getLocationOrder() + 1); 664 } 665 } 666 } 667 668 678 public void setDimensionProperties(String name, DimensionDefinitionLocationType location, DimensionDefinitionOrderType order, int locationOrder, String rankMetricName) throws InfoException { 679 QueryDimension queryDimension = getQueryDimensionFromName(name); 680 queryDimension.setProperties(location, order, locationOrder, rankMetricName); 681 if (locationOrder >= 0) { 682 } 684 } 685 686 692 public void setDimensionRankMetricName(String name, String rankMetricName) throws InfoException { 693 QueryDimension queryDimension = getQueryDimensionFromName(name); 694 queryDimension.setRankMetricName(rankMetricName); 695 } 696 697 703 public void setMetricVisible(String name, boolean visible) throws InfoException { 704 QueryMetric queryMetric = getQueryMetricFromName(name); 705 queryMetric.setVisible(visible); 706 } 707 708 715 public void setMetricOrder(String name, int order) throws InfoException { 716 QueryMetric queryMetric = getQueryMetricFromName(name); 717 queryMetric.setMetricOrder(order); 718 if (order <= 0) { 719 metricOrderUpdated(name, order); 720 } 721 } 722 723 724 729 730 private void metricOrderUpdated(String name, int order) { 731 for (Iterator iterator = metrics.iterator(); iterator.hasNext();) { 732 QueryMetric queryMetric = (QueryMetric) iterator.next(); 733 if (name != queryMetric.getName() && queryMetric.getMetricOrder() >= order) { 734 queryMetric.setMetricOrder(queryMetric.getMetricOrder() + 1); 735 } 736 } 737 } 738 739 746 public void setMetricProperties(String name, boolean visible, int order) throws InfoException { 747 QueryMetric queryMetric = getQueryMetricFromName(name); 748 queryMetric.setMetricProperties(visible, order); 749 if (order <= 0) { 750 metricOrderUpdated(name, order); 751 } 752 } 753 754 760 public QueryMetric getQueryMetricFromName(String name) throws InfoException { 761 for (int i = 0; i < getMetrics().size(); i++) { 762 QueryMetric queryMetric = (QueryMetric) getMetrics().get(i); 763 if (queryMetric.getName().equalsIgnoreCase(name)) { 764 return queryMetric; 765 } 766 } 767 throw new InfoException(MessageFormat.format(LanguageTraslator.traslate("83"), new Object []{name})); 768 } 769 770 776 public QueryDimension getQueryDimensionFromName(String name) throws InfoException { 777 for (int i = 0; i < getDimensions().size(); i++) { 778 QueryDimension queryDimension = (QueryDimension) getDimensions().get(i); 779 if (queryDimension.getName().equalsIgnoreCase(name)) { 780 return queryDimension; 781 } 782 } 783 throw new InfoException(MessageFormat.format(LanguageTraslator.traslate("84"), new Object []{name})); 784 } 785 786 787 791 public boolean isValid() { 792 return getVisibleMetrics().size() > 0; 793 } 794 795 800 public Map getParamValues() { 801 if (paramValues == null) { 802 paramValues = initializeParamValues(); 803 } 804 return paramValues; 805 } 806 807 private Map initializeParamValues() { 808 return ReportFilterBuilder.mergeParamValues(null, getReportSpec().getParamValues(false)); 809 } 810 811 815 816 public Object getParamValue(String key) { 817 return getParamValues().get(key); 818 } 819 820 826 public Object getParamValue(String filterName, String parameterName) { 827 return getParamValue(filterName, parameterName); 828 } 829 830 835 public void setParamValue(String key, Object value) { 836 getParamValues().put(key, value); 837 } 838 839 840 846 public void setParamValue(String filterName, String parameterName, Object value) { 847 getParamValues().put(filterName + parameterName, value); 848 } 849 850 855 public void setParamValues(Map paramValues) { 856 Iterator keys = paramValues.keySet().iterator(); 857 while (keys.hasNext()) { 858 String key = (String ) keys.next(); 859 setParamValue(key, paramValues.get(key)); 860 } 861 } 862 863 867 public int[] getGroupingDimensionsIndexArray() { 868 int[] groupingDimensions; 869 int[] rows = getCubeRowFields(); 870 int[] cols = getCubeColumnFields(); 871 872 int rowsLenght; 873 int columnsLenght; 874 int dimensionsLenght; 875 int index; 876 877 rowsLenght = rows.length; 878 columnsLenght = cols.length; 879 dimensionsLenght = rowsLenght + columnsLenght; 880 groupingDimensions = new int[dimensionsLenght]; 881 for (index = 0; index < rowsLenght; ++index) { 882 groupingDimensions[index] = rows[index]; 883 } 884 for (index = 0; index < columnsLenght; ++index) { 885 groupingDimensions[index + rowsLenght] = cols[index]; 886 } 887 return groupingDimensions; 888 } 889 890 891 895 public EnumerationCubeFilter getEnumFilter() { 896 EnumerationCubeFilter filter = EnumerationCubeFilter.excluding(); 897 Iterator iterator = getDimensionFilterValues().entrySet().iterator(); 898 while (iterator.hasNext()) { 899 Map.Entry entry = (Map.Entry) iterator.next(); 900 filter.addTo((Set) entry.getValue(), ((Integer ) entry.getKey()).intValue()); 901 } 902 return filter; 903 } 904 905 911 protected ExpressionCubeFilter getCubeFilter(Map params) throws InfoException { 912 try { 913 ReportFilterBuilder filterBuilder = new ReportFilterBuilder(getFilterDefinitions(), params, ReportFilterBuilder.VARMODE_INDEX); 914 return filterBuilder.getCubeFilter(getReportSpec(), null); 915 } catch (Exception e) { 916 throw new InfoException(LanguageTraslator.traslate("85"), e); 917 } 918 } 919 920 925 protected List getFilterDefinitions() throws InfoException { 926 if (filterDefinitions == null) { 927 filterDefinitions = new ArrayList(); 928 Collection filterSpecs = getReportSpec().getFilterSpecs(); 929 ReportFilter maxRowsFilter = getMaxRowsFilter(); 930 if (maxRowsFilter != null) { 931 filterDefinitions.add(maxRowsFilter); 932 } 933 for (Iterator iterator = filterSpecs.iterator(); iterator.hasNext();) { 934 ReportFilterSpec reportFilterSpec = (ReportFilterSpec) iterator.next(); 935 ReportFilter reportFilter = new ReportFilter(reportFilterSpec); 936 filterDefinitions.add(reportFilter); 937 } 938 } 939 return filterDefinitions; 940 } 941 942 946 private ReportFilter getMaxRowsFilter() { 947 ReportFilter maxRowsFilter = null; 948 ReportFilterSpec maxRowsFilterSpec = getReportSpec().getMaxRowsFilterSpec(); 949 if (maxRowsFilterSpec != null) { 950 maxRowsFilter = new ReportFilter(maxRowsFilterSpec); 951 } 952 return maxRowsFilter; 953 } 954 955 956 960 protected Map getDimensionFilterValues() { 961 if (dimensionFilterValues == null) { 962 dimensionFilterValues = new HashMap(); 963 } 964 return dimensionFilterValues; 965 } 966 967 972 public StaticQuery getStaticQuery() throws InfoException { 973 try { 974 StaticQuery query = new StaticQuery(); 975 int[] group = getStaticGroupingDimensions(); 976 int[] noGroup = getStaticNonGroupingDimensions(); 977 int[] metrics = getCubeMetricFields(); 978 int[] accumulableMetrics = getStaticAccumulableMetricFields(); 979 boolean[] ascending = getDimensionOrder(); 980 query.setFilter(getCubeFilter(getParamValues())); 981 query.setGroupDimensions(group); 982 query.setNoGroupDimensions(noGroup); 983 query.setMetrics(metrics); 984 query.setAccumulableMetrics(accumulableMetrics); 985 query.setAscending(ascending); 986 query.setRankingFilter(getRankingFilter()); 987 988 return query; 990 } catch (Exception e) { 991 throw new InfoException(LanguageTraslator.traslate("86"), e); 992 } 993 } 994 995 999 private int[] getStaticNonGroupingDimensions() { 1000 return getDimensionIndexArray(getDimensionsByGrouping(false)); 1001 } 1002 1003 1006 1007 1008 private int[] getStaticGroupingDimensions() { 1009 return getDimensionIndexArray(getDimensionsByGrouping(true)); 1010 } 1011 1012 1017 1018 private List getDimensionsByGrouping(boolean groups) { 1019 1020 List list = new ArrayList(); 1021 for (Iterator iterator = getDimensions().iterator(); iterator.hasNext();) { 1022 QueryDimension queryDimension = (QueryDimension) iterator.next(); 1023 if (queryDimension.getGroups() == groups) { 1024 (list).add(queryDimension); 1025 } 1026 } 1027 Collections.sort(list, new Comparator() { 1028 public int compare(Object o1, Object o2) { 1029 QueryDimension dim1 = (QueryDimension) o1; 1030 QueryDimension dim2 = (QueryDimension) o2; 1031 return dim1.getLocationOrder() - dim2.getLocationOrder(); 1032 } 1033 }); 1034 return list; 1035 } 1036 1037 1038 1043 private Set getDimensionFilterValuesFor(int dimension) { 1044 Integer key = new Integer (dimension); 1045 Set values = (Set) getDimensionFilterValues().get(key); 1046 if (values == null) { 1047 values = new TreeSet(); 1048 getDimensionFilterValues().put(key, values); 1049 } 1050 return values; 1051 1052 } 1053 1054 1059 public void excludeValue(int dimension, Object value) { 1060 Set values = getDimensionFilterValuesFor(dimension); 1061 values.add(value); 1062 } 1063 1064 1069 1070 public void includeValue(int dimension, Object value) { 1071 Set values = getDimensionFilterValuesFor(dimension); 1072 values.remove(value); 1073 } 1074 1075 1080 public void setExcludedValues(int dimension, Collection excludedValues) { 1081 getDimensionFilterValues().put(new Integer (dimension), excludedValues); 1082 } 1083 1084 1089 public Set getExcludedValues(int dimension) { 1090 return (Set) getDimensionFilterValues().get(new Integer (dimension)); 1091 } 1092 1093 public boolean isVisibleTotals() { 1094 return visibleTotals; 1095 } 1096 1097 public void setVisibleTotals(boolean visibleTotals) { 1098 this.visibleTotals = visibleTotals; 1099 } 1100 1101 1105 private void setMetricsVisible(Collection otherMetrics) { 1106 for (Iterator iterator = otherMetrics.iterator(); iterator.hasNext();) { 1107 QueryMetric queryMetric = (QueryMetric) iterator.next(); 1108 queryMetric.setVisible(true); 1109 } 1110 } 1111 1112 1116 private void addDimensions(Collection otherDimensions) { 1117 getDimensions().addAll(otherDimensions); 1118 } 1119 1120 1124 public void setUniqueMetricVisible(String rankMetricName) { 1125 if(rankMetricName!=null && !rankMetricName.equalsIgnoreCase("")){ 1126 List metrics = getMetrics(); 1127 Iterator iter = metrics.iterator(); 1128 while(iter.hasNext()){ 1129 QueryMetric metric = (QueryMetric)iter.next(); 1130 if(metric.getName().equalsIgnoreCase(rankMetricName)){ 1132 metric.setVisible(true); 1133 }else{ 1134 metric.setVisible(false); 1135 } 1136 } 1137 } 1138 } 1139 1140 1144 public void addFilters(Collection filters) { 1145 Iterator iter = filters.iterator(); 1146 while(iter.hasNext()){ 1147 getReportSpec().getFilterSpecs().add(iter.next()); 1148 } 1149 } 1150 1151 1155 public void removeFilters(Collection filters) { 1156 Iterator iter = filters.iterator(); 1157 while(iter.hasNext()){ 1158 getReportSpec().getFilterSpecs().remove(iter.next()); 1159 } 1160 } 1161 1162 public EnumerationCubeFilter getRankingFilter() { 1163 if(rankingFilter==null){ 1164 rankingFilter = EnumerationCubeFilter.including(); 1165 } 1166 return rankingFilter; 1167 } 1168 1169 1173 public void setUniqueDimension(ReportDimensionSpec dimension) { 1174 List dimensions = getDimensions(); 1175 String name = dimension.getName(); 1176 Iterator iterator = dimensions.iterator(); 1177 while(iterator.hasNext()){ 1178 QueryDimension dim = (QueryDimension)iterator.next(); 1179 if(dim.getName().equalsIgnoreCase(name)){ 1180 dim.setLocation(DimensionDefinitionLocationType.ROW); 1181 dim.setGroups(true); 1182 }else{ 1183 dim.setLocation(DimensionDefinitionLocationType.HIDDEN); 1184 dim.setGroups(false); 1185 } 1186 } 1187 } 1188 1189 public EnumerationCubeFilter getExcludeGroupFilter() { 1190 if(excludeGroupFilter==null){ 1191 excludeGroupFilter = EnumerationCubeFilter.including(); 1192 } 1193 return excludeGroupFilter; 1194 } 1195 1196 public void setExcludeGroupFilter(EnumerationCubeFilter excludeGroupFilter) { 1197 this.excludeGroupFilter = excludeGroupFilter; 1198 } 1199} 1200 | Popular Tags |