All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.discursive.dao.generic.spring.FinderIntroductionInterceptor Maven / Gradle / Ivy

The newest version!
package com.discursive.dao.generic.spring;

import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.beanutils.MethodUtils;
import org.hibernate.LockMode;
import org.springframework.aop.IntroductionInterceptor;

import com.discursive.dao.generic.hibernate.FinderExecutor;

public class FinderIntroductionInterceptor implements IntroductionInterceptor {
	public static final String FIND     = "find";
	public static final String ITERATE  = "iterate";
	public static final String SCROLL   = "scroll";
	public static final String LOCK     = "lock";
	public static final String UNIQUE   = "unique";
	public static final String COUNT    = "count";

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        Object target       = (Object) methodInvocation.getThis();
        String invokeMethod = null;
        LockMode lockMode   = LockMode.NONE;
        String methodName   = methodInvocation.getMethod().getName();
        Object[] arguments  = methodInvocation.getArguments();
        boolean done        = false;
        
        methodName = methodName.toLowerCase();
        while(!done) {
        	if (methodName.startsWith(FIND)) {
        		invokeMethod = "executeFinder";
                methodName = methodName.substring(FIND.length()); 
        	} else if (methodName.startsWith(LOCK)) {
                lockMode = LockMode.UPGRADE;
                methodName = methodName.substring(LOCK.length()); 
        	} else if (methodName.startsWith(ITERATE)) {
        		invokeMethod = "executeIterator";
                methodName = methodName.substring(ITERATE.length()); 
        	} else if (methodName.startsWith(SCROLL)) {
        		invokeMethod = "executeScroller";
                methodName = methodName.substring(SCROLL.length()); 
        	} else if (methodName.startsWith(UNIQUE)) {
        		invokeMethod = "executeUnique";
                methodName = methodName.substring(UNIQUE.length()); 
        	} else if (methodName.startsWith(COUNT)) {
        		invokeMethod = "executeCount";
                methodName = methodName.substring(COUNT.length()); 
        	} else {
        		done = true;
        	}
        }

        if (arguments == null) {
            arguments = new Object[0];
        }
        if (invokeMethod != null) {
            return MethodUtils.invokeExactMethod(target, invokeMethod, new Object[] { methodInvocation.getMethod(),
                    arguments, lockMode });
        } else {
            return methodInvocation.proceed();
        }
    }

    @SuppressWarnings("unchecked")
	public boolean implementsInterface(Class intf) {
        return intf.isInterface() && FinderExecutor.class.isAssignableFrom(intf);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy