1 package com.calipso.reportgenerator.reportcalculator; 2 3 import com.calipso.reportgenerator.reportcalculator.SharedFloat; 4 5 import java.io.Serializable ; 6 import java.util.*; 7 8 11 public class CubeQuery implements Serializable { 12 private int[] rows; 13 private int[] columns; 14 private int[] pages; 15 private int[] metrics; 16 private int[] dimensions; 17 private boolean[] ascending; 18 private int[] dimensionRank; 19 private ExpressionCubeFilter filter; 20 private EnumerationCubeFilter enumFilter; 21 private EnumerationCubeFilter rankingFilters; 22 private boolean firstFilterMatches; 23 private EnumerationCubeFilter excludeGroupFilter; 24 25 public void setRankingFilter(EnumerationCubeFilter rankingFilters) { 26 this.rankingFilters = rankingFilters; 27 } 28 29 public EnumerationCubeFilter getRankingFilters() { 30 return rankingFilters; 31 } 32 33 36 public CubeQuery() { 37 initialize(); 38 } 39 40 43 private void initialize() { 44 setRows(new int[0]); 45 setColumns(new int[0]); 46 setPages(new int[0]); 47 setMetrics(new int[0]); 48 } 49 50 54 public int[] getRows() { 55 return rows; 56 } 57 58 62 public void setRows(int[] rows) { 63 this.rows = rows; 64 } 65 66 70 public int[] getColumns() { 71 return columns; 72 } 73 74 78 79 public int[] getPages() { 80 return pages; 81 } 82 83 87 88 public void setColumns(int[] columns) { 89 this.columns = columns; 90 } 91 92 96 97 public void setPages(int[] pages) { 98 this.pages = pages; 99 } 100 101 105 public int[] getMetrics() { 106 return metrics; 107 } 108 109 113 public void setMetrics(int[] metrics) { 114 this.metrics = metrics; 115 } 116 117 121 public boolean[] getAscending() { 122 if (ascending == null) { 123 ascending = new boolean[dimensions.length]; 124 Arrays.fill(ascending, true); 125 } 126 return ascending; 127 } 128 129 133 public void setAscending(boolean[] ascending) { 134 this.ascending = ascending; 135 } 136 137 142 143 public int[] getDimensionRank() { 144 if (dimensionRank == null) { 145 dimensionRank = new int[dimensions.length]; 146 Arrays.fill(dimensionRank, -1); 147 } 148 return dimensionRank; 149 } 150 151 154 155 public void setDimensionRank(int[] dimensionRank) { 156 this.dimensionRank = dimensionRank; 157 } 158 159 163 public ExpressionCubeFilter getFilter() { 164 return filter; 165 } 166 167 171 public void setFilter(ExpressionCubeFilter filter) { 172 this.filter = filter; 173 } 174 175 179 180 public EnumerationCubeFilter getEnumFilter() { 181 return enumFilter; 182 } 183 184 188 189 public void setEnumFilter(EnumerationCubeFilter enumFilter) { 190 this.enumFilter = enumFilter; 191 } 192 193 199 private boolean arrayIncludes(int[] array1, int element) { 200 int index; 201 int lenght; 202 203 lenght = array1.length; 204 for (index = 0; index < lenght; index++) { 205 if (array1[index] == element) { 206 return true; 207 } 208 } 209 return false; 210 } 211 212 216 public int[] getDimensions() { 217 if (dimensions == null) { 218 fillDimensions(); 219 } 220 return dimensions; 221 } 222 223 226 private void fillDimensions() { 227 int rowsLenght; 228 int columnsLenght; 229 int dimensionsLenght; 230 int index; 231 232 rowsLenght = rows.length; 233 columnsLenght = columns.length; 234 dimensionsLenght = rowsLenght + columnsLenght; 235 dimensions = new int[dimensionsLenght]; 236 for (index = 0; index < rowsLenght; ++index) { 237 dimensions[index] = rows[index]; 238 } 239 for (index = 0; index < columnsLenght; ++index) { 240 dimensions[index + rowsLenght] = columns[index]; 241 } 242 } 243 244 249 public boolean matches(Object [] row) { 250 firstFilterMatches = ((filter == null) || filter.matches(row)) && ((rankingFilters==null) || rankingFilters.matches(row)); 251 return firstFilterMatches && ((excludeGroupFilter==null) || excludeGroupFilter.matches(row)); 252 } 253 254 public boolean valuesEnabled(Object [] row) { 255 return (enumFilter == null) || enumFilter.matches(row); 256 } 257 258 264 public LinkedList newDimensionsWithRespectTo(CubeQuery otherQuery) { 265 LinkedList newDimension; 266 267 newDimension = new LinkedList(); 268 for (int i = 0; i < dimensions.length; i++) { 269 int dimension = dimensions[i]; 270 if (!arrayIncludes(otherQuery.dimensions, dimension)) { 271 newDimension.add(new Integer (dimension)); 272 } 273 } 274 275 return newDimension; 276 } 277 278 283 public Comparator entryComparatorFor(int dimensionIndex) { 284 int cubeMetricIndex = getDimensionRank()[dimensionIndex] - this.getDimensionRank().length + this.getDimensions().length; 286 return new EntryComparator(getAscending()[dimensionIndex], cubeMetricIndex); 287 } 288 289 294 public Comparator valueComparatorFor(int dimensionIndex) { 295 return new ValueComparator(getAscending()[dimensionIndex]); 297 } 298 299 public boolean equivalentQuery(CubeQuery newQuery) { 300 return newQuery != null && 301 sameRowDimensions(newQuery.getRows()) && 302 sameColumnDimensions(newQuery.getColumns()) && 303 sameMetrics(newQuery.getMetrics()) && 304 sameFilter(newQuery.getFilter()) && 305 sameEnumFilter(newQuery.getEnumFilter()) && 306 sameAscending(newQuery.getAscending()) && 307 sameDimensionRank(newQuery.getDimensionRank()); 308 } 309 310 private boolean sameDimensionRank(int[] otherDimensionRank) { 311 return Arrays.equals(getDimensionRank(), otherDimensionRank); 312 } 313 314 private boolean sameAscending(boolean[] otherAscending) { 315 return Arrays.equals(getAscending(), otherAscending); 316 } 317 318 private boolean sameMetrics(int[] newMetrics) { 319 return Arrays.equals(getMetrics(), newMetrics); 320 } 321 322 private boolean sameEnumFilter(EnumerationCubeFilter enumFilter) { 323 return getEnumFilter().equals(enumFilter); 324 } 325 326 private boolean sameFilter(ExpressionCubeFilter filter) { 327 if ((getFilter() == null) && (filter == null)) { 328 return true; 329 } else { 330 if (getFilter() != null) { 331 return getFilter().equals(filter); 332 } 333 } 334 return false; 335 } 336 337 private boolean sameColumnDimensions(int[] newColumns) { 338 return Arrays.equals(columns, newColumns); 339 } 340 341 342 private boolean sameRowDimensions(int[] newRows) { 343 return Arrays.equals(rows, newRows); 344 } 345 346 public boolean isGroupExcludedValues() { 347 return excludeGroupFilter!=null; 348 } 349 350 public boolean otherFilterMatches() { 351 return firstFilterMatches; 352 } 353 354 public void setExcludeGroupFilter(EnumerationCubeFilter excludeGroupFilter) { 355 this.excludeGroupFilter = excludeGroupFilter; 356 } 357 358 361 private static class EntryComparator implements Comparator { 362 boolean ascending; 363 int dimensionRank; 364 365 public EntryComparator(boolean ascending, int dimensionRank) { 366 this.ascending = ascending; 367 this.dimensionRank = dimensionRank; 368 } 369 370 public int compare(Object o1, Object o2) { 371 int comparison; 372 if (dimensionRank < 0) { 373 Object ok1 = ((Map.Entry) o1).getKey(); 375 Object ok2 = ((Map.Entry) o2).getKey(); 376 comparison = ((Comparable )ok1).compareTo(ok2); 377 } else { 378 comparison = ((SharedFloat) ((Object []) ((Map.Entry) o1).getValue())[dimensionRank]).compareTo(((Object []) ((Map.Entry) o2).getValue())[dimensionRank]); 379 if (comparison == 0){ 380 comparison = 1; 381 } 382 } 383 if (ascending) { 384 return comparison; 385 } else { 386 return 0 - comparison; 387 } 388 } 389 } 390 391 394 private static class ValueComparator implements Comparator { 395 boolean ascending; 396 397 public ValueComparator(boolean ascending) { 398 this.ascending = ascending; 399 } 400 401 public int compare(Object o1, Object o2) { 402 int comparison; 403 404 comparison = o1.toString().compareTo(o2.toString()); 405 if (ascending) { 406 return comparison; 407 } else { 408 return 0 - comparison; 409 } 410 } 411 } 412 413 public EnumerationCubeFilter getExcludeGroupFilter() { 414 return excludeGroupFilter; 415 } 416 417 } 418 | Popular Tags |