net.sf.jasperreports.engine.data.JRBeanCollectionDataSource Java Examples

The following examples show how to use net.sf.jasperreports.engine.data.JRBeanCollectionDataSource. 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: JasperReportsUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the given report data value to a {@code JRDataSource}.
 * <p>In the default implementation, a {@code JRDataSource},
 * {@code java.util.Collection} or object array is detected.
 * The latter are converted to {@code JRBeanCollectionDataSource}
 * or {@code JRBeanArrayDataSource}, respectively.
 * @param value the report data value to convert
 * @return the JRDataSource (never {@code null})
 * @throws IllegalArgumentException if the value could not be converted
 * @see net.sf.jasperreports.engine.JRDataSource
 * @see net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
 * @see net.sf.jasperreports.engine.data.JRBeanArrayDataSource
 */
public static JRDataSource convertReportData(Object value) throws IllegalArgumentException {
	if (value instanceof JRDataSource) {
		return (JRDataSource) value;
	}
	else if (value instanceof Collection) {
		return new JRBeanCollectionDataSource((Collection<?>) value);
	}
	else if (value instanceof Object[]) {
		return new JRBeanArrayDataSource((Object[]) value);
	}
	else {
		throw new IllegalArgumentException("Value [" + value + "] cannot be converted to a JRDataSource");
	}
}
 
Example #2
Source File: ReportManager.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
private static JasperPrint generate (Collection<Report> reports, String jasperFile, HashMap<String, String> params) {
	JasperPrint print = null;
	JRBeanCollectionDataSource source = new JRBeanCollectionDataSource (reports) ;
	try {
		JasperReport report = (JasperReport) JRLoader.loadObjectFromLocation(jasperFile);
		print = JasperFillManager.fillReport(report, params, source);
	} catch (JRException e) {
		logger.error(e.getMessage(), e);
	}
	return print;
}
 
Example #3
Source File: ContractsGrantsAwardBalancesReportServiceImpl.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 * @see org.kuali.kfs.module.cg.report.service.ContractsGrantsAwardBalancesReportService#generateReport(org.kuali.kfs.module.cg.report.ContractsGrantsReportDataHolder, org.kuali.kfs.sys.report.ReportInfo, java.io.ByteArrayOutputStream)
 */
@Override
public String generateReport(ContractsGrantsReportDataHolder reportDataHolder, ReportInfo reportInfo, ByteArrayOutputStream baos) {
    Date runDate = new Date();

    String reportFileName = reportInfo.getReportFileName();
    String reportDirectory = reportInfo.getReportsDirectory();
    String reportTemplateClassPath = reportInfo.getReportTemplateClassPath();
    String reportTemplateName = reportInfo.getReportTemplateName();
    ResourceBundle resourceBundle = reportInfo.getResourceBundle();

    String subReportTemplateClassPath = reportInfo.getSubReportTemplateClassPath();
    Map<String, String> subReports = reportInfo.getSubReports();

    Map<String, Object> reportData = reportDataHolder.getReportData();
    // check title and set
    if (ObjectUtils.isNull(reportData.get(KFSConstants.REPORT_TITLE))) {
        reportData.put(KFSConstants.REPORT_TITLE, reportInfo.getReportTitle());
    }
    reportData.put(JRParameter.REPORT_RESOURCE_BUNDLE, resourceBundle);
    reportData.put(ReportGeneration.PARAMETER_NAME_SUBREPORT_DIR, subReportTemplateClassPath);
    reportData.put(ReportGeneration.PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME, subReports);

    String template = reportTemplateClassPath + reportTemplateName;
    String fullReportFileName = reportGenerationService.buildFullFileName(runDate, reportDirectory, reportFileName, "");

    List<String> data = Arrays.asList(KFSConstants.EMPTY_STRING);
    JRDataSource dataSource = new JRBeanCollectionDataSource(data);

    reportGenerationService.generateReportToOutputStream(reportData, dataSource, template, baos);

    return reportFileName;
}
 
Example #4
Source File: ReportGenerationServiceImpl.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @see org.kuali.kfs.sys.batch.service.ReportGenerationService#generateReportToPdfFile(java.util.Map, java.lang.String, java.lang.String)
 */
public void generateReportToPdfFile(Map<String, Object> reportData, String template, String reportFileName) {
    List<String> data = Arrays.asList(KFSConstants.EMPTY_STRING);
    JRDataSource dataSource = new JRBeanCollectionDataSource(data);

    generateReportToPdfFile(reportData, dataSource, template, reportFileName);
}
 
Example #5
Source File: ContractsGrantsReportHelperServiceImpl.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @see org.kuali.kfs.module.ar.report.service.ContractsGrantsReportHelperService#generateReport(org.kuali.kfs.module.ar.report.ContractsGrantsReportDataHolder, org.kuali.kfs.sys.report.ReportInfo, java.io.ByteArrayOutputStream)
 */
