001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ui.jasperreports;
018:
019: import java.io.OutputStream;
020: import java.io.Writer;
021: import java.util.Collection;
022: import java.util.Map;
023:
024: import net.sf.jasperreports.engine.JRDataSource;
025: import net.sf.jasperreports.engine.JRDataSourceProvider;
026: import net.sf.jasperreports.engine.JRException;
027: import net.sf.jasperreports.engine.JRExporter;
028: import net.sf.jasperreports.engine.JRExporterParameter;
029: import net.sf.jasperreports.engine.JasperFillManager;
030: import net.sf.jasperreports.engine.JasperPrint;
031: import net.sf.jasperreports.engine.JasperReport;
032: import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
033: import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
034: import net.sf.jasperreports.engine.export.JRCsvExporter;
035: import net.sf.jasperreports.engine.export.JRHtmlExporter;
036: import net.sf.jasperreports.engine.export.JRPdfExporter;
037: import net.sf.jasperreports.engine.export.JRXlsExporter;
038:
039: /**
040: * Utility methods for working with JasperReports. Provides a set of convenience
041: * methods for generating reports in a CSV, HTML, PDF and XLS formats.
042: *
043: * @author Rob Harrop
044: * @author Juergen Hoeller
045: * @since 1.1.3
046: */
047: public abstract class JasperReportsUtils {
048:
049: /**
050: * Convert the given report data value to a <code>JRDataSource</code>.
051: * <p>In the default implementation, a <code>JRDataSource</code>,
052: * <code>java.util.Collection</code> or object array is detected.
053: * The latter are converted to <code>JRBeanCollectionDataSource</code>
054: * or <code>JRBeanArrayDataSource</code>, respectively.
055: * @param value the report data value to convert
056: * @return the JRDataSource
057: * @throws IllegalArgumentException if the value could not be converted
058: * @see net.sf.jasperreports.engine.JRDataSource
059: * @see net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
060: * @see net.sf.jasperreports.engine.data.JRBeanArrayDataSource
061: */
062: public static JRDataSource convertReportData(Object value)
063: throws IllegalArgumentException {
064: if (value instanceof JRDataSource) {
065: return (JRDataSource) value;
066: } else if (value instanceof Collection) {
067: return new JRBeanCollectionDataSource((Collection) value);
068: } else if (value instanceof Object[]) {
069: return new JRBeanArrayDataSource((Object[]) value);
070: } else if (value instanceof JRDataSourceProvider) {
071: return null;
072: } else {
073: throw new IllegalArgumentException("Value [" + value
074: + "] cannot be converted to a JRDataSource");
075: }
076: }
077:
078: /**
079: * Render the supplied <code>JasperPrint</code> instance using the
080: * supplied <code>JRAbstractExporter</code> instance and write the results
081: * to the supplied <code>Writer</code>.
082: * <p>Make sure that the <code>JRAbstractExporter</code> implementation
083: * you supply is capable of writing to a <code>Writer</code>.
084: * @param exporter the <code>JRAbstractExporter</code> to use to render the report
085: * @param print the <code>JasperPrint</code> instance to render
086: * @param writer the <code>Writer</code> to write the result to
087: * @throws JRException if rendering failed
088: */
089: public static void render(JRExporter exporter, JasperPrint print,
090: Writer writer) throws JRException {
091: exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
092: exporter
093: .setParameter(JRExporterParameter.OUTPUT_WRITER, writer);
094: exporter.exportReport();
095: }
096:
097: /**
098: * Render the supplied <code>JasperPrint</code> instance using the
099: * supplied <code>JRAbstractExporter</code> instance and write the results
100: * to the supplied <code>OutputStream</code>.
101: * <p>Make sure that the <code>JRAbstractExporter</code> implementation you
102: * supply is capable of writing to a <code>OutputStream</code>.
103: * @param exporter the <code>JRAbstractExporter</code> to use to render the report
104: * @param print the <code>JasperPrint</code> instance to render
105: * @param outputStream the <code>OutputStream</code> to write the result to
106: * @throws JRException if rendering failed
107: */
108: public static void render(JRExporter exporter, JasperPrint print,
109: OutputStream outputStream) throws JRException {
110: exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
111: exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
112: outputStream);
113: exporter.exportReport();
114: }
115:
116: /**
117: * Render a report in CSV format using the supplied report data.
118: * Writes the results to the supplied <code>Writer</code>.
119: * @param report the <code>JasperReport</code> instance to render
120: * @param parameters the parameters to use for rendering
121: * @param writer the <code>Writer</code> to write the rendered report to
122: * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
123: * or object array (converted accordingly), representing the report data to read
124: * fields from
125: * @throws JRException if rendering failed
126: * @see #convertReportData
127: */
128: public static void renderAsCsv(JasperReport report, Map parameters,
129: Object reportData, Writer writer) throws JRException {
130: JasperPrint print = JasperFillManager.fillReport(report,
131: parameters, convertReportData(reportData));
132: render(new JRCsvExporter(), print, writer);
133: }
134:
135: /**
136: * Render a report in CSV format using the supplied report data.
137: * Writes the results to the supplied <code>Writer</code>.
138: * @param report the <code>JasperReport</code> instance to render
139: * @param parameters the parameters to use for rendering
140: * @param writer the <code>Writer</code> to write the rendered report to
141: * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
142: * or object array (converted accordingly), representing the report data to read
143: * fields from
144: * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
145: * @throws JRException if rendering failed
146: * @see #convertReportData
147: */
148: public static void renderAsCsv(JasperReport report, Map parameters,
149: Object reportData, Writer writer, Map exporterParameters)
150: throws JRException {
151: JasperPrint print = JasperFillManager.fillReport(report,
152: parameters, convertReportData(reportData));
153: JRCsvExporter exporter = new JRCsvExporter();
154: exporter.setParameters(exporterParameters);
155: render(exporter, print, writer);
156: }
157:
158: /**
159: * Render a report in HTML format using the supplied report data.
160: * Writes the results to the supplied <code>Writer</code>.
161: * @param report the <code>JasperReport</code> instance to render
162: * @param parameters the parameters to use for rendering
163: * @param writer the <code>Writer</code> to write the rendered report to
164: * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
165: * or object array (converted accordingly), representing the report data to read
166: * fields from
167: * @throws JRException if rendering failed
168: * @see #convertReportData
169: */
170: public static void renderAsHtml(JasperReport report,
171: Map parameters, Object reportData, Writer writer)
172: throws JRException {
173: JasperPrint print = JasperFillManager.fillReport(report,
174: parameters, convertReportData(reportData));
175: render(new JRHtmlExporter(), print, writer);
176: }
177:
178: /**
179: * Render a report in HTML format using the supplied report data.
180: * Writes the results to the supplied <code>Writer</code>.
181: * @param report the <code>JasperReport</code> instance to render
182: * @param parameters the parameters to use for rendering
183: * @param writer the <code>Writer</code> to write the rendered report to
184: * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
185: * or object array (converted accordingly), representing the report data to read
186: * fields from
187: * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
188: * @throws JRException if rendering failed
189: * @see #convertReportData
190: */
191: public static void renderAsHtml(JasperReport report,
192: Map parameters, Object reportData, Writer writer,
193: Map exporterParameters) throws JRException {
194: JasperPrint print = JasperFillManager.fillReport(report,
195: parameters, convertReportData(reportData));
196: JRHtmlExporter exporter = new JRHtmlExporter();
197: exporter.setParameters(exporterParameters);
198: render(exporter, print, writer);
199: }
200:
201: /**
202: * Render a report in PDF format using the supplied report data.
203: * Writes the results to the supplied <code>OutputStream</code>.
204: * @param report the <code>JasperReport</code> instance to render
205: * @param parameters the parameters to use for rendering
206: * @param stream the <code>OutputStream</code> to write the rendered report to
207: * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
208: * or object array (converted accordingly), representing the report data to read
209: * fields from
210: * @throws JRException if rendering failed
211: * @see #convertReportData
212: */
213: public static void renderAsPdf(JasperReport report, Map parameters,
214: Object reportData, OutputStream stream) throws JRException {
215:
216: JasperPrint print = JasperFillManager.fillReport(report,
217: parameters, convertReportData(reportData));
218: render(new JRPdfExporter(), print, stream);
219: }
220:
221: /**
222: * Render a report in PDF format using the supplied report data.
223: * Writes the results to the supplied <code>OutputStream</code>.
224: * @param report the <code>JasperReport</code> instance to render
225: * @param parameters the parameters to use for rendering
226: * @param stream the <code>OutputStream</code> to write the rendered report to
227: * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
228: * or object array (converted accordingly), representing the report data to read
229: * fields from
230: * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
231: * @throws JRException if rendering failed
232: * @see #convertReportData
233: */
234: public static void renderAsPdf(JasperReport report, Map parameters,
235: Object reportData, OutputStream stream,
236: Map exporterParameters) throws JRException {
237: JasperPrint print = JasperFillManager.fillReport(report,
238: parameters, convertReportData(reportData));
239: JRPdfExporter exporter = new JRPdfExporter();
240: exporter.setParameters(exporterParameters);
241: render(exporter, print, stream);
242: }
243:
244: /**
245: * Render a report in XLS format using the supplied report data.
246: * Writes the results to the supplied <code>OutputStream</code>.
247: * @param report the <code>JasperReport</code> instance to render
248: * @param parameters the parameters to use for rendering
249: * @param stream the <code>OutputStream</code> to write the rendered report to
250: * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
251: * or object array (converted accordingly), representing the report data to read
252: * fields from
253: * @throws JRException if rendering failed
254: * @see #convertReportData
255: */
256: public static void renderAsXls(JasperReport report, Map parameters,
257: Object reportData, OutputStream stream) throws JRException {
258:
259: JasperPrint print = JasperFillManager.fillReport(report,
260: parameters, convertReportData(reportData));
261: render(new JRXlsExporter(), print, stream);
262: }
263:
264: /**
265: * Render a report in XLS format using the supplied report data.
266: * Writes the results to the supplied <code>OutputStream</code>.
267: * @param report the <code>JasperReport</code> instance to render
268: * @param parameters the parameters to use for rendering
269: * @param stream the <code>OutputStream</code> to write the rendered report to
270: * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
271: * or object array (converted accordingly), representing the report data to read
272: * fields from
273: * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
274: * @throws JRException if rendering failed
275: * @see #convertReportData
276: */
277: public static void renderAsXls(JasperReport report, Map parameters,
278: Object reportData, OutputStream stream,
279: Map exporterParameters) throws JRException {
280:
281: JasperPrint print = JasperFillManager.fillReport(report,
282: parameters, convertReportData(reportData));
283: JRXlsExporter exporter = new JRXlsExporter();
284: exporter.setParameters(exporterParameters);
285: render(exporter, print, stream);
286: }
287: }
|