Monday, November 30, 2009

Quote to think about

Started to read the book "Refactoring. Improving the Design of Existing Code" by Martin Fowler the other day. Here is a quotation from that book that is what programming is about:

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand"

So tru, so tru...

Monday, October 19, 2009

Simple Ajax sample

Using Ajax in Grails is quite simple. Grails have the prototype library making it easy to use Ajax in Grails.

You can try it out by creating a Grails-project. Here is an example ant some explanation of what is nescessary to think of.

1. Create a grails project:
2. Create a controller:

3. Create 2 .gsp-files in the /views/ajax folder; index.gsp and _itemList.gsp (_ means that it is a template)

4. Change the /views/layout/main.gsp by adding the following row

5. In the index.gsp in your ajax folder, put in this code:

6. In your _itemList.gsp put this code:

7. Now the only thing we need is some code in the controller. Open the AjaxController and put this code:

So what makes this work?

First of all, Grails supply the tags submitToRemote which is used for the "Add" button and remoteLink which is used for the "remove" link. These are used to use Ajax from grails and the only thing needed to make them work is to include the "prototype" javascript library, which we did in the layout to make it available on all pages. If you look on these tags you see the property action="" and update="listItems" this properties tells the framework what action to call and what to do with the result when the action is finished. Look at the index.gsp and you will see a <div> with the id="listItems", if you remember this is the same id that you saw as text in the update-property, this i the glue between the action pointed out and where to put the result. Pretty easy and straightforward, to make it short you need this
  1. Add prototype JavaScript library to the layout
  2. Add a DIV for the result
  3. Add some remote call and point it to the action you want to happen and where to put the result
Note : Ajax is used for enhanced functionality and to not to have to reload pages. Therefor using templates with small fragments of code is a convenient way to reuse code-blocks and functionality. It is better to think a bit ahead of what is needed to make nice solutions and reuse of code.

Wednesday, October 7, 2009

Making SSL calls with Grails

All of a sudden I realised that I have to call a service I have behind a SSL connection.

Using grails, this is easy to set up. Just create a couple of Groovy-classes and use it from the controller. It may be nescessary though to add the certificat of the SSL site to your JVM.

Adding the certificate
  • download  the certificate you need, name it to something usefull, for example theSite.cer.
  • use keytool to add it. (needs JAVA_HOME/bin in the path)
keytool -import -keystore /pathToJava/jre/lib/security/cacerts -file theSite.cer 

Class to make the call
this is the class to actually do the work, calling the SSL url you want to reach.
Class for password authentication
this class is needed to authenticate you at the remote site.

In the controller you can then use the class to get a responsestring from the remote server.


/Peter

Saturday, October 3, 2009

Grails and EJB

Read Jason Rudolphs article about grails and EJB a while ago. Grails + EJB Domain Models Step-by-Step. It is a nice article but not what I want.

However it seamed a little bit of inventing the wheel again to import the classes into Grails in the same package structure, and I also wanted some other things that EJB3 gave me.

  1. I want the EJB level to handle application rules. In that way, I can reuse the EJBs from other places as well
  2. I have information i two different schemas in Oracle and grails was not supporting more than one.
  3. I like "hit and run" session beans to provide services. which means that I use interface in the sessionbeans to provide CRUD and listing methods. 


So I started to elaborate with putting the EJB.jar file in Grails library folder to try it out. It worked out nice, the only thing I have to do is to put some lookup code and start using the session beans. I also get access to all model POJOs.

I like this approach because I can use grails rapid application development with the robustness of EJB3 without doing very much extra.

The downside is that as I am a little bit lazy and avoid writing too much code, is that I have to run it on the same glassfish and I have to copy the jar-file every time I change the interface. I also have to make a war of the grails application and run it on glassfish.

 /Peter

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.