thufir

linksDAO

Oct 6th, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.62 KB | None | 0 0
  1. package romereader;
  2.  
  3. import java.io.Serializable;
  4. import java.util.List;
  5. import java.util.logging.Logger;
  6. import javax.persistence.EntityManager;
  7. import javax.persistence.EntityManagerFactory;
  8. import javax.persistence.Query;
  9. import javax.persistence.EntityNotFoundException;
  10. import javax.persistence.TypedQuery;
  11. import javax.persistence.criteria.CriteriaQuery;
  12. import javax.persistence.criteria.Root;
  13. import romereader.exceptions.NonexistentEntityException;
  14.  
  15. public class LinksDAO implements Serializable {
  16.  
  17.     private final static Logger log = Logger.getLogger(LinksDAO.class.getName());
  18.  
  19.     public LinksDAO(EntityManagerFactory emf) {
  20.         this.emf = emf;
  21.     }
  22.     private EntityManagerFactory emf = null;
  23.  
  24.     public EntityManager getEntityManager() {
  25.         return emf.createEntityManager();
  26.     }
  27.  
  28.     public Links findByLink(Links link) {
  29.         EntityManager em = null;
  30.         Links singleLink = null;
  31.         String linkString = link.getLink();
  32.         try {
  33.             em = getEntityManager();
  34.             TypedQuery<Links> query
  35.                     = em.createQuery("SELECT l FROM Links l WHERE l.link = :linkString", Links.class);
  36.             singleLink = query.getSingleResult();
  37.         } finally {
  38.             if (em != null) {
  39.                 em.close();
  40.             }
  41.         }
  42.         return singleLink;
  43.     }
  44.  
  45.     public void create(Links links) {
  46.         EntityManager em = null;
  47.         try {
  48.             em = getEntityManager();
  49.             em.getTransaction().begin();
  50.             em.persist(links);
  51.             em.getTransaction().commit();
  52.         } finally {
  53.             if (em != null) {
  54.                 em.close();
  55.             }
  56.         }
  57.     }
  58.  
  59.     public void edit(Links links) throws NonexistentEntityException, Exception {
  60.         EntityManager em = null;
  61.         try {
  62.             em = getEntityManager();
  63.             em.getTransaction().begin();
  64.             links = em.merge(links);
  65.             em.getTransaction().commit();
  66.         } catch (Exception ex) {
  67.             String msg = ex.getLocalizedMessage();
  68.             if (msg == null || msg.length() == 0) {
  69.                 Integer id = links.getId();
  70.                 if (findLinks(id) == null) {
  71.                     throw new NonexistentEntityException("The links with id " + id + " no longer exists.");
  72.                 }
  73.             }
  74.             throw ex;
  75.         } finally {
  76.             if (em != null) {
  77.                 em.close();
  78.             }
  79.         }
  80.     }
  81.  
  82.     public void destroy(Integer id) throws NonexistentEntityException {
  83.         EntityManager em = null;
  84.         try {
  85.             em = getEntityManager();
  86.             em.getTransaction().begin();
  87.             Links links;
  88.             try {
  89.                 links = em.getReference(Links.class, id);
  90.                 links.getId();
  91.             } catch (EntityNotFoundException enfe) {
  92.                 throw new NonexistentEntityException("The links with id " + id + " no longer exists.", enfe);
  93.             }
  94.             em.remove(links);
  95.             em.getTransaction().commit();
  96.         } finally {
  97.             if (em != null) {
  98.                 em.close();
  99.             }
  100.         }
  101.     }
  102.  
  103.     public List<Links> findLinksEntities() {
  104.         return findLinksEntities(true, -1, -1);
  105.     }
  106.  
  107.     public List<Links> findLinksEntities(int maxResults, int firstResult) {
  108.         return findLinksEntities(false, maxResults, firstResult);
  109.     }
  110.  
  111.     private List<Links> findLinksEntities(boolean all, int maxResults, int firstResult) {
  112.         EntityManager em = getEntityManager();
  113.         try {
  114.             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
  115.             cq.select(cq.from(Links.class));
  116.             Query q = em.createQuery(cq);
  117.             if (!all) {
  118.                 q.setMaxResults(maxResults);
  119.                 q.setFirstResult(firstResult);
  120.             }
  121.             return q.getResultList();
  122.         } finally {
  123.             em.close();
  124.         }
  125.     }
  126.  
  127.     public Links findLinks(Integer id) {
  128.         EntityManager em = getEntityManager();
  129.         try {
  130.             return em.find(Links.class, id);
  131.         } finally {
  132.             em.close();
  133.         }
  134.     }
  135.  
  136.     public int getLinksCount() {
  137.         EntityManager em = getEntityManager();
  138.         try {
  139.             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
  140.             Root<Links> rt = cq.from(Links.class);
  141.             cq.select(em.getCriteriaBuilder().count(rt));
  142.             Query q = em.createQuery(cq);
  143.             return ((Long) q.getSingleResult()).intValue();
  144.         } finally {
  145.             em.close();
  146.         }
  147.     }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment