New Grails plugin – Timestamped

June 3, 2013 at 09:46

I recently created a small Grails plugin to use it as part of my work at tadoº, the awesome company I’m currently working for 🙂 .

The plugin aims to provide a more declarative way of making a domain class auto timestamped. Right now, for making a domain class auto timestamped, you need to declare two properties with the proper type, which is a bit of a DRY violation. Also, these properties aren’t part of the domain class logic, so I prefer them to be declared elsewhere.

With these things in mind, the plugin provides an annotation which, when used in a domain class, triggers an AST transform that inject the auto timestamping properties in that domain class. The most simple example would be as following:

package timestampeddemo
import com.tado.timestamped.transform.Timestamped
@Timestamped
class FullTimestamped {
}

Since the most common option is to inject both properties, the default is to do so, but you can configure which events you want to consider:

package timestampeddemo
import com.tado.timestamped.transform.Timestamped
@Timestamped(create=false)
class UpdateTimestamped {
}
package timestampeddemo
import com.tado.timestamped.transform.Timestamped
@Timestamped(update=false)
class CreationTimestamped { }

The plugin is prepared to work with the joda-time plugin, so if present, the generated timestamping properties will be typed as org.joda.time.Instant instead of the old, not-so-good java.util.Date. But if you happen to prefer any other class, you can specify it as a part of the Timestamped annotation:

package timestampeddemo
import com.tado.timestamped.transform.Timestamped
@Timestamped(clazz='java.lang.Long')
class CustomClassTimestamped {}

This, of course, only make sense if you provide the proper persistence auto timestamping support for the specified class, as happens for example with the joda-time plugin.

Last, but not least, the plugin can be included as a tradicional plugin or as a maven dependency. The later allows the plugin to be used by other plugins, as it will be precompiled and the AST transform will be then available in plugin compile time as well. More info on this subject can be found in the installation chapter of the guide.