如何将JRBeanCollectionDataSource传递给iReport? [英] How To Pass a JRBeanCollectionDataSource to iReport?

查看:146
本文介绍了如何将JRBeanCollectionDataSource传递给iReport?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用jasper来帮助我创建报告。我有这个方法中显示的信息和数据:

I'm currently trying to use jasper to help me create reports. I have the information and data that I want displayed in this method:

private void writeToFile(final List<ScenarioLoadModel> sceneLoadModel) throws Exception {
  final BufferedWriter bw = new BufferedWriter(new FileWriter("/Uma/nft/result.psv"));

  for (final ScenarioLoadModel slm : sceneLoadModel) {
    bw.write(slm.getScenarioId() + PSP + slm.getScenarioId() + PSP + slm.getScenarioConfig().getName() + PSP + slm.getLoad() + PSP + "" + EOL);
    if (!slm.getScenarios().isEmpty()) {
      final int tempCount = slm.getScenarios().get(0).getTemplates().size();
      final int sceneCount = slm.getScenarios().size();
      for (int tempIdx = 0; tempIdx < tempCount; tempIdx++) {
        String id = null;
        int pass = 0;
        int fail = 0;
        final Map<String, BigDecimal> metricMap = new HashMap<String, BigDecimal>();
        final DefaultStatisticalCategoryDataset dataset = new DefaultStatisticalCategoryDataset();
        for (int sceneIdx = 0; sceneIdx < sceneCount; sceneIdx++) {
          final Template temp = slm.getScenarios().get(sceneIdx).getTemplates().get(tempIdx);
          if (temp.isError()) {
            fail++;
          } else {
            pass++;
          }
          if (sceneIdx == 0) {
            id = temp.getId();
          }
          final MetricGroupModel mgm = slm.getScenarios().get(sceneIdx).getMetricGroupModel().get(tempIdx);
          if (mgm != null) {
            for (final MetricModel mm : mgm.getMetricModel()) {
              for (final MetricValue mv : mm.getMetricValue()) {
                dataset.add(mv.getValue(), new BigDecimal(0.0), mv.getType(), id);
              }
            }
          }
        }
        final TemplateConfig tc = TemplateManager.getTemplateConfig(id);

        bw.write(slm.getScenarioId() + PSP);
        bw.write(id + PSP + tc.getName() + PSP + 1 + PSP + pass + "/" + fail);
        for (final Object row : dataset.getRowKeys()) {
          final Number mean = dataset.getValue((String) row, id);
          bw.write(PSP + row + PSP + mean);
        }
        bw.write(EOL);
      }
    }
  }

  bw.close();
}

根据我的理解,我创建Beans,然后将它们全部放在Bean Factory中,创建我的对象,准备将其传递给iReport。

From my understanding I create Beans and then put them all in a Bean Factory, to create my object that will be ready to be passed to iReport.

如何将所有这些信息都放入Bean中?我基本上希望bean包含场景/测试用例以及它是否通过。 (这是为了测试自动化)

How can I put all this information into a Bean? I essentially want the bean to include the scenario/test case and whether or not it passed. (This is for test automation)

推荐答案

我试着阅读你的代码,以便最好地猜测你想要的列,但没有背景,我不知道。所有的bean都是pojo,有私有字段和公共getter和setter。

I tried to read your code to make a a best guess at what columns you would want, but with no context, I have no clue. All the bean is a pojo, with private fields and public getters and setters.

假设没有分组,基本上每个 ScenarioLoadModel 将对应于报告中的一行,最终会得到这样的bean:

Assuming there is no grouping and essentially each ScenarioLoadModel will correspond to one row in the report you would end up with a bean like this:

public class ScenariaResults {

    private String id;

    private String name;

    private String load;

    private int passCount;

    private int failCount;

