package org.academiadecodigo.bootcamp.fxlogin.persistence.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class JpaSessionManager {

    private EntityManagerFactory emf;
    private EntityManager em;

    public JpaSessionManager(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public void startSession() {
        if (em == null) {
            em = emf.createEntityManager();
        }
    }

    public void stopSession() {
        if (em != null) {
            em.close();
        }

        em = null;
    }

    public EntityManager getCurrentSession() {
        startSession();
        return em;
    }

    public void close() {
        if (emf != null) {
            emf.close();
        }
    }
}
