vaadin 流资源中的动态资源和 jasperreport

发布于 2024-09-08 12:14:49 字数 5927 浏览 1 评论 0原文

STACKOVERFLOW 是我提出问题的最终目的地。 Vaadin 论坛真的很安静,而dynamicreports 没有论坛。

我在将基于 jasperreport 的动态报告与名为“Embedded”的 vaadin 类集成时遇到问题。 “Embedded”类需要 StreamResource 对象,并且所有 getStream() 函数都将结束实现,在我的情况下,该函数永远不会被调用。

这是我的代码:

//
//
//
public void build(Application app) throws IOException, DRException {

    final JasperReportBuilder report = DynamicReports.report();
    report.addColumn(Columns.column("Item",       "item",      DataTypes.stringType()));
    report.addColumn(Columns.column("Quantity",   "quantity",  DataTypes.integerType()));
    report.addColumn(Columns.column("Unit price", "unitprice", DataTypes.bigDecimalType()));
    report.addTitle(Components.text("Getting started"));
    report.addPageFooter(Components.pageXofY());
    report.setDataSource(createDataSource());

    StreamResource.StreamSource resstream = new filePDF(report);
    StreamResource ress = new StreamResource(resstream, "abc.pdf", app);

    //
    ress.setMIMEType("application/pdf");

    //
    Embedded c = new Embedded("Title", ress);
    c.setSource(ress);
    c.setMimeType("application/pdf");
    c.setType(Embedded.TYPE_BROWSER);
    c.setSizeFull();
    c.setHeight("800px");
    c.setParameter("Content-Disposition", "attachment; filename=" + ress.getFilename());

    //
    app.getMainWindow().removeAllComponents();
    app.getMainWindow().addComponent(c);
}

//
//
//
private JRDataSource createDataSource() {
    DataSource dataSource = new DataSource("item", "quantity", "unitprice");

    dataSource.add("Notebook", 1, new BigDecimal(500));
    dataSource.add("DVD", 5, new BigDecimal(30));
    dataSource.add("DVD", 1, new BigDecimal(28));
    dataSource.add("DVD", 5, new BigDecimal(32));
    dataSource.add("Book", 3, new BigDecimal(11));
    dataSource.add("Book", 1, new BigDecimal(15));
    dataSource.add("Book", 5, new BigDecimal(10));
    dataSource.add("Book", 8, new BigDecimal(9));
    return (JRDataSource) dataSource;
}

这是“filePDF”类:

/**
 * 
 */
package com.example.postgrekonek;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;

import com.vaadin.Application;
import com.vaadin.terminal.StreamResource;

/**
 * @author hehehe
 *
 */
public class filePDF implements StreamResource.StreamSource {

    private JasperReportBuilder report;

    //
    public filePDF(final JasperReportBuilder rpt) {
        report = rpt;
    }

    @Override
    public InputStream getStream() {
        //
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        //
        //os.write(JasperRunManager.runReportToPdf(report.toJasperReport(), new HashMap()));
        try {
            report.toPdf(os);
            try {
                os.flush();
            } catch (IOException e) {
                //
                e.printStackTrace();
            }
        } catch (DRException e) {
            //
            e.printStackTrace();
        }
        return new ByteArrayInputStream(os.toByteArray());
    }

}

这是“Datasource”类:

/* Dynamic reports - Free Java reporting library for creating reports dynamically
 *
 * (C) Copyright 2010 Ricardo Mariaca
 *
 * http://dynamicreports.sourceforge.net
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA. 
 */
package net.sf.dynamicreports.examples;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

/**
 * @author Ricardo Mariaca ([email protected])
 */
public class DataSource implements JRDataSource {
    private String[] columns;
    private List<Map<String, Object>> values;
    private Iterator<Map<String, Object>> iterator;
    private Map<String, Object> currentRecord;

    public DataSource(String ...columns) {  
        this.columns = columns;
        this.values = new ArrayList<Map<String, Object>>();     
    }

    public void add(Object ...values) {
        Map<String, Object> row = new HashMap<String, Object>();
        for (int i = 0; i < values.length; i++) {
            row.put(columns[i], values[i]);
        }
        this.values.add(row);
    }

    public Object getFieldValue(JRField field) throws JRException {
        return currentRecord.get(field.getName());
    }

    public boolean next() throws JRException {
        if (iterator == null) {
            this.iterator = values.iterator();
        }
        boolean hasNext = iterator.hasNext();           
        if (hasNext) {
            currentRecord = iterator.next();
        }
        return hasNext;
    }       
}

STACKOVERFLOW is my final destination for my questions. Vaadin forum is really quiet and dynamicreports has no forum.

