/*
 * 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.IllegalOrphanException;
import conexionCredibanco.controller.exceptions.NonexistentEntityException;
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.RefPaises;
import java.util.ArrayList;
import java.util.Collection;
import conexionCredibanco.entity.RefHorarios;
import conexionCredibanco.entity.TblEmpresasCredibanco;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

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

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

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

    public void create(TblEmpresasCredibanco tblEmpresas) {
        if (tblEmpresas.getRefPaisesCollection() == null) {
            tblEmpresas.setRefPaisesCollection(new ArrayList<RefPaises>());
        }
        if (tblEmpresas.getRefHorariosCollection() == null) {
            tblEmpresas.setRefHorariosCollection(new ArrayList<RefHorarios>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<RefPaises> attachedRefPaisesCollection = new ArrayList<RefPaises>();
            for (RefPaises refPaisesCollectionRefPaisesToAttach : tblEmpresas.getRefPaisesCollection()) {
                refPaisesCollectionRefPaisesToAttach = em.getReference(refPaisesCollectionRefPaisesToAttach.getClass(), refPaisesCollectionRefPaisesToAttach.getIdPaises());
                attachedRefPaisesCollection.add(refPaisesCollectionRefPaisesToAttach);
            }
            tblEmpresas.setRefPaisesCollection(attachedRefPaisesCollection);
            Collection<RefHorarios> attachedRefHorariosCollection = new ArrayList<RefHorarios>();
            for (RefHorarios refHorariosCollectionRefHorariosToAttach : tblEmpresas.getRefHorariosCollection()) {
                refHorariosCollectionRefHorariosToAttach = em.getReference(refHorariosCollectionRefHorariosToAttach.getClass(), refHorariosCollectionRefHorariosToAttach.getIdHorario());
                attachedRefHorariosCollection.add(refHorariosCollectionRefHorariosToAttach);
            }
            tblEmpresas.setRefHorariosCollection(attachedRefHorariosCollection);
            em.persist(tblEmpresas);
            for (RefPaises refPaisesCollectionRefPaises : tblEmpresas.getRefPaisesCollection()) {
                TblEmpresasCredibanco oldIdEmpresaOfRefPaisesCollectionRefPaises = refPaisesCollectionRefPaises.getIdEmpresa();
                refPaisesCollectionRefPaises.setIdEmpresa(tblEmpresas);
                refPaisesCollectionRefPaises = em.merge(refPaisesCollectionRefPaises);
                if (oldIdEmpresaOfRefPaisesCollectionRefPaises != null) {
                    oldIdEmpresaOfRefPaisesCollectionRefPaises.getRefPaisesCollection().remove(refPaisesCollectionRefPaises);
                    oldIdEmpresaOfRefPaisesCollectionRefPaises = em.merge(oldIdEmpresaOfRefPaisesCollectionRefPaises);
                }
            }
            for (RefHorarios refHorariosCollectionRefHorarios : tblEmpresas.getRefHorariosCollection()) {
                TblEmpresasCredibanco oldIdEmpresaOfRefHorariosCollectionRefHorarios = refHorariosCollectionRefHorarios.getIdEmpresa();
                refHorariosCollectionRefHorarios.setIdEmpresa(tblEmpresas);
                refHorariosCollectionRefHorarios = em.merge(refHorariosCollectionRefHorarios);
                if (oldIdEmpresaOfRefHorariosCollectionRefHorarios != null) {
                    oldIdEmpresaOfRefHorariosCollectionRefHorarios.getRefHorariosCollection().remove(refHorariosCollectionRefHorarios);
                    oldIdEmpresaOfRefHorariosCollectionRefHorarios = em.merge(oldIdEmpresaOfRefHorariosCollectionRefHorarios);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TblEmpresasCredibanco tblEmpresas) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TblEmpresasCredibanco persistentTblEmpresas = em.find(TblEmpresasCredibanco.class, tblEmpresas.getIdEmpresa());
            Collection<RefPaises> refPaisesCollectionOld = persistentTblEmpresas.getRefPaisesCollection();
            Collection<RefPaises> refPaisesCollectionNew = tblEmpresas.getRefPaisesCollection();
            Collection<RefHorarios> refHorariosCollectionOld = persistentTblEmpresas.getRefHorariosCollection();
            Collection<RefHorarios> refHorariosCollectionNew = tblEmpresas.getRefHorariosCollection();
            List<String> illegalOrphanMessages = null;
            for (RefPaises refPaisesCollectionOldRefPaises : refPaisesCollectionOld) {
                if (!refPaisesCollectionNew.contains(refPaisesCollectionOldRefPaises)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain RefPaises " + refPaisesCollectionOldRefPaises + " since its idEmpresa field is not nullable.");
                }
            }
            for (RefHorarios refHorariosCollectionOldRefHorarios : refHorariosCollectionOld) {
                if (!refHorariosCollectionNew.contains(refHorariosCollectionOldRefHorarios)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain RefHorarios " + refHorariosCollectionOldRefHorarios + " since its idEmpresa field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<RefPaises> attachedRefPaisesCollectionNew = new ArrayList<RefPaises>();
            for (RefPaises refPaisesCollectionNewRefPaisesToAttach : refPaisesCollectionNew) {
                refPaisesCollectionNewRefPaisesToAttach = em.getReference(refPaisesCollectionNewRefPaisesToAttach.getClass(), refPaisesCollectionNewRefPaisesToAttach.getIdPaises());
                attachedRefPaisesCollectionNew.add(refPaisesCollectionNewRefPaisesToAttach);
            }
            refPaisesCollectionNew = attachedRefPaisesCollectionNew;
            tblEmpresas.setRefPaisesCollection(refPaisesCollectionNew);
            Collection<RefHorarios> attachedRefHorariosCollectionNew = new ArrayList<RefHorarios>();
            for (RefHorarios refHorariosCollectionNewRefHorariosToAttach : refHorariosCollectionNew) {
                refHorariosCollectionNewRefHorariosToAttach = em.getReference(refHorariosCollectionNewRefHorariosToAttach.getClass(), refHorariosCollectionNewRefHorariosToAttach.getIdHorario());
                attachedRefHorariosCollectionNew.add(refHorariosCollectionNewRefHorariosToAttach);
            }
            refHorariosCollectionNew = attachedRefHorariosCollectionNew;
            tblEmpresas.setRefHorariosCollection(refHorariosCollectionNew);
            tblEmpresas = em.merge(tblEmpresas);
            for (RefPaises refPaisesCollectionNewRefPaises : refPaisesCollectionNew) {
                if (!refPaisesCollectionOld.contains(refPaisesCollectionNewRefPaises)) {
                    TblEmpresasCredibanco oldIdEmpresaOfRefPaisesCollectionNewRefPaises = refPaisesCollectionNewRefPaises.getIdEmpresa();
                    refPaisesCollectionNewRefPaises.setIdEmpresa(tblEmpresas);
                    refPaisesCollectionNewRefPaises = em.merge(refPaisesCollectionNewRefPaises);
                    if (oldIdEmpresaOfRefPaisesCollectionNewRefPaises != null && !oldIdEmpresaOfRefPaisesCollectionNewRefPaises.equals(tblEmpresas)) {
                        oldIdEmpresaOfRefPaisesCollectionNewRefPaises.getRefPaisesCollection().remove(refPaisesCollectionNewRefPaises);
                        oldIdEmpresaOfRefPaisesCollectionNewRefPaises = em.merge(oldIdEmpresaOfRefPaisesCollectionNewRefPaises);
                    }
                }
            }
            for (RefHorarios refHorariosCollectionNewRefHorarios : refHorariosCollectionNew) {
                if (!refHorariosCollectionOld.contains(refHorariosCollectionNewRefHorarios)) {
                    TblEmpresasCredibanco oldIdEmpresaOfRefHorariosCollectionNewRefHorarios = refHorariosCollectionNewRefHorarios.getIdEmpresa();
                    refHorariosCollectionNewRefHorarios.setIdEmpresa(tblEmpresas);
                    refHorariosCollectionNewRefHorarios = em.merge(refHorariosCollectionNewRefHorarios);
                    if (oldIdEmpresaOfRefHorariosCollectionNewRefHorarios != null && !oldIdEmpresaOfRefHorariosCollectionNewRefHorarios.equals(tblEmpresas)) {
                        oldIdEmpresaOfRefHorariosCollectionNewRefHorarios.getRefHorariosCollection().remove(refHorariosCollectionNewRefHorarios);
                        oldIdEmpresaOfRefHorariosCollectionNewRefHorarios = em.merge(oldIdEmpresaOfRefHorariosCollectionNewRefHorarios);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = tblEmpresas.getIdEmpresa();
                if (findTblEmpresas(id) == null) {
                    throw new NonexistentEntityException("The tblEmpresas with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TblEmpresasCredibanco tblEmpresas;
            try {
                tblEmpresas = em.getReference(TblEmpresasCredibanco.class, id);
                tblEmpresas.getIdEmpresa();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tblEmpresas with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<RefPaises> refPaisesCollectionOrphanCheck = tblEmpresas.getRefPaisesCollection();
            for (RefPaises refPaisesCollectionOrphanCheckRefPaises : refPaisesCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This TblEmpresas (" + tblEmpresas + ") cannot be destroyed since the RefPaises " + refPaisesCollectionOrphanCheckRefPaises + " in its refPaisesCollection field has a non-nullable idEmpresa field.");
            }
            Collection<RefHorarios> refHorariosCollectionOrphanCheck = tblEmpresas.getRefHorariosCollection();
            for (RefHorarios refHorariosCollectionOrphanCheckRefHorarios : refHorariosCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This TblEmpresas (" + tblEmpresas + ") cannot be destroyed since the RefHorarios " + refHorariosCollectionOrphanCheckRefHorarios + " in its refHorariosCollection field has a non-nullable idEmpresa field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(tblEmpresas);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TblEmpresasCredibanco> findTblEmpresasEntities() {
        return findTblEmpresasEntities(true, -1, -1);
    }

    public List<TblEmpresasCredibanco> findTblEmpresasEntities(int maxResults, int firstResult) {
        return findTblEmpresasEntities(false, maxResults, firstResult);
    }

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

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

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