Tuesday, November 23, 2010

Case Insensitive withCriteria

I found this easy way to do a case insensitive query on the DB using with Criteria. Unfortunately Grails documentation does not mention this, but remember you are using Hibernate. So using .ignoreCase() on the eq works perfectly.


 personList = Person.withCriteria {
  if (params.firstName)
   eq('firstName', params.firstName).ignoreCase()
  if (params.lastName)
   eq('lastName', params.lastName).ignoreCase()
  if (params.login)
   eq('login', params.login).ignoreCase()
  if (params.personHashId)
   like('personHashId', params.personHashId)
  maxResults(Constants.SEARCH_MAX_RESULTS)
 }

Wednesday, November 3, 2010

Elvis Operator

Is really neat and not used enough. Basically it is a modified ternary.
Here is and example for returning a sensible value if an expressions resolves to null (or false).
    def a = b ?: "No Value"
In that example a should equal b unless b is null or false, then it would be assigned "No Value"

Monday, October 18, 2010

Service and DB connection pools

(From Kwang Ho-Lee's investigations)

When using a Grails service that is transactional (default) and it uses the DB, the service will try and get a connection to the DB from the pool each time it is called.
So it is a bad idea to have code in a controller that calls a service in a loop. The loop should really be in the service, this will prevent multiple connections being made, plus it makes better design sense.

Wednesday, October 13, 2010

Time durations

Some code that shows durations and works nicely as a timer
    import groovy.time.*

    def timeStart = new Date()
    // Some code you want to time
    def timeStop = new Date()
    TimeDuration duration = TimeCategory.minus(timeStop, timeStart)
    println duration
This will give you the time in hours, minutes, seconds

Monday, September 27, 2010

Great tool

Found a nice tool to track down exactly what XML/SOAP your grails app is building. If you run your application locally you can easily see the outbound XML by using this tool.

HttpAnalyzer

Monday, September 13, 2010

Using the Grails Console

Thanks to Kwang Ho Lee for this

Using the console is very powerful, your console actually runs within your current
environment so it is a very powerful place to rapidly prototype code.

Start the console with something like this, allows you to attach to any environment
  @echo off
  set JAVA_OPTS=-Xmx1024m -XX:MaxPermSize=256m
  grails -Dgrails.env=dev console

Then from within the console you have access to domain objects and the service objects. Just import domain objects and do a getBean to obtain the service objects

  import domain.*
  def aService = grailsApplication.mainContext.getBean('aService')
  def dlr = Dealer.findByDealerId('XXX')
  def qs = aService.bigFinder(dlr)

Sunday, August 22, 2010

Paginations to list

Grails has a great way of paginating domain object lists. It is just build in an works well.
But I had a list object that I needed to pagination, so I found this great post

List.metaClass.paginate = { max, offset=0 ->
  delegate.subList( offset, Math.min( offset + max, delegate.size() ) )
}
 
def list = (1..30).toList()
assert list == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
  18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
assert list.paginate( 5, 7 ) == [8, 9, 10, 11, 12]
assert list.paginate( 5 )    == [1, 2, 3, 4, 5]
assert list.paginate( 5, 0 ) == [1, 2, 3, 4, 5]
assert list.paginate( 5, 1 ) == [2, 3, 4, 5, 6]