DynamicJasper: use array or list as datasource without objects

709 Views Asked by At

I'm using DynamicJasper to build reports but the only problem I face here is that you need to pass a collection of objects as datasource.
But in my case I need to pass a dynamic columns and data, example: check this link http://dynamicjasper.com/documentation-examples/getting-started/: under "Creating a simple Report" section they pass a list of Product as datasource :

FastReportBuilder drb = new FastReportBuilder();
DynamicReport dr = drb.addColumn("State", "state", String.class.getName(),30)
.addColumn("Branch", "branch", String.class.getName(),30)
.addColumn("Product Line", "productLine", String.class.getName(),50)
.addColumn("Item", "item", String.class.getName(),50)
.addColumn("Item Code", "id", Long.class.getName(),30,true)
.addColumn("Quantity", "quantity", Long.class.getName(),60,true)
.addColumn("Amount", "amount", Float.class.getName(),70,true)
.addGroups(2)
.setTitle("November 2006 sales report")
.setSubtitle("This report was generated at " + new Date())
.setPrintBackgroundOnOddRows(true)
.setUseFullPageWidth(true)
.build();

JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());// here they give list of Products
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
JasperViewer.viewReport(jp);

In this example they used list of Product entity where this entity has properties like state, branch, etc .. so it can match the given columns.

But in my case I'm going to get dynamic data, it's not a list of objects, but an array of data (strings, integers, dates, etc...)

my question is: is there way to pass a custom list to JRBeanCollectionDataSource instead of list of predefined objects ?

1

There are 1 best solutions below

0
SlimenTN On BEST ANSWER

the solution is to use JRMapCollectionDataSource instead of JRBeanCollectionDataSource
this way I can pass data like so:

List<Map<String, ?>> list = new ArrayList<>();
Map<String, Object> row = new HashMap<>();
row.put("state", "State value");
row.put("branch", "Branch value");
row.put("productLine", "Product line value");
// add the row to the list
list.add(row);

// and finaly pass the list to datasource
JRDataSource ds = new JRMapCollectionDataSource(list);