Home
Analytics
Passing Java HashMap to Report as Report Parameter
pawasarmol
Hello all,
I have a requirement in which I have a Java HashMap with size of around 100 populated by some other different operation , I want to pass that HashMap to my Report, As I want to populate one of column of my report table based on the value in hashmap, like for eg. if my report SQL query returns "a" for one column (which is key value for my hashmap) i want to print corresponding value in hashmap and so on...
Can somebody give some directions ? Thanks
Find more posts tagged with
Comments
CBR
You have multiple options:<br />
<br />
The best would be to push the data to BIRT using Report Engine API. If you run a report using Report Engine API you can inject any java objects into the report. Reports are run using Tasks (you interact with BIRT using Tasks created in your application. E.g if you want to run and render a report you are using a runandrenderTask<br />
The appContext itself is a java.util.HashMap<String, Object>. It is used to store any values coming from outside of BIRT. So never delete or overwrite it, just add additional values to it.<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
runAndRenderTask.getAppContext().put("myMap", myMap)
</pre>
<br />
In your report you can get back the appContext using:<br />
hashMap = reportContext.getAppContext();<br />
myMap = hashMap.get("myMap")<br />
<br />
The disadvantage is that you have to integrate BIRt into your own application. So if you want to use the out of the box WebViewer this approach won't work.<br />
<br />
<br />
Another approach would be to use the Session on the webserver and put some values in it. This approach can cause serious performance and memory problems depending on the size of the list. Let me know if you can't use the first approach and i ll tell you about the Session approach.
pawasarmol
Thanks for a prompt and in detail response, we do use BIRT using Report Engine API. but we pass the value as report parameters, like <br />
IRunAndRenderTask task = (populate the object)<br />
task.setParameterValues(java.util.HashMap<String, Object>);<br />
where the hashmap is the ("key", "value") pair of ("Report Parameter name", "report parameter value")<br />
<br />
so there i can pass the HashMap using the ("report parameter name", "values(hashmap)"), but was just wondering how do i access this hashmap in my report (as this is a report parameter and would BIRT identify it as a hashmap) and also how exactly i can configure this HashMap to my column , so that values are printed based on the keys which are the SQL Query column. <br />
<br />
Let me know if i am not clear in what I am trying to say. <br />
<br />
Thanks once again. <br />
<br />
<blockquote class='ipsBlockquote' data-author="'cbrell'" data-cid="101521" data-time="1338472537" data-date="31 May 2012 - 06:55 AM"><p>
You have multiple options:<br />
<br />
The best would be to push the data to BIRT using Report Engine API. If you run a report using Report Engine API you can inject any java objects into the report. Reports are run using Tasks (you interact with BIRT using Tasks created in your application. E.g if you want to run and render a report you are using a runandrenderTask<br />
The appContext itself is a java.util.HashMap<String, Object>. It is used to store any values coming from outside of BIRT. So never delete or overwrite it, just add additional values to it.<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
runAndRenderTask.getAppContext().put("myMap", myMap)
</pre>
<br />
In your report you can get back the appContext using:<br />
hashMap = reportContext.getAppContext();<br />
myMap = hashMap.get("myMap")<br />
<br />
The disadvantage is that you have to integrate BIRt into your own application. So if you want to use the out of the box WebViewer this approach won't work.<br />
<br />
<br />
Another approach would be to use the Session on the webserver and put some values in it. This approach can cause serious performance and memory problems depending on the size of the list. Let me know if you can't use the first approach and i ll tell you about the Session approach.<br /></p></blockquote>
CBR
Hi,
you can't use a report parameter in that case. The reason for that is that BIRT only supports Date, DateTime, Time, Integer, Float, Decimal and boolean datatype. The BIRT report engine will not allow to set a parameter to an object of any type (in reality it will check if each parameter given through this method is of specified type, if not it will throw an exception)
If you need to pass anything else to the report you have to use some other approach. If you want to go with the appContext approach just implement the beforeFactory event as follows:
hashMap = reportContext.getAppContext().get("myHashMap")
Place a dynamic text into the detail row of your table. There you can simply use:
hashMap.get(row["key"]) where row["key"] is the dataset column containing the key to lookup the value.
Let me know if you have additional questions.
CBR
sorry forgot to mention that you can use a String report parameter. If you are using this approach you have to invent a String format to encode your hashMap as a String. Inside your BIRT report you would need to parse that String to restore the HashMap. That can become quite messy but it will work.<br />
<br />
Eg: Encode HashMap as String key1:value1;key2:value2;... give that String using reportParameter map<br />
<br />
In beforeFactory:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
singleValues = map.split(";");
myHashMap = new Packages.java.util.HashMap();
for(i=0; i<singleValues.length; i++) {
keyValuePair = singleValues[i].split(":");
myHashMap.put(keyValuePair[0], keyValuePair[1]);
}
</pre>
The rest will be the same.<br />
Nevertheless i would prefer the first approach because you just push the HashMap to BIRT as is instead of encoding the HashMap outside of BIRT and parse String inside your report to restore just the same HashMap again.
pawasarmol
Thanks a lot for a very nice and descriptive response. Really Appreciate it.
chriselviss
<p>Check this one to know more about...<a data-ipb='nomediaparse' href='
http://net-informations.com/java/col/hashmap.htm'>Hashmap</a></p>
;
<p> </p>
<p>Elvis</p>