Merge pull request #8 from ebenezergraham/FINCN-2

Migrated ORM to EclipseLink
diff --git a/build.gradle b/build.gradle
index 4e0721b..a558607 100644
--- a/build.gradle
+++ b/build.gradle
@@ -34,7 +34,8 @@
         springcontext        : '4.3.3.RELEASE',
         springboot           : '1.4.1.RELEASE',
         findbugs             : '3.0.1',
-        frameworklang        : '0.1.0-BUILD-SNAPSHOT'
+        frameworklang        : '0.1.0-BUILD-SNAPSHOT',
+        eclipselink          : '2.7.4'
 ]
 
 apply plugin: 'java'
@@ -59,9 +60,9 @@
             [group: 'org.springframework', name: 'spring-context', version: versions.springcontext],
             [group: 'com.google.code.findbugs', name: 'jsr305', version: versions.findbugs],
             [group: 'org.apache.fineract.cn', name: 'lang', version: versions.frameworklang],
-            [group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: versions.springboot],
             [group: 'com.jolbox', name: 'bonecp', version: '0.8.0.RELEASE'],
-            [group: 'org.flywaydb', name: 'flyway-core', version: '4.0.1']
+            [group: 'org.flywaydb', name: 'flyway-core', version: '4.0.1'],
+            [group: 'org.eclipse.persistence', name: 'org.eclipse.persistence.jpa', version: versions.eclipselink]
     )
 
     //Workaround for bug described here: https://discuss.gradle.org/t/build-failure-when-4-1-0-of-net-java-dev-jna-jna-is-selected-in-place-of-3-3-0-due-to-change-in-artifacts-with-classifiers/9377/3
@@ -70,6 +71,11 @@
     compile('org.mariadb.jdbc:mariadb-java-client:1.4.3') {
         exclude group: 'net.java.dev.jna'
     }
+    compile(group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: versions.springboot)
+            {
+                exclude group: 'org.hibernate', module: 'hibernate-entitymanager'
+                exclude group: 'org.hibernate', module: 'hibernate-core'
+            }
 
     compile 'net.java.dev.jna:jna:4.1.0'
     compile 'net.java.dev.jna:jna-platform:4.1.0'
diff --git a/src/main/java/org/apache/fineract/cn/mariadb/config/EclipseLinkJpaConfiguration.java b/src/main/java/org/apache/fineract/cn/mariadb/config/EclipseLinkJpaConfiguration.java
new file mode 100644
index 0000000..04f4403
--- /dev/null
+++ b/src/main/java/org/apache/fineract/cn/mariadb/config/EclipseLinkJpaConfiguration.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.cn.mariadb.config;
+
+import org.eclipse.persistence.config.BatchWriting;
+import org.eclipse.persistence.config.PersistenceUnitProperties;
+import org.eclipse.persistence.config.TargetDatabase;
+import org.eclipse.persistence.logging.SessionLog;
+import org.springframework.beans.factory.ObjectProvider;
+import org.springframework.boot.autoconfigure.domain.EntityScan;
+import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
+import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
+import org.springframework.orm.jpa.JpaTransactionManager;
+import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
+import org.springframework.orm.jpa.vendor.Database;
+import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+import org.springframework.transaction.jta.JtaTransactionManager;
+
+import javax.persistence.EntityManagerFactory;
+import javax.sql.DataSource;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Ebenezer Graham
+ */
+
+@Configuration
+@EnableTransactionManagement
+@EntityScan({
+		"org.apache.fineract.cn.**.repository",
+		"org.apache.fineract.cn.mariadb.util"
+})
+public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration {
+	
+	
+	protected EclipseLinkJpaConfiguration(DataSource dataSource, JpaProperties properties, ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) {
+		super(dataSource, properties, jtaTransactionManagerProvider);
+	}
+	
+	@Bean
+	public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
+		final JpaTransactionManager transactionManager = new JpaTransactionManager();
+		transactionManager.setEntityManagerFactory(emf);
+		return transactionManager;
+	}
+	
+	@Bean
+	public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
+		return new PersistenceExceptionTranslationPostProcessor();
+	}
+	
+	@Bean
+	protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
+		EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
+		vendorAdapter.setShowSql(true); // Todo: remove sql log
+		vendorAdapter.setDatabase(Database.MYSQL);
+		vendorAdapter.setGenerateDdl(false);
+		return vendorAdapter;
+	}
+	
+	@Bean
+	protected Map<String, Object> getVendorProperties() {
+		HashMap<String, Object> properties = new HashMap<>();
+		properties.put(PersistenceUnitProperties.WEAVING, "static");
+		properties.put(PersistenceUnitProperties.WEAVING_EAGER, "true");
+		properties.put(PersistenceUnitProperties.TARGET_DATABASE, TargetDatabase.MySQL);
+		properties.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.ALL_LABEL);// Todo: Reduce log level after test
+		properties.put(PersistenceUnitProperties.LOGGING_PARAMETERS, "true");
+		properties.put(PersistenceUnitProperties.DEPLOY_ON_STARTUP, "false");
+		return properties;
+	}
+}
diff --git a/src/main/java/org/apache/fineract/cn/mariadb/config/MariaDBJavaConfiguration.java b/src/main/java/org/apache/fineract/cn/mariadb/config/MariaDBJavaConfiguration.java
index 9a6884d..2fce6a4 100644
--- a/src/main/java/org/apache/fineract/cn/mariadb/config/MariaDBJavaConfiguration.java
+++ b/src/main/java/org/apache/fineract/cn/mariadb/config/MariaDBJavaConfiguration.java
@@ -31,6 +31,7 @@
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
 import org.springframework.core.env.Environment;
 import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
 import org.springframework.orm.jpa.JpaTransactionManager;
@@ -47,8 +48,8 @@
 @SuppressWarnings("WeakerAccess")
 @Configuration
 @ConditionalOnProperty(prefix = "mariadb", name = "enabled", matchIfMissing = true)
-@EnableTransactionManagement
 @EnableApplicationName
+@Import(EclipseLinkJpaConfiguration.class)
 public class MariaDBJavaConfiguration {
 
   private final Environment env;
@@ -65,32 +66,6 @@
   }
 
   @Bean
-  public LocalContainerEntityManagerFactoryBean entityManagerFactory(final DataSource dataSource) {
-    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
-    em.setPersistenceUnitName("metaPU");
-    em.setDataSource(dataSource);
-    em.setPackagesToScan("org.apache.fineract.cn.**.repository");
-
-    final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
-    em.setJpaVendorAdapter(vendorAdapter);
-    em.setJpaProperties(additionalProperties());
-
-    return em;
-  }
-
-  @Bean
-  public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
-    final JpaTransactionManager transactionManager = new JpaTransactionManager();
-    transactionManager.setEntityManagerFactory(emf);
-    return transactionManager;
-  }
-
-  @Bean
-  public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
-    return new PersistenceExceptionTranslationPostProcessor();
-  }
-
-  @Bean
   public FlywayFactoryBean flywayFactoryBean(final ApplicationName applicationName) {
     return new FlywayFactoryBean(applicationName);
   }
@@ -131,10 +106,4 @@
     boneCPDataSource.setDriverProperties(driverProperties);
     return new MetaDataSourceWrapper(boneCPDataSource);
   }
-
-  private Properties additionalProperties() {
-    final Properties properties = new Properties();
-    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
-    return properties;
-  }
 }