×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Alexandros Kyriazis
Added: Jan 25, 2022 6:45 PM
Views: 391
Tags: no tags
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package semesterprojectv5;
  7.  
  8. import java.io.Serializable;
  9. import java.util.List;
  10. import javax.persistence.EntityManager;
  11. import javax.persistence.EntityManagerFactory;
  12. import javax.persistence.Query;
  13. import javax.persistence.EntityNotFoundException;
  14. import javax.persistence.criteria.CriteriaQuery;
  15. import javax.persistence.criteria.Root;
  16. import semesterprojectv5.exceptions.NonexistentEntityException;
  17.  
  18. /**
  19.  *
  20.  * @author alexa
  21.  */
  22. public class CoviddataJpaController implements Serializable {
  23.  
  24.     public CoviddataJpaController(EntityManagerFactory emf) {
  25.         this.emf = emf;
  26.     }
  27.     private EntityManagerFactory emf = null;
  28.  
  29.     public EntityManager getEntityManager() {
  30.         return emf.createEntityManager();
  31.     }
  32.  
  33.     public void create(Coviddata coviddata) {
  34.         EntityManager em = null;
  35.         try {
  36.             em = getEntityManager();
  37.             em.getTransaction().begin();
  38.             Country country = coviddata.getCountry();
  39.             if (country != null) {
  40.                 country = em.getReference(country.getClass(), country.getCountry());
  41.                 coviddata.setCountry(country);
  42.             }
  43.             em.persist(coviddata);
  44.             if (country != null) {
  45.                 country.getCoviddataCollection().add(coviddata);
  46.                 country = em.merge(country);
  47.             }
  48.             em.getTransaction().commit();
  49.         } finally {
  50.             if (em != null) {
  51.                 em.close();
  52.             }
  53.         }
  54.     }
  55.  
  56.     public void edit(Coviddata coviddata) throws NonexistentEntityException, Exception {
  57.         EntityManager em = null;
  58.         try {
  59.             em = getEntityManager();
  60.             em.getTransaction().begin();
  61.             Coviddata persistentCoviddata = em.find(Coviddata.class, coviddata.getCoviddata());
  62.             Country countryOld = persistentCoviddata.getCountry();
  63.             Country countryNew = coviddata.getCountry();
  64.             if (countryNew != null) {
  65.                 countryNew = em.getReference(countryNew.getClass(), countryNew.getCountry());
  66.                 coviddata.setCountry(countryNew);
  67.             }
  68.             coviddata = em.merge(coviddata);
  69.             if (countryOld != null && !countryOld.equals(countryNew)) {
  70.                 countryOld.getCoviddataCollection().remove(coviddata);
  71.                 countryOld = em.merge(countryOld);
  72.             }
  73.             if (countryNew != null && !countryNew.equals(countryOld)) {
  74.                 countryNew.getCoviddataCollection().add(coviddata);
  75.                 countryNew = em.merge(countryNew);
  76.             }
  77.             em.getTransaction().commit();
  78.         } catch (Exception ex) {
  79.             String msg = ex.getLocalizedMessage();
  80.             if (msg == null || msg.length() == 0) {
  81.                 Integer id = coviddata.getCoviddata();
  82.                 if (findCoviddata(id) == null) {
  83.                     throw new NonexistentEntityException("The coviddata with id " + id + " no longer exists.");
  84.                 }
  85.             }
  86.             throw ex;
  87.         } finally {
  88.             if (em != null) {
  89.                 em.close();
  90.             }
  91.         }
  92.     }
  93.  
  94.     public void destroy(Integer id) throws NonexistentEntityException {
  95.         EntityManager em = null;
  96.         try {
  97.             em = getEntityManager();
  98.             em.getTransaction().begin();
  99.             Coviddata coviddata;
  100.             try {
  101.                 coviddata = em.getReference(Coviddata.class, id);
  102.                 coviddata.getCoviddata();
  103.             } catch (EntityNotFoundException enfe) {
  104.                 throw new NonexistentEntityException("The coviddata with id " + id + " no longer exists.", enfe);
  105.             }
  106.             Country country = coviddata.getCountry();
  107.             if (country != null) {
  108.                 country.getCoviddataCollection().remove(coviddata);
  109.                 country = em.merge(country);
  110.             }
  111.             em.remove(coviddata);
  112.             em.getTransaction().commit();
  113.         } finally {
  114.             if (em != null) {
  115.                 em.close();
  116.             }
  117.         }
  118.     }
  119.  
  120.     public List<Coviddata> findCoviddataEntities() {
  121.         return findCoviddataEntities(true, -1, -1);
  122.     }
  123.  
  124.     public List<Coviddata> findCoviddataEntities(int maxResults, int firstResult) {
  125.         return findCoviddataEntities(false, maxResults, firstResult);
  126.     }
  127.  
  128.     private List<Coviddata> findCoviddataEntities(boolean all, int maxResults, int firstResult) {
  129.         EntityManager em = getEntityManager();
  130.         try {
  131.             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
  132.             cq.select(cq.from(Coviddata.class));
  133.             Query q = em.createQuery(cq);
  134.             if (!all) {
  135.                 q.setMaxResults(maxResults);
  136.                 q.setFirstResult(firstResult);
  137.             }
  138.             return q.getResultList();
  139.         } finally {
  140.             em.close();
  141.         }
  142.     }
  143.  
  144.     public Coviddata findCoviddata(Integer id) {
  145.         EntityManager em = getEntityManager();
  146.         try {
  147.             return em.find(Coviddata.class, id);
  148.         } finally {
  149.             em.close();
  150.         }
  151.     }
  152.  
  153.     public int getCoviddataCount() {
  154.         EntityManager em = getEntityManager();
  155.         try {
  156.             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
  157.             Root<Coviddata> rt = cq.from(Coviddata.class);
  158.             cq.select(em.getCriteriaBuilder().count(rt));
  159.             Query q = em.createQuery(cq);
  160.             return ((Long) q.getSingleResult()).intValue();
  161.         } finally {
  162.             em.close();
  163.         }
  164.     }
  165.    
  166. }
  167.