javadynamic-jasper

DynamicJasper: accessing values for dynamic columns in a concatenated report


I have a need to create a report in DynamicJasper that has multiple "subreports" which contain different columns. The data for the columns is stored in a HashMap on each row Object. I have not found any way in DynamicJasper to specify that a column's value should come from a particular key on the HashMap.

I have found that if I build a single report, I can extend JRAbstractBeanDataSource and create my own Data Source that knows how to get the data correctly based on how I format the field name. However, when I use addConcatenatedReport to add in multiple reports, the "subreports" use JRBeanCollectionDataSource instead of my custom Data Source.

The only solution I have come up with so far is to have a POJO that has a bunch of properties like "column1value" and "column2value" which I preload and use for the field references in the dynamic columns. I really don't want to do this...can anyone think of any other options? Anything I am missing?

Sidenote: any ideas why you can't pass a custom data source type to the addConcatenatedReport function? Technical issues, or it just hasn't been needed? Seems like this would be a common need for a "dynamic" report.


Solution

  • I was searching for the same thing yesterday and I came across your post (and I saw it was recent). After some search I managed to overcome this problem so I thought it would be nice to share with you my findings.

    I suppose that you defined an POJO for your data and then added all these objects to the HashMap (correct me if I am right).

    What I did was to create a new Object that extends HashMap implementation. (In fact you dont have to extend this, you can use the HashMap object itself). So now instead of having objects inside HashMap I directly inserted values for the properties. But lets just add some code blocks in order to clear things :)

    Suppose that you have the following PoJO

    public class MyPOJO{
    
      private String name;
      private String value;
      //getters, setters etc..
     }
    

    Instead of adding these various objects to a List and providing it as datasource you can use a HashMap this way to define your objects:

    Map<String,String> myObject1=new HashMap<String,String>();
    myObject1.put("name","Name1");
    myObject1.put("value","Value1");
    
    Map<String,String> myObject2=new HashMap<String,String>();
    myObject2.put("name","Name2");
    myObject2.put("value","Value2");
    

    After defining these objects we can add them in a List and provide it as datasource (JRBeanCollectionDataSource). So the keys of each HashMap are considered to be the properties defined in the Columns (the properties of the initial POJO).

    I dont know if my solution is the best but it worked for me!