Thursday, September 17, 2009

Tweaking grails pdf plugin

I had a little bit of problem when I wanted to use grails PDF plugin. (By the way Aaron Eischeid, thanks for providing this). However some things is not as wanted.

pdfLink: this is a nice feature for generating PDF by providing an url to the page to generate as PDF. But I do not want to provide parameters by adding them to the url in get-form.

pdfForm: Somewhat better, But this method gives some other problems. First of all, pictures is lost since the PDF only generates from string, also the layout is not automaticly included in the render call.

So I did the followin tweaks. Sorry Aron, but I had to hack into the PdfService.groovy and PdfController.groovy to get it work and also set up a template in a specific way.

This is what I did:
In PdfService.groovy I added a method to be able to generate a PDF with images for pdfForm, to do this the renderer need to know the baseUri to get find the image.

I added this code:
code

In the method pdfForm for the PdfController.groovy, I replaced code with call to the new method.
code


I then made a template with the following structure to also use the layout, without the applyTemplate, I only got the template code (also note that the template is looked for in views/pdf/):
code

I have not done this with the options for controller and action, just for template, since the g.include seams to return empty content. But I will continue to explore.

! An update. I realized that the pdf-plugin was build for grails 1.1. maybe this was the reason that the g.render in the plugin gave med nothing as a result. I was actually using grails 1.0.4. Using 1.1.1 however makes the action and controller work better. No pictures though, but they show when I am running pdf-plugin as an application after downloading the source.

I will try to find the proper place to address these issues to improve the plugin.

/Peter

Wednesday, September 9, 2009

Creating jasper reports in Grails

I like to work with Grails, so to continue making useful stuff i elaborated a little bit about how to make reports in Grails using Japser (which is a nice reporting tool)

My aim is to do reports in PDF and Excel. And since it don't think it is the f...ing business of the report file to know anything about the database I intend to use my own models to send into the report.

Create an application:
 grails create-app bookshelf

Install the plugin:
 grails install-plugin jasper
This usually takes a while since Plugin list cache is almost always out of date when installing plugins. So go take a coffe or something.

First of all I want a domain-class to hold books. So create that one and set up a few books in the BootStrap.groovy to have somthing to test with.
 grails create-domain-class Book


Add a few properties to the book, for example:

and add a few books in the BootStrap.groovy to have something

To create a report from Jasper, you need to create a .jrxml and then compile it to.jasper report file.


To create a report for my purpose, I used iReport. this is a tricky one because the jasper plugin which I got when installing is 0.9.7 and has a mistake in it that makes the reports fail. However to solve that look up the lib folder in the plugin-installment (using grails1.0.4 is in the project, but using 1.1.1 it ends up in your home folder) and remove the jasperreports-2.0.5.jar, otherwise you will end up with some NullPointerExceptions when tryring to run the report. Loocking there you see that the other jar file is jasperreports-3.1.2.jar, so the iReport version to download is 3.1.2.

I choosed the standalone version but there is a version that plugs into Netbeans.

Making the following report and compile it makes an .jasper file to use from grails.



Now things is starting to get a little bit interesting. We now need to make a report of this. I genereated all stuff for book by using the command:
 grails generate-all Book
we can then use the generated views to present the report.

Add the following line in the view to get a PDF and a XLS report:


(of course they should be tagged, but this stupid blog editor does not want to show it if if make them as tags)

for example under pagination, I used an action in the controller and a little bit of of code, not much really, to to what is needed.

That's it. Try it out yourself!

(For  the lazy one, a zip of the project can be found here: http://sites.google.com/site/ironicprogrammer/home/grails-and-jasper-1 as an attachement)

Friday, September 4, 2009

GroovyWS or Java WS Client?

We had a project this winter/spring which I designed to use Webservices on a web connected to EJBs, the clients we build in Grails for fast development.

Well we started using GroovyWS because that gave us the opportunity to get up and running pretty quick.

First we had some problems with the time it took to generate the stubs everytime we called a web service, however, this was becaus we stupidly put the creation of the client stubs in the actions so it was done every time. This was easy to sol by making a Groovy singleton class and let it create the service stubs once.

Eventually I decided to move over to making JAX-WS clients in Java using Netbeans. The reasons for this was mostly to try to get better performance and the possibility to put on SSL on the Web Service layer. From information I found and the experience during the first month of the project, I realised some shortcommings with GroovyWS mainly:
  • there is no support for https calls using GroovyWS
  • for some reason I had to wrap lists into a class. GroovyWS only regarded the list as one object and took the first one and skipped the rest and making workarounds in the WS-layer to fix this is something I don't want
  • restarting Glassfish sometimes made problems with the grailsapps and made them crash, this I think is because some of the services tokk some time to start and when GroovyWS tried to make stubs they were not there and after that the wars refused to work.
However making a WSClient in Java from Netbeans is an easy task, even if Java 1.6_07 or later is required for JAX-WS it is not such a big deal to make an jar-file and put it into the grailsapps lib folder, after that it is easy to call the java-classes from the Groovy code.

Grails i18n

Got a little bit pissed of regarding language changing on Grails.

Of the information I can find about it it should be fairly straightforward. Just providing the parameter "lang=xx". In my experience this works sometimes, but sometimes something magical happen running on Glassfish for a while. The switcing of language stops working. Don't know if this is a known problem.

Anyway, for now I made a workaround using the following code:
def beforeInterceptor = [action:this.&checkLang, except:['']]

    def checkLang() {
        if(session["lang"] == null){
            session["lang"] = "sv"
        }
        if(params.lang){
            session["lang"] = params.lang
        }
       if(params.lang && params.lang=="en"){
            def defaultLocale = new Locale("en","US")
            java.util.Locale.setDefault(defaultLocale)
       }         return
    }

Which works for now. Should think there is a better solution. Feel free to inform me if you know.