I'm using Jasper Reports with Java to create some reports. In the interface, the user selects 1 or more items, and a report is generated for each item. Now, I have the query:
SELECT * FROM StockInventory
This is in the jrxml file. But, running that through my app, it'll create a report for every single item in the table. What I'd like is:
SELECT * FROM StockInventory WHERE pk IN (?...)
Where "?..." are the keys of the items the user selects. So, not only is the parameter dynamic, but the number of parameters is also dynamic.
My problem is, I don't know how to setup the parameters in the jrxml and I don't know how to set the parameters from within the jasperreports library in Java. Currently, to set values I'm doing this:
Map<String, Object> params = new HashMap<String, Object>();
JasperReport report = JasperCompileManager.compileReport("path\\to\\jrxml");
JasperPrint print = JasperFillManager.fillReport(report, params, new ItemData(keys));
Where ItemData() is :
private class ItemData implements JRDataSource
{
private final List<InventoryItem> items;
private int counter;
public ItemData(List<PrimaryKey> keys)
{
items = new ArrayList<InventoryItem>();
InventoryItemDao dao = new InventoryItemDao();
for(PrimaryKey key : keys)
{
InventoryItem item = dao.find(key.getPk(), key.getCpk());
if (item != null)
{
items.add(item);
}
}
counter = -1;
}
@Override
public boolean next() throws JRException
{
if ( counter < items.size() - 1)
{
counter++;
return true;
}
return false;
}
@Override
public Object getFieldValue(JRField jrf) throws JRException
{
// Return the relevant field
}
}
The problem with this is that it's looping through the primary keys the user selected and running a query for each of those. If I could just do this with a single dynamic query, it would be much simpler.
Thanks for any help!