tweak domain class object Date lastUpdated

buteh

New member
I am trying to tweak the Date lastUpdated property in my domain class so that it only changes when one of the fields(lastName) is updated. Cant seem to get it to work...My code is below..Please i need help

package com.bt.helloworld

class Person {

String firstName
String lastName
Date lastUpdated
Date dateCreated

static mapping ={
//autoTimestamp false
}

static constraints = {
lastUpdated(nullable:true)
dateCreated(nullable:true)

}
def beforeUpdate = {
Person currentPerson = new Person()
//currentPerson = Person.findById(this.id)
if (!currentPerson.lastName.equals(this.lastName)) {
lastUpdated = new Date()
}

if(!currentPerson.firstName.equals(this.firstName)){

lastUpdated = null
}



}
//def onLoad = {
//firstName = this.firstName
//lastName = this.lastName

//}


//String toString() {"${this.firstName} : ${this.lastName} : ${this.lastUpdated}} "}
}
 

buteh

New member
add to info

in addition to the info i have up there, the config in the bootstrap is below

class BootStrap {

def init = { servletContext ->

new Person(lastName:"xxxx", firstName:"xxxxx").save(failOnError: true)
new Person (firstName:"xxxx", lastName:"xxxx").save(failOnError: true)


the Persons objects are initialized in the bootstrap
 

WillK

New member
Re:tweak domain class

I think you really want it to be checked in the edit 'method' of the Person controller
or anywhere a Person is potentially edited. So there could be the dateLastUpdated
field in the gsp but only use it to change the person in the database when the last name is different from that in the database.

Hopefully that makes sense
Will
 

prashant.kum

New member
dude donot put your dirty logic use availabe feature on grail


dirtyPropertyNames
example which may help
Person person = Person.get(1)

person.firstname = "prashant"

println "Output -: ${person.dirtyPropertyNames}"

//Output -: ['firstname']
 
Top