1 package com.calipso.reportgenerator.reportmanager; 2 3 import com.calipso.reportgenerator.reportcalculator.CubeQuery; 5 import com.calipso.reportgenerator.reportcalculator.SharedDate; 6 import com.calipso.reportgenerator.reportcalculator.SharedFloat; 7 import com.calipso.reportgenerator.common.*; 8 import org.apache.xerces.dom.*; 9 import org.apache.xml.serialize.OutputFormat; 10 import org.apache.xml.serialize.XMLSerializer; 11 import org.w3c.dom.*; 12 13 import java.io.StringWriter ; 14 import java.io.IOException ; 15 import java.util.Map ; 16 import java.text.DateFormat ; 17 import java.text.SimpleDateFormat ; 18 import java.text.DecimalFormat ; 19 20 import com.calipso.reportgenerator.common.InfoException; 21 22 26 public class CubeReportXmlWriter implements ReportXmlWriter { 27 private ReportData reportData; 28 private ReportDataIterator reportDataIterator; 29 private ReportSpec reportSpec; 30 private Document document; 31 private Map paramValues; 32 private CubeQuery cubeQuery; 33 private Element root; 34 35 public CubeReportXmlWriter(ReportData reportData, ReportSpec reportSpec, Map paramValues, ReportQuery query) throws InfoException { 36 this.reportData = reportData; 37 this.reportDataIterator = reportData.iterator(); 38 this.reportSpec = reportSpec; 39 this.paramValues = paramValues; 40 cubeQuery = query.getCubeQuery(); 41 } 42 43 47 public StringWriter getXml() throws InfoException { 48 try { 49 59 startDocument(); 60 Element node = createDocument(); 61 root = createElement("Report"); 62 node.appendChild(root); 63 iterateRows(root); 64 return getStream(); 65 } catch (Exception e) { 66 throw new InfoException(LanguageTraslator.traslate("76"), e); 67 } 68 } 69 70 public ReportSpec getReportSpec() { 71 return reportSpec; 72 } 73 74 public Map getParamValues() { 75 return paramValues; 76 } 77 78 81 private void addParameters() { 82 } 84 85 90 private StringWriter getStream() throws IOException { 91 OutputFormat format = new OutputFormat(document, "ISO-8859-1", true); 92 StringWriter stringOut = new StringWriter (); 93 XMLSerializer serial = new XMLSerializer(stringOut, format); 94 serial.asDOMSerializer(); 95 Element elem = document.getDocumentElement(); 96 if (elem != null) { 97 serial.serialize(elem); 98 } 99 return stringOut; 100 } 101 102 private void iterateRows(Element node) { 103 int i; 104 int [] rows = cubeQuery.getRows(); 105 if(rows.length > 0) { 106 while(reportDataIterator.hasNext()) { 107 reportDataIterator.advance(); 108 Object [] values = reportDataIterator.current(); 109 for(i = 0 ; i < rows.length ; i++) { 110 ReportDimensionSpec dimensionSpec = reportSpec.getDimensionFromIndex(rows [i]); 111 node = getNodeFrom(dimensionSpec.getName(), getNodeValue(dimensionSpec,values [i]), node, values); 112 } 113 114 iterateColumns(node, values, i); 115 node = root; 116 } 117 } 118 } 119 120 121 122 private void iterateColumns(Element node, Object [] values, int i) { 123 int [] cols = cubeQuery.getColumns(); 124 if(cols.length > 0) { 125 for(int j = 0 ; j < cols.length ; j++, i++) { 126 ReportDimensionSpec dimensionSpec = reportSpec.getDimensionFromIndex(cols [j]); 127 node = getNodeFrom(dimensionSpec.getName(), getNodeValue(dimensionSpec,values [i]), node, values); 128 } 129 } 130 } 131 132 138 private String getNodeValue(ReportDimensionSpec dimensionSpec, Object value){ 139 if (value instanceof SharedDate) { 140 DateFormat dateFormat = new SimpleDateFormat ("yyyyMMdd"); 141 return dateFormat.format(((SharedDate) value).getDateEx().getDate()); 142 } else if (value instanceof SharedFloat) { 143 DecimalFormat df = new DecimalFormat (); 144 return df.format(((SharedFloat) value).floatValue()); 145 } else { 146 return value.toString(); 147 } 148 } 149 150 private Element getNodeFrom(String name, String value, Element node, Object [] values) { 151 Element returnVal = null; 152 153 NodeList childs = node.getChildNodes(); 154 for(int i = 0 ; i < childs.getLength() ; i++) { 155 Element child = (Element) childs.item(i); 156 Attr attr = child.getAttributeNode("Value"); 157 if(attr.getValue().equalsIgnoreCase(value)) { 158 returnVal = child; 159 break; 160 } 161 } 162 163 if(returnVal == null) { 164 returnVal = document.createElement(name); 165 returnVal.setAttribute("Value", value); 166 node.appendChild(returnVal); 167 } 168 169 addMetrics(returnVal, values, cubeQuery.getRows().length + cubeQuery.getColumns().length); 170 return returnVal; 171 } 172 173 private void addMetrics(Element returnVal, Object [] values, int j) { 174 int [] metrics = cubeQuery.getMetrics(); 175 for(int i = 0 ; i < metrics.length ; i++, j++) { 176 ReportMetricSpec metric = reportSpec.getMetricFromIndex(i); 177 if(returnVal.hasAttribute(metric.getName())) { 178 Attr attr = returnVal.getAttributeNode(metric.getName()); 179 float metricValue = Float.valueOf(attr.getValue()).floatValue(); 180 float result = ((SharedFloat)values [j]).floatValue() + metricValue; 181 attr.setValue(String.valueOf(result)); 182 } else { 183 returnVal.setAttribute(metric.getName(), values [j].toString()); 184 } 185 } 186 } 187 188 194 195 private void iterateCube(int dimension, Element ownerNode, boolean eoc) { 196 Element elemTo; 197 Element child; 198 Object [] totalObj = null; 199 int dimTo; 200 int dimSize; 201 boolean lEoc = false; 202 dimSize = getCubeQuery().getDimensions().length; 205 206 if (!eoc) { 207 child = createNodeElement(getDimensionNameFromIndex(dimension), reportDataIterator.current()[dimension].toString()); 208 ownerNode.appendChild(child); 209 if (dimension < (dimSize - 1)) { 210 iterateCube((dimension + 1), child, eoc); 211 } 212 else { 213 FillValue(child); 214 if (reportDataIterator.hasNext()) { 215 reportDataIterator.advance(); 216 } 217 else { 218 lEoc = true; 219 reportDataIterator.advance(); 220 } 221 elemTo = child; 222 dimTo = dimension; 223 for (int i = 0; i < (reportDataIterator.currentTotals().size()); i++) { 224 dimTo = dimTo - 1; 225 elemTo = (Element) elemTo.getParentNode(); 226 if (i < (reportDataIterator.currentTotals().size())) { 227 totalObj = (Object []) reportDataIterator.currentTotals().toArray()[i]; 228 fillTotals(elemTo, totalObj); 229 } 230 } 231 iterateCube(dimTo, (Element) elemTo.getParentNode(), lEoc); 232 } 233 } 234 } 235 236 private CubeQuery getCubeQuery() { 237 if (cubeQuery == null) { 238 try { 239 cubeQuery = reportData.getQuery().getCubeQuery(); 240 } catch (InfoException e) { 241 return null; 242 } 243 } 244 return cubeQuery; 245 } 246 247 252 private String getDimensionNameFromIndex(int dimension) { 253 return ((QueryDimension)reportData.getQuery().getDimensions().get(dimension)).getName(); 254 256 } 257 258 263 private String getMetricNameFromIndex(int metric) { 264 return ((QueryMetric)reportData.getQuery().getMetrics().get(metric)).getName(); 265 } 267 268 269 274 275 private void fillTotals(Element elem, Object [] total) { 276 int index = 0; 277 int currentMetric = 0; 278 int metricsFound = 0; 279 int metricsCount = reportData.getQuery().getMetrics().size(); 280 281 while (metricsFound < metricsCount) { 282 if (total[index] instanceof SharedFloat) { 283 String metricName = getMetricNameFromIndex(currentMetric); 284 elem.setAttribute(metricName, total[index].toString()); 285 currentMetric++; 286 metricsFound++; 287 } 288 index++; 289 } 290 } 291 292 297 private void fillMetricValues(Element elem, Object [] metricValues) { 298 int index = 0; 299 int currentMetric = 0; 300 int metricsFound = 0; 301 int metricsCount = reportData.getQuery().getMetrics().size(); 302 303 while (metricsFound < metricsCount) { 304 if (metricValues[index] instanceof SharedFloat) { 305 String metricName = getMetricNameFromIndex(currentMetric); 306 elem.setAttribute(metricName, metricValues[index].toString()); 307 currentMetric++; 308 metricsFound++; 309 } 310 index++; 311 } 312 } 313 314 318 private void FillValue(Element elem) { 319 Object [] row; 320 row = reportDataIterator.current(); 321 fillMetricValues(elem, row); 322 } 323 324 328 private Element createDocument() { 329 Element root; 330 root = createElement("Result"); 331 document.appendChild(root); 332 String reportDefName = getReportSpec().getDescription(); 333 root.setAttribute("ReportRefinition", reportDefName); 334 return root; 335 } 336 337 340 private void startDocument() { 341 document = new DocumentImpl(); 342 } 343 344 350 private Element createNodeElement(String tagName, String value) { 351 Element newElem; 352 newElem = document.createElement(tagName); 353 newElem.setAttribute("Value", value); 354 return newElem; 355 } 356 357 362 private Element createElement(String tagName) { 363 return document.createElement(tagName); 364 } 365 } 366 | Popular Tags |