// Path: rjb-blog-multitenancy-extras/src/main/java/com/rjb/blog/multitenancy/context/TenantResolver.java
// public interface TenantResolver {
// 
// 	String getTenantId();
// 
// }
// 
// Path: rjb-blog-multitenancy-extras/src/main/java/com/rjb/blog/multitenancy/dao/DataSegmentationException.java
// public class DataSegmentationException extends DaoException {
// 
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = 6002236837189690311L;
// 
// 	public DataSegmentationException(String message) {
// 		super(message);
// 	}
// }
// 
// Path: rjb-blog-multitenancy-starter/src/main/java/com/rjb/blog/multitenancy/util/ArrayUtil.java
// public class ArrayUtil {
// 
// 	public static <T> boolean isEmpty(T[] a) {
// 		return a == null || a.length == 0;
// 	}
// 
// 	public static <T> int indexOf(T[] array, T object) {
// 		int i = 0;
// 		for (T t : array) {
// 			if (t == object || t.equals(object))
// 				return i;
// 			i++;
// 		}
// 		return -1;
// 	}
// }
// 
// Path: rjb-blog-multitenancy-starter/src/main/java/com/rjb/blog/multitenancy/util/StringUtil.java
// public class StringUtil {
// 
// 	public static boolean isEmpty(String s) {
// 		return s == null || s.isEmpty();
// 	}
// 
// 	public static String[] split(String s, String quote) {
// 		return split(s, quote, true);
// 	}
// 
// 	public static String[] split(String s, String quote, boolean trim) {
// 		String[] returnVal = s.split(Pattern.quote(quote));
// 		if (!trim)
// 			return returnVal;
// 		for (int i = 0; i < returnVal.length; i++) {
// 			returnVal[i] = returnVal[i].trim();
// 		}
// 		return returnVal;
// 	}
// }


import java.io.Serializable;
import org.hibernate.EmptyInterceptor;
import com.rjb.blog.multitenancy.context.TenantResolver;
import com.rjb.blog.multitenancy.dao.DataSegmentationException;
import com.rjb.blog.multitenancy.util.ArrayUtil;
import com.rjb.blog.multitenancy.util.StringUtil;

package com.rjb.blog.multitenancy.hibernate;




public class MultiTenantEntityInterceptor extends EmptyInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2372143420877673397L;

	private TenantResolver tenantResolver;

	public MultiTenantEntityInterceptor(TenantResolver tenantResolver) {
		this.tenantResolver = tenantResolver;
	}

	@Override
	public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
			String[] propertyNames, org.hibernate.type.Type[] types) {
		return this.handleTenant(entity, id, currentState, propertyNames, types);
	}

	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames,
			org.hibernate.type.Type[] types) {
		return this.handleTenant(entity, id, state, propertyNames, types);
	}

	private boolean handleTenant(Object entity, Serializable id, Object[] currentState, String[] propertyNames,
			org.hibernate.type.Type[] types) {

		int index = ArrayUtil.indexOf(propertyNames, "tenantId");
		if (index < 0) {
			return false;
		}


// Path: rjb-blog-multitenancy-extras/src/main/java/com/rjb/blog/multitenancy/context/TenantResolver.java
// public interface TenantResolver {
// 
// 	String getTenantId();
// 
// }
// 
// Path: rjb-blog-multitenancy-extras/src/main/java/com/rjb/blog/multitenancy/dao/DataSegmentationException.java
// public class DataSegmentationException extends DaoException {
// 
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = 6002236837189690311L;
// 
// 	public DataSegmentationException(String message) {
// 		super(message);
// 	}
// }
// 
// Path: rjb-blog-multitenancy-starter/src/main/java/com/rjb/blog/multitenancy/util/ArrayUtil.java
// public class ArrayUtil {
// 
// 	public static <T> boolean isEmpty(T[] a) {
// 		return a == null || a.length == 0;
// 	}
// 
// 	public static <T> int indexOf(T[] array, T object) {
// 		int i = 0;
// 		for (T t : array) {
// 			if (t == object || t.equals(object))
// 				return i;
// 			i++;
// 		}
// 		return -1;
// 	}
// }
// 
// Path: rjb-blog-multitenancy-starter/src/main/java/com/rjb/blog/multitenancy/util/StringUtil.java
// public class StringUtil {
// 
// 	public static boolean isEmpty(String s) {
// 		return s == null || s.isEmpty();
// 	}
// 
// 	public static String[] split(String s, String quote) {
// 		return split(s, quote, true);
// 	}
// 
// 	public static String[] split(String s, String quote, boolean trim) {
// 		String[] returnVal = s.split(Pattern.quote(quote));
// 		if (!trim)
// 			return returnVal;
// 		for (int i = 0; i < returnVal.length; i++) {
// 			returnVal[i] = returnVal[i].trim();
// 		}
// 		return returnVal;
// 	}
// }


// Path: rjb-blog-multitenancy-extras/src/main/java/com/rjb/blog/multitenancy/hibernate/MultiTenantEntityInterceptor.java
import java.io.Serializable;
import org.hibernate.EmptyInterceptor;
import com.rjb.blog.multitenancy.context.TenantResolver;
import com.rjb.blog.multitenancy.dao.DataSegmentationException;
import com.rjb.blog.multitenancy.util.ArrayUtil;
import com.rjb.blog.multitenancy.util.StringUtil;

package com.rjb.blog.multitenancy.hibernate;




public class MultiTenantEntityInterceptor extends EmptyInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2372143420877673397L;

	private TenantResolver tenantResolver;

	public MultiTenantEntityInterceptor(TenantResolver tenantResolver) {
		this.tenantResolver = tenantResolver;
	}

	@Override
	public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
			String[] propertyNames, org.hibernate.type.Type[] types) {
		return this.handleTenant(entity, id, currentState, propertyNames, types);
	}

	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames,
			org.hibernate.type.Type[] types) {
		return this.handleTenant(entity, id, state, propertyNames, types);
	}

	private boolean handleTenant(Object entity, Serializable id, Object[] currentState, String[] propertyNames,
			org.hibernate.type.Type[] types) {

		int index = ArrayUtil.indexOf(propertyNames, "tenantId");
		if (index < 0) {
			return false;
		}


		String activeTenantId = this.tenantResolver.getTenantId();
