简体   繁体   中英

How To Pass a JRBeanCollectionDataSource to iReport?

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();
}

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.

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)

I tried to read your code to make aa 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.

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 + "]";
    }

}

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);

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. 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 .

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;
    }

}

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

  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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM