Tuesday, November 8, 2016

Grails get retrieve GrailsParameterMap params from service class or util class

Have to import:
import org.springframework.web.context.request.RequestContextHolder

and use following:
def params = RequestContextHolder.getRequestAttributes().params

Reload refresh current url or redirect to another url using php script

Refresh/reload current page:
header("Refresh:0");

Or you can redirected to another page using:
header("Refresh:0; url=another-page.php");

Monday, November 7, 2016

Grails Hibernate check if and object is initialized

Hibernate.isInitialized(object)

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


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

Get caller class name from inherited static method using java

Base class to test caller call name extractor


package com.pritom.kumar;

/**
 * Created by pritom on 10/10/2016.
 */
abstract class StaticMethodCallerClassName {
    static String returnCallerClassNameUsingThreadStackTrace() {
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        return stackTraceElements[2].getClassName();
    }

    static String returnCallerClassNameUsingThrowable() {
        return new Throwable().getStackTrace()[2].getClassName();
    }

    static String returnCallerClassNameUsingSecurityManager() {
        Class[] classes = CallingClassManager.INSTANCE.getCallingClasses();
        return classes[classes.length - 2].getCanonicalName();
    }

    private static class CallingClassManager extends SecurityManager {
        static final CallingClassManager INSTANCE = new CallingClassManager();

        Class[] getCallingClasses() {
            return getClassContext();
        }
    }

    static void printElapsedTime(String title, Long startTime, String className) {
        startTime = System.nanoTime() - startTime;
        System.out.println(title + " took=" + ((double) startTime) / 1000000D + " ms and class_name=" + className);
    }
}

Example class 1

package com.pritom.kumar;

/**
 * Created by pritom on 10/10/2016.
 */
public class StaticMethodCallerClassNameTest1 extends StaticMethodCallerClassName {
    public static void main(String[] args) {
        x1();
    }

    static void x1() {
        long startTime = System.nanoTime();
        String className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThreadStackTrace();
        }
        printElapsedTime("ThreadStackTrace", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThrowable();
        }
        printElapsedTime("Throwable", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingSecurityManager();
        }
        printElapsedTime("SecurityManager", startTime, className);
    }
}


Example class 2

package com.pritom.kumar;

/**
 * Created by pritom on 10/10/2016.
 */
public class StaticMethodCallerClassNameTest2 extends StaticMethodCallerClassNameTest1 {
    public static void main(String[] args) {
        x2();
    }

    static void x2() {
        long startTime = System.nanoTime();
        String className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThreadStackTrace();
        }
        printElapsedTime("ThreadStackTrace", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThrowable();
        }
        printElapsedTime("Throwable", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingSecurityManager();
        }
        printElapsedTime("SecurityManager", startTime, className);
    }
}

Example class 3

package com.pritom.kumar;

/**
 * Created by pritom on 10/10/2016.
 */
public class StaticMethodCallerClassNameTest3 extends StaticMethodCallerClassNameTest2 {
    public static void main(String[] args) {
        x3();
    }

    static void x3() {
        long startTime = System.nanoTime();
        String className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThreadStackTrace();
        }
        printElapsedTime("ThreadStackTrace", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThrowable();
        }
        printElapsedTime("Throwable", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingSecurityManager();
        }
        printElapsedTime("SecurityManager", startTime, className);
    }
}

Cannot change column used in a foreign key constraint


Disabling foreign key check using following SQL:
SET FOREIGN_KEY_CHECKS=0;

Enabling foreign key check:
SET FOREIGN_KEY_CHECKS=1;

If you want it globally then:
SET GLOBAL FOREIGN_KEY_CHECKS=0;
SET GLOBAL FOREIGN_KEY_CHECKS=1;