1 package com.calipso.reportgenerator.reportcalculator; 2 3 import com.calipso.reportgenerator.reportcalculator.*; 4 5 import java.util.Iterator ; 6 import java.io.Serializable ; 7 8 12 13 public class DataTree implements Serializable , PivotClient { 14 private DataTreeDefinition defintion; 15 private StaticQuery query; 16 private DataTreeNode root; 17 18 22 public DataTreeDefinition getDefintion() { 23 return defintion; 24 } 25 26 30 public void setDefinition(DataTreeDefinition definition) { 31 this.defintion = definition; 32 } 33 34 public Iterator valuesFor(int dimension, int[] previousDimensions, Object [] values) { 35 return null; 36 } 37 38 42 public DataTreeIterator iterator() { 43 return DataTreeIterator.on(this); 44 } 45 46 public Object [] measuresAtDimensionsValues(int[] dimensions, Object [] values) { 47 return new Object [0]; 48 } 49 50 54 public void setQuery(StaticQuery staticQuery) { 55 this.query = staticQuery; 56 } 57 58 61 public void reset() { 62 root = null; 63 } 64 65 69 public DataTreeNode getRoot() { 70 if (root == null) { 71 root = new DataTreeNode(null, "Root", query.getMetrics().length, -1); 72 } 73 return root; 74 } 75 76 87 public void fillWith(Object [] row) { 88 int[] groupDims = query.getGroupDimensions(); 89 int[] noGroupDims = query.getNoGroupDimensions(); 90 91 if(query.matches(row)) { 92 DataTreeNode currentNode = getRoot(); 93 SharedFloat[] metricValues = new SharedFloat[query.getMetrics().length]; 94 fillMetricValues(metricValues, row); 95 updateMetricValues(currentNode, metricValues); 96 97 for ( int i = 0; i < groupDims.length; i++) { 98 int index = groupDims[i]; 99 currentNode = currentNode.getNodeFrom(row[index], index ); 100 updateMetricValues(currentNode, metricValues); 101 } 102 103 String [] noGroupDimValues = new String [noGroupDims.length]; 104 for (int i = 0; i < noGroupDims.length; i++) { 105 int index = noGroupDims[i]; 106 noGroupDimValues[i] = row[index].toString(); 107 } 108 DataTreeSubItem subItem = currentNode.getSubItem(noGroupDimValues); 109 subItem.updateMetricValues(metricValues); 110 } 111 } 112 113 119 private void fillMetricValues(SharedFloat[] metricValues, Object [] row) { 120 int[] metrics = query.getMetrics(); 121 for (int i = 0; i < metrics.length; i++){ 122 int index = metrics[i]; 123 metricValues[i] = ((SharedFloat) row[index]); 124 } 125 } 126 127 133 private void updateMetricValues(DataTreeNode node, SharedFloat[] metricValues) { 134 for (int i = 0; i < metricValues.length; i++) { 135 node.updateMetricValue(i, metricValues[i]); 136 } 137 } 138 139 140 144 public StaticQuery getQuery() { 145 return query; 146 } 147 152 public void afterFill() { 153 int[] metrics = query.getMetrics(); 154 int[] accMetrics = query.getAccumulableMetrics(); 155 int[] adjAccMetrics = new int[accMetrics.length]; 156 for (int i = 0; i < accMetrics.length; i++) { 157 int metricIndex = -1; 158 for (int j = 0; j < metrics.length; j++) { 159 if (accMetrics[i] == metrics[j]){ 160 metricIndex = j; 161 break; 162 } 163 } 164 adjAccMetrics[i] = metricIndex; 165 } 166 if (getDefintion().getAccumulable()){ 167 root.calculateAccumulable(adjAccMetrics); 168 } 169 } 170 } 171 | Popular Tags |