<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Along Came Betty &#187; birt</title>
	<atom:link href="http://blog.darevay.com/category/birt/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.darevay.com</link>
	<description>You know, software and some other stuff like maybe guitar or something</description>
	<lastBuildDate>Tue, 24 Nov 2009 03:21:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Scripted Data Sources with BIRT</title>
		<link>http://blog.darevay.com/2007/11/scripted-data-sources-with-birt/</link>
		<comments>http://blog.darevay.com/2007/11/scripted-data-sources-with-birt/#comments</comments>
		<pubDate>Thu, 01 Nov 2007 12:42:00 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[birt]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=9</guid>
		<description><![CDATA[I recently started experimenting with the BIRT reporting engine for Java. It seems pretty powerful, well-designed, and has a nice report design tool implemented in Eclipse.  Since the project I&#8217;m working on doesn&#8217;t have a database, I decided to use BIRT&#8217;s scripting data sources. These allow you to create a (hopefully) small JavaScript glue [...]]]></description>
			<content:encoded><![CDATA[<p>I recently started experimenting with the <a href="http://www.eclipse.org/birt/phoenix/">BIRT </a>reporting engine for Java. It seems pretty powerful, well-designed, and has a nice report design tool implemented in Eclipse.  Since the project I&#8217;m working on doesn&#8217;t have a database, I decided to use BIRT&#8217;s <a href="http://wiki.eclipse.org/index.php/BIRT/FAQ/Scripting">scripting data sources</a>. These allow you to create a (hopefully) small JavaScript glue layer between the reporting engine and your <a href="http://en.wikipedia.org/wiki/POJO">plain old Java objects</a>.  The purpose of this post is to cover some of the gotchas I&#8217;ve encountered so far.</p>
<p>The basic idea is that you implement a few JavaScript callback methods that feed row-like back to the reporting engine. BIRT uses Mozilla&#8217;s <a href="http://www.mozilla.org/rhino/">Rhino</a> JavaScript engine. Since the typical use case for BIRT is a database backend, the documentation for the scripting interface seems harder to come by. The FAQ helped, but didn&#8217;t answer all of my questions.</p>
<p>First, all of the <a href="http://tools.osmosis.gr/blog/archives/2005/06/birt_and_object.html">examples</a> retrieve their &#8220;model&#8221; from a static factory method called from JavaScript. This isn&#8217;t that realistic. How do I pass my model in to the JavaScript? Or at least a parameter like a filename?  I think part of the reason all the examples take this approach is so that it&#8217;s possible to run the report at design time with the preview window. I did some <a href="http://www.google.com/codesearch">digging</a> and eventually found the right calls:</p>
<pre name="code" class="java">
MyModel model = ...;
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
Map parameters = new HashMap();
parameters.put("model", model);
task.setParameterValues(parameters);
</pre>
<p>Cool. Now my model&#8217;s being passed to the engine. But wait. How does BIRT find my classes? I know it&#8217;s built on top of Eclipse which means the classpath is not as simple as you&#8217;d like. In some examples I found, you have to manually copy your compiled class files to a directory in one of the BIRT plugins. Uggh. This is a pain, but acceptable for deployment. But during development? I might as well go back to C++ if I want to waste that much time.</p>
<p>Some more searching dug up this <a href="http://wiki.eclipse.org/index.php/BIRT/FAQ/Scripting#Q:_Where_do_I_have_to_put_my_classes_in_order_to_access_them_by_JavaScript.3F">FAQ</a> question. It lists too yucky solutions and then has a tacked on snippet of code that looks promising.</p>
<pre name="code" class="java">
config = new EngineConfig();
HashMap hm = config.getAppContext();
hm.put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,MyClass.class.getClassLoader());
config.setAppContext(hm);
</pre>
<p>However, I guess I think this is for the version of BIRT I&#8217;m not using because it doesn&#8217;t work. Some more digging and I come up with this:</p>
<pre name="code" class="java">
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
HashMap contextMap = new HashMap();
contextMap.put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,
                      MyModel.class.getClassLoader());
task.setAppContext(contextMap);
</pre>
<p>I guess the app context stuff moved from the task to the engine at some point. Anyway, now we can finally try it out.  I set up my <span style="font-weight: bold;">open</span> callback in Java and&#8230; wait, how do I actually get to my model in the JavaScript code? This wasn&#8217;t too tricky to figure out. The parameters passed to the engine above are passed to the JavaScript code in a global (?) parameter aptly named <span style="font-weight: bold;">params</span>:</p>
<pre name="code" class="java">model = params["model"];</pre>
<p>Nice. Now I write some more code to call some methods on the model and run it and&#8230; <span style="font-weight: bold;">NullPointerException.</span> This is where I was stuck for a little while. I thought that I had screwed up the class loader stuff or something so I fiddled with that stuff for a while. Eventually, when I was about to give up, I stumbled upon <a href="http://dev.eclipse.org/mhonarc/lists/birt-dev/msg04771.html">this bug report</a> which, although it doesn&#8217;t mention a NullPointerException, does mention a &#8220;display name&#8221; which I had noticed references to while poking around in the debugger after my crash.</p>
<p>The trick is that the <span style="font-weight: bold;">params</span> variable in JavaScript doesn&#8217;t hold just my model, but an extra wrapper object with a display name and <span style="font-weight: bold;">value</span> is stuck in there. I revise my code to look like this:</p>
<pre name="code" class="java">
model = params["model"].value
</pre>
<p>and voila, I run it again and my report is generated as expected.</p>
<p>Hope this is helpful to someone else out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2007/11/scripted-data-sources-with-birt/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
