org.springframework.ui.jasperreports.JasperReportsUtils Java Examples

The following examples show how to use org.springframework.ui.jasperreports.JasperReportsUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractJasperReportsSingleFormatView.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * We need to write text to the response Writer.
 * @param exporter the JasperReports exporter to use
 * @param populatedReport the populated {@code JasperPrint} to render
 * @param response the HTTP response the report should be rendered to
 * @throws Exception if rendering failed
 */
protected void renderReportUsingWriter(net.sf.jasperreports.engine.JRExporter exporter,
		JasperPrint populatedReport, HttpServletResponse response) throws Exception {

	// Copy the encoding configured for the report into the response.
	String contentType = getContentType();
	String encoding = (String) exporter.getParameter(net.sf.jasperreports.engine.JRExporterParameter.CHARACTER_ENCODING);
	if (encoding != null) {
		// Only apply encoding if content type is specified but does not contain charset clause already.
		if (contentType != null && !contentType.toLowerCase().contains(WebUtils.CONTENT_TYPE_CHARSET_PREFIX)) {
			contentType = contentType + WebUtils.CONTENT_TYPE_CHARSET_PREFIX + encoding;
		}
	}
	response.setContentType(contentType);

	// Render report into HttpServletResponse's Writer.
	JasperReportsUtils.render(exporter, populatedReport, response.getWriter());
}
 
Example #2
Source File: AbstractJasperReportsSingleFormatView.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * We need to write text to the response Writer.
 * @param exporter the JasperReports exporter to use
 * @param populatedReport the populated {@code JasperPrint} to render
 * @param response the HTTP response the report should be rendered to
 * @throws Exception if rendering failed
 */
protected void renderReportUsingWriter(net.sf.jasperreports.engine.JRExporter exporter,
		JasperPrint populatedReport, HttpServletResponse response) throws Exception {

	// Copy the encoding configured for the report into the response.
	String contentType = getContentType();
	String encoding = (String) exporter.getParameter(net.sf.jasperreports.engine.JRExporterParameter.CHARACTER_ENCODING);
	if (encoding != null) {
		// Only apply encoding if content type is specified but does not contain charset clause already.
		if (contentType != null && !contentType.toLowerCase().contains(WebUtils.CONTENT_TYPE_CHARSET_PREFIX)) {
			contentType = contentType + WebUtils.CONTENT_TYPE_CHARSET_PREFIX + encoding;
		}
	}
	response.setContentType(contentType);

	// Render report into HttpServletResponse's Writer.
	JasperReportsUtils.render(exporter, populatedReport, response.getWriter());
}
 
Example #3
Source File: ReportGenerationServiceImpl.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * @see org.kuali.kfs.sys.batch.service.ReportGenerationService#generateReportToOutputStream(java.util.Map, java.lang.Object,
 *      java.lang.String, java.io.ByteArrayOutputStream)
 */
public void generateReportToOutputStream(Map<String, Object> reportData, Object dataSource, String template, ByteArrayOutputStream baos) {
    ClassPathResource resource = getReportTemplateClassPathResource(template.concat(ReportGeneration.DESIGN_FILE_EXTENSION));
    if (resource == null || !resource.exists()) {
        throw new IllegalArgumentException("Cannot find the template file: " + template.concat(ReportGeneration.DESIGN_FILE_EXTENSION));
    }

    try {
        if (reportData != null && reportData.containsKey(ReportGeneration.PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME)) {
            Map<String, String> subReports = (Map<String, String>) reportData.get(ReportGeneration.PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME);
            String subReportDirectory = (String) reportData.get(ReportGeneration.PARAMETER_NAME_SUBREPORT_DIR);
            compileSubReports(subReports, subReportDirectory);
        }

        String designTemplateName = template.concat(ReportGeneration.DESIGN_FILE_EXTENSION);
        InputStream jasperReport = new FileInputStream(compileReportTemplate(designTemplateName));

        JRDataSource jrDataSource = JasperReportsUtils.convertReportData(dataSource);

         JasperRunManager.runReportToPdfStream(jasperReport, baos, decorateReportData(reportData), jrDataSource);
    }
    catch (Exception e) {
        LOG.error(e);
        throw new RuntimeException("Fail to generate report.", e);
    }
}
 
Example #4
Source File: AbstractJasperReportsSingleFormatView.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * We need to write binary output to the response OutputStream.
 * @param exporter the JasperReports exporter to use
 * @param populatedReport the populated {@code JasperPrint} to render
 * @param response the HTTP response the report should be rendered to
 * @throws Exception if rendering failed
 */
protected void renderReportUsingOutputStream(net.sf.jasperreports.engine.JRExporter exporter,
		JasperPrint populatedReport, HttpServletResponse response) throws Exception {

	// IE workaround: write into byte array first.
	ByteArrayOutputStream baos = createTemporaryOutputStream();
	JasperReportsUtils.render(exporter, populatedReport, baos);
	writeToResponse(response, baos);
}
 
Example #5
Source File: AbstractJasperReportsSingleFormatView.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * We need to write binary output to the response OutputStream.
 * @param exporter the JasperReports exporter to use
 * @param populatedReport the populated {@code JasperPrint} to render
 * @param response the HTTP response the report should be rendered to
 * @throws Exception if rendering failed
 */