I have a problem integrating dynamicreports which is based on jasperreport with vaadin class named "Embedded". The "Embedded" class needs StreamResource object and all will be ended implementing getStream() function which is, in my case, never get called.

Here is my code:

//
//
//
public void build(Application app) throws IOException, DRException {

    final JasperReportBuilder report = DynamicReports.report();
    report.addColumn(Columns.column("Item",       "item",      DataTypes.stringType()));
    report.addColumn(Columns.column("Quantity",   "quantity",  DataTypes.integerType()));
    report.addColumn(Columns.column("Unit price", "unitprice", DataTypes.bigDecimalType()));
    report.addTitle(Components.text("Getting started"));
    report.addPageFooter(Components.pageXofY());
    report.setDataSource(createDataSource());

    StreamResource.StreamSource resstream = new filePDF(report);
    StreamResource ress = new StreamResource(resstream, "abc.pdf", app);

    //
    ress.setMIMEType("application/pdf");

    //
    Embedded c = new Embedded("Title", ress);
    c.setSource(ress);
    c.setMimeType("application/pdf");
    c.setType(Embedded.TYPE_BROWSER);
    c.setSizeFull();
    c.setHeight("800px");
    c.setParameter("Content-Disposition", "attachment; filename=" + ress.getFilename());

    //
    app.getMainWindow().removeAllComponents();
    app.getMainWindow().addComponent(c);
}

//
//
//
private JRDataSource createDataSource() {
    DataSource dataSource = new DataSource("item", "quantity", "unitprice");

    dataSource.add("Notebook", 1, new BigDecimal(500));
    dataSource.add("DVD", 5, new BigDecimal(30));
    dataSource.add("DVD", 1, new BigDecimal(28));
    dataSource.add("DVD", 5, new BigDecimal(32));
    dataSource.add("Book", 3, new BigDecimal(11));
    dataSource.add("Book", 1, new BigDecimal(15));
    dataSource.add("Book", 5, new BigDecimal(10));
    dataSource.add("Book", 8, new BigDecimal(9));
    return (JRDataSource) dataSource;
}

And this is "filePDF" class:

/**
 * 
 */
package com.example.postgrekonek;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;

import com.vaadin.Application;
import com.vaadin.terminal.StreamResource;

/**
 * @author hehehe
 *
 */
public class filePDF implements StreamResource.StreamSource {

    private JasperReportBuilder report;

    //
    public filePDF(final JasperReportBuilder rpt) {
        report = rpt;
    }

    @Override
    public InputStream getStream() {
        //
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        //
        //os.write(JasperRunManager.runReportToPdf(report.toJasperReport(), new HashMap()));
        try {
            report.toPdf(os);
            try {
                os.flush();
            } catch (IOException e) {
                //
                e.printStackTrace();
            }
        } catch (DRException e) {
            //
            e.printStackTrace();
        }
        return new ByteArrayInputStream(os.toByteArray());
    }

}

And this is "Datasource" class:

/* Dynamic reports - Free Java reporting library for creating reports dynamically
 *
 * (C) Copyright 2010 Ricardo Mariaca
 *
 * http://dynamicreports.sourceforge.net
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA. 
 */
package net.sf.dynamicreports.examples;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

/**
 * @author Ricardo Mariaca ([email protected])
 */
public class DataSource implements JRDataSource {
    private String[] columns;
    private List<Map<String, Object>> values;
    private Iterator<Map<String, Object>> iterator;
    private Map<String, Object> currentRecord;

    public DataSource(String ...columns) {  
        this.columns = columns;
        this.values = new ArrayList<Map<String, Object>>();     
    }

    public void add(Object ...values) {
        Map<String, Object> row = new HashMap<String, Object>();
        for (int i = 0; i < values.length; i++) {
            row.put(columns[i], values[i]);
        }
        this.values.add(row);
    }

    public Object getFieldValue(JRField field) throws JRException {
        return currentRecord.get(field.getName());
    }

    public boolean next() throws JRException {
        if (iterator == null) {
            this.iterator = values.iterator();
        }
        boolean hasNext = iterator.hasNext();           
        if (hasNext) {
            currentRecord = iterator.next();
        }
        return hasNext;
    }       
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

树深时见影 2024-09-15 12:14:49

也许这是浏览器缓存问题。
您尝试过 res.setCacheTime(1) 吗?

要获得更有效的流式传输,您应该查看 http://ostermiller.org/convert_java_outputstream_inputstream.html
很快添加生产者线程来处理报告输出,并使用循环缓冲区将其托管为输入。

Maybe this is browser cache issue.
Have you been try with ress.setCacheTime(1)?

For more effective streaming you should look at http://ostermiller.org/convert_java_outputstream_inputstream.html
Shortly add producer thread to handle report output and use circular buffer for host it as input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文