Package jpa.controllers

Source Code of jpa.controllers.CourseratesJpaController

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jpa.controllers;

import entity.Courserates;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Enrollment;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jpa.controllers.exceptions.IllegalOrphanException;
import jpa.controllers.exceptions.NonexistentEntityException;
import jpa.controllers.exceptions.PreexistingEntityException;

/**
*
* @author atap
*/
public class CourseratesJpaController implements Serializable {

    public CourseratesJpaController() {
    }
   
    public CourseratesJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("EducationXPU");

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

    public void create(Courserates courserates) throws IllegalOrphanException, PreexistingEntityException, Exception {
        List<String> illegalOrphanMessages = null;
        Enrollment enrollmentOrphanCheck = courserates.getEnrollment();
        if (enrollmentOrphanCheck != null) {
            Courserates oldCourseratesOfEnrollment = enrollmentOrphanCheck.getCourserates();
            if (oldCourseratesOfEnrollment != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Enrollment " + enrollmentOrphanCheck + " already has an item of type Courserates whose enrollment column cannot be null. Please make another selection for the enrollment field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Enrollment enrollment = courserates.getEnrollment();
            if (enrollment != null) {
                enrollment = em.getReference(enrollment.getClass(), enrollment.getId());
                courserates.setEnrollment(enrollment);
            }
            em.persist(courserates);
            if (enrollment != null) {
                enrollment.setCourserates(courserates);
                enrollment = em.merge(enrollment);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCourserates(courserates.getId()) != null) {
                throw new PreexistingEntityException("Courserates " + courserates + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Courserates courserates) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Courserates persistentCourserates = em.find(Courserates.class, courserates.getId());
            Enrollment enrollmentOld = persistentCourserates.getEnrollment();
            Enrollment enrollmentNew = courserates.getEnrollment();
            List<String> illegalOrphanMessages = null;
            if (enrollmentNew != null && !enrollmentNew.equals(enrollmentOld)) {
                Courserates oldCourseratesOfEnrollment = enrollmentNew.getCourserates();
                if (oldCourseratesOfEnrollment != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Enrollment " + enrollmentNew + " already has an item of type Courserates whose enrollment column cannot be null. Please make another selection for the enrollment field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (enrollmentNew != null) {
                enrollmentNew = em.getReference(enrollmentNew.getClass(), enrollmentNew.getId());
                courserates.setEnrollment(enrollmentNew);
            }
            courserates = em.merge(courserates);
            if (enrollmentOld != null && !enrollmentOld.equals(enrollmentNew)) {
                enrollmentOld.setCourserates(null);
                enrollmentOld = em.merge(enrollmentOld);
            }
            if (enrollmentNew != null && !enrollmentNew.equals(enrollmentOld)) {
                enrollmentNew.setCourserates(courserates);
                enrollmentNew = em.merge(enrollmentNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = courserates.getId();
                if (findCourserates(id) == null) {
                    throw new NonexistentEntityException("The courserates with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Courserates courserates;
            try {
                courserates = em.getReference(Courserates.class, id);
                courserates.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The courserates with id " + id + " no longer exists.", enfe);
            }
            Enrollment enrollment = courserates.getEnrollment();
            if (enrollment != null) {
                enrollment.setCourserates(null);
                enrollment = em.merge(enrollment);
            }
            em.remove(courserates);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Courserates> findCourseratesEntities() {
        return findCourseratesEntities(true, -1, -1);
    }

    public List<Courserates> findCourseratesEntities(int maxResults, int firstResult) {
        return findCourseratesEntities(false, maxResults, firstResult);
    }

    private List<Courserates> findCourseratesEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Courserates.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Courserates findCourserates(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Courserates.class, id);
        } finally {
            em.close();
        }
    }

    public int getCourseratesCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Courserates> rt = cq.from(Courserates.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
   
}
TOP

Related Classes of jpa.controllers.CourseratesJpaController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.