Monday, November 1, 2010

JasperReports & the Format Factory

The more I work with JasperReports, the more I admire the project altogether. It seems like every time when I'm like "I wonder if JasperReports can handle that" it does, and it does it well.

Recently one of those issues had to do with localization, specifically date & number formatting. If you have worked with JasperReports you'll probably know that you can format dates & numbers with the pattern (e.g. "MM/dd/yyyy") attribute on a text field element. That is just fine when the pattern is known at design time. But what do you do when the patterns need to be localized or in other words dynamic?

No you can't use a String parameter as a pattern. The pattern attribute on a text field does not accept an expression, only a string literal. Another solution might be to have your application twiddle the JRXML and inject the proper patterns. But that would be hacky and could easily be error prone.

Enter the Format Factory. The Format Factory concept is simple but powerful. The FormatFactory is an interface in the net.sf.jasperreports.engine.util package that you implement to create your own date & number formats.

public class MyFormatFactory implements FormatFactory {

public DateFormat createDateFormat(String pattern, Locale locale, TimeZone timeZone) {
//create your date format here
return your-date-format ;

public NumberFormat createNumberFormat(String pattern, Locale locale) {
// create your number format here
return your-number-format ;


Essentially, your application takes over format creation instead of the JasperReports engine itself.

The best part of all this is your ability to create formats based on your own defined constants. You'll notice that the two methods in the class above have a "pattern" parameter. That is the pattern that you assign to the textfield.

You might say, hey I thought we weren't using explicit patterns anymore. You're right. But, we have to give a hint as to how the textfield needs to be formatted, so we would use format constants (e.g. SHORT_DATE, LONG_DATE, TIMESTAMP, etc). The constants are assigned to our text fields pattern attribute and get passed to our FormatFactory where we determine how to create a formatter based on those constants. Kinda cool, eh?

You might also notice the timezone & locale parameters in the methods above. Those are also parameters you can pass into the report itself which in turn get passed in to the format factory to assist you in creating the appropriate formats. For example, when you create a date format you'll need to know the time zone since that is an important part of actually formatting a date.

The Format Factory is a fantastic feature. This is especially true for large applications that already have formatting logic implemented in the application allowing you to reuse your existing format logic.

Friday, May 14, 2010

Thoughts on Flash from Jobs

So I just happened to log onto Apple's site today and noticed this article on Flash by Steve Jobs:

So in summary here is why Flash isn't going to be on iPad, iPhone & iPods.

1) Flash is not an "open" standard
2) Apple mobile devices can already access tons of video content
3) Flash's reliability, security and performance on Macs is not good
4) Battery life
5) Flash and touch interface not a match
6) The dependability of a third party between Apple and developers

This sounds reasonable to me but I wonder if there still aren't other behind the scenes reasons. I'm sure Apple wants to collect all revenue through the App Store. What do you think?

Tuesday, January 26, 2010

Learning Spring

I am officially starting to learn the Spring Framework. Until recently the projects I have been working on have not been using Spring so I had no driving reason to learn it. Since I am so busy with school (WGU) & work there is almost no way I could learn it unless it is involved in one of my projects. Now that I am involved in a project with Spring, it is a great way to learn it.

I am starting to wrap my mind around the central idea of the Spring Framework which is Inversion of Control & Dependency Injection. Now that I'm starting to understand the framework, I realize that it truly is a great idea. In prior development I always had thoughts about the tight coupling between different areas of the application and wished I could do something about it.

Not that Spring wiring isn't cool enough, but Spring also includes some other very cool features such as Data access support, job scheduling, mail support, web flow, etc.

So I'm looking forward to getting a deep understanding of Spring & getting an app working from scratch with Spring.