1 package com.calipso.reportgenerator.common; 2 3 import net.sf.jasperreports.engine.*; 4 import com.calipso.reportgenerator.reportdefinitions.ReportView; 5 import com.calipso.reportgenerator.reportdefinitions.types.ReportDefinitionReportTypeType; 6 7 import java.util.Map ; 8 import java.util.HashMap ; 9 10 17 public class ReportLayoutBuilder { 18 19 private ReportGeneratorConfiguration reportGeneratorConfiguration; 20 private ReportTableModel reportTableModel; 21 private ReportSpec reportSpec; 22 private ReportResult result; 23 private Map params; 24 25 public ReportLayoutBuilder(ReportGeneratorConfiguration reportGeneratorConfiguration, ReportSpec reportSpec, ReportResult result, Map params) throws InfoException { 26 this.reportGeneratorConfiguration = reportGeneratorConfiguration; 27 this.reportSpec = reportSpec; 28 this.result = result; 29 this.params = params; 30 if(result==null){ 31 this.reportTableModel = new StaticReportTableModel(reportSpec); 32 }else{ 33 this.reportTableModel = result.getReportTableModel(); 34 } 35 } 36 37 public ReportLayoutBuilder(ReportGeneratorConfiguration reportGeneratorConfiguration, ReportResult result, ReportSpec spec) throws InfoException { 38 this.reportGeneratorConfiguration = reportGeneratorConfiguration; 39 this.reportSpec = spec; 40 this.result = result; 41 if(result==null){ 42 this.reportTableModel = new StaticReportTableModel(spec); 43 }else{ 44 this.reportTableModel = result.getReportTableModel(); 45 } 46 this.params = getParams(); 47 } 48 49 private Map getParams() { 50 if(params==null){ 51 params = new HashMap (); 52 params.putAll(reportSpec.getPreParamValues()); 53 params.putAll(reportSpec.getPosParamValues()); 54 if(result!=null){ 55 params.putAll(result.getParamValues()); 56 } 57 } 58 return params; 59 } 60 61 62 public ReportGeneratorConfiguration getReportGeneratorConfiguration() { 63 return reportGeneratorConfiguration; 64 } 65 66 public JasperPrint getJasperPrint(IJasperDefinition design, boolean isLandscape) throws JRException { 67 JasperReport report = null; 68 setCompiler(reportGeneratorConfiguration); 69 System.out.println(LanguageTraslator.traslate("484")); 70 try{ 71 report = JasperCompileManager.compileReport(design.getJasperDefinition(isLandscape)); 72 }catch(JRException e){ 73 throw new JRException(LanguageTraslator.traslate("534"),e); 74 } 75 System.out.println(LanguageTraslator.traslate("485")); 76 ReportMap.setParametersToSimpleType(params); 77 JasperPrint print = JasperFillManager.fillReport(report, params, reportTableModel); 78 return print; 79 } 80 81 private void setCompiler(ReportGeneratorConfiguration reportGeneratorConfiguration) { 82 if (!reportGeneratorConfiguration.getJasperCompilerClass().equals("")) { 83 System.setProperty("jasper.reports.compiler.class", reportGeneratorConfiguration.getJasperCompilerClass()); 84 } else if (!reportGeneratorConfiguration.getJasperReportPath().equals("")) { 85 System.setProperty("jasper.reports.compile.class.path", reportGeneratorConfiguration.getJasperReportPath()); 86 } 87 } 88 89 public IJasperDefinition getJasperDefinition(ReportView reportView) throws InfoException{ 90 IJasperDefinition definition = getJasperDesign(reportView); 91 if(definition instanceof StaticSQLJasperReportDefinition){ 92 ReportDataSourceSpec dataSourceSpec = (ReportDataSourceSpec)reportSpec.getDataSourceSpecs().toArray()[0]; 93 ((StaticSQLJasperReportDefinition)definition).setSQLText(dataSourceSpec.getExpression()); 94 } 95 if(!isExternal(definition)){ 96 definition = addStyle(definition); 97 } 98 return definition; 99 } 100 101 public IJasperDefinition addStyle(IJasperDefinition design) { 102 String style = reportGeneratorConfiguration.getReportLayoutStyle(); 103 if(style==null || style.equalsIgnoreCase("")){ 104 return design; 105 }else if(style.equalsIgnoreCase(LanguageTraslator.traslate("486"))){ 106 return new CalipsoDecoratedReportLayout(design); 107 } 108 return design; 109 } 110 111 public static boolean isExternal(IJasperDefinition design) { 112 return (design instanceof ExternalJasperDefinition); 113 } 114 115 private IJasperDefinition getJasperDesign(ReportView reportView) throws InfoException{ 116 String reportLayoutId = null; 117 if(reportView!=null && reportView.getReportLayout()!=null && !reportView.getReportLayout().equalsIgnoreCase("")){ 118 reportLayoutId = reportView.getReportLayout(); 119 }else if ((reportSpec.getLayoutDesign()!=null) && !reportSpec.getLayoutDesign().equals("")) { 120 reportLayoutId = reportSpec.getLayoutDesign(); 121 }else{ 122 System.out.println("LayoutDesign: AUTO"); 123 return buildDefaulJasperDefinition(); 124 } 125 System.out.println("LayoutDesign: OVERRIDE"); 126 reportLayoutId = reportLayoutId.endsWith(".xml") ? reportLayoutId : reportLayoutId + ".xml"; 127 return new ExternalJasperDefinition(reportGeneratorConfiguration.getSourceReportLayoutPath() + "/" + reportLayoutId); 128 } 129 130 public IJasperDefinition buildDefaulJasperDefinition() throws InfoException { 131 if (reportSpec.getReportType().getType() == ReportDefinitionReportTypeType.CUBE_TYPE) { 132 return new CubeJasperReportDefinition(result, reportTableModel.getModel(), reportTableModel.getGroupingDimCount(), reportTableModel.getCommonMetricsCount(), 133 reportTableModel.getNonGroupingDimCount(), reportTableModel.getAccMetricsCount(), reportSpec.getTitle()); 134 } else if (reportSpec.getReportType().getType() == ReportDefinitionReportTypeType.ACCUM_TYPE){ 135 return new StaticJasperReportDefinition(result, reportTableModel.getModel(), reportTableModel.getGroupingDimCount(), reportTableModel.getCommonMetricsCount(), 136 reportTableModel.getNonGroupingDimCount(), reportTableModel.getAccMetricsCount(), reportSpec.getTitle()); 137 } else if (reportSpec.getReportType().getType() == ReportDefinitionReportTypeType.STATICSQL_TYPE){ 138 reportTableModel = new StaticReportTableModel(reportSpec); 139 StaticSQLJasperReportDefinition definition = new StaticSQLJasperReportDefinition(reportSpec, reportTableModel.getModel(), reportTableModel.getGroupingDimCount(), reportTableModel.getCommonMetricsCount(), 140 reportTableModel.getNonGroupingDimCount(), reportTableModel.getAccMetricsCount(), reportSpec.getTitle(), params); 141 ReportDataSourceSpec dataSourceSpec = (ReportDataSourceSpec)reportSpec.getDataSourceSpecs().toArray()[0]; 142 definition.setSQLText(dataSourceSpec.getExpression()); 143 return definition; 144 } 145 return null; 146 } 147 148 public JasperPrint getJasperPrint(ReportView reportView, boolean isLandscape) throws InfoException, JRException { 149 IJasperDefinition definition = getJasperDefinition(reportView); 150 return getJasperPrint(definition, isLandscape); 151 } 152 153 public JasperPrint getJasperPrint(boolean isLandscape) throws InfoException, JRException { 154 IJasperDefinition definition = getJasperDefinition(null); 155 return getJasperPrint(definition, isLandscape); 156 } 157 158 } | Popular Tags |