<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Right Outer Join</title>
	<atom:link href="http://mdahlman.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mdahlman.wordpress.com</link>
	<description>Open Source Musings</description>
	<lastBuildDate>Wed, 25 Jan 2012 20:08:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mdahlman.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Right Outer Join</title>
		<link>http://mdahlman.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mdahlman.wordpress.com/osd.xml" title="Right Outer Join" />
	<atom:link rel='hub' href='http://mdahlman.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Filtering on multiple fields in MongoDB</title>
		<link>http://mdahlman.wordpress.com/2011/11/03/mongodb-multi-field-filter/</link>
		<comments>http://mdahlman.wordpress.com/2011/11/03/mongodb-multi-field-filter/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 06:07:33 +0000</pubDate>
		<dc:creator>mdahlman</dc:creator>
				<category><![CDATA[JasperReports]]></category>
		<category><![CDATA[MongoDB]]></category>

		<guid isPermaLink="false">http://mdahlman.wordpress.com/?p=234</guid>
		<description><![CDATA[Filtering on multiple fields in MongoDB This thread got me thinking about filtering on multiple fields in a MongoDB query. The author of the original question attempted to use the $and operator which was introduced in MongoDB 2.0. But that&#8217;s not needed for simple queries that filter on multiple fields. I created a simple report [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=234&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>Filtering on multiple fields in MongoDB</h1>
<p><a href="http://groups.google.com/group/mongodb-user/browse_thread/thread/475c1e0627694958" target="_blank">This thread</a> got me thinking about filtering on multiple fields in a MongoDB query. The author of the original question attempted to use the $and operator which was introduced in MongoDB 2.0. But that&#8217;s not needed for simple queries that filter on multiple fields. I created a simple report to show the idea.</p>
<div id="attachment_235" class="wp-caption aligncenter" style="width: 583px"><a href="http://mdahlman.files.wordpress.com/2011/11/screen-shot-2011-11-03-at-22-53-29.png"><img class="size-full wp-image-235" title="Filtering on multiple fields at once in MongoDB" src="http://mdahlman.files.wordpress.com/2011/11/screen-shot-2011-11-03-at-22-53-29.png" alt="Filtering on multiple fields at once in MongoDB" width="573" height="367" /></a><p class="wp-caption-text">Filtering on multiple fields at once in MongoDB</p></div>
<p>The report is available <a href="https://docs.google.com/open?id=0B9apCz8LCsSnYzUyYjA3MGYtZmNhOC00NzBhLWE5MDYtZWU2MTcwZGUxODhm" target="_blank">here</a> if you want to play with it. Here&#8217;s the sample data I used:</p>
<pre>db.shiraz.save( { "ServiceType" : "sms", "ServiceHealthState" : "ok", "DocumentID" : "11" } )
db.shiraz.save( { "ServiceType" : "sms", "ServiceHealthState" : "ok", "DocumentID" : "12" } )
db.shiraz.save( { "ServiceType" : "sms", "ServiceHealthState" : "ko", "DocumentID" : "13" } )
db.shiraz.save( { "ServiceType" : "jms", "ServiceHealthState" : "critical", "DocumentID" : "21" } )
db.shiraz.save( { "ServiceType" : "jms", "ServiceHealthState" : "critical", "DocumentID" : "22" } )</pre>
<p>If you haven&#8217;t used (or heard of!) the Jaspersoft connector for MongoDB, then start with <a title="Simple Reporting on MongoDB" href="http://mdahlman.wordpress.com/2011/09/02/simple-reporting-on-mongodb/" target="_blank">this article</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdahlman.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdahlman.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdahlman.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdahlman.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdahlman.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdahlman.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdahlman.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdahlman.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdahlman.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdahlman.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdahlman.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdahlman.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdahlman.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdahlman.wordpress.com/234/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=234&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdahlman.wordpress.com/2011/11/03/mongodb-multi-field-filter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2d40af941f746115e07c340c0c0eadf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdahlman</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/11/screen-shot-2011-11-03-at-22-53-29.png" medium="image">
			<media:title type="html">Filtering on multiple fields at once in MongoDB</media:title>
		</media:content>
	</item>
		<item>
		<title>Emailing reports from JasperReports Server</title>
		<link>http://mdahlman.wordpress.com/2011/09/28/emailing-reports-from-jasperreports-server/</link>
		<comments>http://mdahlman.wordpress.com/2011/09/28/emailing-reports-from-jasperreports-server/#comments</comments>
		<pubDate>Thu, 29 Sep 2011 05:12:06 +0000</pubDate>
		<dc:creator>mdahlman</dc:creator>
				<category><![CDATA[JasperReports Server]]></category>
		<category><![CDATA[JasperServer]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[scheduled reports]]></category>
		<category><![CDATA[smtp]]></category>

		<guid isPermaLink="false">http://mdahlman.wordpress.com/?p=225</guid>
		<description><![CDATA[Emailing reports from JasperReports Server Standard Answer Normally JasperReports Server is installed in a corporate environment. You already have an email server, and configuring scheduled reports to get sent out using this email server is simple. The settings are well documented in &#8220;JasperReports-Server-Install-Guide.pdf&#8221;. Make-my-life-easy Answer But what about a simple evaluation situation? You don&#8217;t have [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=225&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>Emailing reports from JasperReports Server</h1>
<h2>Standard Answer</h2>
<p>Normally JasperReports Server is installed in a corporate environment. You already have an email server, and configuring scheduled reports to get sent out using this email server is simple. The settings are well documented in &#8220;JasperReports-Server-Install-Guide.pdf&#8221;.</p>
<h2>Make-my-life-easy Answer</h2>
<p>But what about a simple evaluation situation? You don&#8217;t have a corporate email server or you don&#8217;t have simple access to it. Shouldn&#8217;t it be easy to configure JasperReports Server to use GMail or some other publicly available email server? Yes. It&#8217;s possible, and it&#8217;s easy. But it&#8217;s sometimes hard to track down all of the little details as these email threads can attest:</p>
<ul>
<li><a href="http://jasperforge.org/plugins/espforum/view.php?group_id=112&amp;forumid=102&amp;topicid=17149" target="_blank">What are the setting values in order to use Gmail</a></li>
<li><a href="http://jasperforge.org/plugins/espforum/view.php?group_id=112&amp;forumid=102&amp;topicid=71349" target="_blank">jasperserver 3.7 mail schedule using gmail smtp</a></li>
<li><a href="http://jasperforge.org/plugins/espforum/view.php?group_id=112&amp;forumid=102&amp;topicid=78346" target="_blank">From variable isn&#8217;t working?</a></li>
<li><a href="http://jasperforge.org/plugins/espforum/view.php?group_id=112&amp;forumid=102&amp;topicid=29512" target="_blank">how can send reports by emails ??</a></li>
</ul>
<p>If you read through all of those threads and read <a href="https://mail.google.com/support/bin/answer.py?answer=13287" target="_blank">GMail support information</a>, you can indeed find all of the information that you need. But for those that don&#8217;t want to wade through the threads, here are the complete details for setting up JasperReports Server to send out email using GMail.</p>
<p>First, edit &#8230;/WEB-INF/js.quartz.properties. Use these settings:</p>
<div>
<pre style="padding-left:30px;">report.scheduler.web.deployment.uri=http://localhost/jasperserver-pro
report.scheduler.mail.sender.host=smtp.gmail.com
report.scheduler.mail.sender.username=me@gmail.com
report.scheduler.mail.sender.password=mypassword
report.scheduler.mail.sender.from=me@gmail.com
report.scheduler.mail.sender.protocol=smtp
report.scheduler.mail.sender.port=587</pre>
</div>
<p>Of course you&#8217;ll need to set your own information appropriately. But the GMail server and port need to be as I have indicated.</p>
<p>Second, edit &#8230;/WEB-INF/applicationContext-report-scheduling.xml</p>
<p>original version:</p>
<pre style="padding-left:30px;">&lt;property name="javaMailProperties"&gt;
    &lt;props&gt;
        &lt;prop key="mail.smtp.auth"&gt;false&lt;/prop&gt;
    &lt;/props&gt;
&lt;/property&gt;</pre>
<p>modified version:</p>
<pre style="padding-left:30px;">&lt;property name="javaMailProperties"&gt;
    &lt;props&gt;
        &lt;prop key="mail.smtp.auth"&gt;true&lt;/prop&gt;  &lt;!--as indicated in JasperReports-Server-Install-Guide.pdf)--&gt;
        &lt;prop key="mail.smtp.starttls.enable"&gt;true&lt;/prop&gt;  &lt;!--not indicated in JasperReports-Server-Install-Guide.pdf--&gt;
    &lt;/props&gt;
&lt;/property&gt;</pre>
<p>Restart your application, and that&#8217;s it. Two quick changes are all that are needed. You should be able to get reports emailed out to your users in under 5 minutes.</p>
<h2>Questions</h2>
<p>As noted in one of the email threads listed above, the value that you set for &#8221;<em>report.scheduler.mail.sender.from</em>&#8221; seems to get ignored. I suppose that&#8217;s probably a GMail security issue. They don&#8217;t allow you to use <strong>me@gmail.com</strong> to authenticate but then claim that the email came from <strong>you@gmail.com</strong>. That&#8217;s just speculation; maybe someone can comment and confirm. When you are using a corporate email server under your own control it should be possible to set that value to, for example, <strong>no-reply@mydomain.com</strong>.</p>
<h2>Improvements</h2>
<p>Perhaps JasperReports Server should be smart enough to figure out that authentication ought to be used based on the fact that you provide a password in js.quartz.properties. I&#8217;m not sure if that really would work or not. Ideally we should come up with a mechanism to set javaMailProperties in the properties file. Maybe mail.smtp.starttls.enable ought to be in the documentation. If you want to see any of these changes, then log a bug on the <a href="http://jasperforge.org/projects/jasperserver/tracker" target="_blank">tracker</a>.</p>
<h2>Error Reference</h2>
<p>If you don&#8217;t configure applicationContext-report-scheduling.xml correctly, then you will get an error. I have included the error that I got below in case it helps folks with the same error to track down the solution.</p>
<pre style="padding-left:30px;">2011-09-27 23:18:55,301 ERROR ReportExecutionJob,quartzScheduler_Worker-0:309 - The job error notification was not completed. An error occurred while sending it.
org.springframework.mail.MailSendException; nested exception details (1) are:
Failed message 1:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. bw6sm18434073qab.16
        at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
        at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
        at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:403)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:342)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:338)
        at com.jaspersoft.jasperserver.api.engine.scheduling.quartz.ReportExecutionJob.sendMailNotification(ReportExecutionJob.java:1264)
        at com.jaspersoft.jasperserver.api.engine.scheduling.quartz.ReportExecutionJob.executeAndSendReport(ReportExecutionJob.java:446)
        at com.jaspersoft.jasperserver.api.engine.scheduling.quartz.ReportExecutionJob.execute(ReportExecutionJob.java:232)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:195)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdahlman.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdahlman.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdahlman.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdahlman.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdahlman.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdahlman.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdahlman.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdahlman.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdahlman.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdahlman.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdahlman.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdahlman.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdahlman.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdahlman.wordpress.com/225/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=225&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdahlman.wordpress.com/2011/09/28/emailing-reports-from-jasperreports-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2d40af941f746115e07c340c0c0eadf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdahlman</media:title>
		</media:content>
	</item>
		<item>
		<title>Date Queries in MongoDB Reports</title>
		<link>http://mdahlman.wordpress.com/2011/09/22/date-queries-in-mongodb-reports/</link>
		<comments>http://mdahlman.wordpress.com/2011/09/22/date-queries-in-mongodb-reports/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 19:21:09 +0000</pubDate>
		<dc:creator>mdahlman</dc:creator>
				<category><![CDATA[JasperReports Server]]></category>
		<category><![CDATA[Dates]]></category>
		<category><![CDATA[JasperReports]]></category>
		<category><![CDATA[MongoDB]]></category>

		<guid isPermaLink="false">http://mdahlman.wordpress.com/?p=221</guid>
		<description><![CDATA[First Dates with MongoDB Background You need some software before you can make use of these ideas: MongoDB iReport or JasperReports Server Jaspersoft MongoDB connector You should probably start by reading my previous articles before reading this one: Simple MongoDB reporting More MongoDB reporting Date queries in MongoDB reports This idea actually applies to all [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=221&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>First Dates with MongoDB</h1>
<h4>Background</h4>
<p>You need some software before you can make use of these ideas:</p>
<ul>
<li><a href="http://www.10gen.com/" target="_blank">MongoDB</a></li>
<li><a href="http://jasperforge.org/" target="_blank">iReport or JasperReports Server</a></li>
<li><a href="http://jasperforge.org/projects/bigdatareportingfornosqlandhadoop" target="_blank">Jaspersoft MongoDB connector</a></li>
</ul>
<p>You should probably start by reading my previous articles before reading this one:</p>
<ul>
<li><a title="Simple Reporting on MongoDB" href="http://mdahlman.wordpress.com/2011/09/02/simple-reporting-on-mongodb/">Simple MongoDB reporting</a></li>
<li><a title="Cool Reporting on MongoDB" href="http://mdahlman.wordpress.com/2011/09/02/cool-reporting-on-mongodb/">More MongoDB reporting</a></li>
</ul>
<h4>Date queries in MongoDB reports</h4>
<p>This idea actually applies to all types of Java objects, not just Dates. But most data types likes Strings and simple numbers get converted magically to what you probably want. Dates are probably the most common object that is too complex to be handled by this automatic conversion process.</p>
<p>Let&#8217;s take what is perhaps the simplest possible example. I want a query that will return only the documents for which a specified field, status_date, is later than some date. It&#8217;s a simple JSON-style query in the MongoDbQuery language of the Jaspersoft MongoDB connector:</p>
<pre>{
  'collectionName' : 'active_users',
  'findQuery' : { 'status_date' : {'$gte':$P{StartDate}} }
}</pre>
<p>It&#8217;s easy enough to understand even if it&#8217;s a bit verbose with curly braces in curly braces in curly braces. My report has a java.util.Date parameter which gets passed into the query. In earlier versions of the connector—as pointed out by one astute reader—there was no way to do this. Attempts to embed syntax like this into the query resulted in JSON parsing exceptions: { &#8216;status_date&#8217; : {&#8216;$gte&#8217;: new ISODate(’2011-09-15T00:00:00.000Z’)&#8217; } }</p>
<h4>Sample Data</h4>
<p>In case you&#8217;re relatively new to MongoDB, like me, it can smooth your path to testing to have a sample data set and sample report that are known to work well together. You can insert a these few documents on the MongoDB shell and then use the sample report listed below to get something that ought to work with minimal effort. Then you&#8217;re ready to start writing your own reports.</p>
<p>The first document is listed on many lines to make it easier to parse it by eye to see what&#8217;s in it. The second two documents are compressed onto a single line. The final &#8220;find&#8221; command is useful to confirm that the earlier inserts worked as expected.</p>
<pre>db.active_users.save(
{
"status_date" : ISODate("2011-09-01T07:48:58Z"),
"id" : "123",
"events" : {
"ACHIEVEMENT_Minor" : 1,
"ACHIEVEMENT_Major" : 1,
"ACHIEVEMENT_Ultimate" : 0
},
"install_source" : {
"detail" : "campaign - Free iPad",
"vt1" : "email",
"vt2" : "facebook"
},
"rev" : {
"net_total" : 700
}
}
)
db.active_users.save({"status_date" : ISODate("2011-09-09T22:17:27Z"),"id" : "456","events" : {"ACHIEVEMENT_Minor" : 1,},"install_source" : {"detail" : "campaign - Free iPad","vt1" : "email","vt2" : "facebook"},"rev" : {"net_total" : 450}})
d = new Date()
db.active_users.save({"status_date" : d,"id" : "789","events" : {"ACHIEVEMENT_Minor" : 1,},"install_source" : {"detail" : "campaign - Like us on Facebook","vt1" : "email","vt2" : "facebook"},"rev" : {"net_total" : 222}})
db.active_users.find()</pre>
<p><a title="Download sample report" href="https://docs.google.com/leaf?id=0B9apCz8LCsSnZDkzMWJiYWItYjc2YS00NzNhLWIyMTgtOGU3ZDY3MmMxNWYz&amp;hl=en_US" target="_blank">Sample Report</a></p>
<h4>Reminder</h4>
<p>The idea for this article (and for this new feature to the Jaspersoft MongoDB Connector) came from a reader comment. Please let me know when you run into questions, but for general questions about the MongoDB connector please post to the <a href="http://jasperforge.org/plugins/espforum/browse.php?group_id=112&amp;forumid=102" target="_blank">JasperReports Server forum</a> instead of posting a comment here. That&#8217;s a better location for asking and answering questions.</p>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdahlman.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdahlman.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdahlman.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdahlman.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdahlman.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdahlman.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdahlman.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdahlman.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdahlman.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdahlman.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdahlman.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdahlman.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdahlman.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdahlman.wordpress.com/221/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=221&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdahlman.wordpress.com/2011/09/22/date-queries-in-mongodb-reports/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2d40af941f746115e07c340c0c0eadf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdahlman</media:title>
		</media:content>
	</item>
		<item>
		<title>Cool Reporting on MongoDB</title>
		<link>http://mdahlman.wordpress.com/2011/09/02/cool-reporting-on-mongodb/</link>
		<comments>http://mdahlman.wordpress.com/2011/09/02/cool-reporting-on-mongodb/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 23:24:17 +0000</pubDate>
		<dc:creator>mdahlman</dc:creator>
				<category><![CDATA[iReport]]></category>
		<category><![CDATA[JasperReports]]></category>
		<category><![CDATA[Big Data]]></category>
		<category><![CDATA[MongoDB]]></category>

		<guid isPermaLink="false">http://mdahlman.wordpress.com/?p=208</guid>
		<description><![CDATA[Interesting Reporting on MongoDB Before you can do interesting reporting, you need to do simple reporting. You have to walk before you can run. Please start with the article Simple Reporting on MongoDB. That article explains where to get the MongoDB connector for iReport and JasperReports Server. When you have completed that, then read this article [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=208&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>Interesting Reporting on MongoDB</h1>
<p>Before you can do <em>interesting</em> reporting, you need to do simple reporting. You have to walk before you can run. Please start with the article <a href="http://mdahlman.wordpress.com/2011/09/02/simple-reporting-on-mongodb/">Simple Reporting on MongoDB</a>. That article explains where to get the MongoDB connector for iReport and JasperReports Server. When you have completed that, then read this article more advanced and more interesting reporting techniques.</p>
<h2>Simple Filters</h2>
<p>Let&#8217;s start with a simple hard-coded filter.</p>
<pre>{
  'collectionName' : 'accounts',
  'findQuery' : { 'name' : 'M &amp; Y Takemura Communications, Ltd'  }
}</pre>
<p>Filtering on a single value makes sense when that value is a complex document. There could be lots of information in that document to create a report. To make this truly interesting though, we need this query to be parameterized. So our first truly realistic sample query is the following:</p>
<pre>{
  'collectionName' : 'accounts',
  'findQuery' : { 'name' : '$P{CUSTOMER_NAME}'  }
}</pre>
<h2>Field Selection and Dot Notation</h2>
<p>Documents in MongoDB can be big. The queries above will return every field in the document. When this is not appropriate, it&#8217;s easy to create a query to return only the fields that you need.</p>
<p>Imagine the case where we have lots of users and we want a report showing which users had logged certain events. There could be lots and lots of events, but there&#8217;s only one particular event that I care about in this report. Rather than return the complete document for each user we instead return only the fields we care about.</p>
<pre>{
  'collectionName': 'active_users',
  'findFields' : { 'id':1, 'date':1, 'events.ACHIEVEMENT_Minor':1 }
}</pre>
<p>Enter this query into iReport&#8217;s query field, and then iReport is able to execute the query and return only the fields we&#8217;re interested in.</p>
<div id="attachment_209" class="wp-caption aligncenter" style="width: 152px"><a href="http://mdahlman.files.wordpress.com/2011/09/ireportqueryeditor.png"><img class="size-thumbnail wp-image-209" title="iReport Query Editor" src="http://mdahlman.files.wordpress.com/2011/09/ireportqueryeditor.png?w=142&#038;h=150" alt="iReport Query Editor" width="142" height="150" /></a><p class="wp-caption-text">The query editor makes it easy to test query syntax and to see query results</p></div>
<p>We could then combine this with a filter if we only want certain users returned.</p>
<pre>{
  'collectionName': 'active_users',
  'findFields' : { 'id':1, 'date':1, 'events.ACHIEVEMENT_Minor':1 },
  'findQuery' : { 'events.ACHIEVEMENT_Major' : 1 }
}</pre>
<h2>Joins!</h2>
<p>Joins? MongoDB doesn&#8217;t support joins. In general your data should be modeled so that you don&#8217;t need joins. If you need them, then perhaps you should be using a traditional RDBMS instead of MongoDB. But what about simple joins that are required in some types of reports? Subreports can be used in JasperReports to effectively join two collections together.</p>
<p>Imagine a collection that stores summary data by country and a collection that stores customers. I want to query the country collection to find my top countries for some time period and then query my customers collection to find the top customers in the those countries.</p>
<p>It&#8217;s simple in JasperReports. We create a query in the top level report to return the countries that I want. Then for each country I run a subreport that returns the customers from that country.  Here&#8217;s a simple version.</p>
<p>Parent query:</p>
<p>{ &#8216;collectionName&#8217;:'countries&#8217; }</p>
<p>The subreport query finds all accounts that are in that country and whose name starts with &#8220;B&#8221;. (It&#8217;s a silly example. Why would I want companies that start with &#8220;B&#8221;? But I wanted to show the syntax for Regular Expressions in queries.)</p>
<pre>{
	'collectionName':'accounts',
	'findFields':{'name':1,'phone_office':1,'billing_address_city':1,'billing_address_street':1,'billing_address_country':1},
	'sort':{'billing_address_city':1,'name':1},
	'findQueryRegEx':{'billing_address_country':'/$P{AccountCountry}/','name':'/^B /'}
}</pre>
<p>The above set of two queries gives me exactly what I want. It has the drawback of running &#8220;N + 1&#8243; queries. By that I mean that runs the subreport query once for each country. In some cases that could be bad. In these cases it could be even better to use the &#8220;IN&#8221; syntax so that I only need to run each query once.</p>
<p>I can use the same parent query. But rather than pass the countries one at a time to the subreport we can pass the complete list of countries as a Collection. Then the subreport can run a single query to get all of the accounts that I want. The working reports are attached below, but here&#8217;s the query to show the idea.</p>
<pre>{
	'collectionName':'accounts',
	'findFields':{'name':1,'phone_office':1,'billing_address_city':1,'billing_address_street':1,'billing_address_country':1},
	'sort':{'billing_address_country':1,'billing_address_city':1,'name':1},
	'findQuery':{ 'billing_address_country': {'$in':$P{AccountCountryCollection}} }
}</pre>
<h2>Summary</h2>
<p>MongoDB has more flexibility than most of today&#8217;s Big Data databases in its query syntax. That&#8217;s one thing that made it attractive as a reporting target for Jaspersoft. Hopefully this article gives a good idea of some of the possibilities that exist for reporting directly against a MongoDB data source.</p>
<p><a href="https://docs.google.com/viewer?a=v&amp;pid=explorer&amp;chrome=true&amp;srcid=0B9apCz8LCsSnMDQzYWI4MGMtYjEzNy00NDRhLTk2YjAtZTYxOTc0Mjc5OTE3&amp;hl=en_US" target="_blank">Sample reports using all of the above queries are available here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdahlman.wordpress.com/208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdahlman.wordpress.com/208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdahlman.wordpress.com/208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdahlman.wordpress.com/208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdahlman.wordpress.com/208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdahlman.wordpress.com/208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdahlman.wordpress.com/208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdahlman.wordpress.com/208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdahlman.wordpress.com/208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdahlman.wordpress.com/208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdahlman.wordpress.com/208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdahlman.wordpress.com/208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdahlman.wordpress.com/208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdahlman.wordpress.com/208/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=208&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdahlman.wordpress.com/2011/09/02/cool-reporting-on-mongodb/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2d40af941f746115e07c340c0c0eadf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdahlman</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/09/ireportqueryeditor.png?w=142" medium="image">
			<media:title type="html">iReport Query Editor</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple Reporting on MongoDB</title>
		<link>http://mdahlman.wordpress.com/2011/09/02/simple-reporting-on-mongodb/</link>
		<comments>http://mdahlman.wordpress.com/2011/09/02/simple-reporting-on-mongodb/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 23:22:37 +0000</pubDate>
		<dc:creator>mdahlman</dc:creator>
				<category><![CDATA[iReport]]></category>
		<category><![CDATA[JasperReports]]></category>
		<category><![CDATA[Big Data]]></category>
		<category><![CDATA[MongoDB]]></category>

		<guid isPermaLink="false">http://mdahlman.wordpress.com/?p=204</guid>
		<description><![CDATA[Big Data (MongoDB) with Reporting and Analytics Big Data databases are being adopted with rapidly increasing frequency. Opinions on NoSQL and Hadoop are being generated nearly as quickly as the data going into these databases. This article doesn&#8217;t contribute a viewpoint on any of that. I&#8217;m assuming that you already have data in MongoDB. You [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=204&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>Big Data (MongoDB) with Reporting and Analytics</h1>
<p>Big Data databases are being adopted with rapidly increasing frequency. Opinions on NoSQL and Hadoop are being generated nearly as quickly as the data going into these databases. This article doesn&#8217;t contribute a viewpoint on any of that.</p>
<p>I&#8217;m assuming that you already have data in MongoDB. You probably had a great reason for using MongoDB, and reporting on that data was probably not a top consideration. But now that the data is there, <em>what options exist for reporting and analysis</em>?</p>
<h2>Option 1: Dump it to a traditional RDBMS</h2>
<p>This technique is not exciting or sexy, but it&#8217;s ideal in many cases. Create ETL jobs to get the data out of MongoDB and into PostgreSQL, MySQL, Vertica, Teradata, Netezza, or whatever good old relational database you prefer. From there you have a plethora of tools at your disposal for slicing and dicing the data or for designing reports. Having acknowledged the utility of this technique for many requirements&#8230; I will proceed to ignore it.</p>
<h2>Option 2: Connect your BI solution directly to MongoDB</h2>
<p>You&#8217;ve got a lot of data in MongoDB. That&#8217;s why it&#8217;s called &#8220;Mongo&#8221; after all; that&#8217;s why the whole field is referred to as &#8220;Big Data&#8221;. So there are plenty of downsides to pulling all of the data back out and putting it somewhere else.</p>
<p>If requirements are simple enough, you can code it up yourself. Write some PHP scripts to create reports. The MongoDB PHP driver makes it easy enough to connect and get data.</p>
<p>If your requirements are a bit more extensive, then the build-it-yourself method has important drawbacks. A simple tabular report can be created quickly. But what happens when your users like it and then say, &#8220;Great, now can I get the same thing in PDF?&#8221; Or soon they&#8217;ll ask for charting. Or you&#8217;ll find you need to implement security on who can see which reports. There are plenty of benefits to using standard reporting and analysis tools for solving problems like these. The biggest barrier to this is that most Big Data databases (including MongoDB) are not accessible via standard methods like JDBC. That means traditional Business Intelligence (BI) tools can&#8217;t get directly to Big Data data sources.</p>
<p>Jaspersoft has addressed this problem for a number of Big Data databases including MongoDB. It offers connectors to a variety of datasources. Read the <a href="http://www.jaspersoft.com/bigdata" target="_blank">marketing fluff</a>. See the <a href="http://jasperforge.org/projects/bigdatareportingfornosqlandhadoop" target="_blank">connectors</a>.</p>
<p>OK, that&#8217;s enough background. The rest of the article below this point is a hands-on guide to creating reports in iReport against a MongoDB data source.</p>
<h2>Prerequisites</h2>
<ul>
<li><a href="http://jasperforge.org/projects/ireport" target="_blank">iReport</a> installed</li>
<li><a href="http://www.10gen.com/" target="_blank">MongoDB</a> installed</li>
<li><a href="http://jasperforge.org/projects/bigdatareportingfornosqlandhadoop" target="_blank">MongoDB connector</a> downloaded</li>
</ul>
<h2>First Report</h2>
<p>The installation of the connector is simple. It&#8217;s covered in the documentation included with the connector. After installing the plugin and restarting iReport, you&#8217;re ready to define your connection. The connection needs to know the host, port, and database that you&#8217;ll be connecting to. This is expressed in a JDBC-like url shown below.</p>
<div id="attachment_210" class="wp-caption aligncenter" style="width: 447px"><a href="http://mdahlman.files.wordpress.com/2011/09/mongodbconnection.png"><img class="size-full wp-image-210" title="MongoDB Connection" src="http://mdahlman.files.wordpress.com/2011/09/mongodbconnection.png" alt="" width="437" height="206" /></a><p class="wp-caption-text">Simple JDBC-like connection URL</p></div>
<p>Now you&#8217;re ready to create your first report. Choose File -&gt; New to launch the report wizard. Choose the Coffee template (well&#8230; choose any template besides the blank ones). Then click &#8220;Launch Report Wizard&#8221;. When you reach the step of the wizard that requires a Query in the MongoDbQuery language you have reached your first real test. MongoDB doesn&#8217;t have a query language. What should you enter here?</p>
<p>For your first report, use the simplest possible query:</p>
<p>{ &#8216;collectionName&#8217; : &#8216;myCollection&#8217; }</p>
<div id="attachment_211" class="wp-caption aligncenter" style="width: 310px"><a href="http://mdahlman.files.wordpress.com/2011/09/simple-mongodb-query.png"><img class="size-medium wp-image-211" title="Simple MongoDB Query" src="http://mdahlman.files.wordpress.com/2011/09/simple-mongodb-query.png?w=300&#038;h=181" alt="" width="300" height="181" /></a><p class="wp-caption-text">Simple MongoDB Query</p></div>
<p>Click &#8220;Next&#8221;. iReport then runs your query. It connects to MongoDB, reads each document from the specified collection, finds each field and its datatype, and presents these fields to you. Documents in MongoDB do not necessarily have the same fields. The list of fields presented is the union of all fields found for each document.</p>
<div id="attachment_212" class="wp-caption aligncenter" style="width: 310px"><a href="http://mdahlman.files.wordpress.com/2011/09/list-of-fields.png"><img class="size-medium wp-image-212" title="List of Fields" src="http://mdahlman.files.wordpress.com/2011/09/list-of-fields.png?w=300&#038;h=195" alt="" width="300" height="195" /></a><p class="wp-caption-text">The available fields are automatically discovered. Choose the ones you want.</p></div>
<p>If you&#8217;re dealing with a very large collection, then you perhaps don&#8217;t want to wait for iReport to read through every record. You can tell it to read only the first 100 documents like this:</p>
<p>{ &#8216;collectionName&#8217; : &#8216;myCollection&#8217;,<br />
&#8216;rowsToProcess&#8217; : 100 }</p>
<p>Note: the field &#8216;rowsToProcess&#8217; is read only by iReport&#8217;s Fields Provider. That means when you run the report it will still process all documents from the collection.</p>
<p>Finish the report wizard and then preview the report. You have just created a report against a MongoDB database without having to first dump the data into MySQL. Woo hoo!</p>
<div id="attachment_213" class="wp-caption aligncenter" style="width: 310px"><a href="http://mdahlman.files.wordpress.com/2011/09/first-report-working.png"><img class="size-medium wp-image-213" title="First Report working" src="http://mdahlman.files.wordpress.com/2011/09/first-report-working.png?w=300&#038;h=148" alt="" width="300" height="148" /></a><p class="wp-caption-text">Your first report showing data from MongoDB should be working now!</p></div>
<h2>Useful Reports</h2>
<p>Creating the first report was easy. But let&#8217;s face it: a report that includes every record from a collection is not really interesting. It&#8217;s bound to be a hu<strong>mongo</strong>us report. Let&#8217;s add filters to get genuinely useful reports.</p>
<p>Continue to my article <a href="http://mdahlman.wordpress.com/2011/09/02/cool-reporting-on-mongodb/">Cool Reporting on MongoDB</a> for some advanced techniques.</p>
<p>You can <a href="https://docs.google.com/viewer?a=v&amp;pid=explorer&amp;chrome=true&amp;srcid=0B9apCz8LCsSnMDQzYWI4MGMtYjEzNy00NDRhLTk2YjAtZTYxOTc0Mjc5OTE3&amp;hl=en_US" target="_blank">download the report</a> from this article (and all of the reports from the second article) <a href="https://docs.google.com/viewer?a=v&amp;pid=explorer&amp;chrome=true&amp;srcid=0B9apCz8LCsSnMDQzYWI4MGMtYjEzNy00NDRhLTk2YjAtZTYxOTc0Mjc5OTE3&amp;hl=en_US" target="_blank">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdahlman.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdahlman.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdahlman.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdahlman.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdahlman.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdahlman.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdahlman.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdahlman.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdahlman.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdahlman.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdahlman.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdahlman.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdahlman.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdahlman.wordpress.com/204/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=204&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdahlman.wordpress.com/2011/09/02/simple-reporting-on-mongodb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2d40af941f746115e07c340c0c0eadf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdahlman</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/09/mongodbconnection.png" medium="image">
			<media:title type="html">MongoDB Connection</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/09/simple-mongodb-query.png?w=300" medium="image">
			<media:title type="html">Simple MongoDB Query</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/09/list-of-fields.png?w=300" medium="image">
			<media:title type="html">List of Fields</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/09/first-report-working.png?w=300" medium="image">
			<media:title type="html">First Report working</media:title>
		</media:content>
	</item>
		<item>
		<title>JasperReports Server and Hibernate HQL</title>
		<link>http://mdahlman.wordpress.com/2011/07/05/jasperreports-server-and-hibernate-hql/</link>
		<comments>http://mdahlman.wordpress.com/2011/07/05/jasperreports-server-and-hibernate-hql/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 19:06:08 +0000</pubDate>
		<dc:creator>mdahlman</dc:creator>
				<category><![CDATA[iReport]]></category>
		<category><![CDATA[JasperReports]]></category>
		<category><![CDATA[JasperReports Server]]></category>
		<category><![CDATA[JasperServer]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[HQL]]></category>

		<guid isPermaLink="false">http://mdahlman.wordpress.com/?p=181</guid>
		<description><![CDATA[Ad Hoc Reports with Hibernate HQL queries Recently someone asked my if it was possible to create Ad Hoc reports in JasperReports Server (JRS) using a Hibernate data source. I was quick to say yes. I have heard of projects at Jaspersoft where that was done, and in general one of the great strengths of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=181&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>Ad Hoc Reports with Hibernate HQL queries</h1>
<p>Recently someone asked my if it was possible to create Ad Hoc reports in JasperReports Server (JRS) using a <a href="http://www.hibernate.org/" target="_blank">Hibernate</a> data source. I was quick to say yes. I have heard of projects at Jaspersoft where that was done, and in general one of the great strengths of Jaspersoft is that custom data sources are easy to implement. (Sidenote: that flexibility is one reason that lots of <a href="http://jasperforge.org/projects/bigdatareportingfornosqlandhadoop" target="_blank">big data</a> datasources were so easy to implement.) But when I investigated the precise steps around how to setup Ad Hoc reporting with Hibernate, I found that things were not well documented. I will log bugs and enhancement requests to create new samples and document existing samples better. But for an immediate solution I thought it would be useful to document this for future reference.</p>
<h1>JasperReports and Hibernate</h1>
<p>Can JasperReports (JR) work with Hibernate? This one is almost too easy. Yes. Not only can it work with hibernate, but there&#8217;s even a sample that ships with JR showing it in action. Here&#8217;s how to get it:</p>
<ul>
<li>Head to <strong>JasperForge.org</strong>, then to the <strong>JR project</strong>, then click &#8220;<strong>Download JasperReports Now</strong>&#8220;, then <strong>sign-in</strong> or join jasperforge, then get <strong>re-directed</strong> to the downloads hosted on sourceforge.net, then <strong>choose the 4.0.1 JR release</strong> to correspond to the current [as of 1 July 2011] JasperReports Server 4.1 release (not a typo: JRS 4.1 uses JR 4.0.1).</li>
<li>Or <a title="JasperReports downloads" href="http://sourceforge.net/projects/jasperreports/files/jasperreports/" target="_blank">click here</a>.</li>
</ul>
<p>Then choose <code>jasperreports-4.0.1-project.tar.gz</code> or <code>jasperreports-4.0.1-project.zip</code>.</p>
<div id="attachment_185" class="wp-caption aligncenter" style="width: 160px"><a href="http://mdahlman.files.wordpress.com/2011/07/jasperreports-project.png"><img class="size-thumbnail wp-image-185" title="jasperreports-project downloads" src="http://mdahlman.files.wordpress.com/2011/07/jasperreports-project.png?w=150&#038;h=103" alt="jasperreports-project downloads" width="150" height="103" /></a><p class="wp-caption-text">Download either of the &quot;project&quot; files to get the JR binaries and source and, most importantly, the samples.</p></div>
<p>After unzipping the archive you&#8217;ll find the sample database and the sample hibernate example:</p>
<pre>.../jasperreports-4.0.1/demo/hsqldb
.../jasperreports-4.0.1/demo/samples/hibernate</pre>
<p>To start the sample database you can simply run &#8220;ant&#8221; in the directory &#8220;hsqldb&#8221;.<br />
To execute the sample reports you can simply run &#8220;ant&#8221; in the directory &#8220;hibernate&#8221;. You&#8217;ll to open a command line to do this. It will look something like this:</p>
<div id="attachment_186" class="wp-caption aligncenter" style="width: 160px"><a href="http://mdahlman.files.wordpress.com/2011/07/launch-hypersonic-sample-database.png"><img class="size-thumbnail wp-image-186" title="Launch Hypersonic Sample Database" src="http://mdahlman.files.wordpress.com/2011/07/launch-hypersonic-sample-database.png?w=150&#038;h=45" alt="Launch Hypersonic Sample Database" width="150" height="45" /></a><p class="wp-caption-text">Run &quot;ant&quot; to launch the sample database.</p></div>
<p>Then you&#8217;ll find the generated reports here:</p>
<pre>.../jasperreports-4.0.1/demo/samples/hibernate/build/reports</pre>
<p>There are lots more details to investigate. You can see the sample HQL queries in the .jrxml files. You can see the hibernate cfg files and hbm files, etc. But to just get it working, it takes only those two commands. Here&#8217;s the sample HQL query from one of the provided reports:</p>
<pre>select address as address, document.id as documentId, document.total as documentTotal
from Address as address join address.documents as document
where city not in ($P{CityFilter})
order by address.city, address.lastName, address.firstName, address.id</pre>
<h1>iReport and Hibernate</h1>
<p>Having seen that JasperReports works with Hibernate, the next practical step is to start creating your own reports. The best place to do this is in <a href="http://jasperforge.org/projects/ireport" target="_blank">iReport</a>. (Coming soon: the best place to do this will be <a href="http://jasperforge.org/projects/jaspersoftstudio" target="_blank">Jaspersoft Studio</a>!)</p>
<p>Open the iReport Preferences window to the tab &#8220;Query Executers&#8221; and you can see that &#8220;hql&#8221; is supported as a query language by default. This is a nice start. But to be able to execute a query you need your Hibernate classes and configuration files in <strong>iReport&#8217;s classpath</strong>. To set the classpath go to the menu Tools → Options&#8230; (on Mac: ⌘,). I wanted to open and execute the reports that came with the JR sample shown above, so I went to the tab &#8220;Classpath&#8221; and added this directory:</p>
<p>&#8230;/jasperreports-4.0.1/demo/samples/hibernate/build/classes</p>
<p>Next define a Hibernate connection, and you&#8217;re all set. The connection has only a single parameter to set; for this sample be sure to use Hibernate Annotations. Click the button &#8220;Test&#8221; to make sure all is well. Make a puzzled expression when it prompts you for a password and just click OK (isn&#8217;t the login info included in the file hibernate.cfg.xml?). Make a questioning noise when it prompts you for a password again (Huh? What possible reason could it have for prompting twice?) and click OK again without entering a password. Smile when the connection is successful.</p>
<div id="attachment_187" class="wp-caption aligncenter" style="width: 160px"><a href="http://mdahlman.files.wordpress.com/2011/07/hibernate-connection.png"><img class="size-thumbnail wp-image-187" title="Hibernate Connection in iReport" src="http://mdahlman.files.wordpress.com/2011/07/hibernate-connection.png?w=150&#038;h=120" alt="Hibernate Connection in iReport" width="150" height="120" /></a><p class="wp-caption-text">A Hibernate connection has only one parameter.</p></div>
<p>Now you can open and run the reports from the hibernate sample project. I started with &#8220;HibernateQueryReport.jrxml&#8221;. As a convenience to the user, it would be nice if the three parameters in this report had default values. They don&#8217;t. As a convenience to the reader, I have suggested reasonable values below.</p>
<div id="attachment_188" class="wp-caption aligncenter" style="width: 160px"><a href="http://mdahlman.files.wordpress.com/2011/07/hql-param1-reporttitle.png"><img class="size-thumbnail wp-image-188 " title="HQL Report Parameter ReportTitle" src="http://mdahlman.files.wordpress.com/2011/07/hql-param1-reporttitle.png?w=150&#038;h=74" alt="HQL Report Parameter ReportTitle" width="150" height="74" /></a><p class="wp-caption-text">ReportTitle: My Hibernate Report</p></div>
<div id="attachment_189" class="wp-caption aligncenter" style="width: 160px"><a href="http://mdahlman.files.wordpress.com/2011/07/hql-param2-cityfilter.png"><img class="size-thumbnail wp-image-189 " title="HQL Report Parameter CityFilter" src="http://mdahlman.files.wordpress.com/2011/07/hql-param2-cityfilter.png?w=150&#038;h=74" alt="HQL Report Parameter CityFilter" width="150" height="74" /></a><p class="wp-caption-text">CityFilter: Lexington,Indianapolis,Jackson</p></div>
<div id="attachment_190" class="wp-caption aligncenter" style="width: 160px"><a href="http://mdahlman.files.wordpress.com/2011/07/hql-param3-orderclause.png"><img class="size-thumbnail wp-image-190" title="HQL Report Parameter OrderClause" src="http://mdahlman.files.wordpress.com/2011/07/hql-param3-orderclause.png?w=150&#038;h=74" alt="HQL Report Parameter OrderClause" width="150" height="74" /></a><p class="wp-caption-text">OrderClause: id</p></div>
<p>Make a derisive snort when iReport prompts again for the password. Just click OK. Roll your eyes when iReport prompts yet again for a password. (Surely someone should log a bug in the <a href="http://jasperforge.org/projects/ireport/tracker" target="_blank">iReport tracker</a> for this. I haven&#8217;t.) And voilà: a report using HQL has been automatically translated into SQL and executed against the sample Hypersonic database. This is great! Those JR guys didn&#8217;t shirk their duties either: it&#8217;s not just a trivial query populating a tabular report. This is a reasonably interesting HQL query including parameters, and the report even has a subreport. Here is a snippet from the generated report using the parameter values indicated above:</p>
<div id="attachment_191" class="wp-caption aligncenter" style="width: 160px"><a href="http://mdahlman.files.wordpress.com/2011/07/hibernatequeryreport.png"><img class="size-thumbnail wp-image-191" title="HibernateQueryReport" src="http://mdahlman.files.wordpress.com/2011/07/hibernatequeryreport.png?w=150&#038;h=102" alt="HibernateQueryReport" width="150" height="102" /></a><p class="wp-caption-text">Preview of the generated HQL report</p></div>
<h1>JasperReports Server and Hibernate</h1>
<p>Now that we can build reports, let&#8217;s deploy them.</p>
<h3>Deploy the Hibernate Data Source</h3>
<p>JasperReports Server (JRS) does not ship with built-in support for Hibernate like it does for JDBC SQL data sources. Creating a custom data source isn&#8217;t too hard, but it&#8217;s not trivial either. Fortunately, we don&#8217;t have to create the datasource ourselves. JRS ships with sample custom data sources to show you the way.</p>
<p>The precise location of the samples folder will depend on where you installed JRS, of course, but in my case the folder is here:</p>
<pre>.../jasperreports-server-4.1-bin/samples/customDataSource</pre>
<p>So how do you install these sample data sources? Fortunately, there&#8217;s a readme.txt with instructions. Unfortunately, it hasn&#8217;t been turned over to the documentation team for proofreading (sorry, Bob). It reads &#8220;JasperServer&#8221; where it should indicate &#8220;JasperReports Server&#8221;. Jaspersoft changes product names pretty often, so this is forgivable in a readme that was written long ago. It has things like &#8220;you will have Ant installed already&#8221;. That future tense is awkward. &#8220;Ant may be run with the following command&#8221; Passive voice would have been disliked by a doc writer (he he). &#8220;The example reports for this data source read a web page &#8230;&#8221; Subject verb agreement. Etc.</p>
<p>But ignoring style considerations, the readme file contains quite a bit of useful information which can be distilled from 76 lines into two critical steps for us in our task of enabling Hibernate reports:</p>
<ol>
<li>Edit build.xml to set the webAppDir property</li>
<li>Run ant with no arguments</li>
</ol>
<p>Edit build.xml to set the webAppDir property<br />
original: <span style="color:#008000;"><code>&lt;property name="webAppDir" value="c:/download/apache-tomcat-5.5.12/webapps/jasperserver-pro"/&gt;</code></span><br />
modified: <span style="color:#008000;"><code>&lt;property name="webAppDir" value="/usr/local/tomcat/webapps/jasperserver-pro"/&gt;</code></span><br />
Your modified version will be slightly different. Be sure that it points to the location where JRS is deployed.</p>
<p>This copies 20 files into your existing JRS deployment. Since we only care about the Hibernate at the moment, let&#8217;s look at the 7 files relevant to this data source. (The web scraper sample is pretty cool. Maybe I&#8217;ll write about that another time.) These are the files:</p>
<pre>WEB-INF/applicationContext-hibernateDS.xml
bundles/hibernateds.properties
classes/hibernate.cfg.xml
classes/example/cds/BasicHib.class
classes/example/cds/basichib.hbm.xml
classes/example/cds/HibernateDataSourceService.class
classes/example/cds/HibernateSessionFactoryFinder.class</pre>
<p>The applicationContext file makes sense if you&#8217;re familiar with Spring. If you aren&#8217;t, don&#8217;t worry. It just defines the bean hibernateDataSource, and you don&#8217;t need to worry about it. The properties file defines some text strings that show up in the JRS GUI.</p>
<p>The files in the directory &#8220;classes&#8221; are the interesting ones. These define our Hibernate data model. Presumably you have your own version of these or you wouldn&#8217;t be reading this article. But let&#8217;s test with the sample version before you swap in your own version of these.</p>
<h3>Create a Hibernate Connection (to the JRS Repository)</h3>
<p>Start JasperReports Server (JRS). Or restart it if it was running. It must restart to pickup the new configuration files. Create a new data source. Notice that you now have three new data source types.</p>
<div id="attachment_192" class="wp-caption aligncenter" style="width: 238px"><a href="http://mdahlman.files.wordpress.com/2011/07/customdatasources.png"><img class="size-full wp-image-192" title="Custom Datasources in JRS" src="http://mdahlman.files.wordpress.com/2011/07/customdatasources.png" alt="Custom Datasources in JRS" width="228" height="145" /></a><p class="wp-caption-text">Three new data sources in JRS</p></div>
<p>Now that the data source &#8220;Hibernate Data Source&#8221; has been installed, you can create a data source pointing to your particular Hibernate sources. First, we can test a special case. The JRS Repository uses Hibernate to connect the web application to the database. This is what makes it easy to support PostgreSQL, MySQL, Oracle, MS SQL Server, etc. We can use the JRS Repository as the data source for a report.</p>
<p>Right-click on a folder in the JRS repository view and add a data source. It should look like this:</p>
<div id="attachment_193" class="wp-caption aligncenter" style="width: 124px"><a href="http://mdahlman.files.wordpress.com/2011/07/hibernate-connection-jrs-to-jrs-repository.png"><img class="size-thumbnail wp-image-193" title="Hibernate Connection JRS to JRS Repository" src="http://mdahlman.files.wordpress.com/2011/07/hibernate-connection-jrs-to-jrs-repository.png?w=114&#038;h=150" alt="Hibernate Connection JRS to JRS Repository" width="114" height="150" /></a><p class="wp-caption-text">Special case: use the JRS Repository as the data source for a report</p></div>
<p>To use the JRS Repository as the data source you must set the Session Factory Bean Name value to &#8220;<strong>sessionFactory</strong>&#8220;. This is a standard bean that JRS uses for its repository connection. This allows the data source to get data out of the JRS repository, and we count on Hibernate to make sure that the query works regardless of whether the repository is on PostgreSQL or MySQL or Oracle or MS SQL Server etc.</p>
<p>Now that the data source is defined, next you need to upload the report. Find the file &#8220;hqlTest.jrxml&#8221; here:</p>
<pre>.../jasperreports-server-4.1-bin/samples/customDataSource/reports/hqlTest.jrxml</pre>
<p>Right-click a folder in the JRS Repository view and add a JasperReport. Use the data source that you just created (it&#8217;s called &#8220;Hibernate_Sample_Datasource&#8221; in the image above.)</p>
<h3>Run the Hibernate Report</h3>
<p>The sample report report hqlTest.jrxml isn&#8217;t a beautiful one. It&#8217;s unsorted, and it&#8217;s not particularly well formated. But it&#8217;s running HQL to get its data. Therefore it&#8217;s pretty cool. It could have quite a few practical uses. We could use this to get an interesting list of reports from the repository, and we could add a drill-down link to launch each report. The possibilities are very interesting.</p>
<div id="attachment_194" class="wp-caption aligncenter" style="width: 160px"><a href="http://mdahlman.files.wordpress.com/2011/07/hibernate-repository-report.png"><img class="size-thumbnail wp-image-194" title="Hibernate Repository Report" src="http://mdahlman.files.wordpress.com/2011/07/hibernate-repository-report.png?w=150&#038;h=84" alt="Hibernate Repository Report" width="150" height="84" /></a><p class="wp-caption-text">Preview of the sample Hibernate Repository Report</p></div>
<p>But the task at hand—the reason you&#8217;ve labored through this lengthy article—is to run a query against your own Hibernate data model. Let&#8217;s move on.</p>
<h3>Create a Hibernate Connection (to an arbitrary Hibernate source)</h3>
<p>The file &#8220;hibernate.cfg.xml&#8221; that is currently deployed to your JRS defines a connection to a MySQL database called hibernatesample. To use this connection you could create the database &#8220;hibernatesample&#8221; and create a table like this:</p>
<pre>create table basichib ( EVENT_ID int, name varchar(10), value int );
insert into basichib values ( 1, 'nameA', 1 );
insert into basichib values ( 2, 'nameB', 42 );</pre>
<p>Creating a connection to this Hibernate data source is easier than one might expect. Indeed, it took me a few days to realize what I needed to do, since I thought there were probably quite a few steps needed. In fact, the only thing you need to do is create another Hibernate data source and NOT specify anything for the Session Factory Bean Name. It was great to figure this out&#8230; but it was also a bit anti-climactic. The data source definition looks like this (it should have no parameters set at all):</p>
<div id="attachment_195" class="wp-caption aligncenter" style="width: 124px"><a href="http://mdahlman.files.wordpress.com/2011/07/hibernate-connection-jrs-to-jr-sample.png"><img class="size-thumbnail wp-image-195" title="Hibernate Connection JRS to any Hibernate source" src="http://mdahlman.files.wordpress.com/2011/07/hibernate-connection-jrs-to-jr-sample.png?w=114&#038;h=150" alt="Hibernate Connection JRS to any Hibernate source" width="114" height="150" /></a><p class="wp-caption-text">General case: Connection to any Hibernate source</p></div>
<p>Then create a report with an HQL query like this:</p>
<pre>from example.cds.BasicHib h</pre>
<p>I created the requisite table and report. I found that I had a report which is dead boring from a practical standpoint&#8230; but I confess to being wildly excited as I first got data from my database without writing any SQL. On the other hand, you&#8217;ll be far more interested in connecting to your existing Hibernate data source. Fear not, the task is finally at home. Read on.</p>
<h3>Create a Hibernate Connection (to your own Hibernate source!)</h3>
<p>Creating and reporting on sample data lacks a certain level of gravitas. Let&#8217;s modify this connection to use your own database. We don&#8217;t need to make any connection to the data source defined in JRS. It doesn&#8217;t have any properties after all, so there&#8217;s nothing to modify. Instead you must copy the relevant Hibernate configuration files into the JRS classpath.</p>
<p>For my article I&#8217;ll use the Hypersonic database that ships with the JR samples which is referenced at the start of this article which is by default <em>inaccessible</em> to a JRS instance. I simply need to copy the hibernate configuration files from the JR sample to the deployed JRS to make it accessible.</p>
<p>from: <span style="color:#008000;"><code>.../jasperreports-4.0.1/demo/samples/hibernate/build/classes</code></span><br />
to: <span style="color:#008000;"><code>.../apache-tomcat-6.0.32/webapps/jasperserver-pro/WEB-INF/classes</code></span></p>
<p>In theory, all that&#8217;s left is to restart JRS and start deploying reports. Hooray! In fact, I ran into three problems with this idea. Bummer.</p>
<p>First, JRS didn&#8217;t have the Hypersonic database driver. This is pretty obvious requirement, so I wasn&#8217;t too surprised. I had to copy <strong>hsqldb-1.8.0-10.jar</strong> into WEB-INF/lib.</p>
<p>Second, it seems that JR had no need for the current session&#8230; but the data source in JRS needs this. This wasn&#8217;t quite as obvious. But the error message was quite clear. So it wasn&#8217;t hard to figure out what was needed. I had to add this additional property to hibernate.cfg.xml:</p>
<pre>&lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;</pre>
<p>After these two changes I was able to run the sample reports that came with the JR hibernate sample as canned reports. This was great, and now I&#8217;m nearly done. Next, I wanted to use these queries as Ad Hoc topics, so I copied the reports into the repository folder /adhoc/topics.  You can see them here:</p>
<div id="attachment_196" class="wp-caption aligncenter" style="width: 160px"><a href="http://mdahlman.files.wordpress.com/2011/07/hibernate-topics.png"><img class="size-thumbnail wp-image-196" title="Hibernate Topics" src="http://mdahlman.files.wordpress.com/2011/07/hibernate-topics.png?w=150&#038;h=54" alt="Hibernate Topics" width="150" height="54" /></a><p class="wp-caption-text">Closeup view of the Topics folder</p></div>
<p>The first topic—the one that was a copy of the report used the repository as a data source—worked immediately. I was able to create a new Ad Hoc report using the drag and drop Ad Hoc interface to get whatever I wanted.</p>
<p>My second test used the sample query listed above: &#8220;from example.cds.BasicHib h&#8221;. It worked perfectly as well.</p>
<p>My third test used the sample report that shipped with the JR hibernate sample. <strong>This one failed.</strong> It ran with no errors, but it didn&#8217;t return any data. This one was more confusing. In the end I realized that the problem is that the HQL query executer relies on the field description as important metadata. But the Ad Hoc engine assumes that the description is not important, so it ignores this information. Having found this crucial issue, the solution was straightforward. It&#8217;s a single line of code that needs to change. But for a JRS user like myself this is not a trivial demand. In the release of JasperReports Server 4.2 this change will be included, so HQL reports should work immediately as topics. Until that&#8217;s available, I have made the relevant re-compiled .jar file available. Just replace your existing version of ji-gyroscope-4.1.0.jar with the one attached to this article. Note: my second test worked, and you may find that your HQL reports work just fine as topics right out of the box too. Since the fix will be released quite soon, I won&#8217;t bother documenting the circumstances when the failure to pass in the field description matters and when it doesn&#8217;t.</p>
<p>After that final tweak I was all set. The report AddressesReport.jrxml was already working in the JRS repository as a canned report. Now it&#8217;s working as expected as a Topic. Hooray!</p>
<div id="attachment_197" class="wp-caption aligncenter" style="width: 310px"><a href="http://mdahlman.files.wordpress.com/2011/07/ad-hoc-editor-with-hibernate-source.png"><img class="size-medium wp-image-197" title="Ad Hoc Editor with Hibernate source" src="http://mdahlman.files.wordpress.com/2011/07/ad-hoc-editor-with-hibernate-source.png?w=300&#038;h=141" alt="Ad Hoc Editor with Hibernate source" width="300" height="141" /></a><p class="wp-caption-text">Ad Hoc Editor with sample hibernate source</p></div>
<h3>Summary</h3>
<p>This article is surprisingly long. That&#8217;s because there are samples in a couple of different places, and there are independent steps involved in getting HQL working in iReport and in JasperReports Server. I wanted to include  extensive details on each stage of the process for future reference. Good luck. Please let me know if you have questions about the setup.</p>
<h3>Appendix</h3>
<p>For the curious, here is a diff of the code change that was needed:</p>
<pre>--- gyroscope/src/main/java/com/jaspersoft/ji/adhoc/strategy/NewClassicDataStrategy.java        (revision 20735)
+++ gyroscope/src/main/java/com/jaspersoft/ji/adhoc/strategy/NewClassicDataStrategy.java        (working copy)
@@ -146,7 +146,7 @@
                                slField.setName(f.getName());
                                slField.setType(f.getType());
                                // pass description through because MDX queries need it
-                               if (lang != null &amp;&amp; lang.equalsIgnoreCase("mdx")) {
+                               if (lang != null &amp;&amp; (lang.equalsIgnoreCase("mdx") <span style="color:#008000;">|| lang.equalsIgnoreCase("hql")</span>)) {
                                        slField.setProperty(BaseField.PROP_JR_DESCRIPTION, f.getDescription());
                                }
                                // if other props possibly needed by QE are there (e.g. webscraper custom QE sample), pass them through</pre>
<p>You can see that the solution was to treat the HQL query executer just like we already treat the MDX query executer.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdahlman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdahlman.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdahlman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdahlman.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdahlman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdahlman.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdahlman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdahlman.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdahlman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdahlman.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdahlman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdahlman.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdahlman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdahlman.wordpress.com/181/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=181&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdahlman.wordpress.com/2011/07/05/jasperreports-server-and-hibernate-hql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2d40af941f746115e07c340c0c0eadf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdahlman</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/jasperreports-project.png?w=150" medium="image">
			<media:title type="html">jasperreports-project downloads</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/launch-hypersonic-sample-database.png?w=150" medium="image">
			<media:title type="html">Launch Hypersonic Sample Database</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/hibernate-connection.png?w=150" medium="image">
			<media:title type="html">Hibernate Connection in iReport</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/hql-param1-reporttitle.png?w=150" medium="image">
			<media:title type="html">HQL Report Parameter ReportTitle</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/hql-param2-cityfilter.png?w=150" medium="image">
			<media:title type="html">HQL Report Parameter CityFilter</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/hql-param3-orderclause.png?w=150" medium="image">
			<media:title type="html">HQL Report Parameter OrderClause</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/hibernatequeryreport.png?w=150" medium="image">
			<media:title type="html">HibernateQueryReport</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/customdatasources.png" medium="image">
			<media:title type="html">Custom Datasources in JRS</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/hibernate-connection-jrs-to-jrs-repository.png?w=114" medium="image">
			<media:title type="html">Hibernate Connection JRS to JRS Repository</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/hibernate-repository-report.png?w=150" medium="image">
			<media:title type="html">Hibernate Repository Report</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/hibernate-connection-jrs-to-jr-sample.png?w=114" medium="image">
			<media:title type="html">Hibernate Connection JRS to any Hibernate source</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/hibernate-topics.png?w=150" medium="image">
			<media:title type="html">Hibernate Topics</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/07/ad-hoc-editor-with-hibernate-source.png?w=300" medium="image">
			<media:title type="html">Ad Hoc Editor with Hibernate source</media:title>
		</media:content>
	</item>
		<item>
		<title>Useful Chart Customizers in JasperReports, iReport, and JasperReports Server</title>
		<link>http://mdahlman.wordpress.com/2011/04/17/chart-customizers-2/</link>
		<comments>http://mdahlman.wordpress.com/2011/04/17/chart-customizers-2/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 05:46:10 +0000</pubDate>
		<dc:creator>mdahlman</dc:creator>
				<category><![CDATA[iReport]]></category>
		<category><![CDATA[JasperReports]]></category>
		<category><![CDATA[JasperServer]]></category>
		<category><![CDATA[bar chart]]></category>
		<category><![CDATA[line chart]]></category>
		<category><![CDATA[pie chart]]></category>

		<guid isPermaLink="false">http://mdahlman.wordpress.com/?p=166</guid>
		<description><![CDATA[Useful Chart Customizers in JasperReports, iReport, and JasperReports Server In a previous article I described some reasons that chart customizers are useful and gave some links to useful resources. (I&#8217;ll call that article &#8220;Chart Customizers 1&#8243;.) If you are not already familiar with chart customizers, then please read Chart Customizers 1 before reading this one. If you are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=166&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1><span style="text-decoration:underline;">Useful</span> Chart Customizers in JasperReports, iReport, and JasperReports Server</h1>
<p>In a <a title="Chart Customizers in JasperReports, iReport, and JasperServer" href="http://mdahlman.wordpress.com/2010/08/18/chart-customizers-1/" target="_blank">previous article</a> I described some reasons that chart customizers are useful and gave some links to useful resources. (I&#8217;ll call that article &#8220;Chart Customizers 1&#8243;.) If you are not already familiar with chart customizers, then please read <a title="Chart Customizers in JasperReports, iReport, and JasperServer" href="http://mdahlman.wordpress.com/2010/08/18/chart-customizers-1/" target="_blank">Chart Customizers 1</a> before reading this one. If you are already familiar with them, then please skim <a title="Chart Customizers in JasperReports, iReport, and JasperServer" href="http://mdahlman.wordpress.com/2010/08/18/chart-customizers-1/" target="_blank">Chart Customizers 1</a> before reading this one.</p>
<h2>Overview</h2>
<p>&#8220;Chart Customizers 1&#8243; explained what chart customizers are and provided some code. It was useful for Java developers. It made all changes as hard-coded settings, so the samples were not immediately useful for anyone without further coding.</p>
<p>&#8220;Chart Customizers 2&#8243; (this article) provides a useful chart customizer and describes why it&#8217;s useful. You can immediately use it to solve some common problems (no coding required!).</p>
<p>If you just want to grab the chart customizer (binary and source) scroll all the way to the bottom.</p>
<h2>Using Additional Properties</h2>
<p>The concept of &#8220;properties&#8221; is a key idea that was missing in &#8220;Chart Customizers 1&#8243;. Using properties lets you set values in the report (in the .jrxml file) rather than hard-coding them as I did in my earlier sample. As one would hope (expect?) it&#8217;s simple. The chart customizer extends JRAbstractChartCustomizer. Therefore we can get the chart properties like this:</p>
<pre>JRPropertiesMap pm = jasperChart.getPropertiesMap();</pre>
<p>Then we can get a particular property like this:</p>
<pre>double upperMargin = (pm.getProperty("UpperMargin") == null) ? -1 : Double.parseDouble(pm.getProperty("UpperMargin"));</pre>
<p>This is the addition that made it possible for me to extend my chart customizers from being purely proof of concept samples to being useful chart customizers.</p>
<h2>Bar Chart Improvements</h2>
<h3>Integer Tick Marks</h3>
<p>Let&#8217;s start with some common requests for bar charts. Perhaps the most common request that I have seen is to prevent charts from showing non-integer values on value axis. Here&#8217;s an example. The number of times that a person shopped on a particular day is necessarily an integer. I don&#8217;t want to see &#8220;2.5&#8243; on the chart axis.</p>
<div id="attachment_167" class="wp-caption aligncenter" style="width: 210px"><a href="http://mdahlman.files.wordpress.com/2011/04/bar_shoppingdays_original.png"><img class="size-full wp-image-167" title="Bar Chart: Shopping Days" src="http://mdahlman.files.wordpress.com/2011/04/bar_shoppingdays_original.png" alt="" width="200" height="95" /></a><p class="wp-caption-text">The bars are completely accurate. Yet it&#039;s annoying to have the non-decimal value displayed.</p></div>
<p>How easy is it to tell JFreeChart to only use integers? Really easy! Hooray! It has a method called &#8220;createIntegerTickUnits()&#8221;. By the way, if you&#8217;re doing any significant work with JFreeChart then you should buy their <a href="http://www.jfree.org/jfreechart/devguide.html" target="_blank">Developer Guide</a>. It&#8217;s well done.</p>
<p>How easy is it tell iReport and JasperReports to use only this method? Impossible. That&#8217;s a big bummer.</p>
<p>Fortunately, we can amend that somewhat pessimistic answer like this:</p>
<p>How easy is it tell iReport and JasperReports to use only integers? Really easy if we use a chart customizer! Hooray! It&#8217;s a single line. Since we might not always want this applied, we can surround it by an if statement:</p>
<pre>if (useIntegerTickUnits) {
	valueAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
}</pre>
<p>Now if I add the property &#8220;IntegerTickUnits&#8221; with the value &#8220;true&#8221; to my bar chart I&#8217;ll only get integers.</p>
<div id="attachment_174" class="wp-caption aligncenter" style="width: 457px"><a href="http://mdahlman.files.wordpress.com/2011/04/setting_chart_property_ireport.png"><img class="size-full wp-image-174" title="Setting a Chart Property in iReport" src="http://mdahlman.files.wordpress.com/2011/04/setting_chart_property_ireport.png" alt="" width="447" height="212" /></a><p class="wp-caption-text">On the chart &quot;Shopping Days&quot; I set this property</p></div>
<p>If I set the value to &#8220;false&#8221; (or to anything besides &#8220;true&#8221;) or if I don&#8217;t set the property at all, then I get the standard behavior. If you&#8217;re new to JasperReports Server then refer to the <strong>Detailed Steps</strong> at the end of this article to exactly exactly what you need to do to apply this chart customizer.</p>
<div id="attachment_168" class="wp-caption aligncenter" style="width: 210px"><a href="http://mdahlman.files.wordpress.com/2011/04/bar_shoppingdays_better.png"><img class="size-full wp-image-168" title="Bar Chart: Shopping Days (Improved!)" src="http://mdahlman.files.wordpress.com/2011/04/bar_shoppingdays_better.png" alt="" width="200" height="95" /></a><p class="wp-caption-text">The bars look much better when aligned with tick marks (or exactly half way between them)</p></div>
<h3>Category Labels on multiple lines</h3>
<p>By default there is no way to tell the chart to use multiple lines to display a category label. Again it&#8217;s easy in JFreeChart, so it&#8217;s easy in a chart customizer. Now I just have to add the property &#8220;MaximumCategoryLabelLines&#8221; with the value &#8220;2&#8243; to my chart to get what I want.</p>
<div id="attachment_169" class="wp-caption alignnone" style="width: 250px"><a href="http://mdahlman.files.wordpress.com/2011/04/bar_eating_original.png"><img class="size-full wp-image-169" title="Bar Chart: Eating Competition" src="http://mdahlman.files.wordpress.com/2011/04/bar_eating_original.png" alt="" width="240" height="116" /></a><p class="wp-caption-text">Who is it that drank more ţuică?</p></div>
<div id="attachment_170" class="wp-caption alignnone" style="width: 250px"><a href="http://mdahlman.files.wordpress.com/2011/04/bar_eating_better.png"><img class="size-full wp-image-170" title="Bar Chart: Eating Competition Better" src="http://mdahlman.files.wordpress.com/2011/04/bar_eating_better.png" alt="" width="240" height="116" /></a><p class="wp-caption-text">Now its clear that I ate more hot dogs.</p></div>
<h3>Using more space for category labels</h3>
<p>JFreeChart is nice enough to truncate long labels for me. But it&#8217;s quite restrictive about how large it lets the labels be. A call to setMaximumCategoryLabelWidthRatio let&#8217;s me allot more space for the labels. Here&#8217;s an alternative solution to the previous problem where labels are cut off.</p>
<div id="attachment_169" class="wp-caption alignnone" style="width: 250px"><a href="http://mdahlman.files.wordpress.com/2011/04/bar_eating_original.png"><img class="size-full wp-image-169" title="Bar Chart: Eating Competition" src="http://mdahlman.files.wordpress.com/2011/04/bar_eating_original.png" alt="" width="240" height="116" /></a><p class="wp-caption-text">Who is it that ate more hot dogs?</p></div>
<div id="attachment_171" class="wp-caption alignnone" style="width: 250px"><a href="http://mdahlman.files.wordpress.com/2011/04/bar_eating_better2.png"><img class="size-full wp-image-171" title="Bar Chart: Eating Competition Alternative Better" src="http://mdahlman.files.wordpress.com/2011/04/bar_eating_better2.png" alt="" width="240" height="116" /></a><p class="wp-caption-text">Alternative solution increases the width allowed for the labels</p></div>
<h3>Space between bars</h3>
<p>The default amount of space put between the bars within each category is pretty generous. That can be reduced as a percentage of the available space. I added this to the .jrxml (well, iReport adds this to the .jrxml) to completely eliminate the space between bars:</p>
<pre>&lt;property name="ItemMargin" value="0.0f"/&gt;</pre>
<div id="attachment_173" class="wp-caption alignnone" style="width: 250px"><a href="http://mdahlman.files.wordpress.com/2011/04/bar_eating_better3.png"><img class="size-full wp-image-173" title="Bar Chart: Eating (Space removed)" src="http://mdahlman.files.wordpress.com/2011/04/bar_eating_better3.png" alt="" width="240" height="116" /></a><p class="wp-caption-text">In some charts its more effective to remove the space between bars</p></div>
<h3>Upper Margin</h3>
<p>I will not bother showing this property since it&#8217;s covered in &#8220;Chart Customizers 1&#8243;. The only difference now is that in that case I hard-coded the margin to .40. Now I can set that percentage as a property in the chart.</p>
<h2>Pie Chart Improvements</h2>
<p>My most common annoyance with pie charts was when I put several pie charts onto the same page to show a trend. There is no easy way to specify a color for a named section. It&#8217;s easy to specify a color for the first section and a color for the second section. This is often sufficient. But this has the potential to be extremely confusing when two different charts have different items or when they have the same items returned in a different order. The following sample report shows the idea clearly.</p>
<p><a href="http://mdahlman.files.wordpress.com/2011/04/pie_good_and_bad.png"><img class="aligncenter size-full wp-image-172" title="Pie Chart" src="http://mdahlman.files.wordpress.com/2011/04/pie_good_and_bad.png" alt="" width="758" height="769" /></a>Passing in the colors for the sections in a pie chart is the most complex piece in all of my chart customizers. I give a special thank you to <a href="http://www.jaspersoft.com/management-team#tdanciu" target="_blank">Teodor Danciu</a> for the idea and to <a href="http://www.lukeshannon.com/" target="_blank">Luke Shannon</a> for help with some implementation details.</p>
<p>You must create a property on the chart called &#8220;Predefined Colors&#8221;. The property value is a String containing key:value pairs separated from each other by semi-colons. For example, for a report that I run which shows the databases that JasperReports Server instances use for their repository database I pass this string:</p>
<pre>MySQL:#015A84;Oracle:red;PostgreSQL:#0085B0;Microsoft SQL Server:#F8EB33</pre>
<p>For most databases I choose a color using RGB values. For Oracle I use the shorthand syntax &#8220;red&#8221;. These values get converted into colors using a utility class that is built-in to JasperReports: http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/type/ColorEnum.html</p>
<p>To see exactly how it&#8217;s done, refer to the sample report (Pie_Specified_Series_Colors.jrxml) and to the comments in the chart customizer (PieLabels.java).</p>
<p>These are some of the most common chart tweaks that I have seen. Some are ones that I have needed myself, and others are ones that I have seen requested on <a href="http://jasperforge.org/" target="_blank">JasperForge</a>. Please let me know if you find the chart customer useful, and let me know what other properties you would find useful in a chart customizer.</p>
<h2>Detailed Steps to update a report</h2>
<p>Here are the steps I followed to apply my new chart customizer to an existing report. This is the report &#8220;Customer Overview&#8221; that ships with <a href="http://www.jaspersoft.com/jaspersoft-business-intelligence-software-trial" target="_blank">Jaspersoft BI evaluation version</a>. So you can easily apply this chart customizer to that sample before trying it on your own charts.</p>
<ol>
<li>Upload ChartCustomizers2.jar into JasperReports Server. (Use either the web UI or iReport&#8217;s JasperReports Server plug-in.)</li>
<li>Edit the report to add a link to the resource ChartCustomizers2.jar. (Use either the web UI or iReport&#8217;s JasperReports Server plug-in.)</li>
<li>Edit the report in iReport to make two changes:</li>
<ol>
<li>Find the property &#8220;Customizer Class&#8221; and set it to &#8220;com.jaspersoft.bizdev.BetterBarLabels&#8221; (without the quotes).</li>
<li>Find the property &#8220;Properties expressions&#8221; and add a new property called &#8220;IntegerTickUnits&#8221; with the value &#8220;true&#8221; (without the quotes in both cases).</li>
</ol>
<li>Save the report back to JasperReports Server. Run the report.</li>
</ol>
<h2>Sample reports and the Chart Customizer</h2>
<p>WordPress seems to lose my attachments with some regularity. So instead of using that I&#8217;ll try GoogleDocs for sharing my samples.</p>
<ul>
<li><a href="https://docs.google.com/leaf?id=0B9apCz8LCsSnMzY4MTA4Y2QtODMwMC00YTBkLWE2ZDItMTQyZGI0NDc0ZGU2&amp;hl=en" target="_blank">Chart Customizer 2</a> &#8211; This is a .jar file. Just add it to your classpath and you can use it. The .jar also includes the source .java files.</li>
<li>Pie Chart sample report <a href="https://docs.google.com/leaf?id=0B9apCz8LCsSnZWYwMWMxMmQtM2ZjZi00YjEzLWE4NDktN2Y1NzkzNTEyZjhm&amp;sort=name&amp;layout=list&amp;num=50" target="_blank">JRXML</a></li>
<li>Pie Chart sample report <a href="https://docs.google.com/viewer?a=v&amp;pid=explorer&amp;chrome=true&amp;srcid=0B9apCz8LCsSnMzM4NjdjNmMtODMyZC00NDczLWFmNTItYjRlMzdjNmE3YTBh&amp;hl=en" target="_blank">PDF</a></li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdahlman.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdahlman.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdahlman.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdahlman.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdahlman.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdahlman.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdahlman.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdahlman.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdahlman.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdahlman.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdahlman.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdahlman.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdahlman.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdahlman.wordpress.com/166/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=166&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdahlman.wordpress.com/2011/04/17/chart-customizers-2/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2d40af941f746115e07c340c0c0eadf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdahlman</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/04/bar_shoppingdays_original.png" medium="image">
			<media:title type="html">Bar Chart: Shopping Days</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/04/setting_chart_property_ireport.png" medium="image">
			<media:title type="html">Setting a Chart Property in iReport</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/04/bar_shoppingdays_better.png" medium="image">
			<media:title type="html">Bar Chart: Shopping Days (Improved!)</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/04/bar_eating_original.png" medium="image">
			<media:title type="html">Bar Chart: Eating Competition</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/04/bar_eating_better.png" medium="image">
			<media:title type="html">Bar Chart: Eating Competition Better</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/04/bar_eating_original.png" medium="image">
			<media:title type="html">Bar Chart: Eating Competition</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/04/bar_eating_better2.png" medium="image">
			<media:title type="html">Bar Chart: Eating Competition Alternative Better</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/04/bar_eating_better3.png" medium="image">
			<media:title type="html">Bar Chart: Eating (Space removed)</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2011/04/pie_good_and_bad.png" medium="image">
			<media:title type="html">Pie Chart</media:title>
		</media:content>
	</item>
		<item>
		<title>Chart Customizers in JasperReports, iReport, and JasperServer</title>
		<link>http://mdahlman.wordpress.com/2010/08/18/chart-customizers-1/</link>
		<comments>http://mdahlman.wordpress.com/2010/08/18/chart-customizers-1/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 23:01:57 +0000</pubDate>
		<dc:creator>mdahlman</dc:creator>
				<category><![CDATA[iReport]]></category>
		<category><![CDATA[JasperReports]]></category>
		<category><![CDATA[JasperServer]]></category>
		<category><![CDATA[bar chart]]></category>
		<category><![CDATA[pie chart]]></category>

		<guid isPermaLink="false">http://mdahlman.wordpress.com/?p=144</guid>
		<description><![CDATA[Chart Customizers in JasperReports, iReport, and JasperServer Update: Much of the content of this article should still be useful. But I wrote a more recent article with a much more useful chart customizer attached. So please take a look at that article as well. When charts in reports designed in iReport don&#8217;t look the way [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=144&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>Chart Customizers in JasperReports, iReport, and JasperServer</h1>
<p>Update: Much of the content of this article should still be useful. But I wrote a more recent article with a much more useful chart customizer attached. So please take a look at <a title="Useful Chart Customizers in JasperReports, iReport, and JasperReports Server" href="http://mdahlman.wordpress.com/2011/04/17/chart-customizers-2/" target="_blank">that article</a> as well.</p>
<p>When charts in reports designed in iReport don&#8217;t look the way that you want them to there are a few common next steps that you can take. Here are the most common:</p>
<ul>
<li>Silently curse those Jaspersoft developers (what a stupid default display style)</li>
<li>Silently curse those JFreeChart developers (surely everyone in the world would prefer it differently)</li>
<li>Loudly curse them (come on guys! hurry up and add my new feature!)</li>
<li>Search jasperforge.org, figure out that you need a chart customizer or a chart theme, speculate that this sounds too difficult, and then finally decide that you don&#8217;t really care so much about whatever it was that you really wanted to change afterall.</li>
</ul>
<p>In this article I&#8217;ll take a look at some of the less-used alternative next steps:</p>
<ul>
<li>Refer to the documentation</li>
<li>Refer to the samples</li>
<li>Create and use a Chart Customizer</li>
</ul>
<h2>Documentation</h2>
<p>The JasperReports Ultimate Guide includes the section &#8220;Chart Customizer and Chart Theme&#8221;. This is a good starting point. It explains the reason these objects exist: to provide full control over chart customization. And it describes the interface that you must implement and the class you should extend in order to create a chart theme. The iReport Ultimate Guide mentions the existence of chart themes, but it doesn&#8217;t provide any additional examples or insight.</p>
<p>The online documentation on jasperforge.org provides additional information. The <a href="http://jasperforge.org/uploads/publish/jasperreportswebsite/trunk/sample.reference.html">Sample Reference</a> section includes a page on charts which has a section on <a href="http://jasperforge.org/uploads/publish/jasperreportswebsite/trunk/sample.reference/charts/index.html#chartcustomizers">chart customizers</a>. It has sample chart customizer code that can hard-code a bar chart to ignore the colors that you have chosen when designing the report and to use only green and orange instead. The customizer has no practical value, but it&#8217;s nice to have a working sample as a starting point.</p>
<h2>Samples</h2>
<p>The page mentioned above refers the reader to a resource that should already have been consulted&#8230; the samples that ship with JasperReports. If you have only installed JasperServer or iReport, then you&#8217;ll need to download <a href="http://sourceforge.net/projects/jasperreports/files/jasperreports">JasperReports</a> directly. It includes this folder containing over 50 samples:</p>
<p>&#8230;/jasperreports-3.7.4/demo/samples</p>
<p>In the subfolder &#8220;charts&#8221; you&#8217;ll find the aforementioned BarChartCustomizer.java to generate green and orange bars along with reports that use this chart customizer. In the folder &#8220;chartthemes&#8221; you&#8217;ll find sample code for chart themes.</p>
<p>The other sample that everyone has easy access to is the set of chart customizers that ships with <a href="http://www.jaspersoft.com/jaspersoft-business-intelligence-software-trial" target="_blank">JasperServer Professional</a>. Use iReport to browse the JasperServer repository and download the file DemoChartLabels.jar. It contains both the binaries used by the server as well as the source java files.</p>
<div id="attachment_146" class="wp-caption aligncenter" style="width: 216px"><a href="http://mdahlman.files.wordpress.com/2010/08/01_jasperserver_plugin_ireport.png"><img class="size-medium wp-image-146" title="DemoChartLabels.jar in JasperServer Plugin in iReport" src="http://mdahlman.files.wordpress.com/2010/08/01_jasperserver_plugin_ireport.png?w=206&#038;h=300" alt="DemoChartLabels.jar in JasperServer Plugin in iReport" width="206" height="300" /></a><p class="wp-caption-text">The highlighted file, DemoChartLabels.jar, may be downloaded using the JasperServer Plugin in iReport</p></div>
<h2>Chart Customizer Samples</h2>
<p>I&#8217;m not a Java developer. I will not be able to create an impressive chart customizer to amaze your friends and family. But I know enough Java to create some very basic chart customizers. I have set my sights slightly higher than just creating alternating green and orange bars in a bar chart. But not much higher.</p>
<h3>Pie Chart Labels</h3>
<h4>The problem:</h4>
<p>I want a pie chart with no labels on it. It sounds funny right? But it&#8217;s pretty common. It came up in <a href="http://jasperforge.org/plugins/espforum/view.php?group_id=102&amp;forumid=103&amp;topicid=7462">this thread</a> on jasperforge.org. I have wanted it in the past. Most recently, Jaspersoft partner <a href="http://www.openbi.com/">OpenBI</a> asked me about it. Here&#8217;s a simple example.</p>
<div id="attachment_147" class="wp-caption aligncenter" style="width: 514px"><a href="http://mdahlman.files.wordpress.com/2010/08/02-pie-chart-default.png"><img class="size-full wp-image-147" title="Pie Chart with the Default Look" src="http://mdahlman.files.wordpress.com/2010/08/02-pie-chart-default.png" alt="Pie Chart with the Default Look" width="504" height="286" /></a><p class="wp-caption-text">Pie Chart with the Default Look</p></div>
<h4>The solution:</h4>
<p>I need a way to tell the chart not to display labels. iReport (and JasperReports) does not provide a way to do this. The underlying chart engine is JFreeChart. The JFreeChart API provides a mechanism avoid displaying labels. I found the answer on <a href="http://stackoverflow.com/questions/1804145/hide-labels-on-jfreechart-pieplot3d-piechart">stackoverflow.com</a>. The good news is that it&#8217;s a single line of code. Where do I put this code? In a chart customizer! It&#8217;s simple enough once you have the idea. Here is the complete code for the chart customizer:</p>
<pre>/**
  * OK, it's not really the COMPLETE code. I removed the imports and comments.
  * But those are included in the attachments.
  */
public class PieLabels implements JRChartCustomizer {
  public void customize(JFreeChart chart, JRChart jasperChart) {
    Plot plot = chart.getPlot();
    if (plot instanceof PiePlot) {
      PiePlot piePlot = (PiePlot)plot;
      piePlot.setLabelGenerator(null);
    }
  }
}</pre>
<p>Next, I compiled this class into the very small file DemoChartCustomizers.jar. Finally, I need to tell the report to use the chart customizer. Select the chart and set the property &#8220;Customizer Class&#8221;.</p>
<div id="attachment_148" class="wp-caption aligncenter" style="width: 269px"><a href="http://mdahlman.files.wordpress.com/2010/08/04-chart-properties.png"><img class="size-full wp-image-148 " title="Set the Customizer Class property" src="http://mdahlman.files.wordpress.com/2010/08/04-chart-properties.png" alt="Set the Customizer Class property" width="259" height="343" /></a><p class="wp-caption-text">Set the Customizer Class property</p></div>
<p>Remember to add the JAR file to iReport&#8217;s classpath.</p>
<div id="attachment_149" class="wp-caption aligncenter" style="width: 848px"><a href="http://mdahlman.files.wordpress.com/2010/08/05-ireport-classpath.png"><img class="size-full wp-image-149" title="Set the iReport Classpath" src="http://mdahlman.files.wordpress.com/2010/08/05-ireport-classpath.png" alt="Set the iReport Classpath" width="838" height="436" /></a><p class="wp-caption-text">Set the iReport Classpath</p></div>
<p>The final result is shown below. It&#8217;s modest, but it&#8217;s certainly nicer than the original.</p>
<div id="attachment_150" class="wp-caption aligncenter" style="width: 511px"><a href="http://mdahlman.files.wordpress.com/2010/08/03-pie-chart-customized.png"><img class="size-full wp-image-150" title="Pie Chart with the Customized Look" src="http://mdahlman.files.wordpress.com/2010/08/03-pie-chart-customized.png" alt="Pie Chart with the Customized Look" width="501" height="285" /></a><p class="wp-caption-text">Pie Chart with the Customized Look</p></div>
<h3>Bar Chart Labels</h3>
<h4>The problem:</h4>
<p>This one has annoyed me for years. But I never quite got around to attempting to fix it. I went with option number two from my list above: I&#8217;ve been silently cursing those JFreeChart developers. JFreeChart&#8217;s default way of arranging labels on bar charts puts them above the bars. But its default way of rendering the chart doesn&#8217;t leave enough room above the tallest bar to display the label. This <a href="http://www.jfree.org/phpBB2/viewtopic.php?f=3&amp;t=20716&amp;start=0">old thread</a> discusses the problem. The project leader of JFreeChart, David Gilbert, writes, &#8220;Eventually, it would be nice if the renderer could calculate an appropriate range so that the item labels are always visible.&#8221; Here&#8217;s a typical example of the issue. (This one ships with JasperServer Professional.)</p>
<div id="attachment_151" class="wp-caption aligncenter" style="width: 158px"><a href="http://mdahlman.files.wordpress.com/2010/08/06-bar-chart-original.png"><img class="size-full wp-image-151" title="Bar Chart Original Labels" src="http://mdahlman.files.wordpress.com/2010/08/06-bar-chart-original.png" alt="Bar Chart Original Labels" width="148" height="112" /></a><p class="wp-caption-text">Bar Chart Original Labels</p></div>
<p>The average sale price for All Customers is 27.54. But what is the average sale price for M. Benigar? It&#8217;s significantly higher than the average&#8230; but where is its label? This annoys me to no end. Perhaps the problem has already been solved in JFreeChart&#8217;s default settings. The thread identifying the problem is more than three years old as I write this article. But as far as I can tell, there have been no changes in this regard.</p>
<h4>The solution:</h4>
<p>Until JFreeChart cleans this up internally, we have to manually calculate a better margin to allow space for the label to be displayed. I use a somewhat crude calculation: hard-code the margin to 40%. Here is the result.</p>
<div id="attachment_152" class="wp-caption aligncenter" style="width: 158px"><a href="http://mdahlman.files.wordpress.com/2010/08/07-bar-chart-customized.png"><img class="size-full wp-image-152" title="Bar Chart with Customized Labels" src="http://mdahlman.files.wordpress.com/2010/08/07-bar-chart-customized.png" alt="Bar Chart with Customized Labels" width="148" height="112" /></a><p class="wp-caption-text">Bar Chart with Customized Labels</p></div>
<p>Woo hoo! Now we can see the exact value of the tallest bar.</p>
<h2>Summary</h2>
<p>iReport gives good control over chart options for dozens of properties. But if you want complete control over the display options, you need to use JasperReport&#8217;s chart customizers. This requires Java coding. Sorry. But it&#8217;s not much coding. My two chart customizers really have only one functional line each.</p>
<h2>Next Steps</h2>
<p>You can download my <a href="http://mdahlman.files.wordpress.com/2011/01/chartcustomizersamples.odt">ChartCustomizerSamples</a> to try it yourself. The download file is ChartCustomizerSamples.odt. You need to rename this to ChartCustomizerSamples.zip. (WordPress won&#8217;t let me post a .zip file.) The JAR file included inside will let you run the pie chart reports in iReport. It also includes the source code. The reports use the FoodMart sample database that ships with JasperServer.</p>
<p>Additional Note: I have conveniently ignored Chart Themes. In fact, chart themes are newer and better than chart customizers. Chart customizers have been deprecated in favor of chart themes. Hopefully I&#8217;ll have time to investigate and write about chart themes someday in the future.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdahlman.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdahlman.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdahlman.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdahlman.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdahlman.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdahlman.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdahlman.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdahlman.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdahlman.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdahlman.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdahlman.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdahlman.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdahlman.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdahlman.wordpress.com/144/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=144&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdahlman.wordpress.com/2010/08/18/chart-customizers-1/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2d40af941f746115e07c340c0c0eadf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdahlman</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/08/01_jasperserver_plugin_ireport.png?w=206" medium="image">
			<media:title type="html">DemoChartLabels.jar in JasperServer Plugin in iReport</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/08/02-pie-chart-default.png" medium="image">
			<media:title type="html">Pie Chart with the Default Look</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/08/04-chart-properties.png" medium="image">
			<media:title type="html">Set the Customizer Class property</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/08/05-ireport-classpath.png" medium="image">
			<media:title type="html">Set the iReport Classpath</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/08/03-pie-chart-customized.png" medium="image">
			<media:title type="html">Pie Chart with the Customized Look</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/08/06-bar-chart-original.png" medium="image">
			<media:title type="html">Bar Chart Original Labels</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/08/07-bar-chart-customized.png" medium="image">
			<media:title type="html">Bar Chart with Customized Labels</media:title>
		</media:content>
	</item>
		<item>
		<title>Jaspersoft v3 Font Extensions</title>
		<link>http://mdahlman.wordpress.com/2010/05/28/jaspersoft-v3-7-font-extensions/</link>
		<comments>http://mdahlman.wordpress.com/2010/05/28/jaspersoft-v3-7-font-extensions/#comments</comments>
		<pubDate>Fri, 28 May 2010 21:38:31 +0000</pubDate>
		<dc:creator>mdahlman</dc:creator>
				<category><![CDATA[iReport]]></category>
		<category><![CDATA[JasperReports]]></category>
		<category><![CDATA[JasperServer]]></category>
		<category><![CDATA[font extensions]]></category>
		<category><![CDATA[fonts]]></category>

		<guid isPermaLink="false">http://mdahlman.wordpress.com/?p=128</guid>
		<description><![CDATA[Define font extensions easily in Jaspersoft iReport so that you can use the fonts you want, then deploy the font extensions to JasperServer so that your reports render exactly as intended.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=128&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>Jaspersoft Font Extensions</h1>
<h2>Background</h2>
<p>To get reports to display exactly the way that you want them to display you need to ensure that you have exactly the font that you want. Jaspersoft makes this relatively easy through the mechanism of &#8220;Font Extensions&#8221;. To understand the technical workings of font extensions well, your best resources is the <a href="http://www.jaspersoft.com/jasperreports-ultimate-guide-documentation" target="_blank">JasperReports Ultimate Guide</a>. If you are working with JasperReports you owe it to yourself to buy a subscription to this resources; it&#8217;s great.</p>
<p>I&#8217;m not writing about the details of how font extensions work.</p>
<p>In the past creating font extensions was relatively difficult. I wrote about it in this post. It wasn&#8217;t really overly difficult if you refer to the documentation and took your time, but it involves creating XML files and is certainly not a point-and-click process. With the release of iReport 3.7 it&#8217;s <strong>much</strong> easier. For details on how to use the wizard to create font extensions, take a look at the <a href="http://www.jaspersoft.com/ireport-ultimate-guide-documentation" target="_blank">iReport Ultimate Guide</a>. It has a whole chapter on &#8220;Fonts and Styles&#8221;. It&#8217;s great.</p>
<p>I&#8217;m not trying to replace the content already included in the iReport premium documentation.</p>
<p>So what is the point of this article? This is an example of the complete process of creating a new report, specifying fonts, confirming that the report works well, then deploying the report to a server where the font doesn&#8217;t already exist and configuring that server correctly.</p>
<h2>Create a report and specify fonts</h2>
<p>In the attached report &#8220;<a title="Rename the file from *.odt to *.jrxml" href="http://mdahlman.files.wordpress.com/2010/05/fontextensions.odt">FontExtensions.jrxml</a>&#8221; you can see that I have selected 4 different fonts for the 4 different text fields. The title is set to &#8220;DejaVu Serif&#8221;. I chose this because it is already installed as a font extension by default in iReport (and JasperServer). How do I know that it&#8217;s already installed? You can see the installed fonts from the menu Tools -&gt; Options -&gt; Fonts. You can also tell by looking at the dropdown list of fonts in the property &#8220;Font name&#8221; for the field. DejaVu Serif is above the line. That means it&#8217;s installed. The fonts below the line are on my machine, but they aren&#8217;t registered as font extensions, so there could be trouble at deployment time.</p>
<div id="attachment_129" class="wp-caption aligncenter" style="width: 310px"><a href="http://mdahlman.files.wordpress.com/2010/05/01-tools-options-fonts.png"><img class="size-medium wp-image-129 " title="Tools Options Fonts" src="http://mdahlman.files.wordpress.com/2010/05/01-tools-options-fonts.png?w=300&#038;h=236" alt="Tools -&gt; Options -&gt; Fonts" width="300" height="236" /></a><p class="wp-caption-text">Tools -&gt; Options -&gt; Fonts</p></div>
<div id="attachment_130" class="wp-caption aligncenter" style="width: 310px"><a href="http://mdahlman.files.wordpress.com/2010/05/02-font-dropdown.png"><img class="size-medium wp-image-130 " title="Font dropdown" src="http://mdahlman.files.wordpress.com/2010/05/02-font-dropdown.png?w=300&#038;h=142" alt="Font dropdown" width="300" height="142" /></a><p class="wp-caption-text">Dropdown list of available fonts</p></div>
<p>Next, I want to set the subtitle to Microsoft&#8217;s Comic Sans font. (Please note: I forbid you to use this font in your own reports. It&#8217;s terrible. Maybe if you&#8217;re creating a report on preschool statistics it would be OK. I only chose it for this article because it&#8217;s well known and distinctive.) Oh no! This font appears only below the line! (as &#8220;Comic Sans MS&#8221;)</p>
<div id="attachment_133" class="wp-caption alignright" style="width: 160px"><a href="http://mdahlman.files.wordpress.com/2010/05/04-family-details.png"><img class="size-thumbnail wp-image-133" title="Family Details" src="http://mdahlman.files.wordpress.com/2010/05/04-family-details.png?w=150&#038;h=118" alt="Family Details" width="150" height="118" /></a><p class="wp-caption-text">Family Details</p></div>
<div id="attachment_132" class="wp-caption alignright" style="width: 160px"><a href="http://mdahlman.files.wordpress.com/2010/05/03-select-a-truetype-font.png"><img class="size-thumbnail wp-image-132" title="Select a TrueType Font" src="http://mdahlman.files.wordpress.com/2010/05/03-select-a-truetype-font.png?w=150&#038;h=109" alt="Select a TrueType Font" width="150" height="109" /></a><p class="wp-caption-text">Select a TrueType Font</p></div>
<p>Don&#8217;t worry: installing a font is simple. From the Tools -&gt; Options -&gt; Fonts screen shown above click &#8220;Install Font&#8221;. On the first step of the wizard browse to the font file. On the second step specify the separate file with the bold version of the font. The final steps of the wizard are described in detail in the iReport Ultimate Guide, so I won&#8217;t bother with details here. For our purposes we can skip through these steps with no changes.</p>
<p>Hooray. Now Comic Sans MS appears &#8220;above the line&#8221; as well as on the window Tools -&gt; Options -&gt; Fonts. It can safely be selected as the font for the subtitle field.</p>
<div id="attachment_134" class="wp-caption alignright" style="width: 136px"><a href="http://mdahlman.files.wordpress.com/2010/05/05-edit-code2000.png"><img class="size-thumbnail wp-image-134" title="Edit Code2000" src="http://mdahlman.files.wordpress.com/2010/05/05-edit-code2000.png?w=126&#038;h=150" alt="Code2000 PDF Encoding" width="126" height="150" /></a><p class="wp-caption-text">Code2000 PDF Encoding</p></div>
<p>Next, I created a field for my group header. I included the Japanese characters &#8220;日本語&#8221; in this field to test support for non-ascii characters. Of course I need to select a font that includes support for these characters. I chose <a href="http://www.code2000.net/" target="_blank">Code2000</a>. It&#8217;s not a particularly elegant font visually. But it supports lots and lots of characters. As before, the next step is to install Code2000 as a font extension.</p>
<p>This time be sure to set the PDF Encoding to &#8220;Identity-H&#8221;. If you forget to set it during the initial run of the wizard, you can edit the font details later as shown in the screenshot here. Embedding the font in the PDF document is not required, but I recommend it. Without that option you are at the mercy of the machine viewing the PDF document to supply the font. With the font embedded you are sure to get what you&#8217;re expecting. The file size will be larger, but it&#8217;s really not a big difference. The PDF creation process is smart enough to embed only the characters that are required; you don&#8217;t get the entire font file embedded into the document.</p>
<p>My final text field is in the detail band. For this field I went with the simple option of using the Java logical font &#8220;SansSerif&#8221;. This logical font will get translated to some physical font by the JVM when the report gets run. It&#8217;s impossible for the report designer to know what font will be selected, so this option is appropriate only when you don&#8217;t need a precise font. As a practical matter, one can make a good guess. I see that my JVM on Windows XP is configured to use Arial for its SansSerif font. I see that my JVM on CentOS will use lucidasans. These are common default values, but they are not guaranteed.</p>
<h2>Run the report in iReport</h2>
<p>Now that the report is created, I&#8217;ll run it to confirm that all fonts are working as expected. But to make the details perfectly clear I first deleted my font extensions. Let&#8217;s see what this report looks like <em>without the font extensions</em> defined in iReport.</p>
<div id="attachment_135" class="wp-caption aligncenter" style="width: 1034px"><a href="http://mdahlman.files.wordpress.com/2010/05/06-no-font-extensions.png"><img class="size-large wp-image-135" title="No font extensions" src="http://mdahlman.files.wordpress.com/2010/05/06-no-font-extensions.png?w=1024&#038;h=280" alt="No font extensions" width="1024" height="280" /></a><p class="wp-caption-text">With no font extensions defined the output is somewhat surprising</p></div>
<p>The good news is that the report looks good without the font extensions. The bad news is that the report looks good without the font extensions. It&#8217;s bad news because it can make you think that things are configured correctly. Only the PDF export of the report shows the problems caused by not defining the needed font extensions.</p>
<p>Now I put the font extensions back and re-run the report. The internal preview and HTML were already fine before, so they remain fine. But <em>with the font extensions</em> PDF is working well too.</p>
<div id="attachment_136" class="wp-caption aligncenter" style="width: 310px"><a href="http://mdahlman.files.wordpress.com/2010/05/07-pdf-with-font-extensions.png"><img class="size-medium wp-image-136" title="PDF with font extensions" src="http://mdahlman.files.wordpress.com/2010/05/07-pdf-with-font-extensions.png?w=300&#038;h=234" alt="PDF with font extensions" width="300" height="234" /></a><p class="wp-caption-text">With the fonts installed the PDF is now good</p></div>
<p>The sharp-eyed reader may notice that the Japanese characters in the detail band still do not display well. I&#8217;m willing to ignore this for now. It happens because my JVM translates SansSerif to Arial, and Arial does not include support for Japanese characters. I could solve the problem by updating my JVM properties to use another font or by modifying my report to use Code2000 or Arial Unicode MS or another font with support for these characters.</p>
<h2>Deploy the Report to JasperServer</h2>
<p>From iReport use the Repository Navigator to deploy the report to JasperServer. Open a browser, find the report, and woo hoo! Everything is good: the title uses DevaVu Serif, the subtitle uses MS Comic Sans, the group header shows Japanese characters correctly using Code2000, and the detail band shows Japanese characters correctly using some unspecified sans serif font.</p>
<div id="attachment_137" class="wp-caption aligncenter" style="width: 310px"><a href="http://mdahlman.files.wordpress.com/2010/05/08-html-on-jasperserver.png"><img class="size-medium wp-image-137" title="HTML on JasperServer" src="http://mdahlman.files.wordpress.com/2010/05/08-html-on-jasperserver.png?w=300&#038;h=224" alt="HTML Report on JasperServer" width="300" height="224" /></a><p class="wp-caption-text">HTML Report on JasperServer</p></div>
<p>Just to be sure, let&#8217;s take a look at the report in PDF format. On no! It looks exactly like it did in iReport before we defined the font extensions. Japanese characters are missing and it uses the wrong fonts.</p>
<p>If you have been following along closely, you should already know what is wrong. Although we defined the font extensions in iReport, we have not deployed them to JasperServer. That&#8217;s the final step we need before the reports will work exactly as intended.</p>
<h2>Export and upload the font extensions</h2>
<p>We need to get the font extension from iReport over to JasperServer. In JasperServer 3.5 this required tracking down (or creating) the relevant XML files, properties files, and TTF font files. I wrote about <a href="http://mdahlman.wordpress.com/2009/05/13/fonts-in-jasperserver/" target="_blank">font extensions in JasperServer 3.5</a> previously. In JasperServer 3.7 it&#8217;s a single click to export the extension and only a few more clicks to load it up to JasperServer.</p>
<p>Return to the window Tools -&gt; Options -&gt; Fonts and click the button &#8220;Export as extension&#8221;. That was easy.</p>
<p>In the Repository Navigator pane right-click a folder and choose Add -&gt; Jar Archive. Then on the report unit add a reference to this JAR file. It is possible to add the JAR Archive directly into the report unit. But it&#8217;s better to add the JAR independently so that it may be referenced by many reports. In the screenshot below you can see that MyFontExtensions.jar has already been added, and now I am just adding a reference link from my report unit to this resource.</p>
<div id="attachment_138" class="wp-caption aligncenter" style="width: 241px"><a href="http://mdahlman.files.wordpress.com/2010/05/10-add-jar-archive.png"><img class="size-medium wp-image-138" title="Add JAR Archive" src="http://mdahlman.files.wordpress.com/2010/05/10-add-jar-archive.png?w=231&#038;h=300" alt="Add JAR Archive" width="231" height="300" /></a><p class="wp-caption-text">Add JAR Archive and a reference to the JAR Archive</p></div>
<h2>Success</h2>
<p>Now that the font extensions have been loaded up into JasperServer—and the report makes use of it—the report works perfectly. HTML, XLS, OpenOffice.org spreadsheets and text documents, and even PDF are all rendered as intended. Don&#8217;t forget that most formats, like the ODS file below, <em>refer </em>to the correct font but do not <em>embed </em>it. So these will not render as intended on client machines lacking the relevant font. But PDF with its font embedding can be counted on to produce a pixel-perfect output.</p>
<div id="attachment_139" class="wp-caption aligncenter" style="width: 310px"><a href="http://mdahlman.files.wordpress.com/2010/05/11-ods-report.png"><img class="size-medium wp-image-139" title="ODS report" src="http://mdahlman.files.wordpress.com/2010/05/11-ods-report.png?w=300&#038;h=218" alt="ODS report" width="300" height="218" /></a><p class="wp-caption-text">The same report in ODS with correct fonts</p></div>
<h2>Appendix</h2>
<p>One way to be more certain that your reports are well defined is to tell JasperServer to more rigorously enforce fonts. By default JasperServer makes a best guess and substitutes another font in place of one that cannot be found. This is helpful in some respects, but I find that it promotes laziness. I much prefer to set the following option in &#8230;/WEB-INF/classes/jasperreports.properties:</p>
<pre># ignore missing fonts in reports
net.sf.jasperreports.awt.ignore.missing.font=false</pre>
<p>The default value is true. By setting it to false JasperServer will throw an exception when it cannot find the font that it needs. Here&#8217;s what the user sees when running the above report before deploying the font extensions when JasperServer does not ignore missing fonts:</p>
<pre>net.sf.jasperreports.engine.util.JRFontNotFoundException: Font 'Comic Sans MS' is not available to the JVM. See the Javadoc for more details.
	at net.sf.jasperreports.engine.util.JRFontUtil.checkAwtFont(JRFontUtil.java:321)
	at net.sf.jasperreports.engine.util.JRStyledText.getAwtAttributedString(JRStyledText.java:226)
	at net.sf.jasperreports.engine.fill.TextMeasurer.measure(TextMeasurer.java:362)
	at net.sf.jasperreports.engine.util.JdkGlyphFixTextMeasurer.measure(JdkGlyphFixTextMeasurer.java:133)
	at net.sf.jasperreports.engine.fill.JRFillTextElement.chopTextElement(JRFillTextElement.java:1008)
	at net.sf.jasperreports.engine.fill.JRFillTextField.prepare(JRFillTextField.java:539)
	at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:327)
	at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:402)
	...
</pre>
<p>Of course you wouldn&#8217;t want to let errors like this creep into a production system. But that&#8217;s why we have testing. During testing it&#8217;s far better to see these errors. Without this error I might not have noticed that a report was rendering without Comic Sans MS. This way I see exactly what the problem is, so I can take steps to fix it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdahlman.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdahlman.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdahlman.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdahlman.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdahlman.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdahlman.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdahlman.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdahlman.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdahlman.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdahlman.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdahlman.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdahlman.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdahlman.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdahlman.wordpress.com/128/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=128&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdahlman.wordpress.com/2010/05/28/jaspersoft-v3-7-font-extensions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2d40af941f746115e07c340c0c0eadf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdahlman</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/05/01-tools-options-fonts.png?w=300" medium="image">
			<media:title type="html">Tools Options Fonts</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/05/02-font-dropdown.png?w=300" medium="image">
			<media:title type="html">Font dropdown</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/05/04-family-details.png?w=150" medium="image">
			<media:title type="html">Family Details</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/05/03-select-a-truetype-font.png?w=150" medium="image">
			<media:title type="html">Select a TrueType Font</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/05/05-edit-code2000.png?w=126" medium="image">
			<media:title type="html">Edit Code2000</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/05/06-no-font-extensions.png?w=1024" medium="image">
			<media:title type="html">No font extensions</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/05/07-pdf-with-font-extensions.png?w=300" medium="image">
			<media:title type="html">PDF with font extensions</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/05/08-html-on-jasperserver.png?w=300" medium="image">
			<media:title type="html">HTML on JasperServer</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/05/10-add-jar-archive.png?w=231" medium="image">
			<media:title type="html">Add JAR Archive</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/05/11-ods-report.png?w=300" medium="image">
			<media:title type="html">ODS report</media:title>
		</media:content>
	</item>
		<item>
		<title>Fonts in JasperServer 3.7</title>
		<link>http://mdahlman.wordpress.com/2010/04/16/fonts-in-jasperserver-3-7/</link>
		<comments>http://mdahlman.wordpress.com/2010/04/16/fonts-in-jasperserver-3-7/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 19:05:11 +0000</pubDate>
		<dc:creator>mdahlman</dc:creator>
				<category><![CDATA[JasperServer]]></category>
		<category><![CDATA[font extensions]]></category>
		<category><![CDATA[fonts]]></category>
		<category><![CDATA[iReport]]></category>
		<category><![CDATA[Japanese]]></category>
		<category><![CDATA[JasperReports]]></category>
		<category><![CDATA[l10n]]></category>

		<guid isPermaLink="false">http://mdahlman.wordpress.com/?p=121</guid>
		<description><![CDATA[Installing fonts so that JasperServer sample reports display correctly in Japanese.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=121&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>You can&#8217;t display fonts that you don&#8217;t have</h1>
<h2>The Problem</h2>
<p>I have written about <a href="http://mdahlman.wordpress.com/2009/05/13/fonts-in-jasperserver/">fonts in JasperServer</a> in the past, but I&#8217;m revisiting it now. It&#8217;s popular topic because fonts are very important to the look of your reports, but it&#8217;s easy to get things setup wrong (or incompletely). Let&#8217;s start with the &#8220;Sales by Month&#8221; report which ships with JasperServer Professional and Enterprise 3.7.</p>
<div id="attachment_122" class="wp-caption aligncenter" style="width: 310px"><a href="http://mdahlman.files.wordpress.com/2010/04/01_sales_by_month_japanese.png"><img class="size-medium wp-image-122" title="01_Sales_by_Month_Japanese" src="http://mdahlman.files.wordpress.com/2010/04/01_sales_by_month_japanese.png?w=300&#038;h=284" alt="Sales by Month report in Japanese" width="300" height="284" /></a><p class="wp-caption-text">Sales by Month report in Japanese</p></div>
<p>Let me be the first to say, &#8220;Hey! didn&#8217;t those Jaspersoft guys improve anything in v3.7? Why doesn&#8217;t my chart correctly display the legend characters in Japanese by default!?&#8221;</p>
<h2>The Solution</h2>
<p>The issue—as is common with font issues—turns out to be more subtle than one might expect. First we have the somewhat confusing situation that some Japanese characters display well. It&#8217;s only the chart that has a problem. This is because my local Windows XP machine has appropriate Japanese fonts. So things like this render nicely: &#8220;キー：  低い値    高い値&#8221;. That&#8217;s because they get rendered by my browser. But the chart gets created on the server. A bad choice of font must be happening there.</p>
<p>In JasperServer v3.5 the problem was that the report specified Verdana as the font. Verdana doesn&#8217;t include glyphs for Japanese characters, so it wasn&#8217;t really JasperServer&#8217;s fault that it couldn&#8217;t render things well. It was the report designer&#8217;s fault. In JasperServer v3.7 all of the sample reports have been updated to use Java logical fonts like SansSerif, Monospaced, etc. This means that JasperServer pushes the job of selecting the exact physical font down to the JVM which works with the underlying OS to find an appropriate font.</p>
<p>Aha! this means we have a new culprit to point fingers at: Java. What font is the JVM deciding to use? I&#8217;m running JasperServer in the cloud on a CentOS Linux box. What font faces are available? Let&#8217;s see:</p>
<pre># fc-list
#</pre>
<p>That&#8217;s right. No fonts at all are available. That is likely the root of my problem. Are there fonts available to install? Let&#8217;s see:</p>
<pre># yum list | grep japanese
fonts-japanese.noarch                    0.20061016-4.el5       base
m17n-db-japanese.noarch                  1.3.3-48.el5           base</pre>
<p>There are fonts available. It takes only a few seconds to install them:</p>
<pre># yum install fonts-japanese.noarch</pre>
<p>Now I have lots of fonts available:</p>
<pre># fc-list
fxd:style=Bold semicondensed
sys:style=Bold
hlv:style=Bold Italic
Fixed:style=Bold
goth_p:style=Bold
sys:style=Bold Italic
...</pre>
<p>Finally, I need to restart Tomcat. Now when I log back into JasperServer I see everything rendered correctly. Woo hoo! The report was good. JasperServer was good. Tomcat was good. Everything was good&#8230; except the underlying operating system didn&#8217;t have any appropriate fonts.</p>
<div id="attachment_123" class="wp-caption aligncenter" style="width: 310px"><a href="http://mdahlman.files.wordpress.com/2010/04/02_sales_by_month_japanese.png"><img class="size-medium wp-image-123" title="02_Sales_by_Month_Japanese" src="http://mdahlman.files.wordpress.com/2010/04/02_sales_by_month_japanese.png?w=300&#038;h=281" alt="Sales by Month report in Japanese" width="300" height="281" /></a><p class="wp-caption-text">Sales by Month correctly displaying all Japanese characters</p></div>
<h1>Other Fonts</h1>
<p>What about when you want to specify the precise font that you need? That&#8217;s a good question. I ignored it in this article. The short answer is that you need to use Font Extensions. I wrote about it previously in my article &#8220;<a href="http://mdahlman.wordpress.com/2009/05/13/fonts-in-jasperserver/">Fonts in JasperServer</a>&#8220;.</p>
<p>In general the use of Java logical fonts makes things more flexible. That&#8217;s what made it the ideal choice for sample reports that ship with JasperServer. But it&#8217;s possible that different systems will map the logical fonts to different physical fonts. These fonts may have slightly different metrics, so the results in the report can be slightly unpredictable.</p>
<p>In many reports, you simply don&#8217;t care about the minor font metric differences. On one machine a particular value fits on a single line. On another machine it overflows into a second line. So what?</p>
<p>In other reports you have more precise positioning requirements. Your title fits nicely on a single line at design time. But when the report is deployed the final word in the title overflows into a second line. That won&#8217;t look impressive. Worse: that field isn&#8217;t allowed to expand, so you don&#8217;t see the final word anywhere. Bummer. In cases like this you need to specify the precise font that gets used. Of course it therefore becomes your responsibility to make sure that the server has the font that you want to use. For these situations Font Extensions are your savior.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdahlman.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdahlman.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdahlman.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdahlman.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdahlman.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdahlman.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdahlman.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdahlman.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdahlman.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdahlman.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdahlman.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdahlman.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdahlman.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdahlman.wordpress.com/121/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdahlman.wordpress.com&amp;blog=7374664&amp;post=121&amp;subd=mdahlman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdahlman.wordpress.com/2010/04/16/fonts-in-jasperserver-3-7/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2d40af941f746115e07c340c0c0eadf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdahlman</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/04/01_sales_by_month_japanese.png?w=300" medium="image">
			<media:title type="html">01_Sales_by_Month_Japanese</media:title>
		</media:content>

		<media:content url="http://mdahlman.files.wordpress.com/2010/04/02_sales_by_month_japanese.png?w=300" medium="image">
			<media:title type="html">02_Sales_by_Month_Japanese</media:title>
		</media:content>
	</item>
	</channel>
</rss>
