This plugin allows you to do a logical deletion of the domain classes. The main intention of the plugin is to handle cases when certain entities cannot be physically removed from the database.
The plugin is now in the grails.org repository: Gorm Logical Delete Plugin.
To install it, add it to your BuildConfig.groovy:
compile "org.grails.plugins:gorm-logical-delete:1.1"
A boolean property is injected in the annotated domain class using AST transformations. This property is used to track the logical deleted state of the entity. The name and deleted state value can be customized.
The GORM method delete() is modified to avoid the physical removal and just change the value of the delete state property.
To handle read queries, a PreQueryEvent listener is added in order to make the logical delete logic transparent, adding another criteria to the query so it doesn't match deleted entities.
To provide logical deletion to a domain class you just need to add the @GormLogicalDelete annotation to it.
@GormLogicalDelete
class User {
String lastName
String firstName
String nickName
...
}
To make a logical removal you just need to use the GORM method delete.
user.delete()
which would be the equivalent to: (using default settings)
user.deleted = true
user.save()
If you want to force a physical deletion to an annotated class, you have to add the logicalDelete parameter set to false to the delete method:
user.delete(logicalDelete: false)
If you want to query the logically deleted elements, you can use the withDeleted method to execute a closure that includes the deleted items:
user.withDeleted {
def deletedUserList = User.list()
}
The plugin also supports customization of the property used and the deletedState value, in order to support current manual implementations. For example, if your current implementation relies on the enabled property being set to false in order to logically delete an entity, you would declare your annotation like this:
@GormLogicalDelete(property = "enabled",deletedState = false)
class User {
String lastName
String firstName
String nickName
...
}
It defaults to property = "deleted" and deletedState = true
There's a GDSL file bundled with code-completion for the deletdState property and withDeleted method.
This plugin is a fork from the logical-delete plugin. The original plugin relied on Hibernate filters and HawkEventing, and that wouldn't work for me since I needed it to stay 100% on GORM to be database-agnostic.