protected void renderReportUsingOutputStream(net.sf.jasperreports.engine.JRExporter exporter,
		JasperPrint populatedReport, HttpServletResponse response) throws Exception {

	// IE workaround: write into byte array first.
	ByteArrayOutputStream baos = createTemporaryOutputStream();
	JasperReportsUtils.render(exporter, populatedReport, baos);
	writeToResponse(response, baos);
}
 
Example #6
Source File: ReportGenerationServiceImpl.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * The dataSource can be an instance of JRDataSource, java.util.Collection or object array.
 * 
 * @see org.kuali.kfs.sys.batch.service.ReportGenerationService#generateReportToPdfFile(java.util.Map, java.lang.Object, java.lang.String,
 *      java.lang.String)
 */
public void generateReportToPdfFile(Map<String, Object> reportData, Object dataSource, String template, String reportFileName) {
    ClassPathResource resource = getReportTemplateClassPathResource(template.concat(ReportGeneration.DESIGN_FILE_EXTENSION));
    if (resource == null || !resource.exists()) {
        throw new IllegalArgumentException("Cannot find the template file: " + template.concat(ReportGeneration.DESIGN_FILE_EXTENSION));
    }

    try {
        if (reportData != null && reportData.containsKey(ReportGeneration.PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME)) {
            Map<String, String> subReports = (Map<String, String>) reportData.get(ReportGeneration.PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME);
            String subReportDirectory = (String) reportData.get(ReportGeneration.PARAMETER_NAME_SUBREPORT_DIR);
            compileSubReports(subReports, subReportDirectory);
        }

        String designTemplateName = template.concat(ReportGeneration.DESIGN_FILE_EXTENSION);
        InputStream jasperReport = new FileInputStream(compileReportTemplate(designTemplateName));

        JRDataSource jrDataSource = JasperReportsUtils.convertReportData(dataSource);

        reportFileName = reportFileName + ReportGeneration.PDF_FILE_EXTENSION;
        File reportDirectory = new File(StringUtils.substringBeforeLast(reportFileName, File.separator));
        if(!reportDirectory.exists()) {
            reportDirectory.mkdir();
        }

        JasperRunManager.runReportToPdfStream(jasperReport, new FileOutputStream(reportFileName), decorateReportData(reportData), jrDataSource);
    }
    catch (Exception e) {
        LOG.error(e);
        throw new RuntimeException("Fail to generate report.", e);
    }
}
 
Example #7
Source File: AbstractJasperReportsView.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Convert the given report data value to a {@code JRDataSource}.
 * <p>The default implementation delegates to {@code JasperReportUtils} unless
 * the report data value is an instance of {@code JRDataSourceProvider}.
 * A {@code JRDataSource}, {@code JRDataSourceProvider},
 * {@code java.util.Collection} or object array is detected.
 * {@code JRDataSource}s are returned as is, whilst {@code JRDataSourceProvider}s
 * are used to create an instance of {@code JRDataSource} which is then returned.
 * The latter two are converted to {@code JRBeanCollectionDataSource} or
 * {@code JRBeanArrayDataSource}, respectively.
 * @param value the report data value to convert
 * @return the JRDataSource
 * @throws IllegalArgumentException if the value could not be converted
 * @see org.springframework.ui.jasperreports.JasperReportsUtils#convertReportData
 * @see net.sf.jasperreports.engine.JRDataSource
 * @see net.sf.jasperreports.engine.JRDataSourceProvider
 * @see net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
 * @see net.sf.jasperreports.engine.data.JRBeanArrayDataSource
 */
protected JRDataSource convertReportData(Object value) throws IllegalArgumentException {
	if (value instanceof JRDataSourceProvider) {
		return createReport((JRDataSourceProvider) value);
	}
	else {
		return JasperReportsUtils.convertReportData(value);
	}
}
 
Example #8
Source File: AbstractJasperReportsView.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Convert the given report data value to a {@code JRDataSource}.
 * <p>The default implementation delegates to {@code JasperReportUtils} unless
 * the report data value is an instance of {@code JRDataSourceProvider}.
 * A {@code JRDataSource}, {@code JRDataSourceProvider},
 * {@code java.util.Collection} or object array is detected.
 * {@code JRDataSource}s are returned as is, whilst {@code JRDataSourceProvider}s
 * are used to create an instance of {@code JRDataSource} which is then returned.
 * The latter two are converted to {@code JRBeanCollectionDataSource} or
 * {@code JRBeanArrayDataSource}, respectively.
 * @param value the report data value to convert
 * @return the JRDataSource
 * @throws IllegalArgumentException if the value could not be converted
 * @see org.springframework.ui.jasperreports.JasperReportsUtils#convertReportData
 * @see net.sf.jasperreports.engine.JRDataSource
 * @see net.sf.jasperreports.engine.JRDataSourceProvider
 * @see net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
 * @see net.sf.jasperreports.engine.data.JRBeanArrayDataSource
 */
protected JRDataSource convertReportData(Object value) throws IllegalArgumentException {
	if (value instanceof JRDataSourceProvider) {
		return createReport((JRDataSourceProvider) value);
	}
	else {
		return JasperReportsUtils.convertReportData(value);
	}
}