Amit Jain

Grails

Grails : Difference b/w two dates with fine-grained details

Hi Friends, To retrieve difference between two dates with finer details which would display their difference as 2 years 7 weeks 3 days 2 hours 5 mins ago is given below. /** * Returns differnce b/w new Date and old date as Map holding difference in years, weeks, days, hrs, mins & secs */ public static Map...

14-May-2010

Grails

Grails : Save time while writing Integration tests

Hi Friends, I always used to spend lot of time writing integration tests as running even a single test involves loading the complete application, which includes loading complete database every time. Then I realized that data which I use for testing is always the same, why can't we restore the database once created, before we run the...

06-May-2010

Technology

Backup & Restore MySql Database Table(s)

Hi Friends, I needed to take the backup and restore the selected table(s) of mysql database. And I found Gaurav Chauhan's blog on taking dump of the database and to restore the database. So I tried with the same commands but with table names passed as parameters and that worked. mysqldump -u root -p my_database Table1 Table2 >...

23-Apr-2010

Technology

XmlSlurper made it simple

Hi! We had a myForm.html file (accessible only from the third party) where in we needed to update the form containing input/select/check boxes with the values we had in the request params and then render it. So we used xmlslurper for the same. Without much effort, it just worked great. Following is the code we wrote: import...

26-Mar-2010

Grails

Grails: handling StaleObjectStateException and making executeUpdate transactional

Recently I encountered a StaleObjectStateException which said "Row was updated or deleted by another transaction." We were just updating multiple objects of the same domain inside a loop. So we used executeUpdate statement instead of myClass.save() method to update the records, which worked. ReserveAccount.executeUpdate("update...

06-Mar-2010

Grails

My experience of using jasper reports with grails application

In my project we needed to generate certain pdf reports. Then while exploring the options available, I encountered a PDF plugin, which renders gsp page and returns the PDF report. It looked good for the simple reports but may not be a good idea if the report are complex. Then with the help of one of my collegue, we decided to rather...

25-Feb-2010

Grails

Grails Integration Tests : Access your application path

Hi Friends, Recently I needed to access my grails application's path while writing an integration test. I tried to do it with servlet context, Application Holder and few more options. But none worked. Then I encountered the simplest way as given below, which worked. System.properties['base.dir'] Cheers! ~~Amit Jain~~ ...

21-Jan-2010

Grails

Login user for Integration test when using Jsecurity plugin

Hello Friends, I was using Jsecurity plugin in my project. There was an action in a controller which needed logged in user information and I was finding it difficult to write an integration test for the same. Then Brent Fisher shared the following code which worked nicely for both services and controllers: import...

07-Dec-2009

Grails

Grails: Few tips for writing integration tests

Just thought I would share with you few tips or key points we should remember while writing integration test cases: 1.  Flush and clear the session object for every single test case for example class FooTests extends GroovyTestCase { def sessionFactory void setUp() { sessionFactory.currentSession.flush() ...

05-Nov-2009

Technology

Identify merchant provider from the given credit card no.

Hi Friends, I was working on the financial application, where the user doesn't want to select merchant provider while feeding in credit card details and yet we needed to know the merchant provider. Following is the code that helped: function getMerchantProvider(cardNo){ //cardNo is the credit card number ...

04-Nov-2009

Technology

Add hotkeys to the web application

Let us have a look at the simple javascript code, that can be used to add hotkeys to our web application. I tried using jquery hotkeys plugin and two more plugins, but they didn't work for me. So I ended up handling the keydown event of my own. var isAlt = false; document.onkeyup = function(e) { if (e.which == 18) //18 is...

28-Sep-2009

Grails

Grails: reverse url mapping to generate urls ending with “.html”

Hi Friends, Recently I came across a problem, where the url of the links were to be generated dynamically based on the controller and action to be executed and  should end with ".html". Reverse url mappings came to my rescue, which can be implemented using <g:link> GSP tag. Then I tried this, In UrlMappings.groovy file : ...

28-Aug-2009