001: package com.calipso.reportgenerator.common;
002:
003: import net.sf.jasperreports.engine.*;
004: import com.calipso.reportgenerator.reportdefinitions.ReportView;
005: import com.calipso.reportgenerator.reportdefinitions.types.ReportDefinitionReportTypeType;
006:
007: import java.util.Map;
008: import java.util.HashMap;
009:
010: /**
011: *
012: * User: jbassino
013: * Date: 25/10/2004
014: * Time: 17:18:24
015: *
016: */
017: public class ReportLayoutBuilder {
018:
019: private ReportGeneratorConfiguration reportGeneratorConfiguration;
020: private ReportTableModel reportTableModel;
021: private ReportSpec reportSpec;
022: private ReportResult result;
023: private Map params;
024:
025: public ReportLayoutBuilder(
026: ReportGeneratorConfiguration reportGeneratorConfiguration,
027: ReportSpec reportSpec, ReportResult result, Map params)
028: throws InfoException {
029: this .reportGeneratorConfiguration = reportGeneratorConfiguration;
030: this .reportSpec = reportSpec;
031: this .result = result;
032: this .params = params;
033: if (result == null) {
034: this .reportTableModel = new StaticReportTableModel(
035: reportSpec);
036: } else {
037: this .reportTableModel = result.getReportTableModel();
038: }
039: }
040:
041: public ReportLayoutBuilder(
042: ReportGeneratorConfiguration reportGeneratorConfiguration,
043: ReportResult result, ReportSpec spec) throws InfoException {
044: this .reportGeneratorConfiguration = reportGeneratorConfiguration;
045: this .reportSpec = spec;
046: this .result = result;
047: if (result == null) {
048: this .reportTableModel = new StaticReportTableModel(spec);
049: } else {
050: this .reportTableModel = result.getReportTableModel();
051: }
052: this .params = getParams();
053: }
054:
055: private Map getParams() {
056: if (params == null) {
057: params = new HashMap();
058: params.putAll(reportSpec.getPreParamValues());
059: params.putAll(reportSpec.getPosParamValues());
060: if (result != null) {
061: params.putAll(result.getParamValues());
062: }
063: }
064: return params;
065: }
066:
067: public ReportGeneratorConfiguration getReportGeneratorConfiguration() {
068: return reportGeneratorConfiguration;
069: }
070:
071: public JasperPrint getJasperPrint(IJasperDefinition design,
072: boolean isLandscape) throws JRException {
073: JasperReport report = null;
074: setCompiler(reportGeneratorConfiguration);
075: System.out.println(LanguageTraslator.traslate("484"));
076: try {
077: report = JasperCompileManager.compileReport(design
078: .getJasperDefinition(isLandscape));
079: } catch (JRException e) {
080: throw new JRException(LanguageTraslator.traslate("534"), e);
081: }
082: System.out.println(LanguageTraslator.traslate("485"));
083: ReportMap.setParametersToSimpleType(params);
084: JasperPrint print = JasperFillManager.fillReport(report,
085: params, reportTableModel);
086: return print;
087: }
088:
089: private void setCompiler(
090: ReportGeneratorConfiguration reportGeneratorConfiguration) {
091: if (!reportGeneratorConfiguration.getJasperCompilerClass()
092: .equals("")) {
093: System.setProperty("jasper.reports.compiler.class",
094: reportGeneratorConfiguration
095: .getJasperCompilerClass());
096: } else if (!reportGeneratorConfiguration.getJasperReportPath()
097: .equals("")) {
098: System.setProperty("jasper.reports.compile.class.path",
099: reportGeneratorConfiguration.getJasperReportPath());
100: }
101: }
102:
103: public IJasperDefinition getJasperDefinition(ReportView reportView)
104: throws InfoException {
105: IJasperDefinition definition = getJasperDesign(reportView);
106: if (definition instanceof StaticSQLJasperReportDefinition) {
107: ReportDataSourceSpec dataSourceSpec = (ReportDataSourceSpec) reportSpec
108: .getDataSourceSpecs().toArray()[0];
109: ((StaticSQLJasperReportDefinition) definition)
110: .setSQLText(dataSourceSpec.getExpression());
111: }
112: if (!isExternal(definition)) {
113: definition = addStyle(definition);
114: }
115: return definition;
116: }
117:
118: public IJasperDefinition addStyle(IJasperDefinition design) {
119: String style = reportGeneratorConfiguration
120: .getReportLayoutStyle();
121: if (style == null || style.equalsIgnoreCase("")) {
122: return design;
123: } else if (style.equalsIgnoreCase(LanguageTraslator
124: .traslate("486"))) {
125: return new CalipsoDecoratedReportLayout(design);
126: }
127: return design;
128: }
129:
130: public static boolean isExternal(IJasperDefinition design) {
131: return (design instanceof ExternalJasperDefinition);
132: }
133:
134: private IJasperDefinition getJasperDesign(ReportView reportView)
135: throws InfoException {
136: String reportLayoutId = null;
137: if (reportView != null && reportView.getReportLayout() != null
138: && !reportView.getReportLayout().equalsIgnoreCase("")) {
139: reportLayoutId = reportView.getReportLayout();
140: } else if ((reportSpec.getLayoutDesign() != null)
141: && !reportSpec.getLayoutDesign().equals("")) {
142: reportLayoutId = reportSpec.getLayoutDesign();
143: } else {
144: System.out.println("LayoutDesign: AUTO");
145: return buildDefaulJasperDefinition();
146: }
147: System.out.println("LayoutDesign: OVERRIDE");
148: reportLayoutId = reportLayoutId.endsWith(".xml") ? reportLayoutId
149: : reportLayoutId + ".xml";
150: return new ExternalJasperDefinition(
151: reportGeneratorConfiguration
152: .getSourceReportLayoutPath()
153: + "/" + reportLayoutId);
154: }
155:
156: public IJasperDefinition buildDefaulJasperDefinition()
157: throws InfoException {
158: if (reportSpec.getReportType().getType() == ReportDefinitionReportTypeType.CUBE_TYPE) {
159: return new CubeJasperReportDefinition(result,
160: reportTableModel.getModel(), reportTableModel
161: .getGroupingDimCount(), reportTableModel
162: .getCommonMetricsCount(), reportTableModel
163: .getNonGroupingDimCount(), reportTableModel
164: .getAccMetricsCount(), reportSpec
165: .getTitle());
166: } else if (reportSpec.getReportType().getType() == ReportDefinitionReportTypeType.ACCUM_TYPE) {
167: return new StaticJasperReportDefinition(result,
168: reportTableModel.getModel(), reportTableModel
169: .getGroupingDimCount(), reportTableModel
170: .getCommonMetricsCount(), reportTableModel
171: .getNonGroupingDimCount(), reportTableModel
172: .getAccMetricsCount(), reportSpec
173: .getTitle());
174: } else if (reportSpec.getReportType().getType() == ReportDefinitionReportTypeType.STATICSQL_TYPE) {
175: reportTableModel = new StaticReportTableModel(reportSpec);
176: StaticSQLJasperReportDefinition definition = new StaticSQLJasperReportDefinition(
177: reportSpec, reportTableModel.getModel(),
178: reportTableModel.getGroupingDimCount(),
179: reportTableModel.getCommonMetricsCount(),
180: reportTableModel.getNonGroupingDimCount(),
181: reportTableModel.getAccMetricsCount(), reportSpec
182: .getTitle(), params);
183: ReportDataSourceSpec dataSourceSpec = (ReportDataSourceSpec) reportSpec
184: .getDataSourceSpecs().toArray()[0];
185: definition.setSQLText(dataSourceSpec.getExpression());
186: return definition;
187: }
188: return null;
189: }
190:
191: public JasperPrint getJasperPrint(ReportView reportView,
192: boolean isLandscape) throws InfoException, JRException {
193: IJasperDefinition definition = getJasperDefinition(reportView);
194: return getJasperPrint(definition, isLandscape);
195: }
196:
197: public JasperPrint getJasperPrint(boolean isLandscape)
198: throws InfoException, JRException {
199: IJasperDefinition definition = getJasperDefinition(null);
200: return getJasperPrint(definition, isLandscape);
201: }
202:
203: }
|