1 2 package com.calipso.reportgenerator.reportmanager; 3 4 import com.calipso.reportgenerator.reportcalculator.*; 5 import com.calipso.reportgenerator.reportdefinitions.types.*; 6 import com.calipso.reportgenerator.reportdefinitions.ReportView; 7 import com.calipso.reportgenerator.common.*; 8 import java.util.*; 9 import java.io.*; 10 11 import com.calipso.reportgenerator.common.InfoException; 12 13 17 18 public abstract class Report implements Serializable{ 19 20 private ReportSource reportSource; 21 private Pivot pivot; 22 private ReportData reportData; 23 private ReportSpec reportSpec; 24 private ReportQuery query; 25 private ReportGeneratorConfiguration configuration; 26 27 30 public Report(){ 31 } 32 33 34 40 public Report(ReportSpec reportSpec, ReportSource reportSource, ReportGeneratorConfiguration configuration) throws InfoException { 41 if ((reportSpec == null) || (reportSource == null)) { 42 throw new InfoException(LanguageTraslator.traslate("47")); 43 } 44 else { 45 this.configuration = configuration; 46 this.reportSpec = reportSpec; 47 this.reportSource = reportSource; 48 pivot = buildPivot(); 49 pivot.setMatrix(getReportSource().getMatrix()); 50 } 51 } 52 53 private Pivot buildPivot() throws InfoException { 54 try{ 55 if(reportSource.getMatrix().getClass().getName().endsWith("DatawarehouseMatrix")){ 56 return (Pivot)Class.forName("com.calipso.reportgenerator.reportcalculator.DatawarehousePivot").newInstance(); 57 } 58 return new Pivot(); 59 }catch (Exception e){ 60 throw new InfoException(LanguageTraslator.traslate("591"), e); 61 } 62 } 63 64 public ReportSpec getReportSpec() { 65 return reportSpec; 66 } 67 68 72 public ReportSource getReportSource() { 73 return reportSource; 74 } 75 76 81 82 protected Pivot getPivot() { 83 return pivot; 84 } 85 86 90 protected ReportData getReportData() { 91 return reportData; 92 } 93 94 protected void setReportData(ReportData reportData) { 95 this.reportData = reportData; 96 } 97 98 protected DataVectorBuilder getDataVectorBuilder(DimensionValueNode rowsRoot,DimensionValueNode columnsRoot) throws InfoException { 99 return new DataVectorBuilder(rowsRoot, columnsRoot,getReportData().getQuery(),getReportData(),getReportData().getQuery().isVisibleTotals()); 100 } 101 102 106 107 public ReportResult ExecQuery(Map paramValues) throws InfoException { 108 return ExecQuery(getDefaultQuery(paramValues)); 109 } 110 111 public ReportGeneratorConfiguration getConfiguration() { 112 return configuration; 113 } 114 115 120 protected ReportQuery getDefaultQuery() throws InfoException { 121 ReportQuery query = new ReportQuery(getReportSpec()); 122 return query; 123 } 124 125 131 protected ReportQuery getDefaultQuery(Map paramValues) throws InfoException { 132 ReportQuery query = new ReportQuery(getReportSpec()); 133 query.setParamValues(paramValues); 134 return query; 135 } 136 137 143 protected ReportQuery getDefaultQuery(ReportView reportView) throws InfoException { 144 return new ReportQuery(getReportSpec(),reportView); 145 } 146 147 152 protected ReportQuery getQuery() throws InfoException { 153 if (query==null){ 154 query = new ReportQuery(getReportSpec(), false); 155 } 156 return query; 157 } 158 159 protected void setReportQuery(ReportQuery query) { 160 this.query = query; 161 } 162 163 168 public Set getDimensionValues(String name) throws InfoException { 169 try { 170 QueryDimension dimension = getQuery().getQueryDimensionFromName(name); 171 int index = dimension.getIndex(); 172 173 return getReportData().getDimensionValues(index); 174 } catch (Exception e) { 175 throw new InfoException(LanguageTraslator.traslate("89"), e); 176 } 177 } 178 179 184 public abstract String getXml() throws InfoException; 185 186 193 public abstract ReportResult ExecQuery(ReportQuery query) throws InfoException; 194 195 204 protected void fillEnumeration(ReportQuery reportQuery) throws InfoException{ 205 Collection ranking = getFiltersByType(reportSpec, FilterDefinitionFilterTypeType.RANKING); 207 Iterator iterator = ranking.iterator(); 208 while(iterator.hasNext()){ 209 Set includes = new TreeSet(); 210 ReportQuery query = new ReportQuery(reportQuery.getReportSpec()); 212 ReportFilterSpec filter = (ReportFilterSpec)iterator.next(); 213 ReportDimensionSpec dimension = reportQuery.getReportSpec().getDimensionFromName(filter.getDimensionName()); 214 query.setUniqueDimension(dimension); 216 query.setUniqueMetricVisible(dimension.getRankMetricName()); 217 query.setDimensionRankMetricName(filter.getDimensionName(), dimension.getRankMetricName()); 218 query.removeFilters(ranking); 220 ReportResult result = ExecQuery(query); 222 Collection values = result.getValuesCollection(dimension.getOrder()==DimensionDefinitionOrderType.A); 223 if(!values.isEmpty()){ 224 includes.addAll(getTopFor(reportQuery, values, filter)); 226 } 227 query.addFilters(ranking); 229 reportQuery.getRankingFilter().addTo(includes, dimension.getIndex()); 231 } 232 } 233 234 protected void fillExcludedEnumeration(ReportQuery reportQuery) throws InfoException{ 235 Collection exclude = getFiltersByType(reportSpec, FilterDefinitionFilterTypeType.EXCLUDEGROUP); 237 Iterator iterator = exclude.iterator(); 238 while(iterator.hasNext()){ 239 Set includes = new TreeSet(); 240 ReportQuery query = new ReportQuery(reportQuery.getReportSpec()); 242 ReportFilterSpec filter = (ReportFilterSpec)iterator.next(); 243 ReportDimensionSpec dimension = reportQuery.getReportSpec().getDimensionFromName(filter.getDimensionName()); 244 query.setUniqueDimension(dimension); 246 query.setUniqueMetricVisible(dimension.getRankMetricName()); 247 query.setDimensionRankMetricName(filter.getDimensionName(), dimension.getRankMetricName()); 248 query.removeFilters(exclude); 250 ReportResult result = ExecQuery(query); 252 includes.addAll(getTopFor(result, reportQuery, filter)); 254 Collection obligatory = getObligatoryValues(reportQuery, filter); 255 addValues(includes, obligatory); 256 query.addFilters(exclude); 258 reportQuery.getExcludeGroupFilter().addTo(includes, dimension.getIndex()); 260 } 261 } 262 263 private void addValues(Set includes, Collection obligatory) { 264 for (Iterator iterator = obligatory.iterator(); iterator.hasNext();) { 265 Object value = iterator.next(); 266 includes.add(value); 267 } 268 } 269 270 private Collection getObligatoryValues(ReportQuery query, ReportFilterSpec filter) throws InfoException{ 271 Object param = FilterOperation.getParam(query, filter, ParameterValueFilterParameterType.OBLIGATORYVALUE); 272 if(param==null){ 273 return new HashSet(); 274 } 275 String values = param.toString(); 276 ReportDimensionSpec dimension = query.getReportSpec().getDimensionFromName(filter.getDimensionName()); 277 ReportDataSourceSpec dataSource = (ReportDataSourceSpec)query.getReportSpec().getDataSourceSpecs().iterator().next(); 278 try{ 279 return filter.getValues(ReportFilterBuilder.VARMODE_INDEX, dataSource, dimension, values); 280 }catch (Exception e){ 281 throw new InfoException("85",e); 282 } 283 } 284 285 292 private Collection getTopFor(ReportQuery reportQuery, Collection values, ReportFilterSpec filter) throws InfoException{ 293 Collection result = new Vector(); 294 int max; 295 Object o = reportQuery.getParamValues().get(filter.getParamNames().get(0)); 296 if(o instanceof SharedInteger){ 297 max = ((SharedInteger)o).intValue(); 298 }else if(o instanceof SharedString){ 299 max = Integer.valueOf(((SharedString)o).value()).intValue(); 300 }else{ 301 try{ 302 max = new Integer (o.toString().trim()).intValue(); 303 }catch (Exception e){ 304 throw new InfoException(e); 305 } 306 } 307 Iterator iter = values.iterator(); 308 for(int i=0; i < max && iter.hasNext(); i++){ 309 Object ob = iter.next(); 310 if(ob instanceof DimensionValueNode){ 311 result.add(((DimensionValueNode)ob).getValue()); 312 }else if(ob instanceof Map.Entry){ 313 result.add(((Map.Entry)((Map.Entry)ob).getValue()).getKey()); 314 } 315 } 316 return result; 317 } 318 319 323 private Collection getTopFor(ReportResult reportResult, ReportQuery reportQuery, ReportFilterSpec filter) throws InfoException{ 324 FilterOperation filterOperation = FilterOperation.newFrom(reportResult, reportQuery, filter); 325 return filterOperation.operate(reportResult); 326 } 327 328 333 public static Collection getFiltersByType(ReportSpec reportSpec, FilterDefinitionFilterTypeType type) throws InfoException{ 334 Collection result = new Vector(); 335 Iterator iterator = reportSpec.getFilterSpecs().iterator(); 336 while(iterator.hasNext()){ 337 ReportFilterSpec current = (ReportFilterSpec)iterator.next(); 338 if(current.getFilterType()==type){ 339 result.add(current); 340 } 341 } 342 return result; 343 } 344 345 } 346 | Popular Tags |