All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.aequologica.neo.buildhub.persist.BuildDaoImpl Maven / Gradle / Ivy

package net.aequologica.neo.buildhub.persist;

import static net.aequologica.neo.geppaequo.persistence.DbUtils.getEntityManagerFactory;

import java.util.List;

import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.aequologica.neo.buildhub.persist.model.Build;
import net.aequologica.neo.buildhub.persist.model.Build_;


// TODO unit test me !!!
@Singleton
public class BuildDaoImpl implements BuildDao {
    
    private static final Logger log = LoggerFactory.getLogger(BuildDao.class);

    private final EntityManagerFactory emf;
    
    
    public BuildDaoImpl() {
        this.emf = getEntityManagerFactory("buildhubPersist");
    }

    @Override
    public List readAll() {
        EntityManager em = emf.createEntityManager();
        try {
            CriteriaBuilder       cb    = em.getCriteriaBuilder();
            CriteriaQuery  cq    = cb.createQuery(Build.class);
            Root           build = cq.from(Build.class);
    
            cq.select(build);
    
            TypedQuery q       = em.createQuery(cq);
            List       results = q.getResultList();
            return results;
        } finally {
            em.close();
        }
    }

    @Override
    public boolean exists(String buildId) {
        EntityManager em = emf.createEntityManager();
        try {
            CriteriaBuilder       cb    = em.getCriteriaBuilder();
            CriteriaQuery  cq    = cb.createQuery(Build.class);
            Root           build = cq.from(Build.class);
            cq.select(build).where(cb.equal(build.get(Build_.id), buildId));
    
            TypedQuery q       = em.createQuery(cq).setMaxResults(1);
            List       results = q.getResultList();
            return !results.isEmpty();
        } finally {
            em.close();
        }
    }

    @Override
    public Build write(Build build) {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            em.persist(build);
            em.getTransaction().commit();
            log.info("build {} written", build.getId());
            return build;
        } finally {
            em.close();
        }
        
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy