i have a need to block or cancel a save of a domain object based on some property.
Can this be done in a constraint?
Example:
An ‘Order’ domain object has a state of ‘invoiced’ then the order should not be able to be updated anymore..
Any suggestions on how to tackle this?
I see no reason why you couldn’t simply use a constraint for this (as you suggested). Something like this should do it
If for some reason you can’t use a constraint, here are a couple of alternative suggestions:
Meta-Programming
Use Groovy’s method-interception capabilities to intercept calls to
save(). Your interceptor should only forward the call to the interceptedsave()if the order does not have an invoiced state.There are some good examples of how to do this in the Programming Groovy book
GORM Events
GORM provides a number of events that are triggered during a persisted objects lifecycle. It may be possible in the
beforeUpdateorbeforeValidateevents to prevent updating the object (I guess throwing an exception would work)