package rzaharov.example.repository;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rzaharov.example.database.AllModels;
import rzaharov.example.database.DBManager;
import rzaharov.example.database.DBOperation;
import rzaharov.example.database.ID;

import java.util.List;

/**
 * @author rzaharov
 * @version 0.1
 * @since 07.06.2018
 */

public abstract class CommonRepository<T> {

    private DBManager dbManager;

    private static final Logger log = LoggerFactory.getLogger(DBManager.class);

    private Session sessions = null;

    /**
     * Default constructor.
     */
    public CommonRepository() {
        this.dbManager = DBManager.getInstance();
    }

    public void execute(DBOperation db, T value) {
        try(Session session = this.dbManager.getSession()) {
            sessions = session;
            session.beginTransaction();
            db.execute(session, value);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            log.error(e.getMessage(), e);
            sessions.getTransaction().rollback();
        }
    }

    public T getById(ID operation) {
        T value = null;
        try(Session session = this.dbManager.getSession()) {
            sessions = session;
            session.beginTransaction();
            value = (T) operation.getById(session);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            log.error(e.getMessage(), e);
            sessions.getTransaction().rollback();
        }
        return value;
    }

    public List<T> getAll(AllModels allModels) {
        List<T> list = null;
        try(Session session = this.dbManager.getSession()) {
            sessions = session;
            session.beginTransaction();
            list = allModels.getAll(session);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            log.error(e.getMessage(), e);
            sessions.getTransaction().rollback();
        }
        return list;
    }

}

@@ -98,24 +98,58 @@
 te.Session;%0A
+import org.hibernate.Transaction;%0A
 import org.s
@@ -619,46 +619,8 @@
 );%0A%0A
-    private Session sessions = null;%0A%0A
     
@@ -799,36 +799,38 @@
 alue) %7B%0A        
-try(
+final 
 Session session 
@@ -850,35 +850,33 @@
 ger.getSession()
-) %7B
+;
 %0A            ses
@@ -872,43 +872,30 @@
     
-    sessions = session;%0A           
+final Transaction tx =
  ses
@@ -911,32 +911,46 @@
 nTransaction();%0A
+        try %7B%0A
             db.e
@@ -977,55 +977,8 @@
 e);%0A
-            session.getTransaction().commit();%0A
     
@@ -1067,33 +1067,32 @@
          session
-s
 .getTransaction(
@@ -1097,32 +1097,106 @@
 n().rollback();%0A
+        %7D finally %7B%0A            tx.commit();%0A            session.close();%0A
         %7D%0A    %7D%0A
@@ -1245,36 +1245,14 @@
     
-T value = null;%0A        try(
+final 
 Sess
@@ -1288,38 +1288,34 @@
 getSession()
-) %7B%0A  
+;%0A
         
   sessions =
@@ -1306,41 +1306,30 @@
     
-  sessions = session;%0A           
+final Transaction tx =
  ses
@@ -1345,32 +1345,71 @@
 nTransaction();%0A
+        T value = null;%0A        try %7B%0A 
             valu
@@ -1448,55 +1448,8 @@
 n);%0A
-            session.getTransaction().commit();%0A
     
@@ -1538,33 +1538,32 @@
          session
-s
 .getTransaction(
@@ -1568,32 +1568,106 @@
 n().rollback();%0A
+        %7D finally %7B%0A            tx.commit();%0A            session.close();%0A
         %7D%0A      
@@ -1779,12 +1779,14 @@
     
-try(
+final 
 Sess
@@ -1826,31 +1826,26 @@
 ession()
-) %7B%0A   
+;%0A
         
  session
@@ -1840,40 +1840,30 @@
     
- sessions = session;%0A           
+final Transaction tx =
  ses
@@ -1879,32 +1879,46 @@
 nTransaction();%0A
+        try %7B%0A
             list
@@ -1951,55 +1951,8 @@
 n);%0A
-            session.getTransaction().commit();%0A
     
@@ -2049,17 +2049,16 @@
  session
-s
 .getTran
@@ -2071,32 +2071,106 @@
 n().rollback();%0A
+        %7D finally %7B%0A            tx.commit();%0A            session.close();%0A
         %7D%0A      

task 6872