1 package com.calipso.reportgenerator.reportmanager; 2 3 import com.calipso.reportgenerator.reportdefinitions.types.DataSourceDefinitionSourceTypeType; 4 import com.calipso.reportgenerator.reportcalculator.*; 5 import com.calipso.reportgenerator.common.*; 6 import com.calipso.reportgenerator.services.DataSourceDefinitionConnectionString; 7 import com.calipso.common.DateEx; 8 9 import java.util.*; 10 import java.text.DateFormat ; 11 import java.text.SimpleDateFormat ; 12 import java.io.Serializable ; 13 14 import com.calipso.reportgenerator.reportcalculator.IDataSource; 15 import com.calipso.reportgenerator.common.InfoException; 16 17 23 24 public class ReportSource implements Serializable { 25 26 private Matrix matrix; 27 private List filterDefinitions; 28 private Date lastExecution; 29 private Map paramValues; 30 private ReportGeneratorConfiguration reportGeneratorConfiguration; 31 private ReportSpec reportSpec; 32 33 34 42 43 private ReportSource(ReportSpec reportSpec, ReportGeneratorConfiguration reportGeneratorConfiguration, boolean init) throws InfoException { 44 if ((reportSpec == null) || (reportGeneratorConfiguration == null)) { 45 throw new InfoException(LanguageTraslator.traslate("42")); 46 } 47 else { 48 this.reportSpec = reportSpec; 49 this.reportGeneratorConfiguration = reportGeneratorConfiguration; 50 this.lastExecution = null; 51 this.paramValues = null; 52 if (init){ 53 initializeContents(); 54 } 55 } 56 } 57 58 64 65 public ReportSource(ReportSpec reportSpec, ReportGeneratorConfiguration reportGeneratorConfiguration) throws InfoException { 66 this(reportSpec, reportGeneratorConfiguration, true); 67 } 68 69 70 78 public ReportSource(ReportSpec reportSpec, Matrix matrix, ReportGeneratorConfiguration reportGeneratorConfiguration) throws InfoException { 79 this(reportSpec, reportGeneratorConfiguration, false); 80 if (matrix == null) { 81 throw new InfoException(LanguageTraslator.traslate("43")); 82 } 83 else { 84 this.matrix = matrix; 85 initializeContents(); 86 } 87 } 88 89 97 public ReportSource(ReportSpec reportSpec, Map paramValues, ReportGeneratorConfiguration reportGeneratorConfiguration) throws InfoException { 98 this(reportSpec, reportGeneratorConfiguration, false); 99 this.paramValues = paramValues; 100 initializeContents(); 101 } 102 103 113 public ReportSource(ReportSpec reportSpec, Matrix matrix, Date lastExecution, ReportGeneratorConfiguration reportGeneratorConfiguration) throws InfoException { 114 this(reportSpec, reportGeneratorConfiguration, false); 115 this.matrix = matrix; 116 this.lastExecution = lastExecution; 117 initializeContents(); 118 } 119 120 121 130 private void initializeContents() throws InfoException { 131 fillFromSources(matrix); 132 } 133 134 135 140 public Matrix getMatrix() { 141 return matrix; 142 } 143 144 148 149 public void setMatrix(Matrix matrix) { 150 this.matrix = matrix; 151 } 152 153 160 private ReportDataSource newReportDataSourceFrom(ReportDataSourceSpec dataSourceSpec) throws InfoException { 161 try { 162 ReportDataSource reportDataSource; 163 switch (dataSourceSpec.getSourceType().getType()) { 164 case DataSourceDefinitionSourceTypeType.XML_TYPE: 165 ReportManagerLogger.debug(LanguageTraslator.traslate("204")); 166 reportDataSource = new XmlReportDataSource(getReportSpec(), dataSourceSpec, getReportGeneratorConfiguration()); 167 break; 168 case DataSourceDefinitionSourceTypeType.SQL_TYPE: 169 ReportManagerLogger.debug(LanguageTraslator.traslate("202")); 170 reportDataSource = new SQLReportDataSource(getReportSpec(), dataSourceSpec, getReportGeneratorConfiguration(), new DataSourceDefinitionConnectionString(dataSourceSpec.getExternalConnectionValues())); 171 break; 172 176 case DataSourceDefinitionSourceTypeType.EXCEL_TYPE: 177 ReportManagerLogger.debug(LanguageTraslator.traslate("317")); 178 reportDataSource = new ExcelReportDataSource(getReportSpec(), dataSourceSpec, getReportGeneratorConfiguration()); 179 break; 180 default: 181 reportDataSource = null; 182 } 183 if (reportDataSource != null) { 184 reportDataSource.setFilter(getCubeFilter(reportDataSource.getFilterVarMode(), dataSourceSpec)); 185 } 186 return reportDataSource; 187 } catch (Exception e) { 188 throw new InfoException(LanguageTraslator.traslate("44"), e); 189 } 190 } 191 192 193 198 private void fillFromSources(Matrix matrix) throws InfoException { 199 ReportManagerLogger.debug(LanguageTraslator.traslate("201")); 200 if(matrix==null){ 201 matrix = DataSourceBuilder.buildMatrix(getReportGeneratorConfiguration(), getReportSpec()); 202 } 203 boolean incremental = !getReportSpec().getIncrementalDimension().equals(""); 204 boolean cached = getReportSpec().getCached() || getReportSpec().getDatawarehouseSaved(); 205 boolean empty = matrix.isEmpty(); 206 if (incremental || !cached || empty) { 207 Iterator iterator = getReportSpec().getDataSourceSpecs().iterator(); 208 while (iterator.hasNext()) { 209 if ( isFull(matrix.size())) return; 210 ReportDataSourceSpec dataSourceSpec = (ReportDataSourceSpec) iterator.next(); 211 ReportDataSource reportDataSource = newReportDataSourceFrom(dataSourceSpec); 212 System.out.println("Comienzo carga" + new Date()); 213 reportDataSource.getDataSource(matrix); 214 System.out.println("Fin carga" + new Date()); 215 226 } 227 } 228 this.matrix = matrix; 229 } 230 231 246 247 253 private void fillRows(IDataSource dataSource, Matrix matrix, ReportDataSourceSpec dataSourceSpec) throws InfoException { 254 int rowSize = getDimensionsCount() + getMetricsCount(); 255 try { 256 if (dataSource != null) { 257 for (int i = 0; i < dataSource.getRowCount(); i++) { 258 if (isFull(matrix.size())) return; 259 Object [] row = new Object [rowSize]; 260 fillRow(row, dataSource, i, dataSourceSpec); 261 matrix.add(row); 262 } 263 } 264 } catch (Exception e) { 265 throw new InfoException(LanguageTraslator.traslate("45"), e); 266 } 267 } 268 269 274 275 private boolean isFull(int rowCount) { 276 return getMaxRowCount() > 0 ? getMaxRowCount() <= rowCount : false; 277 } 278 279 284 private int getMaxRowCount() { 285 return getReportSpec().getSourceMaxRowCount(); 286 } 287 288 289 294 public ReportDimensionSpec getDimensionfromName(String name) { 295 return getReportSpec().getDimensionFromName(name); 296 } 297 298 299 304 public ReportMetricSpec getMetricFromName(String name) { 305 return getReportSpec().getMetricFromName(name); 306 } 307 308 316 private void fillRow(Object [] row, IDataSource dataSource, int index, ReportDataSourceSpec dataSourceSpec) throws InfoException { 317 try { 318 List dimensions = getReportSpec().getDimensionsByIndex(); 319 Iterator dimensionsIter = dimensions.iterator(); 320 while (dimensionsIter.hasNext()) { 321 ReportDimensionSpec dimensionSpec = (ReportDimensionSpec) dimensionsIter.next(); 322 row[dimensionSpec.getReportSourceIndex()] = dimensionSpec.getValue(dataSource.getRowValues(index).toArray(), dataSourceSpec); 323 } 324 325 List metrics = getReportSpec().getMetricsByIndex(); 326 Iterator metricsIter = metrics.iterator(); 327 while (metricsIter.hasNext()) { 328 ReportMetricSpec metricSpec = (ReportMetricSpec) metricsIter.next(); 329 row[metricSpec.getReportSourceIndex()] = metricSpec.getValue(dataSource.getRowValues(index).toArray()); 330 } 331 } catch (Exception e) { 332 throw new InfoException(LanguageTraslator.traslate("46"), e); 333 } 334 } 335 336 340 341 private int getDimensionsCount() { 342 return getReportSpec().getDimensionSpecs().size(); 343 } 344 345 346 350 private int getMetricsCount() { 351 return getReportSpec().getMetricSpecs().size(); 352 } 353 354 359 protected List getFilterDefinitions(ReportDataSourceSpec reportDataSourceSpec) { 360 if (filterDefinitions == null) { 361 filterDefinitions = new ArrayList(); 362 363 ReportFilter incFilter = getIncrementalFilter(reportDataSourceSpec); 364 if (incFilter != null) { 365 filterDefinitions.add(incFilter); 366 } 367 ReportFilter maxRowsFilter = getMaxRowsFilter(); 368 if (maxRowsFilter != null) { 369 filterDefinitions.add(maxRowsFilter); 370 } 371 Collection filterSpecs = getReportSpec().getPreFilterSpecs(); 372 for (Iterator iterator = filterSpecs.iterator(); iterator.hasNext();) { 373 ReportFilterSpec reportFilterSpec = (ReportFilterSpec) iterator.next(); 374 ReportFilter reportFilter = new ReportFilter(reportFilterSpec); 375 filterDefinitions.add(reportFilter); 376 } 377 } 378 return filterDefinitions; 379 } 380 381 public ReportSpec getReportSpec() { 382 return reportSpec; 383 } 384 385 390 private ReportFilter getIncrementalFilter(ReportDataSourceSpec reportDataSourceSpec) { 391 ReportFilter incFilter = null; 392 ReportFilterSpec incFilterSpec = getReportSpec().getIncrementalFilterSpec(getLastExecution()); 393 if (incFilterSpec != null){ 394 incFilter = new ReportFilter(incFilterSpec); 395 getParamValues().put("INCREMENTALVALUE", SharedDate.newFrom(new DateEx(getLastExecution()))); } 397 return incFilter; 398 } 399 400 private ReportFilter getMaxRowsFilter() { 401 ReportFilter maxRowsFilter = null; 402 ReportFilterSpec maxRowsFilterSpec = getReportSpec().getSourceMaxRowsFilterSpec(); 403 if (maxRowsFilterSpec != null){ 404 maxRowsFilter = new ReportFilter(maxRowsFilterSpec ); 405 } 406 return maxRowsFilter; 407 } 408 409 413 private Map getParamValues() { 414 if (paramValues == null) { 415 paramValues = new HashMap(); 416 } 417 return paramValues; 418 } 419 420 421 426 private String getDateString(Date date, ReportDataSourceSpec reportDataSourceSpec) { 427 int dataType = getReportSpec().getDimensionFromName(getReportSpec().getIncrementalDimension()).getDataType().getType(); 428 DateFormat dateFormat = new SimpleDateFormat (reportDataSourceSpec.getPattern(dataType)); 429 return dateFormat.format(date); 430 } 431 432 440 441 442 447 protected ExpressionCubeFilter getCubeFilter(int mode, ReportDataSourceSpec dataSourceSpec) throws InfoException{ 448 ExpressionCubeFilter cubeFilter; 449 Map currentParamValues = new HashMap(); 450 List filterDefs = getFilterDefinitions(dataSourceSpec); 451 currentParamValues = ReportFilterBuilder.mergeParamValues(currentParamValues, getReportSpec().getParamValues(true)); 452 currentParamValues = ReportFilterBuilder.mergeParamValues(currentParamValues, getParamValues()); 453 ReportFilterBuilder filterBuilder = new ReportFilterBuilder(filterDefs, currentParamValues, mode); 454 cubeFilter = filterBuilder.getCubeFilter(getReportSpec(), dataSourceSpec); 455 return cubeFilter; 456 } 457 458 459 463 public Date getLastExecution() { 464 return lastExecution; 465 } 466 467 471 472 public void setLastExecution(Date lastExecution) { 473 this.lastExecution = lastExecution; 474 } 475 476 480 public ReportGeneratorConfiguration getReportGeneratorConfiguration() { 481 return reportGeneratorConfiguration; 482 } 483 } 484 | Popular Tags |