package org.endeavourhealth.ui.database;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.HashMap;
import java.util.Map;

public enum PersistenceManager {
    INSTANCE;
    private static final String JDBC_URL_ENV_VAR = "LOGBACK_JDBC_URL";
    private static final String JDBC_USER_ENV_VAR = "LOGBACK_JDBC_USERNAME";
    private static final String JDBC_PASSWORD_ENV_VAR = "LOGBACK_JDBC_PASSWORD";

    private EntityManagerFactory emFactory;

    private PersistenceManager() {
        Map<String, String> envVars = System.getenv();
        Map<String, Object> override = new HashMap<>();

        if (envVars.containsKey(JDBC_URL_ENV_VAR)) override.put("hibernate.connection.url", envVars.get(JDBC_URL_ENV_VAR));
        if (envVars.containsKey(JDBC_USER_ENV_VAR)) override.put("hibernate.connection.username", envVars.get(JDBC_USER_ENV_VAR));
        if (envVars.containsKey(JDBC_PASSWORD_ENV_VAR)) override.put("hibernate.connection.password", envVars.get(JDBC_PASSWORD_ENV_VAR));

        emFactory = Persistence.createEntityManagerFactory("NewPersistenceUnit", override);
    }

    public EntityManager getEntityManager() {
        return emFactory.createEntityManager();
    }

    public void close() {
        emFactory.close();
    }
}

@@ -35,16 +35,74 @@
 abase;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import j
@@ -542,16 +542,105 @@
 SSWORD%22;
+%0A    private static final Logger LOG = LoggerFactory.getLogger(PersistenceManager.class);
 %0A%0A    pr
@@ -682,43 +682,302 @@
 y;%0A%0A
+%0A
     p
-rivate PersistenceManager() %7B%0A
+ublic EntityManager getEntityManager() %7B%0A        return getEmFactory().createEntityManager();%0A    %7D%0A%0A    public void close() %7B%0A        getEmFactory().close();%0A    %7D%0A%0A    private synchronized EntityManagerFactory getEmFactory() %7B%0A        if (emFactory == null) %7B%0A            try %7B%0A        
     
@@ -1027,32 +1027,40 @@
 tenv();%0A        
+      
+  
 Map%3CString, Obje
@@ -1088,24 +1088,32 @@
 shMap%3C%3E();%0A%0A
+        
         if (
@@ -1142,32 +1142,52 @@
 BC_URL_ENV_VAR))
+%0A                   
  override.put(%22h
@@ -1236,32 +1236,40 @@
 _URL_ENV_VAR));%0A
+        
         if (envV
@@ -1295,32 +1295,52 @@
 C_USER_ENV_VAR))
+%0A                   
  override.put(%22h
@@ -1395,32 +1395,40 @@
 USER_ENV_VAR));%0A
+        
         if (envV
@@ -1466,16 +1466,36 @@
 NV_VAR))
+%0A                   
  overrid
@@ -1571,32 +1571,40 @@
 VAR));%0A%0A        
+    
+    
 emFactory = Pers
@@ -1679,137 +1679,165 @@
     
-%7D%0A%0A    public EntityManager getEntityManager() %7B%0A        return emFactory.createEntityMan
+        %7D%0A            catch (Exception e) %7B%0A                LOG.error(%22Error initializing persistence manager : %22 + e.getMess
 age
-r
 ()
+)
 ;%0A    
-%7D%0A%0A    public void close() %7B
+        %7D%0A        %7D%0A
 %0A   
@@ -1833,32 +1833,39 @@
      %7D%0A%0A        
+return 
 emFactory.close(
@@ -1857,24 +1857,16 @@
 mFactory
-.close()
 ;%0A    %7D%0A

Rework of persistence manager