@Override
public String generateReport(ContractsGrantsReportDataHolder reportDataHolder, ReportInfo reportInfo, ByteArrayOutputStream baos) {
    Date runDate = new Date();

    String reportFileName = reportInfo.getReportFileName();
    String reportDirectory = reportInfo.getReportsDirectory();
    String reportTemplateClassPath = reportInfo.getReportTemplateClassPath();
    String reportTemplateName = reportInfo.getReportTemplateName();
    ResourceBundle resourceBundle = reportInfo.getResourceBundle();

    String subReportTemplateClassPath = reportInfo.getSubReportTemplateClassPath();
    Map<String, String> subReports = reportInfo.getSubReports();

    Map<String, Object> reportData = reportDataHolder.getReportData();
    // check title and set
    if (ObjectUtils.isNull(reportData.get(KFSConstants.REPORT_TITLE))) {
        reportData.put(KFSConstants.REPORT_TITLE, reportInfo.getReportTitle());
    }
    reportData.put(JRParameter.REPORT_RESOURCE_BUNDLE, resourceBundle);
    reportData.put(ReportGeneration.PARAMETER_NAME_SUBREPORT_DIR, subReportTemplateClassPath);
    reportData.put(ReportGeneration.PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME, subReports);

    String template = reportTemplateClassPath + reportTemplateName;
    String fullReportFileName = reportGenerationService.buildFullFileName(runDate, reportDirectory, reportFileName, "");

    List<String> data = Arrays.asList(KFSConstants.EMPTY_STRING);
    JRDataSource dataSource = new JRBeanCollectionDataSource(data);

    reportGenerationService.generateReportToOutputStream(reportData, dataSource, template, baos);

    return reportFileName;
}
 
Example #6
Source File: FunctionsApp.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 */
public JRDataSource createDataSource(
	JasperReportsContext jasperReportsContext, 
	Locale locale 
	) 
{
	FunctionsInfo functionsInfo = FunctionsInfo.getInstance(jasperReportsContext, locale);
	Collection<FunctionCategoryBean> categories = functionsInfo.getCategories(); 
	return new JRBeanCollectionDataSource(categories);
}
 
Example #7
Source File: DataSourceApp.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 */
public void fill4() throws JRException
{
	long start = System.currentTimeMillis();
	//Preparing parameters
	Map<String, Object> parameters = new HashMap<String, Object>();
	parameters.put("ReportTitle", "Address Report");
	parameters.put("DataFile", "CustomBeanFactory.java - Bean Collection");

	JasperFillManager.fillReportToFile("build/reports/DataSourceReport.jasper", parameters, new JRBeanCollectionDataSource(CustomBeanFactory.getBeanCollection()));
	System.err.println("Filling time : " + (System.currentTimeMillis() - start));
}
 
Example #8
Source File: AbstractJasperReportsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected Map<String, Object> getModel() {
	Map<String, Object> model = new HashMap<String, Object>();
	model.put("ReportTitle", "Dear Lord!");
	model.put("dataSource", new JRBeanCollectionDataSource(getData()));
	extendModel(model);
	return model;
}
 
Example #9
Source File: PdfView.java    From spring-boot-doma2-sample with Apache License 2.0 5 votes vote down vote up
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    // IEの場合はContent-Lengthヘッダが指定されていないとダウンロードが失敗するので、
    // サイズを取得するための一時的なバイト配列ストリームにコンテンツを書き出すようにする
    val baos = createTemporaryOutputStream();

    // 帳票レイアウト
    val report = loadReport();

    // データの設定
    val dataSource = new JRBeanCollectionDataSource(this.data);
    val print = JasperFillManager.fillReport(report, model, dataSource);

    val exporter = new JRPdfExporter();
    exporter.setExporterInput(new SimpleExporterInput(print));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
    exporter.exportReport();

    // ファイル名に日本語を含めても文字化けしないようにUTF-8にエンコードする
    val encodedFilename = EncodeUtils.encodeUtf8(filename);
    val contentDisposition = String.format("attachment; filename*=UTF-8''%s", encodedFilename);
    response.setHeader(CONTENT_DISPOSITION, contentDisposition);

    writeToResponse(response, baos);
}
 
Example #10
Source File: JasperReportsUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Convert the given report data value to a {@code JRDataSource}.
 * <p>In the default implementation, a {@code JRDataSource},
 * {@code java.util.Collection} or object array is detected.
 * The latter are converted to {@code JRBeanCollectionDataSource}
 * or {@code JRBeanArrayDataSource}, respectively.
 * @param value the report data value to convert
 * @return the JRDataSource (never {@code null})
 * @throws IllegalArgumentException if the value could not be converted
 * @see net.sf.jasperreports.engine.JRDataSource
 * @see net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
 * @see net.sf.jasperreports.engine.data.JRBeanArrayDataSource
 */
