How to pass JRBeanCollectionDataSource to iReport?

I'm currently trying to use jasper to help me generate reports. I have information and data that I want to show 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 Bean Factory to create my object, which will be ready for transfer to iReport.

How can I put all this information in a Bean? I essentially want Bean to include a script / test case and whether it passes. (This is for test automation)

+4
source share
1 answer

I tried reading your code to better understand which columns you want, but without context, I have no idea. All bean are pojo, with private fields and public getters and setters.

Assuming there is no grouping, and essentially each ScenarioLoadModel will correspond to one line in the report, you will get a bean as follows:

 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 create instances of ScenarioResults and add them to the list. Once you have a list, all you have 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 that you are designing a report in iReport, it can be a little difficult to get the automatically imported fields. Basically, first add your project using bean to the class path in iReports (you can simply point it to the bin folder or jar file): Tools β†’ options β†’ classpath tab. Now, to add fields, follow these steps:

  • Click the following icon: Create datasource
  • Select the JavaBean Datasource tab.
  • Enter the class name of your bean. (e.g. ScenarioResults )
  • Click Read attributes
  • Highlight the required fields in the report and click Add Selected Field(s) .
  • Click OK .

Now, if you want to check how a report with data looks, and not just an empty data source, Factory comes here. It is intended for testing only when using iReport. You need to create a class that essentially creates a dummy dataset for you. It should look something like this:

 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 data source pointing to it in iReport.

  • Next to the Datasource drop-down list in the toolbar, click the icon using the `Report Datasources.
  • Click New .
  • Select JavaBeans set datasource . Click Next .
  • For the name, enter ScenarioResultsFactory .
  • For the Factory class, you need to put the class name, including the package. Therefore, if the class is in the com package, you should have com.ScenarioResultsFactory here.
  • For the static method, set createBeanCollection if it does not already exist.
  • Check the Use field description box. Click Test to verify that it worked.
  • Click Save .
+7
source

Source: https://habr.com/ru/post/1446974/


All Articles