1 package com.calipso.reportgenerator.common; 2 3 import com.calipso.reportgenerator.reportcalculator.SharedFloat; 4 import com.calipso.reportgenerator.reportcalculator.CubeQuery; 5 import com.calipso.reportgenerator.reportcalculator.CubeValue; 6 7 import java.util.Collection ; 8 import java.util.Iterator ; 9 import java.util.Vector ; 10 11 18 19 public class CubeReportTableModel extends ReportTableModel { 20 21 private CubeReportResult reportResult; 22 private HeaderTableModel rowsModel; 23 private HeaderTableModel columnsModel; 24 private int metricsCount; 25 26 27 public CubeReportTableModel(CubeReportResult reportResult) throws InfoException { 28 super(); 29 this.reportResult = reportResult; 30 initialize(); 31 } 32 33 private void initialize() throws InfoException { 34 Object [] groupingDimensions = getGroupingDimensions(); 35 Object [] nonGroupingDimensions = getNonGroupingDimensions(); 36 Object [] totalMetrics = getTotalMetrics(); 37 setGroupingDimCount(groupingDimensions.length); 38 setNonGroupingDimCount(nonGroupingDimensions.length); 39 setCommonMetricsCount(reportResult.getReportQuery().getVisibleMetrics().size()); 40 setAccMetricsCount(reportResult.getReportQuery().getVisibleAccumulableMetrics().size()); 41 setTableData(getTableModelData()); 42 setTableColumnNames(getTableModelColumns(groupingDimensions, nonGroupingDimensions, totalMetrics)); 43 newDefaultTableModel(); 44 } 45 46 52 private Object [] getNonGroupingDimensions() throws InfoException { 53 CubeQuery cubeQuery = reportResult.getReportQuery().getCubeQuery(); 54 int[] dimensionIndexes = cubeQuery.getColumns(); 55 return getQueryDimensionsArray(dimensionIndexes, reportResult); 56 } 57 58 64 private Object [] getGroupingDimensions() throws InfoException { 65 CubeQuery cubeQuery = reportResult.getReportQuery().getCubeQuery(); 66 int[] dimensionIndexes = cubeQuery.getRows(); 67 return getQueryDimensionsArray(dimensionIndexes, reportResult); 68 } 69 70 77 private Object [] getQueryDimensionsArray(int[] indexes, ReportResult reportResult) throws InfoException { 78 ReportQuery query = reportResult.getReportQuery(); 79 Object [] result = new Object [indexes.length]; 80 for (int i = 0; i < indexes.length; i++) { 81 String name = reportResult.getReportSpec().getDimensionFromIndex(indexes[i]).getName(); 82 QueryDimension dimension = query.getQueryDimensionFromName(name); 83 result[i] = dimension; 84 } 85 return result; 86 } 87 88 private Object [] getTotalMetrics() { 89 Object [] metricsNames = reportResult.getReportQuery().getVisibleMetrics().toArray(); 90 Object [] accuMetricsNames = reportResult.getReportQuery().getAccumulableMetrics().toArray(); 91 Object [] allMetrics = new Object [metricsNames.length + accuMetricsNames.length]; 92 int i = 0; 93 for( ; i < metricsNames.length ; i++) { 94 allMetrics[i] = metricsNames[i]; 95 } 96 for(int j = 0 ; j < accuMetricsNames.length ; j++, i++) { 97 QueryMetric queryMetric = (QueryMetric) accuMetricsNames[j]; 98 queryMetric.setName(queryMetric.getName() + "_ACCUM"); allMetrics[i] = queryMetric; 100 } 101 return allMetrics; 102 } 103 104 private Vector getTableModelData() { 105 rowsModel = reportResult.getRowsModel(); 106 columnsModel = reportResult.getColumnsModel(); 107 metricsCount = reportResult.getReportQuery().getVisibleMetrics().size(); 109 Vector dataVector = reportResult.getDataVector(); 110 Vector main = new Vector (); 111 if(super.getNonGroupingDimCount() == 0) { 112 fillGroupingVectorFrom2(new Vector (), main, rowsModel.getDimensionValueNode().getSubNodesList(), dataVector, 0); 113 } else { 114 fillGroupingVectorFrom(new Vector (), main, rowsModel.getDimensionValueNode().getSubNodesList(), dataVector, 0); 115 } 116 return main; 117 } 118 119 private int fillGroupingVectorFrom(Vector first, Vector main, Collection subnodes, Vector dataVector, int row) { 120 if(subnodes.size() > 0) { 121 Iterator iterator = subnodes.iterator(); 122 while(iterator.hasNext()) { 123 DimensionValueNode node = (DimensionValueNode) iterator.next(); 124 first.add(node.getValue().toString()); 125 if(node.getSubNodesList().size() == 0) { 126 while(rowsModel.isTotalRow(row)){ 127 row++; 128 } 129 132 fillNonGroupingVectorFrom(main, first, new Vector (), columnsModel.getDimensionValueNode().getSubNodesList(), dataVector, row, 0); 133 row++; 134 } else { 135 row = fillGroupingVectorFrom(first, main, node.getSubNodesList(), dataVector, row); 136 } 137 first.removeElementAt(first.size() - 1); 138 } 139 return row; 140 } else { 141 fillNonGroupingVectorFrom(main, first, new Vector (), columnsModel.getDimensionValueNode().getSubNodesList(), dataVector, row, 0); 142 return 0; 143 } 144 } 145 146 private int fillNonGroupingVectorFrom(Vector main, Vector first, Vector second, Collection subNodes, Vector dataVector, int row, int col) { 147 boolean leafReached = false; 148 DimensionValueNode node = null; 149 if(subNodes.size() > 0) { 150 Iterator iterator = subNodes.iterator(); 151 while(iterator.hasNext() && !leafReached) { 152 node = (DimensionValueNode) iterator.next(); 153 if(node.getSubNodesList().size() == 0) { 154 leafReached = true; 155 col = getItemVectorFrom(main, first, second, dataVector, row, col, false, node); 156 } else { 157 second.add(node.getValue().toString()); 158 col = fillNonGroupingVectorFrom(main, first, second, node.getSubNodesList(), dataVector, row, col); 159 leafReached = false; 160 second.removeElementAt(second.size() - 1); 161 } 162 } 163 return col; 164 } else { 165 getItemVectorFrom(main, first, second, dataVector, row, col, true, node); 166 return 0; 167 } 168 } 169 170 private int getItemVectorFrom(Vector main, Vector first, Vector second, Vector dataVector, int row, int col, boolean isColModelEmpty, DimensionValueNode node) { 171 Vector itemVector = new Vector (); 172 173 for(int i = 0 ; i < first.size() ; i++) { 174 itemVector.add(first.elementAt(i)); 175 } 176 177 for(int i = 0 ; i < second.size() ; i++) { 178 itemVector.add(second.elementAt(i)); 179 } 180 181 int analizedCols = getDatasFrom(dataVector, row, col, itemVector, isColModelEmpty, node); 182 183 main.add(itemVector); 184 return analizedCols; 185 } 186 187 private int getDatasFrom(Vector dataVector, int row, int col, Vector itemVector, boolean isColModelEmpty, DimensionValueNode node) { 188 int i = 0, p = 0; 189 Vector ocurrences = new Vector (); 190 fillOcurrences(ocurrences, columnsModel.getDimensionValueNode().getSubNodesList()); 191 192 for(int x = 0 ; x < ocurrences.size() ; x++) { 194 String currentOcurrence = (String ) ocurrences.elementAt(x); 195 if(currentOcurrence.equals(node.getValue().toString())) { 196 break; 197 } else { 198 for(int z = 0 ; z < metricsCount ; z++) { 199 SharedFloat sharedFloat = SharedFloat.newFrom(0); 200 itemVector.add(new Float (sharedFloat.floatValue())); 201 p++; 202 } 203 } 204 } 205 206 Vector vector = (Vector ) dataVector.elementAt(row); 208 for( ; i < (metricsCount * node.getParentNode().getSubNodesList().size()) ; i++) { 209 if(!isColModelEmpty) { 210 while(columnsModel.isTotalCol(col)){ 211 col++; 212 } 213 216 } 217 SharedFloat sharedFloat = (SharedFloat) vector.elementAt(col); 218 if (sharedFloat==null){ 219 itemVector.add(new Float (0)); 220 }else{ 221 itemVector.add(new Float (sharedFloat.floatValue())); 222 } 223 col++; 224 } 225 226 while((i + p) < (ocurrences.size() * metricsCount)) { 228 SharedFloat sharedFloat = SharedFloat.newFrom(0); 229 itemVector.add(new Float (sharedFloat.floatValue())); 230 i++; 231 } 232 if(reportResult.getReportQuery().isVisibleTotals()){ 234 int index = col; 235 for(int j = 0 ; j < metricsCount ; j++) { 236 SharedFloat sharedFloat = (SharedFloat) vector.elementAt(index); 237 itemVector.add(new Float (sharedFloat.floatValue())); 238 index++; 239 } 240 } 241 242 return col; 243 } 244 245 246 protected Vector getTableModelColumns(Object [] groupingDimensions, Object [] nonGroupingDimensions, Object [] metricsArray) { 247 Vector columnNames = new Vector (); 248 for (int i = 0; i < groupingDimensions.length; i++) { 249 QueryDimension qd = (QueryDimension) groupingDimensions[i]; 250 columnNames.add(qd.getName()); 251 } 252 253 occurrences = new Vector (); 254 fillOcurrences(occurrences, reportResult.getColumnsModel().getDimensionValueNode().getSubNodesList()); 255 256 if(nonGroupingDimensions.length > 0) { 257 for (int i = 0; i < nonGroupingDimensions.length; i++) { 258 QueryDimension qd = (QueryDimension) nonGroupingDimensions[i]; 259 if(i + 1 == nonGroupingDimensions.length) { 260 for(int j = 0 ; j < occurrences.size() ; j++) { 261 for (int z = 0; z < metricsArray.length; z++) { 262 QueryMetric qm = (QueryMetric) metricsArray[z]; 263 columnNames.add(j + "_" + qm.getName()); 264 } 265 } 266 if(reportResult.getReportQuery().isVisibleTotals()){ 268 for (int j = 0; j < metricsArray.length; j++) { 269 QueryMetric qm = (QueryMetric) metricsArray[j]; 270 columnNames.add("TOTAL_" + qm.getName()); 271 } 272 } 273 } else { 274 columnNames.add(qd.getName()); 275 } 276 } 277 } else { 278 for (int i = 0; i < metricsArray.length; i++) { 279 QueryMetric qm = (QueryMetric) metricsArray[i]; 280 columnNames.add(qm.getName()); 281 } 282 283 } 288 return columnNames; 289 } 290 291 private String getWellFormedString(String s) { 292 StringBuffer buffer = null; 293 String [] strings = null; 294 295 if(s.split("-").length > 1) { 296 buffer = new StringBuffer (); 297 strings = s.split("-"); 298 } else { 299 if(s.split("/").length > 1) { 300 buffer = new StringBuffer (); 301 strings = s.split("/"); 302 } else { 303 int i = s.indexOf(46); 304 if(i != -1) { 305 buffer = new StringBuffer (); 306 for(int j = 0 ; j < s.length() ; j++) { 307 if(j != i) { 308 String tmp = String.valueOf(s.charAt(j)); 309 buffer.append(tmp); 310 } 311 } 312 return buffer.toString(); 313 } 314 } 315 } 316 317 if(strings != null) { 318 for(int i = 0 ; i < strings.length ; i++) { 319 buffer.append(strings [i]); 320 } 321 return buffer.toString(); 322 } else { 323 return s; 324 } 325 } 326 327 private void fillOcurrences(Vector occurrences, Collection subNodesList) { 328 Iterator childs = subNodesList.iterator(); 329 while(childs.hasNext()) { 330 DimensionValueNode currentNode = (DimensionValueNode) childs.next(); 331 if(currentNode.getSubNodesList().size() == 0) { 332 if(!(occurrences.contains(currentNode.getValue().toString()))) { 333 occurrences.add(currentNode.getValue().toString()); 334 } 335 } else { 336 fillOcurrences(occurrences, currentNode.getSubNodesList()); 337 } 338 } 339 } 340 341 private int fillGroupingVectorFrom2(Vector first, Vector main, Collection subnodes, Vector dataVector, int row) { 342 if(subnodes.size() > 0) { 343 Iterator iterator = subnodes.iterator(); 344 while(iterator.hasNext()) { 345 DimensionValueNode node = (DimensionValueNode) iterator.next(); 346 Object value = node.getValue(); 347 if(value instanceof CubeValue){ 348 value = ((CubeValue)value).getValue(); 349 } 350 first.add(value); 351 if(node.getSubNodesList().size() == 0) { 352 while(rowsModel.isTotalRow(row)){ 353 row++; 354 } 355 fillNonGroupingVectorFrom2(main, first, new Vector (), columnsModel.getDimensionValueNode().getSubNodesList(), dataVector, row, 0); 356 row++; 357 } else { 358 row = fillGroupingVectorFrom2(first, main, node.getSubNodesList(), dataVector, row); 359 } 360 first.removeElementAt(first.size() - 1); 361 } 362 return row; 363 } else { 364 fillNonGroupingVectorFrom2(main, first, new Vector (), columnsModel.getDimensionValueNode().getSubNodesList(), dataVector, row, 0); 365 return 0; 366 } 367 } 368 369 private int fillNonGroupingVectorFrom2(Vector main, Vector first, Vector second, Collection subNodes, Vector dataVector, int row, int col) { 370 if(subNodes.size() > 0) { 371 Iterator iterator = subNodes.iterator(); 372 while(iterator.hasNext()) { 373 DimensionValueNode node = (DimensionValueNode) iterator.next(); 374 second.add(node.getValue().toString()); 375 if(node.getSubNodesList().size() == 0) { 376 col = getItemVectorFrom2(main, first, second, dataVector, row, col, false); 377 } else { 378 col = fillNonGroupingVectorFrom2(main, first, second, node.getSubNodesList(), dataVector, row, col); 379 } 380 second.removeElementAt(second.size() - 1); 381 } 382 return col; 383 } else { 384 getItemVectorFrom2(main, first, second, dataVector, row, col, true); 385 return 0; 386 } 387 } 388 389 private int getItemVectorFrom2(Vector main, Vector first, Vector second, Vector dataVector, int row, int col, boolean isColModelEmpty) { 390 Vector itemVector = new Vector (); 391 392 for(int i = 0 ; i < first.size() ; i++) { 393 itemVector.add(first.elementAt(i)); 394 } 395 396 for(int i = 0 ; i < second.size() ; i++) { 397 itemVector.add(second.elementAt(i)); 398 } 399 400 int analizedCols = getDatasFrom2(dataVector, row, col, itemVector, isColModelEmpty); 401 402 main.add(itemVector); 403 return analizedCols; 404 } 405 406 private int getDatasFrom2(Vector dataVector, int row, int col, Vector itemVector, boolean isColModelEmpty) { 407 Vector vector = (Vector ) dataVector.elementAt(row); 408 for(int i = 0 ; i < metricsCount ; i++) { 409 if(!isColModelEmpty) { 410 while(columnsModel.isTotalCol(col)){ 411 col++; 412 } 413 } 414 SharedFloat sharedFloat = (SharedFloat) vector.elementAt(col); 415 itemVector.add(new Float (sharedFloat.floatValue())); 416 col++; 417 } 418 return col; 419 } 420 421 } 422 | Popular Tags |