1 package com.calipso.reportgenerator.common; 2 3 import com.calipso.reportgenerator.reportcalculator.*; 4 import com.calipso.reportgenerator.common.ReportTableModel; 5 6 import java.util.Vector ; 7 import java.util.Iterator ; 8 9 16 17 public class StaticReportTableModel extends ReportTableModel { 18 19 private StaticReportResult reportResult; 20 21 public StaticReportTableModel(StaticReportResult reportResult) throws InfoException { 22 super(); 23 this.reportResult = reportResult; 24 initialize(); 25 } 26 27 public StaticReportTableModel(ReportSpec reportSpec, ReportQuery query){ 28 super(); 29 initialize(reportSpec, query); 30 } 31 32 public StaticReportTableModel(ReportSpec reportSpec) throws InfoException{ 33 super(); 34 ReportQuery query = new ReportQuery(reportSpec); 35 initialize(reportSpec, query); 36 } 37 38 private void initialize(ReportSpec reportSpec, ReportQuery query){ 39 Object [] groupingDimensions; 40 Object [] nonGroupingDimensions; 41 groupingDimensions = reportSpec.getVisibleDimensionsByGrouping(true); 42 nonGroupingDimensions = reportSpec.getVisibleDimensionsByGrouping(false); 43 Object [] totalMetrics = reportSpec.getMetrics(); 44 setGroupingDimCount(groupingDimensions.length); 45 setNonGroupingDimCount(nonGroupingDimensions.length); 46 setCommonMetricsCount(query.getVisibleMetrics().size()); 47 setAccMetricsCount(query.getVisibleAccumulableMetrics().size()); 48 setTableColumnNames(getSpecTableModelColumns(groupingDimensions, nonGroupingDimensions, totalMetrics)); 49 newNonDataTableModel(); 51 } 52 53 protected Vector getSpecTableModelColumns(Object [] groupingDimensions, Object [] nonGroupingDimensions, Object [] metricsArray) { 54 Vector columnNames = new Vector (); 55 for (int i = 0; i < groupingDimensions.length; i++) { 56 ReportDimensionSpec qd = (ReportDimensionSpec) groupingDimensions[i]; 57 columnNames.add(qd.getName()); 58 } 59 for (int i = 0; i < nonGroupingDimensions.length; i++) { 60 ReportDimensionSpec qd = (ReportDimensionSpec) nonGroupingDimensions[i]; 61 columnNames.add(qd.getName()); 62 } 63 for (int i = 0; i < metricsArray.length; i++) { 64 ReportMetricSpec qm = (ReportMetricSpec) metricsArray[i]; 65 columnNames.add(qm.getName()); 66 } 67 return columnNames; 68 } 69 70 private void initialize() throws InfoException { 71 Object [] groupingDimensions = getGroupingDimensions(); 72 Object [] nonGroupingDimensions = getNonGroupingDimensions(); 73 Object [] totalMetrics = getTotalMetrics(); 74 setGroupingDimCount(groupingDimensions.length); 75 setNonGroupingDimCount(nonGroupingDimensions.length); 76 setCommonMetricsCount(reportResult.getReportQuery().getVisibleMetrics().size()); 77 setAccMetricsCount(reportResult.getReportQuery().getVisibleAccumulableMetrics().size()); 78 setTableColumnNames(getTableModelColumns(groupingDimensions, nonGroupingDimensions, totalMetrics)); 79 setTableData(getTableModelData()); 80 newDefaultTableModel(); 81 } 82 83 89 private Object [] getGroupingDimensions() throws InfoException { 90 StaticQuery staticQuery = reportResult.getDataTree().getQuery(); 91 int[] dimensionIndexes = staticQuery.getGroupDimensions(); 92 return getQueryDimensionsArray(dimensionIndexes, reportResult); 93 } 94 95 101 private Object [] getNonGroupingDimensions() throws InfoException { 102 StaticQuery staticQuery = reportResult.getDataTree().getQuery(); 103 int[] dimensionIndexes = staticQuery.getNoGroupDimensions(); 104 return getQueryDimensionsArray(dimensionIndexes, reportResult); 105 } 106 107 114 private Object [] getQueryDimensionsArray(int[] indexes, ReportResult reportResult) throws InfoException { 115 ReportQuery query = reportResult.getReportQuery(); 116 Object [] result = new Object [indexes.length]; 117 for (int i = 0; i < indexes.length; i++) { 118 String name = reportResult.getReportSpec().getDimensionFromIndex(indexes[i]).getName(); 119 QueryDimension dimension = query.getQueryDimensionFromName(name); 120 result[i] = dimension; 121 } 122 return result; 123 } 124 125 private Object [] getTotalMetrics() { 126 Object [] metricsNames = reportResult.getReportQuery().getVisibleMetrics().toArray(); 127 Object [] accuMetricsNames = reportResult.getReportQuery().getAccumulableMetrics().toArray(); 128 Object [] allMetrics = new Object [metricsNames.length + accuMetricsNames.length]; 129 int i = 0; 130 for( ; i < metricsNames.length ; i++) { 131 allMetrics[i] = metricsNames[i]; 132 } 133 for(int j = 0 ; j < accuMetricsNames.length ; j++, i++) { 134 QueryMetric queryMetric = (QueryMetric) accuMetricsNames[j]; 135 queryMetric.setName(queryMetric.getName() + "_ACCUM"); allMetrics[i] = queryMetric; 137 } 138 return allMetrics; 139 } 140 141 146 private Vector getTableModelData() { 147 DataTree dataTree = reportResult.getDataTree(); 148 return getDataVector(dataTree); 149 } 150 151 157 private Vector getDataVector(DataTree dataTree) { 158 Vector dataVector = new Vector (); 159 Vector previousValues = new Vector (); 160 fillFromNode(dataVector, dataTree.getRoot(), previousValues); 161 return dataVector; 162 } 163 164 171 private void fillFromNode(Vector dataVector, DataTreeNode node, Vector previousValues) { 172 if (node.getSubNodes().size() == 0) { 173 Iterator iterator = node.getSubItems().iterator(); 174 while (iterator.hasNext()) { 175 DataTreeSubItem subItem = (DataTreeSubItem) iterator.next(); 176 dataVector.add(getItemVector(previousValues, subItem)); 177 } 178 } 179 else { 180 Iterator iterator = node.getSubNodes().values().iterator(); 181 while (iterator.hasNext()) { 182 DataTreeNode subNode = (DataTreeNode) iterator.next(); 183 previousValues.add(subNode.getValue()); 184 fillFromNode(dataVector, subNode, previousValues); 185 previousValues.remove(previousValues.size() - 1); 186 } 187 } 188 } 189 190 196 private Vector getItemVector(Vector previousValues, DataTreeSubItem subItem) { 197 Vector itemVector = new Vector (previousValues); 198 String [] ngValues = subItem.getNonGroupingDimensionValues(); 199 for (int i = 0; i < ngValues.length; i++) { 200 String ngValue = ngValues[i]; 201 itemVector.add(ngValue); 202 } 203 204 SharedFloat[] metricValues = subItem.getMetricValues(); 205 for (int i = 0; i < metricValues.length; i++) { 206 SharedFloat metricValue = metricValues[i]; 207 itemVector.add(new Float (metricValue.floatValue())); 208 } 209 210 211 SharedFloat[] accMetricValues = subItem.getAccumulableMetricValues(); 212 for (int i = 0; i < accMetricValues.length; i++) { 213 SharedFloat accMetricValue = accMetricValues[i]; 214 itemVector.add(new Float (accMetricValue.floatValue())); 215 } 216 217 return itemVector; 218 } 219 } 220 | Popular Tags |