Code Junkie
Code Junkie

Reputation: 7788

Groovy / Grails / Gorm dates with no minutes.

I have a composite primary key with a date.

id composite: ["division", "expenseDate"]

String division
Date expenseDate

def getPK() {
    ["division":division, "expenseDate":expenseDate]
}

I'm generating page links like so.

<g:link controller="recoveryDetail" action="edit" params="${it.getPK()}">
    <g:formatDate format="MM/dd/yyyy" date="${it.expenseDate}" />
</g:link>

I end up with a generated URL like so

http://localhost:8080/pisr/recoveryDetail/edit?division=BUFFALO&expenseDate=2007-04-25+00%3A00%3A00.0

I'd like to eliminate the time from appearing in the URL.

I also find it difficult to compare objects do to the fact the dates coming from the database have a time of zero, but my local objects have the local time. Example "division":"BUFFALO","expenseDate":"2015-07-07T04:00:00Z". How do I return a zero time rather than my local time.

Lastly I'm using the grails datePicker component and when I receive the date with params, I'm getting expenseDate_day:7, expenseDate_year:2015, expenseDate_month:7. How do I convert these back to a date object in groovy so that I can do the following query against the date?

RecoveryDetail.createCriteria().get  {
    eq("division", recoverySetupInstance.division)
    eq("expenseDate", expenseDate)
}

Upvotes: 0

Views: 203

Answers (1)

V H
V H

Reputation: 8587

I will try to answer this after comment from: Koloritnij

The resolution in this case is to change getPK to format date correctly. All code junkie is doing through getPK is creating his own parameter map in the domainClass which he could manually do when required in gsp:

So here are the different styles in GSP, with the correct call required in this instance at the top:

<g:link action="edit" params="${instance.getPK2()}">
<g:formatDate format="MM/dd/yyyy" date="${instance.expenseDate}" />  - PK2 WAY
</g:link><br>

<g:link action="edit" params="${instance.getPK()}">
<g:formatDate format="MM/dd/yyyy" date="${instance.expenseDate}" />  - PK WAY
</g:link><br>

<g:link action="edit" params="${["division":instance.division, "expenseDate":g.formatDate(format:'MM/dd/yyy', date:instance.expenseDate)]}">
<g:formatDate format="MM/dd/yyyy" date="${instance.expenseDate}" />  - Formatted using g.format
</g:link><br>

<g:link action="edit" params="${["division":instance.division, "expenseDate":instance.expenseDate.format("MM/dd/yyy")]}">
    <g:formatDate format="MM/dd/yyyy" date="${instance.expenseDate}" />  - Formatted using format
</g:link><br>

Various GSP styles doing identical things.

DomainClass with new getPK2 (which formats date right at the core)

class RecoveryDetail {

    //id composite: ["division", "expenseDate"]

    String division
    Date expenseDate

    def getPK() {
        ["division":division, "expenseDate":expenseDate]
    }

    def getPK2() {
        ["division":division, "expenseDate":expenseDate.format("MM/dd/yyy")]
    }
}

Your controller outputting this:

class RecoverDetailController {
    def index() {
        def instance = RecoveryDetail.findByDivision('foo')
        [instance:instance]
    }

    def edit() { 
        render "params are ${params}"
        // -PK2 way:
        //params are [division:foo, expenseDate:07/08/2015, action:edit, format:null, controller:recoverDetail]

        // - PK WAY
        // params are [division:foo, expenseDate:2015-07-08 17:53:48.565, action:edit, format:null, controller:recoverDetail]

        //- Formatted using g.format
        //params are [division:foo, expenseDate:07/08/15, action:edit, format:null, controller:recoverDetail]

        //- Formatted using format
        //params are [division:foo, expenseDate:07/08/2015, action:edit, format:null, controller:recoverDetail]
    }

}

Your bootstrap loading in a dummy test entry:

import test.RecoveryDetail
class BootStrap {
    def init = { servletContext ->
        RecoveryDetail.findOrSaveWhere(division: 'foo', expenseDate:new Date()).save(flush:true)
    }
}

Upvotes: 1

Related Questions