Showing posts with label hooking GormInstanceApi. Show all posts
Showing posts with label hooking GormInstanceApi. Show all posts

Monday, November 7, 2016

Hooking into or extends the GormInstanceApi methods of the GORM API for Grails

Main class defination


package com.autobill.enhancer

import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass
import org.codehaus.groovy.grails.orm.hibernate.HibernateDatastore
import org.codehaus.groovy.grails.orm.hibernate.HibernateGormInstanceApi
/**
 * Created by pritom on 17/10/2016.
 */
class MyCustomInstanceApi<D> extends HibernateGormInstanceApi<D> {
    private HibernateGormInstanceApi gormInstanceApi

    MyCustomInstanceApi(HibernateGormInstanceApi gormInstanceApi) {
        super(gormInstanceApi.persistentClass, gormInstanceApi.datastore as HibernateDatastore, gormInstanceApi.classLoader)
        this.gormInstanceApi = gormInstanceApi
    }

    @Override
    D save(D instance) {
        println("Saving-${instance}")
        super.save(instance)
    }

    static void init(servletContext) {
        servletContext.grailsApplication.domainClasses.each { DefaultGrailsDomainClass domainClass ->
            def gormInstanceApi = domainClass.clazz.currentGormInstanceApi()
            domainClass.clazz.setInstanceGormInstanceApi(new MyCustomInstanceApi(gormInstanceApi))
        }
    }
}

And invoke MyCustomInstanceApi.init(servletContext) from Bootstrap.groovy