Package org.internna.iwebmvc.core.reports.jasper

Source Code of org.internna.iwebmvc.core.reports.jasper.JasperReportEngine

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.core.reports.jasper;

import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Date;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JExcelApiExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import org.internna.iwebmvc.core.IWebMvcException;
import org.internna.iwebmvc.core.reports.ReportEngine;
import org.internna.iwebmvc.model.GridDTO;
import org.springframework.stereotype.Service;

/**
* Report engine based on JasperReports
*
* @author Jose Noheda
* @since 1.0
*/
@Service
public class JasperReportEngine implements ReportEngine {

    protected InputStream exportToExcel(JasperPrint jp) throws Exception {
        JRXlsExporter excelExporter = new JRXlsExporter();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        excelExporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jp);
        excelExporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, output);
        excelExporter.exportReport();
        return new ByteArrayInputStream(output.toByteArray());
    }

    protected InputStream exportToPDF(JasperPrint jp) throws Exception {
        return new ByteArrayInputStream(JasperExportManager.exportReportToPdf(jp));
    }

    @Override
    public InputStream generateGridReport(String docType, GridDTO data) {
        try {
            FastReportBuilder reportBuilder = new FastReportBuilder();
            reportBuilder.setTitle("IWebMvc - Sample Grid Report").setSubtitle("Generated at " + new Date()).setUseFullPageWidth(true);
            for (String column : data.getColumns()) reportBuilder.addColumn(column, column, String.class.getName(), 1);
            JRDataSource ds = new JRBeanCollectionDataSource(data.getData(), false);
            JasperPrint jp = DynamicJasperHelper.generateJasperPrint(reportBuilder.build(), new ClassicLayoutManager(), ds);
            return "EXCEL".equals(docType) ? exportToExcel(jp) : exportToPDF(jp);
        } catch (Exception e) {
            throw new IWebMvcException("Error building grid report", e);
        }
    }

}
TOP

Related Classes of org.internna.iwebmvc.core.reports.jasper.JasperReportEngine

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.