Monday, November 7, 2016

Hooking into or extends the GormStaticApi 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.HibernateGormStaticApi
/**
 * Created by pritom on 17/10/2016.
 */
class MyCustomStaticApi<D> extends HibernateGormStaticApi<D> {
    private HibernateGormStaticApi gormStaticApi

    MyCustomStaticApi(HibernateGormStaticApi gormStaticApi) {
        super(gormStaticApi.persistentClass, gormStaticApi.datastore as HibernateDatastore, gormStaticApi.gormDynamicFinders, gormStaticApi.classLoader, gormStaticApi.transactionManager)
        this.gormStaticApi = gormStaticApi
    }

    @Override
    D read(Serializable id) {
        println("Reading-${id}")
        super.read(id)
    }

    static void init(servletContext) {
        servletContext.grailsApplication.domainClasses.each { DefaultGrailsDomainClass domainClass ->
            def gormStaticApi = domainClass.clazz.currentGormStaticApi()
            domainClass.clazz.setStaticGormStaticApi(new MyCustomStaticApi(gormStaticApi))
        }
    }
}

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


No comments:

Post a Comment