001/*
002 * Copyright 2002-2014 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 *      https://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
017package org.springframework.ui.jasperreports;
018
019import java.io.OutputStream;
020import java.io.Writer;
021import java.util.Collection;
022import java.util.Map;
023
024import net.sf.jasperreports.engine.JRDataSource;
025import net.sf.jasperreports.engine.JRException;
026import net.sf.jasperreports.engine.JasperFillManager;
027import net.sf.jasperreports.engine.JasperPrint;
028import net.sf.jasperreports.engine.JasperReport;
029import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
030import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
031import net.sf.jasperreports.engine.export.JRCsvExporter;
032import net.sf.jasperreports.engine.export.JRPdfExporter;
033import net.sf.jasperreports.engine.export.JRXlsExporter;
034
035/**
036 * Utility methods for working with JasperReports. Provides a set of convenience
037 * methods for generating reports in a CSV, HTML, PDF and XLS formats.
038 *
039 * <p><b>This class is compatible with classic JasperReports releases back until 2.x.</b>
040 * As a consequence, it keeps using the {@link net.sf.jasperreports.engine.JRExporter}
041 * API which has been deprecated in early 2014.
042 *
043 * @author Rob Harrop
044 * @author Juergen Hoeller
045 * @since 1.1.3
046 */
047@SuppressWarnings({"deprecation", "rawtypes"})
048public abstract class JasperReportsUtils {
049
050        /**
051         * Convert the given report data value to a {@code JRDataSource}.
052         * <p>In the default implementation, a {@code JRDataSource},
053         * {@code java.util.Collection} or object array is detected.
054         * The latter are converted to {@code JRBeanCollectionDataSource}
055         * or {@code JRBeanArrayDataSource}, respectively.
056         * @param value the report data value to convert
057         * @return the JRDataSource (never {@code null})
058         * @throws IllegalArgumentException if the value could not be converted
059         * @see net.sf.jasperreports.engine.JRDataSource
060         * @see net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
061         * @see net.sf.jasperreports.engine.data.JRBeanArrayDataSource
062         */
063        public static JRDataSource convertReportData(Object value) throws IllegalArgumentException {
064                if (value instanceof JRDataSource) {
065                        return (JRDataSource) value;
066                }
067                else if (value instanceof Collection) {
068                        return new JRBeanCollectionDataSource((Collection<?>) value);
069                }
070                else if (value instanceof Object[]) {
071                        return new JRBeanArrayDataSource((Object[]) value);
072                }
073                else {
074                        throw new IllegalArgumentException("Value [" + value + "] cannot be converted to a JRDataSource");
075                }
076        }
077
078        /**
079         * Render the supplied {@code JasperPrint} instance using the
080         * supplied {@code JRAbstractExporter} instance and write the results
081         * to the supplied {@code Writer}.
082         * <p>Make sure that the {@code JRAbstractExporter} implementation
083         * you supply is capable of writing to a {@code Writer}.
084         * @param exporter the {@code JRAbstractExporter} to use to render the report
085         * @param print the {@code JasperPrint} instance to render
086         * @param writer the {@code Writer} to write the result to
087         * @throws JRException if rendering failed
088         */
089        public static void render(net.sf.jasperreports.engine.JRExporter exporter, JasperPrint print, Writer writer)
090                        throws JRException {
091
092                exporter.setParameter(net.sf.jasperreports.engine.JRExporterParameter.JASPER_PRINT, print);
093                exporter.setParameter(net.sf.jasperreports.engine.JRExporterParameter.OUTPUT_WRITER, writer);
094                exporter.exportReport();
095        }
096
097        /**
098         * Render the supplied {@code JasperPrint} instance using the
099         * supplied {@code JRAbstractExporter} instance and write the results
100         * to the supplied {@code OutputStream}.
101         * <p>Make sure that the {@code JRAbstractExporter} implementation you
102         * supply is capable of writing to a {@code OutputStream}.
103         * @param exporter the {@code JRAbstractExporter} to use to render the report
104         * @param print the {@code JasperPrint} instance to render
105         * @param outputStream the {@code OutputStream} to write the result to
106         * @throws JRException if rendering failed
107         */
108        public static void render(net.sf.jasperreports.engine.JRExporter exporter, JasperPrint print,
109                        OutputStream outputStream) throws JRException {
110
111                exporter.setParameter(net.sf.jasperreports.engine.JRExporterParameter.JASPER_PRINT, print);
112                exporter.setParameter(net.sf.jasperreports.engine.JRExporterParameter.OUTPUT_STREAM, 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}.
119         * @param report the {@code JasperReport} instance to render
120         * @param parameters the parameters to use for rendering
121         * @param writer the {@code Writer} to write the rendered report to
122         * @param reportData a {@code JRDataSource}, {@code java.util.Collection} or object array
123         * (converted accordingly), representing the report data to read fields from
124         * @throws JRException if rendering failed
125         * @see #convertReportData
126         */
127        public static void renderAsCsv(JasperReport report, Map<String, Object> parameters, Object reportData,
128                        Writer writer) throws JRException {
129
130                JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
131                render(new JRCsvExporter(), print, writer);
132        }
133
134        /**
135         * Render a report in CSV format using the supplied report data.
136         * Writes the results to the supplied {@code Writer}.
137         * @param report the {@code JasperReport} instance to render
138         * @param parameters the parameters to use for rendering
139         * @param writer the {@code Writer} to write the rendered report to
140         * @param reportData a {@code JRDataSource}, {@code java.util.Collection} or object array
141         * (converted accordingly), representing the report data to read fields from
142         * @param exporterParameters a {@link Map} of {@code JRExporterParameter exporter parameters}
143         * @throws JRException if rendering failed
144         * @see #convertReportData
145         */
146        public static void renderAsCsv(JasperReport report, Map<String, Object> parameters, Object reportData,
147                        Writer writer, Map<net.sf.jasperreports.engine.JRExporterParameter, Object> exporterParameters)
148                        throws JRException {
149
150                JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
151                JRCsvExporter exporter = new JRCsvExporter();
152                exporter.setParameters(exporterParameters);
153                render(exporter, print, writer);
154        }
155
156        /**
157         * Render a report in HTML format using the supplied report data.
158         * Writes the results to the supplied {@code Writer}.
159         * @param report the {@code JasperReport} instance to render
160         * @param parameters the parameters to use for rendering
161         * @param writer the {@code Writer} to write the rendered report to
162         * @param reportData a {@code JRDataSource}, {@code java.util.Collection} or object array
163         * (converted accordingly), representing the report data to read fields from
164         * @throws JRException if rendering failed
165         * @see #convertReportData
166         */
167        public static void renderAsHtml(JasperReport report, Map<String, Object> parameters, Object reportData,
168                        Writer writer) throws JRException {
169
170                JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
171                render(new net.sf.jasperreports.engine.export.JRHtmlExporter(), print, writer);
172        }
173
174        /**
175         * Render a report in HTML format using the supplied report data.
176         * Writes the results to the supplied {@code Writer}.
177         * @param report the {@code JasperReport} instance to render
178         * @param parameters the parameters to use for rendering
179         * @param writer the {@code Writer} to write the rendered report to
180         * @param reportData a {@code JRDataSource}, {@code java.util.Collection} or object array
181         * (converted accordingly), representing the report data to read fields from
182         * @param exporterParameters a {@link Map} of {@code JRExporterParameter exporter parameters}
183         * @throws JRException if rendering failed
184         * @see #convertReportData
185         */
186        public static void renderAsHtml(JasperReport report, Map<String, Object> parameters, Object reportData,
187                        Writer writer, Map<net.sf.jasperreports.engine.JRExporterParameter, Object> exporterParameters)
188                        throws JRException {
189
190                JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
191                net.sf.jasperreports.engine.export.JRHtmlExporter exporter = new net.sf.jasperreports.engine.export.JRHtmlExporter();
192                exporter.setParameters(exporterParameters);
193                render(exporter, print, writer);
194        }
195
196        /**
197         * Render a report in PDF format using the supplied report data.
198         * Writes the results to the supplied {@code OutputStream}.
199         * @param report the {@code JasperReport} instance to render
200         * @param parameters the parameters to use for rendering
201         * @param stream the {@code OutputStream} to write the rendered report to
202         * @param reportData a {@code JRDataSource}, {@code java.util.Collection} or object array
203         * (converted accordingly), representing the report data to read fields from
204         * @throws JRException if rendering failed
205         * @see #convertReportData
206         */
207        public static void renderAsPdf(JasperReport report, Map<String, Object> parameters, Object reportData,
208                        OutputStream stream) throws JRException {
209
210                JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
211                render(new JRPdfExporter(), print, stream);
212        }
213
214        /**
215         * Render a report in PDF format using the supplied report data.
216         * Writes the results to the supplied {@code OutputStream}.
217         * @param report the {@code JasperReport} instance to render
218         * @param parameters the parameters to use for rendering
219         * @param stream the {@code OutputStream} to write the rendered report to
220         * @param reportData a {@code JRDataSource}, {@code java.util.Collection} or object array
221         * (converted accordingly), representing the report data to read fields from
222         * @param exporterParameters a {@link Map} of {@code JRExporterParameter exporter parameters}
223         * @throws JRException if rendering failed
224         * @see #convertReportData
225         */
226        public static void renderAsPdf(JasperReport report, Map<String, Object> parameters, Object reportData,
227                        OutputStream stream, Map<net.sf.jasperreports.engine.JRExporterParameter, Object> exporterParameters)
228                        throws JRException {
229
230                JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
231                JRPdfExporter exporter = new JRPdfExporter();
232                exporter.setParameters(exporterParameters);
233                render(exporter, print, stream);
234        }
235
236        /**
237         * Render a report in XLS format using the supplied report data.
238         * Writes the results to the supplied {@code OutputStream}.
239         * @param report the {@code JasperReport} instance to render
240         * @param parameters the parameters to use for rendering
241         * @param stream the {@code OutputStream} to write the rendered report to
242         * @param reportData a {@code JRDataSource}, {@code java.util.Collection} or object array
243         * (converted accordingly), representing the report data to read fields from
244         * @throws JRException if rendering failed
245         * @see #convertReportData
246         */
247        public static void renderAsXls(JasperReport report, Map<String, Object> parameters, Object reportData,
248                        OutputStream stream) throws JRException {
249
250                JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
251                render(new JRXlsExporter(), print, stream);
252        }
253
254        /**
255         * Render a report in XLS format using the supplied report data.
256         * Writes the results to the supplied {@code OutputStream}.
257         * @param report the {@code JasperReport} instance to render
258         * @param parameters the parameters to use for rendering
259         * @param stream the {@code OutputStream} to write the rendered report to
260         * @param reportData a {@code JRDataSource}, {@code java.util.Collection} or object array
261         * (converted accordingly), representing the report data to read fields from
262         * @param exporterParameters a {@link Map} of {@code JRExporterParameter exporter parameters}
263         * @throws JRException if rendering failed
264         * @see #convertReportData
265         */
266        public static void renderAsXls(JasperReport report, Map<String, Object> parameters, Object reportData,
267                        OutputStream stream, Map<net.sf.jasperreports.engine.JRExporterParameter, Object> exporterParameters)
268                        throws JRException {
269
270                JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
271                JRXlsExporter exporter = new JRXlsExporter();
272                exporter.setParameters(exporterParameters);
273                render(exporter, print, stream);
274        }
275
276}