Friday, March 7, 2014

Overloading or Extending standard GORM CRUD methods


class BootStrap {
    def grailsApplication;

    def init = { servletContext ->
        grailsApplication.domainClasses.each {
            def domainClass = it;

            def savem = domainClass.metaClass.getMetaMethod("save");
            domainClass.metaClass.save = {
                // do your before save work here...
                def ins = savem.invoke(delegate);
                // do your after save work here...
                return ins;
            }

            def savem2 = domainClass.metaClass.getMetaMethod("save", Map);
            domainClass.metaClass.save = { Map paramMap ->
                // do your before save work here...
                def ins = savem2.invoke(delegate, paramMap);
                // do your after save work here...
                return ins;
            }

            def deletem = domainClass.metaClass.getMetaMethod("delete");
            domainClass.metaClass.delete = {
                // do your before delete work here...
                def ins = deletem.invoke(delegate);
                // do your after delete work here...
                return ins;
            }

            def deletem2 = domainClass.metaClass.getMetaMethod("delete", Map);
            domainClass.metaClass.delete = { Map paramMap ->
                // do your before delete work here...
                def ins = deletem2.invoke(delegate, paramMap);
                // do your after delete work here...
                return ins;
            }
        }
    }
    def destroy = {

    }
}

No comments:

Post a Comment