package fr.openwide.core.jpa.hibernate.ejb;

import java.util.Map;

import org.hibernate.Interceptor;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Inject an Hibernate Interceptor managed by Spring in the SessionFactory.
 * 
 * The original idea came from the following blog:
 * http://blog.krecan.net/2009/01
 * /24/spring-managed-hibernate-interceptor-in-jpa/
 * but has been completely overhauled with the ORM 4.3 upgrade.
 */
public class InterceptorAwareHibernatePersistenceProvider extends HibernatePersistenceProvider {

	private static final Logger LOGGER = Logger.getLogger(InterceptorAwareHibernatePersistenceProvider.class);

	@Autowired
	private Interceptor interceptor;

	@Override
	@SuppressWarnings("rawtypes")
	public EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(
			PersistenceUnitDescriptor persistenceUnitDescriptor, Map integration, ClassLoader providedClassLoader) {
		return new EntityManagerFactoryBuilderImpl(persistenceUnitDescriptor, integration, providedClassLoader) {
			@Override
			protected void populate(SessionFactoryBuilder sfBuilder, StandardServiceRegistry ssr) {
				super.populate(sfBuilder, ssr);
				
				if (InterceptorAwareHibernatePersistenceProvider.this.interceptor != null) {
					LOGGER.warn("Installing our Spring managed interceptor.");
					sfBuilder.applyInterceptor(InterceptorAwareHibernatePersistenceProvider.this.interceptor);
				}
			}
		};
	}

}

@@ -61,16 +61,114 @@
 l.Map;%0A%0A
+import javax.persistence.EntityManagerFactory;%0Aimport javax.persistence.spi.PersistenceUnitInfo;%0A%0A
 import o
@@ -463,74 +463,16 @@
 oot.
-spi.EntityManagerFactoryBuilder;%0Aimport org.hibernate.jpa.boot.spi
+internal
 .Per
@@ -475,32 +475,36 @@
 .PersistenceUnit
+Info
 Descriptor;%0Aimpo
@@ -1144,221 +1144,551 @@
 ;%0A%0A%09
-@Override%0A%09@SuppressWarnings(%22rawtypes%22)%0A%09public EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(%0A%09%09%09PersistenceUnitDescriptor persistenceUnitDescriptor, Map integration, ClassLoader providedClassLoader
+/**%0A%09 * 2017-05-24 %C2%B7 reworked from SpringHibernateJpaPersistenceProvider so that we can inject a custom%0A%09 * %7B@link EntityManagerFactoryBuilderImpl%7D; previous implementation that overrides%0A%09 * %7B@link InterceptorAwareHibernatePersistenceProvider#getEntityManagerFactoryBuilder%7D no longer works%0A%09 * as there are several paths with various arguments and the overloaded one was no longer called.%0A%09 */%0A%09@Override%0A%09@SuppressWarnings(%22rawtypes%22)%0A%09public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties
 ) %7B%0A
@@ -1732,17 +1732,21 @@
 derImpl(
-p
+new P
 ersisten
@@ -1751,16 +1751,20 @@
 enceUnit
+Info
 Descript
@@ -1769,42 +1769,26 @@
 ptor
-, integration, providedClassLoader
+(info), properties
 ) %7B%0A
@@ -2187,16 +2187,24 @@
 %09%09%09%7D%0A%09%09%7D
+.build()
 ;%0A%09%7D%0A%0A%7D%0A

fix hibernate's interceptor handling