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

package manager;

import entities.CurrentSubject;
import entities.Semester;
import entities.Subject;
import helper.SchoolYearState;
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 CurrentSubjectSessionBean implements CurrentSubjectSessionBeanRemote {
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    private EntityManager em;

    public void newCurrentSubject(CurrentSubject c) {
        em.persist(c);
    }

    public List<CurrentSubject> allCurrentSubjects() {
        Query query = em.createNamedQuery("CurrentSubject.findAll");
        return query.getResultList();
    }

    public List<CurrentSubject> findByIDs(Semester semester, Subject subject ) {
        Query query = em.createNamedQuery("CurrentSubject.findBySemesterAndSubject");
        query.setParameter("semesterId", semester);
        query.setParameter("subjectId", subject);
        return query.getResultList();
    }

    public List<Subject> findAktiv(){
        Semester aktivSemester;
        Query q = em.createNamedQuery("Semester.findOpen");
        q.setParameter("state", SchoolYearState.OPEN);
        aktivSemester =(Semester) q.getResultList().get(0);

        Query query = em.createNamedQuery("CurrentSubject.findByOpen");
        query.setParameter("semesterId", aktivSemester);
        return query.getResultList();
    }

    public List<Subject> findBySemester(Semester sem){
        Query query = em.createNamedQuery("CurrentSubject.findByOpen");
        query.setParameter("semesterId", sem);
        return query.getResultList();
    }

 
}
