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

package manager;

import entities.Lesson;
import entities.LessonStudent;
import entities.Users;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Stateless;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Query;

/**
 *
 * @author Szilárd
 */
@Stateless
public class LessonStudentSessionBean implements LessonStudentSessionBeanRemote {
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    private EntityManager em;

    public void newLessonStudent(LessonStudent l) {
        em.persist(l);
    }

    public void update(Long id, Lesson lesson) {
        Query q = em.createNamedQuery("LessonStudent.update");
        q.setParameter("lessonId", lesson);
        q.setParameter("id", id);
        q.executeUpdate();
    }

    public boolean findByLessonIdAndStudentId(Lesson id, Users studentId){
        Query q = em.createNamedQuery("LessonStudent.findByLessonId");
        q.setParameter("lessonId", id);
        q.setParameter("studentId", studentId);
        if (q.getResultList().isEmpty())
            return false;
        else
            return true;

    }

    public LessonStudent findByLessonId(Lesson id, Users studentId){
        Query q = em.createNamedQuery("LessonStudent.findByLessonId");
        q.setParameter("lessonId", id);
        q.setParameter("studentId", studentId);
        return (LessonStudent) q.getResultList().get(0);
    }

    public LessonStudent findId(Long id){
        Query q = em.createNamedQuery("LessonStudent.findById");
        q.setParameter("id", id);
        return (LessonStudent)q.getResultList().get(0);
    }

    public void delete(Long id){
        Query q = em.createNamedQuery("LessonStudent.delete");
        q.setParameter("id", id);
        q.executeUpdate();
    }

     public List<LessonStudent> findStudents(Lesson id){
        Query q = em.createNamedQuery("LessonStudent.findStudents");
        q.setParameter("lessonId", id);
        return q.getResultList();
    }

 
}
