/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package conexionCredibanco.controller;

import conexionCredibanco.controller.exceptions.NonexistentEntityException;
import conexionCredibanco.entity.RefHorarios;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import conexionCredibanco.entity.TblEmpresasCredibanco;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author viatori
 */
public class RefHorariosJpaController implements Serializable {

    public RefHorariosJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

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

    public void create(RefHorarios refHorarios) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TblEmpresasCredibanco idEmpresa = refHorarios.getIdEmpresa();
            if (idEmpresa != null) {
                idEmpresa = em.getReference(idEmpresa.getClass(), idEmpresa.getIdEmpresa());
                refHorarios.setIdEmpresa(idEmpresa);
            }
            em.persist(refHorarios);
            if (idEmpresa != null) {
                idEmpresa.getRefHorariosCollection().add(refHorarios);
                idEmpresa = em.merge(idEmpresa);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(RefHorarios refHorarios) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RefHorarios persistentRefHorarios = em.find(RefHorarios.class, refHorarios.getIdHorario());
            TblEmpresasCredibanco idEmpresaOld = persistentRefHorarios.getIdEmpresa();
            TblEmpresasCredibanco idEmpresaNew = refHorarios.getIdEmpresa();
            if (idEmpresaNew != null) {
                idEmpresaNew = em.getReference(idEmpresaNew.getClass(), idEmpresaNew.getIdEmpresa());
                refHorarios.setIdEmpresa(idEmpresaNew);
            }
            refHorarios = em.merge(refHorarios);
            if (idEmpresaOld != null && !idEmpresaOld.equals(idEmpresaNew)) {
                idEmpresaOld.getRefHorariosCollection().remove(refHorarios);
                idEmpresaOld = em.merge(idEmpresaOld);
            }
            if (idEmpresaNew != null && !idEmpresaNew.equals(idEmpresaOld)) {
                idEmpresaNew.getRefHorariosCollection().add(refHorarios);
                idEmpresaNew = em.merge(idEmpresaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = refHorarios.getIdHorario();
                if (findRefHorarios(id) == null) {
                    throw new NonexistentEntityException("The refHorarios with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RefHorarios refHorarios;
            try {
                refHorarios = em.getReference(RefHorarios.class, id);
                refHorarios.getIdHorario();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The refHorarios with id " + id + " no longer exists.", enfe);
            }
            TblEmpresasCredibanco idEmpresa = refHorarios.getIdEmpresa();
            if (idEmpresa != null) {
                idEmpresa.getRefHorariosCollection().remove(refHorarios);
                idEmpresa = em.merge(idEmpresa);
            }
            em.remove(refHorarios);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<RefHorarios> findRefHorariosEntities() {
        return findRefHorariosEntities(true, -1, -1);
    }

    public List<RefHorarios> findRefHorariosEntities(int maxResults, int firstResult) {
        return findRefHorariosEntities(false, maxResults, firstResult);
    }

    private List<RefHorarios> findRefHorariosEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(RefHorarios.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public RefHorarios findRefHorarios(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(RefHorarios.class, id);
        } finally {
            em.close();
        }
    }

    public int getRefHorariosCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<RefHorarios> rt = cq.from(RefHorarios.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
