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

package manager;

import entities.CurrentSubject;
import entities.Lesson;
import entities.Semester;
import entities.Users;
import helper.LessonType;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Szilárd
 */
@Stateless
public class LessionSessionBean implements LessionSessionBeanRemote {
    
    @PersistenceContext
    private EntityManager em;

    public void newLesson(Lesson l) {
        em.persist(l);
    }

    public boolean findById(Long id) {
        Lesson l;
        Query query = em.createNamedQuery("Lesson.findId");
        query.setParameter("id", id);
        l = (Lesson)query.getResultList().get(0);
        if(l.getLimit()>l.getRegistered())
            return true;
        else
            return false;
    }

    public List<Lesson> findByL(CurrentSubject cs) {
        Query query = em.createNamedQuery("Lesson.findType");
        query.setParameter("lessonType", LessonType.LECTURE);
        query.setParameter("currentSubjectId", cs);
        return query.getResultList();
    }

    public List<Lesson> findByP(CurrentSubject cs) {
        Query query = em.createNamedQuery("Lesson.findType");
        query.setParameter("lessonType", LessonType.PRACTICE);
        query.setParameter("currentSubjectId", cs);
        return query.getResultList();
    }

    public List<Lesson> findBySemesterANDTeacher(Users u, Semester s) {
        Query query = em.createNamedQuery("Lesson.findBySemesterANDTeacher");
        query.setParameter("semesterId", s);
        query.setParameter("teacherId", u);
        return query.getResultList();
    }

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

    public void updatedec(Long id) {
        Query q = em.createNamedQuery("Lesson.updateDec");
        q.setParameter("id", id);
        q.executeUpdate();
    }
 
}