    public ScenariaResults(String id, String name, String load, int passCount,
            int failCount) {
        super();
        this.id = id;
        this.name = name;
        this.load = load;
        this.passCount = passCount;
        this.failCount = failCount;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLoad() {
        return load;
    }

    public void setLoad(String load) {
        this.load = load;
    }

    public int getPassCount() {
        return passCount;
    }

    public void setPassCount(int passCount) {
        this.passCount = passCount;
    }

    public int getFailCount() {
        return failCount;
    }

    public void setFailCount(int failCount) {
        this.failCount = failCount;
    }

    @Override
    public String toString() {
        return "ScenariaResults [id=" + id + ", name=" + name + ", load="
                + load + ", passCount=" + passCount + ", failCount="
                + failCount + "]";
    }

}

所以基本上在你的代码中在上面构建 ScenarioResults 的实例,并将它们添加到列表中。获得列表后,您需要做的就是创建一个JRDataSource:

So basically in the code you have above you build instances of ScenarioResults and add them to a list. Once you have the list, all you need to do is create a JRDataSource:

List<ScenarioResults> dataBeanList = ...call your method to get the list of results
//create the datasource
JRDataSource dataSource = new JRBeanCollectionDataSource(dataBeanList);

现在,在iReport中设计报表时,自动导入字段可能有点棘手。基本上首先将你的项目与bean一起添加到iReports中的类路径中(可以将它指向bin文件夹或jar文件`):工具 - >选项 - >类路径选项卡。现在按照以下步骤添加字段。

Now when designing the report in iReport it can be a little tricky to get the fields imported automatically. Basically first add your project with the bean to the classpath in iReports (could just point it to the bin folder or jar file`): Tools -> options -> classpath tab. Now follow these steps to add the fields.


  1. 单击以下图标:

  2. 选择 JavaBean数据源选项卡。

  3. 输入bean的类名。 (例如 ScenarioResults

  4. 点击读取属性

  5. 在报告中突出显示所需的字段,然后单击添加所选字段

  6. 单击确定

  1. Click the following icon:
  2. Select the JavaBean Datasource tab.
  3. Enter the classname of your bean. (ex. ScenarioResults)
  4. Click Read attributes
  5. Highlight the fields you want in the report and click Add Selected Field(s).
  6. Click OK.

现在,如果您想测试报告的数据结果,而不仅仅是一个空的数据源,这就是工厂的用武之地。它仅用于在使用iReport时进行测试。您需要创建一个基本上为您创建虚拟数据集的类。它应该类似于:

Now if you want to test what the report looks like with data, and not just an empty datasource, this is where the Factory comes in. It is only for testing while using iReport. You need to create a class that will essentially create a dummy data set for you. It should look something like:

import java.util.ArrayList;
import java.util.List;

public class ScenarioResultsFactory {

    public static List<ScenarioResults> createBeanCollection() {
        List<ScenarioResults> list = new ArrayList<ScenarioResults>();       
        list.add(new ScenarioResults("1", "test", "load", 10, 5));
        //add as many as you want       
        return list;
    }

}

现在你需要创建一个数据源在iReport中指向它。

Now you need to create a Datasource pointing to it in iReport.


  1. 在工具栏中的数据源下拉列表旁边,单击带有工具提示报告数据源的图标。

  2. 点击

  3. 选择 JavaBeans设置数据源。单击下一步

  4. 对于名称,请输入 ScenarioResultsFactory

  5. 对于Factory类,您需要输入包含包的类名。因此,如果该类位于 com 包中,那么您应该在此处使用 com.ScenarioResultsFactory

  6. 对于静态方法,如果不存在,则放入 createBeanCollection

  7. 选中使用字段说明复选框。单击测试以确保其有效。

  8. 单击保存

  1. Next to the Datasource dropdown in the toolbar click the icon with the tooltip `Report Datasources.
  2. Click New.
  3. Select JavaBeans set datasource. Click Next.
  4. For name enter ScenarioResultsFactory.
  5. For the Factory class you need to put the classname including package. So if the class is in the com package you should have com.ScenarioResultsFactory here.
  6. For the static method put createBeanCollection if not already there.
  7. Check the Use field description check box. Click Test to make sure it worked.
  8. Click Save.

这篇关于如何将JRBeanCollectionDataSource传递给iReport?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