package dao;

import com.tecgraf.dao.FacadeJPA;
import com.tecgraf.dao.GenericDAOJPA;
import java.util.List;
import javax.persistence.Query;
import negocio.Aluno;
import negocio.Disciplina;

/**
 * Singleton AlunoDAO
* 
* 
 * @author Ismael H F Santos
 */
public class AlunoDAO extends GenericDAOJPA<Aluno, Integer> {
    //--------------------------------------------------  
    // Public methods
    //--------------------------------------------------  
    public static synchronized AlunoDAO getInstance( FacadeJPA f ) {
        if( instance == null ) {
            instance = new AlunoDAO(f);
        }
        return instance;
    }
    
    public Aluno findByMatricula(String matricula){
        List<Aluno> la = queryPositionalParams(
                "SELECT a FROM Aluno a WHERE a.matricula = ?",
                matricula);
        if(la.size() == 0){
            return null;
        }
        return la.get(0);
    }

    public List<Disciplina> findAllDisciplinas(String matricula){
        String cSQL = "SELECT a.* FROM disciplina a, aluno b" +
                " where a.idaluno = b.id and b.matricula = " + matricula;
        Query query = em.createNativeQuery(cSQL, Disciplina.class);
        return query.getResultList();
    }

    //--------------------------------------------------  
    // Private constructor
    //--------------------------------------------------  
    private AlunoDAO( FacadeJPA f ) {
        super(Aluno.class, f);
    }
    //--------------------------------------------------  
    // Private atributes
    //--------------------------------------------------  
    private static AlunoDAO instance;
    
}
