package rzaharov.todolist.repository;

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

import java.util.List;

public abstract class CommonRepository<T> {

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

    private DBManager dbManager;

    private Session sessions;

    public CommonRepository() {
        this.dbManager = DBManager.getInstance();
    }

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

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

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

@@ -99,24 +99,58 @@
 te.Session;%0A
+import org.hibernate.Transaction;%0A
 import org.s
@@ -673,32 +673,34 @@
 tance();%0A    %7D%0A%0A
+//
     public void 
@@ -739,24 +739,26 @@
  T value) %7B%0A
+//
         try(
@@ -798,32 +798,34 @@
 getSession()) %7B%0A
+//
             this
@@ -837,32 +837,34 @@
 ions = session;%0A
+//
             sess
@@ -879,32 +879,34 @@
 nTransaction();%0A
+//
             dbOp
@@ -934,24 +934,26 @@
 on, value);%0A
+//
             
@@ -979,32 +979,34 @@
 ion().commit();%0A
+//
             sess
@@ -1014,24 +1014,26 @@
 on.close();%0A
+//
         %7D ca
@@ -1053,32 +1053,34 @@
 eException e) %7B%0A
+//
             log.
@@ -1097,32 +1097,34 @@
 tMessage(), e);%0A
+//
             sess
@@ -1149,32 +1149,502 @@
 n().rollback();%0A
+//        %7D%0A//    %7D%0A%0A    public void execute(DBOperation dbOperation, T value) %7B%0A        final Session session = this.dbManager.getSession();%0A        final Transaction tx = session.beginTransaction();%0A        try %7B%0A            dbOperation.execute(session, value);%0A        %7D catch (HibernateException e) %7B%0A            log.error(e.getMessage(), e);%0A            session.getTransaction().rollback();%0A        %7D finally %7B%0A            tx.commit();%0A            session.close();%0A
         %7D%0A    %7D%0A
@@ -1722,36 +1722,38 @@
 = null;%0A        
-try(
+final 
 Session session 
@@ -1777,39 +1777,34 @@
 getSession()
-) %7B%0A   
+;%0A
         
  this.sessio
@@ -1795,45 +1795,30 @@
     
- this.sessions = session;%0A           
+final Transaction tx =
  ses
@@ -1834,32 +1834,46 @@
 nTransaction();%0A
+        try %7B%0A
             list
@@ -1906,46 +1906,8 @@
 n);%0A
-            session.getTransaction();%0A
     
@@ -1996,33 +1996,32 @@
          session
-s
 .getTransaction(
@@ -2026,32 +2026,106 @@
 n().rollback();%0A
+        %7D finally %7B%0A            tx.commit();%0A            session.close();%0A
         %7D%0A      
@@ -2197,36 +2197,14 @@
     
-T value = null;%0A        try(
+final 
 Sess
@@ -2248,88 +2248,106 @@
 on()
-) %7B%0A            this.sessions = session;%0A            session.beginTransaction();
+;%0A        final Transaction tx = session.beginTransaction();%0A        T value = null;%0A        try %7B
 %0A   
@@ -2401,55 +2401,8 @@
 n);%0A
-            session.getTransaction().commit();%0A
     
@@ -2491,33 +2491,32 @@
          session
-s
 .getTransaction(
@@ -2521,32 +2521,106 @@
 n().rollback();%0A
+        %7D finally %7B%0A            tx.commit();%0A            session.close();%0A
         %7D%0A      

task 3786