/*
 * 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.RefPaises;
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 RefPaisesJpaController implements Serializable {

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

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

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

    public void edit(RefPaises refPaises) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RefPaises persistentRefPaises = em.find(RefPaises.class, refPaises.getIdPaises());
            TblEmpresasCredibanco idEmpresaOld = persistentRefPaises.getIdEmpresa();
            TblEmpresasCredibanco idEmpresaNew = refPaises.getIdEmpresa();
            if (idEmpresaNew != null) {
                idEmpresaNew = em.getReference(idEmpresaNew.getClass(), idEmpresaNew.getIdEmpresa());
                refPaises.setIdEmpresa(idEmpresaNew);
            }
            refPaises = em.merge(refPaises);
            if (idEmpresaOld != null && !idEmpresaOld.equals(idEmpresaNew)) {
                idEmpresaOld.getRefPaisesCollection().remove(refPaises);
                idEmpresaOld = em.merge(idEmpresaOld);
            }
            if (idEmpresaNew != null && !idEmpresaNew.equals(idEmpresaOld)) {
                idEmpresaNew.getRefPaisesCollection().add(refPaises);
                idEmpresaNew = em.merge(idEmpresaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = refPaises.getIdPaises();
                if (findRefPaises(id) == null) {
                    throw new NonexistentEntityException("The refPaises 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();
            RefPaises refPaises;
            try {
                refPaises = em.getReference(RefPaises.class, id);
                refPaises.getIdPaises();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The refPaises with id " + id + " no longer exists.", enfe);
            }
            TblEmpresasCredibanco idEmpresa = refPaises.getIdEmpresa();
            if (idEmpresa != null) {
                idEmpresa.getRefPaisesCollection().remove(refPaises);
                idEmpresa = em.merge(idEmpresa);
            }
            em.remove(refPaises);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<RefPaises> findRefPaisesEntities() {
        return findRefPaisesEntities(true, -1, -1);
    }

    public List<RefPaises> findRefPaisesEntities(int maxResults, int firstResult) {
        return findRefPaisesEntities(false, maxResults, firstResult);
    }

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

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

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