Package jpa.controllers

Source Code of jpa.controllers.LectureJpaController

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

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Courses;
import entity.Lecture;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jpa.controllers.exceptions.NonexistentEntityException;

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

    public LectureJpaController() {
    }

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

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

    public void create(Lecture lecture) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Courses courseId = lecture.getCourseId();
            if (courseId != null) {
                courseId = em.getReference(courseId.getClass(), courseId.getCourseId());
                lecture.setCourseId(courseId);
            }
            em.persist(lecture);
            if (courseId != null) {
                courseId.getLectureList().add(lecture);
                courseId = em.merge(courseId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Lecture lecture) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Lecture persistentLecture = em.find(Lecture.class, lecture.getId());
            Courses courseIdOld = persistentLecture.getCourseId();
            Courses courseIdNew = lecture.getCourseId();
            if (courseIdNew != null) {
                courseIdNew = em.getReference(courseIdNew.getClass(), courseIdNew.getCourseId());
                lecture.setCourseId(courseIdNew);
            }
            lecture = em.merge(lecture);
            if (courseIdOld != null && !courseIdOld.equals(courseIdNew)) {
                courseIdOld.getLectureList().remove(lecture);
                courseIdOld = em.merge(courseIdOld);
            }
            if (courseIdNew != null && !courseIdNew.equals(courseIdOld)) {
                courseIdNew.getLectureList().add(lecture);
                courseIdNew = em.merge(courseIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = lecture.getId();
                if (findLecture(id) == null) {
                    throw new NonexistentEntityException("The lecture 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();
            Lecture lecture;
            try {
                lecture = em.getReference(Lecture.class, id);
                lecture.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The lecture with id " + id + " no longer exists.", enfe);
            }
            Courses courseId = lecture.getCourseId();
            if (courseId != null) {
                courseId.getLectureList().remove(lecture);
                courseId = em.merge(courseId);
            }
            em.remove(lecture);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Lecture> findLectureEntities() {
        return findLectureEntities(true, -1, -1);
    }

    public List<Lecture> findLectureEntities(int maxResults, int firstResult) {
        return findLectureEntities(false, maxResults, firstResult);
    }

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

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

    public int getLectureCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Lecture> rt = cq.from(Lecture.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.LectureJpaController

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.