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

package manager;

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

    public void newSemester(Semester sem) {
        em.persist(sem);
    }

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

    public Semester findOpen() {
        Query q = em.createNamedQuery("Semester.findOpen");
        q.setParameter("state", SchoolYearState.OPEN);
        return (Semester)q.getResultList().get(0);
    }

    public List<Semester> findOpenAndClosed() {
        Query q = em.createNamedQuery("Semester.findOpenAndClosed");
        q.setParameter("state1", SchoolYearState.CLOSED);
        q.setParameter("state2", SchoolYearState.OPEN);
        return q.getResultList();
    }

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

    public void update(Semester semester) {
        Query q = em.createNamedQuery("Semester.update");
        q.setParameter("schoolYearState", semester.getSchoolYearState());
        q.setParameter("semesterId", semester.getId());
        q.executeUpdate();
    }

}
