/*
 * 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.TblTransaccionesCredibanco;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

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

    private EntityManagerFactory emf = null;
    EntityManager em = null;

    public TblTransaccionesCredibancoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
        em = getEntityManager();
    }

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

    public void create(TblTransaccionesCredibanco tblTransaccionesCredibanco) {
        EntityManager em = null;
        em = getEntityManager();
        em.getTransaction().begin();
        em.persist(tblTransaccionesCredibanco);
        em.getTransaction().commit();
    }

    public TblTransaccionesCredibanco getInfoTransaccionOkXIdTransaccionAndEmpresa(Long idTransaccion, String codEmpresa) {
        try {
            Query q = em.createNamedQuery("TransaccionIdTransaccionAndEmpresa", TblTransaccionesCredibanco.class);
            q.setParameter("idTransaccion", (idTransaccion));
            q.setParameter("codEmpresa", (codEmpresa));
            return (TblTransaccionesCredibanco) q.getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }
    }

    public TblTransaccionesCredibanco getInfoTransaccionXIdTransaccionAndEmpresa(Long idTransaccion, String codEmpresa) {
        try {
            Query q = em.createNamedQuery("TransaccionIdTransaccionAndEmpresa", TblTransaccionesCredibanco.class);
            q.setParameter("idTransaccion", (idTransaccion));
            q.setParameter("codEmpresa", (codEmpresa));
            return (TblTransaccionesCredibanco) q.getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }
    }

    public TblTransaccionesCredibanco setOkEstadoEnvioXIdTransaccion(Long idTransaccion) {
        try {
            Query q = em.createNamedQuery("ActualizarEstadoEnviadoXIdTransaccion", TblTransaccionesCredibanco.class);
            q.setParameter("idTransaccion", (idTransaccion));
            return (TblTransaccionesCredibanco) q.getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }
    }

    public List<TblTransaccionesCredibanco> getInfoTransaccionesXEmpresa(String codEmpresa) {
        try {
            Query q = em.createNamedQuery("TransaccionesPendientesEnviarXEmpresas", TblTransaccionesCredibanco.class);
            q.setParameter("codEmpresa", (codEmpresa));
            return q.getResultList();
        } catch (NoResultException ex) {
            return null;
        }
    }

    public void edit(TblTransaccionesCredibanco tblTransaccionesCredibanco) throws NonexistentEntityException, Exception {
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            tblTransaccionesCredibanco = em.merge(tblTransaccionesCredibanco);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = tblTransaccionesCredibanco.getIdTransaccion();
                if (findTblTransaccionesCredibanco(id) == null) {
                    throw new NonexistentEntityException("The tblTransaccionesCredibanco with id " + id + " no longer exists.");
                }
            }
            throw ex;
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        em = getEntityManager();
        em.getTransaction().begin();
        TblTransaccionesCredibanco tblTransaccionesCredibanco;
        try {
            tblTransaccionesCredibanco = em.getReference(TblTransaccionesCredibanco.class, id);
            tblTransaccionesCredibanco.getIdTransaccion();
        } catch (EntityNotFoundException enfe) {
            throw new NonexistentEntityException("The tblTransaccionesCredibanco with id " + id + " no longer exists.", enfe);
        }
        em.remove(tblTransaccionesCredibanco);
        em.getTransaction().commit();
    }

    public List<TblTransaccionesCredibanco> findTblTransaccionesCredibancoEntities() {
        return findTblTransaccionesCredibancoEntities(true, -1, -1);
    }

    public List<TblTransaccionesCredibanco> findTblTransaccionesCredibancoEntities(int maxResults, int firstResult) {
        return findTblTransaccionesCredibancoEntities(false, maxResults, firstResult);
    }

    private List<TblTransaccionesCredibanco> findTblTransaccionesCredibancoEntities(boolean all, int maxResults, int firstResult) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(TblTransaccionesCredibanco.class));
        Query q = em.createQuery(cq);
        if (!all) {
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
        }
        return q.getResultList();
    }

    public TblTransaccionesCredibanco findTblTransaccionesCredibanco(Long id) {
        return em.find(TblTransaccionesCredibanco.class, id);
    }

    public int getTblTransaccionesCredibancoCount() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<TblTransaccionesCredibanco> rt = cq.from(TblTransaccionesCredibanco.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
    
    public void cerrar() {
        em.close();
    }

}
