×

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:44 PM
Views: 390
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 javax.persistence.Query;
  10. import javax.persistence.EntityNotFoundException;
  11. import javax.persistence.criteria.CriteriaQuery;
  12. import javax.persistence.criteria.Root;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.List;
  16. import javax.persistence.EntityManager;
  17. import javax.persistence.EntityManagerFactory;
  18. import semesterprojectv5.exceptions.IllegalOrphanException;
  19. import semesterprojectv5.exceptions.NonexistentEntityException;
  20.  
  21. /**
  22.  *
  23.  * @author alexa
  24.  */
  25. public class CountryJpaController implements Serializable {
  26.  
  27.     public CountryJpaController(EntityManagerFactory emf) {
  28.         this.emf = emf;
  29.     }
  30.     private EntityManagerFactory emf = null;
  31.  
  32.     public EntityManager getEntityManager() {
  33.         return emf.createEntityManager();
  34.     }
  35.  
  36.     public void create(Country country) {
  37.         if (country.getCoviddataCollection() == null) {
  38.             country.setCoviddataCollection(new ArrayList<Coviddata>());
  39.         }
  40.         EntityManager em = null;
  41.         try {
  42.             em = getEntityManager();
  43.             em.getTransaction().begin();
  44.             Collection<Coviddata> attachedCoviddataCollection = new ArrayList<Coviddata>();
  45.             for (Coviddata coviddataCollectionCoviddataToAttach : country.getCoviddataCollection()) {
  46.                 coviddataCollectionCoviddataToAttach = em.getReference(coviddataCollectionCoviddataToAttach.getClass(), coviddataCollectionCoviddataToAttach.getCoviddata());
  47.                 attachedCoviddataCollection.add(coviddataCollectionCoviddataToAttach);
  48.             }
  49.             country.setCoviddataCollection(attachedCoviddataCollection);
  50.             em.persist(country);
  51.             for (Coviddata coviddataCollectionCoviddata : country.getCoviddataCollection()) {
  52.                 Country oldCountryOfCoviddataCollectionCoviddata = coviddataCollectionCoviddata.getCountry();
  53.                 coviddataCollectionCoviddata.setCountry(country);
  54.                 coviddataCollectionCoviddata = em.merge(coviddataCollectionCoviddata);
  55.                 if (oldCountryOfCoviddataCollectionCoviddata != null) {
  56.                     oldCountryOfCoviddataCollectionCoviddata.getCoviddataCollection().remove(coviddataCollectionCoviddata);
  57.                     oldCountryOfCoviddataCollectionCoviddata = em.merge(oldCountryOfCoviddataCollectionCoviddata);
  58.                 }
  59.             }
  60.             em.getTransaction().commit();
  61.         } finally {
  62.             if (em != null) {
  63.                 em.close();
  64.             }
  65.         }
  66.     }
  67.  
  68.     public void edit(Country country) throws IllegalOrphanException, NonexistentEntityException, Exception {
  69.         EntityManager em = null;
  70.         try {
  71.             em = getEntityManager();
  72.             em.getTransaction().begin();
  73.             Country persistentCountry = em.find(Country.class, country.getCountry());
  74.             Collection<Coviddata> coviddataCollectionOld = persistentCountry.getCoviddataCollection();
  75.             Collection<Coviddata> coviddataCollectionNew = country.getCoviddataCollection();
  76.             List<String> illegalOrphanMessages = null;
  77.             for (Coviddata coviddataCollectionOldCoviddata : coviddataCollectionOld) {
  78.                 if (!coviddataCollectionNew.contains(coviddataCollectionOldCoviddata)) {
  79.                     if (illegalOrphanMessages == null) {
  80.                         illegalOrphanMessages = new ArrayList<String>();
  81.                     }
  82.                     illegalOrphanMessages.add("You must retain Coviddata " + coviddataCollectionOldCoviddata + " since its country field is not nullable.");
  83.                 }
  84.             }
  85.             if (illegalOrphanMessages != null) {
  86.                 throw new IllegalOrphanException(illegalOrphanMessages);
  87.             }
  88.             Collection<Coviddata> attachedCoviddataCollectionNew = new ArrayList<Coviddata>();
  89.             for (Coviddata coviddataCollectionNewCoviddataToAttach : coviddataCollectionNew) {
  90.                 coviddataCollectionNewCoviddataToAttach = em.getReference(coviddataCollectionNewCoviddataToAttach.getClass(), coviddataCollectionNewCoviddataToAttach.getCoviddata());
  91.                 attachedCoviddataCollectionNew.add(coviddataCollectionNewCoviddataToAttach);
  92.             }
  93.             coviddataCollectionNew = attachedCoviddataCollectionNew;
  94.             country.setCoviddataCollection(coviddataCollectionNew);
  95.             country = em.merge(country);
  96.             for (Coviddata coviddataCollectionNewCoviddata : coviddataCollectionNew) {
  97.                 if (!coviddataCollectionOld.contains(coviddataCollectionNewCoviddata)) {
  98.                     Country oldCountryOfCoviddataCollectionNewCoviddata = coviddataCollectionNewCoviddata.getCountry();
  99.                     coviddataCollectionNewCoviddata.setCountry(country);
  100.                     coviddataCollectionNewCoviddata = em.merge(coviddataCollectionNewCoviddata);
  101.                     if (oldCountryOfCoviddataCollectionNewCoviddata != null && !oldCountryOfCoviddataCollectionNewCoviddata.equals(country)) {
  102.                         oldCountryOfCoviddataCollectionNewCoviddata.getCoviddataCollection().remove(coviddataCollectionNewCoviddata);
  103.                         oldCountryOfCoviddataCollectionNewCoviddata = em.merge(oldCountryOfCoviddataCollectionNewCoviddata);
  104.                     }
  105.                 }
  106.             }
  107.             em.getTransaction().commit();
  108.         } catch (Exception ex) {
  109.             String msg = ex.getLocalizedMessage();
  110.             if (msg == null || msg.length() == 0) {
  111.                 Integer id = country.getCountry();
  112.                 if (findCountry(id) == null) {
  113.                     throw new NonexistentEntityException("The country with id " + id + " no longer exists.");
  114.                 }
  115.             }
  116.             throw ex;
  117.         } finally {
  118.             if (em != null) {
  119.                 em.close();
  120.             }
  121.         }
  122.     }
  123.  
  124.     public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
  125.         EntityManager em = null;
  126.         try {
  127.             em = getEntityManager();
  128.             em.getTransaction().begin();
  129.             Country country;
  130.             try {
  131.                 country = em.getReference(Country.class, id);
  132.                 country.getCountry();
  133.             } catch (EntityNotFoundException enfe) {
  134.                 throw new NonexistentEntityException("The country with id " + id + " no longer exists.", enfe);
  135.             }
  136.             List<String> illegalOrphanMessages = null;
  137.             Collection<Coviddata> coviddataCollectionOrphanCheck = country.getCoviddataCollection();
  138.             for (Coviddata coviddataCollectionOrphanCheckCoviddata : coviddataCollectionOrphanCheck) {
  139.                 if (illegalOrphanMessages == null) {
  140.                     illegalOrphanMessages = new ArrayList<String>();
  141.                 }
  142.                 illegalOrphanMessages.add("This Country (" + country + ") cannot be destroyed since the Coviddata " + coviddataCollectionOrphanCheckCoviddata + " in its coviddataCollection field has a non-nullable country field.");
  143.             }
  144.             if (illegalOrphanMessages != null) {
  145.                 throw new IllegalOrphanException(illegalOrphanMessages);
  146.             }
  147.             em.remove(country);
  148.             em.getTransaction().commit();
  149.         } finally {
  150.             if (em != null) {
  151.                 em.close();
  152.             }
  153.         }
  154.     }
  155.  
  156.     public List<Country> findCountryEntities() {
  157.         return findCountryEntities(true, -1, -1);
  158.     }
  159.  
  160.     public List<Country> findCountryEntities(int maxResults, int firstResult) {
  161.         return findCountryEntities(false, maxResults, firstResult);
  162.     }
  163.  
  164.     private List<Country> findCountryEntities(boolean all, int maxResults, int firstResult) {
  165.         EntityManager em = getEntityManager();
  166.         try {
  167.             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
  168.             cq.select(cq.from(Country.class));
  169.             Query q = em.createQuery(cq);
  170.             if (!all) {
  171.                 q.setMaxResults(maxResults);
  172.                 q.setFirstResult(firstResult);
  173.             }
  174.             return q.getResultList();
  175.         } finally {
  176.             em.close();
  177.         }
  178.     }
  179.  
  180.     public Country findCountry(Integer id) {
  181.         EntityManager em = getEntityManager();
  182.         try {
  183.             return em.find(Country.class, id);
  184.         } finally {
  185.             em.close();
  186.         }
  187.     }
  188.  
  189.     public int getCountryCount() {
  190.         EntityManager em = getEntityManager();
  191.         try {
  192.             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
  193.             Root<Country> rt = cq.from(Country.class);
  194.             cq.select(em.getCriteriaBuilder().count(rt));
  195.             Query q = em.createQuery(cq);
  196.             return ((Long) q.getSingleResult()).intValue();
  197.         } finally {
  198.             em.close();
  199.         }
  200.     }
  201.    
  202. }
  203.