package com.axelor.db;

import java.util.Properties;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.AbstractModule;
import com.google.inject.persist.PersistService;
import com.google.inject.persist.jpa.JpaPersistModule;

/**
 * A Guice module to configure JPA.
 * 
 * This module takes care of initialising JPA and registers an Hibernate custom
 * scanner that automatically scans all the classpath entries for Entity
 * classes.
 * 
 */
public class JpaModule extends AbstractModule {
	
	private static Logger log = LoggerFactory.getLogger(JpaModule.class);

	private String jpaUnit;
	private boolean autoscan;
	private boolean autostart;

	/**
	 * Create new instance of the {@link JpaModule} with the given persistence
	 * unit name.
	 * 
	 * If <i>autoscan</i> is true then a custom Hibernate scanner will be used to scan
	 * all the classpath entries for Entity classes.
	 * 
	 * If <i>autostart</i> is true then the {@link PersistService} will be started
	 * automatically.
	 * 
	 * @param jpaUnit
	 *            the persistence unit name
	 * @param autoscan
	 *            whether to enable autoscan
	 * @param autostart
	 *            whether to automatically start persistence service
	 */
	public JpaModule(String jpaUnit, boolean autoscan, boolean autostart) {
		this.jpaUnit = jpaUnit;
		this.autoscan = autoscan;
		this.autostart = autostart;
	}

	/**
	 * Create a new instance of the {@link JpaModule} with the given persistence
	 * unit name with <i>autoscan</i> and <i>autostart</i> enabled.
	 * 
	 * @param jpaUnit
	 *            the persistence unit name
	 */
	public JpaModule(String jpaUnit) {
		this(jpaUnit, true, true);
	}

	@Override
	protected void configure() {
		log.info("Initialize JPA...");
		Properties properties = new Properties();
		properties.put("hibernate.connection.autocommit", "false");
		properties.put("hibernate.ejb.interceptor", "com.axelor.auth.db.AuditInterceptor");
		if (this.autoscan) {
			properties.put("hibernate.ejb.resource_scanner", "com.axelor.db.JpaScanner");
		}
		install(new JpaPersistModule(jpaUnit).properties(properties));
		if (this.autostart) {
			bind(Initializer.class).asEagerSingleton();
		}
		bind(JPA.class).asEagerSingleton();
	}

	public static class Initializer {

		@Inject
		Initializer(PersistService service) {
			service.start();
		}
	}
}

@@ -698,16 +698,50 @@
 tostart;
+%0A%09%0A%09private Properties properties;
 %0A%0A%09/**%0A%09
@@ -1738,16 +1738,359 @@
 rue);%0A%09%7D
+%0A%09%0A%09/**%0A%09 * Configures the JPA persistence provider with a set of properties.%0A%09 * %0A%09 * @param properties%0A%09 *            A set of name value pairs that configure a JPA persistence%0A%09 *            provider as per the specification.%0A%09 */%0A%09public JpaModule properties(final Properties properties) %7B%0A%09%09this.properties = properties;%0A%09%09return this;%0A%09%7D
 %0A%0A%09@Over
@@ -2199,24 +2199,33 @@
 erties();%0A%09%09
+if (this.
 properties.p
@@ -2226,143 +2226,62 @@
 ties
-.put(%22hibernate.connection.autocommit%22, %22false%22);%0A%09%09properties.put(%22hibernate.ejb.interceptor%22, %22com.axelor.auth.db.AuditInterceptor%22);
+ != null) %7B%0A%09%09%09properties.putAll(this.properties);%0A%09%09%7D
 %0A%09%09i
@@ -2385,16 +2385,78 @@
 %22);%0A%09%09%7D%0A
+%09%09properties.put(%22hibernate.connection.autocommit%22, %22false%22);%0A
 %09%09instal

Support for configuring JpaModule with properties.