public static JRDataSource convertReportData(Object value) throws IllegalArgumentException {
	if (value instanceof JRDataSource) {
		return (JRDataSource) value;
	}
	else if (value instanceof Collection) {
		return new JRBeanCollectionDataSource((Collection<?>) value);
	}
	else if (value instanceof Object[]) {
		return new JRBeanArrayDataSource((Object[]) value);
	}
	else {
		throw new IllegalArgumentException("Value [" + value + "] cannot be converted to a JRDataSource");
	}
}
 
Example #11
Source File: ShowReport.java    From Hotel-Properties-Management-System with GNU General Public License v2.0 5 votes vote down vote up
public void loadReport(String reportName, ReportObject reportObject) {

		logging = LoggingEngine.getInstance();
		
		try {
			
			final InputStream inputStream = ShowReport.class
					.getResourceAsStream("/com/coder/hms/reportTemplates/" + reportName + ".jrxml");
			JasperReport report = JasperCompileManager.compileReport(inputStream);

			HashMap<String, Object> parameters = new HashMap<String, Object>();	
			List<ReportObject> list = new ArrayList<ReportObject>();
			list.add(reportObject);
			JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(list);
			JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, beanColDataSource);
			final JRViewer viewer = new JRViewer(jasperPrint);

			setType(Type.POPUP);
			setResizable(false);
			setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
			this.setTitle("Reservation [Report]");
			this.setExtendedState(Frame.MAXIMIZED_BOTH);
			this.setAlwaysOnTop(isAlwaysOnTopSupported());
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			getContentPane().setLayout(new BorderLayout());
			this.setIconImage(Toolkit.getDefaultToolkit().
					getImage(LoginWindow.class.getResource(LOGOPATH)));
			this.setResizable(false);
			getContentPane().add(viewer, BorderLayout.CENTER);

		} catch (JRException e) {
			logging.setMessage("JRException report error!");
		}

	}
 
Example #12
Source File: AbstractJasperReportsViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public JRDataSource create(JasperReport jasperReport) throws JRException {
	return new JRBeanCollectionDataSource(getData());
}
 
Example #13
Source File: GeneratePDFReport.java    From elasticsearch-report-engine with GNU General Public License v3.0 4 votes vote down vote up
private JRDataSource getDatasource(List data) {
  if (data != null) {
    return new JRBeanCollectionDataSource(data);
  }
  return new JRBeanCollectionDataSource(new ArrayList());
}
 
Example #14
Source File: JasperReportsUtilsTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private JRDataSource getDataSource() {
	return new JRBeanCollectionDataSource(getData());
}
 
Example #15
Source File: ExportService.java    From Asqatasun with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Processes the download for Excel format
 * @param response
 * @param resourceId
 * @param auditStatistics
 * @param dataSource
 * @param locale
 * @param format
 * @throws ColumnBuilderException
 * @throws ClassNotFoundException
 * @throws JRException
 * @throws NotSupportedExportFormatException 
 */
@SuppressWarnings("unchecked")
public void export (
        HttpServletResponse response,
        long resourceId,
        AuditStatistics auditStatistics,
        Collection<?> dataSource,
        Locale locale,
        String format)
        throws ColumnBuilderException, ClassNotFoundException, JRException, NotSupportedExportFormatException {

    if (!exportFormatMap.containsKey(format)) {
        throw new NotSupportedExportFormatException(format);
    }
    ExportFormat exportFormat = exportFormatMap.get(format);

    DynamicReport dr = LayoutFactory.getInstance().buildReportLayout(locale, auditStatistics, format);
    // Retrieve our data source
    JRDataSource ds = new JRBeanCollectionDataSource(dataSource);

    // params is used for passing extra parameters
    JasperPrint jp =
            DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
    
    // Create our output byte stream
    // This is the stream where the data will be written
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    JRExporter exporter;
    try {
        exporter = (JRExporter) Class.forName(exportFormat.getExporterClassName()).newInstance();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
        exporter.exportReport();
        response.setHeader(CONTENT_DISPOSITION, INLINE_FILENAME
            + getFileName(resourceId,exportFormat.getFileExtension()));
        // Make sure to set the correct content type
        // Each format has its own content type
        response.setContentType(exportFormat.getFileType());
        response.setContentLength(baos.size());
        // Write to reponse stream
        writeReportToResponseStream(response, baos);
    } catch (InstantiationException | IllegalAccessException ex) {
        LOGGER.warn(ex);
    }

}
 
Example #16
Source File: GenerateHTMLReport.java    From elasticsearch-report-engine with GNU General Public License v3.0 4 votes vote down vote up
private JRDataSource getDatasource(List data) {
  if (null != data) {
    return new JRBeanCollectionDataSource(data);
  }
  return new JRBeanCollectionDataSource(new ArrayList());
}
 
Example #17
Source File: AdminJasperDataSource.java    From Spring-MVC-Blueprints with MIT License 2 votes vote down vote up
@Override
public JRDataSource create(JasperReport jrReport) throws JRException {


	return new JRBeanCollectionDataSource(userList);
}