com.r3bl.dao.FilaDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.r3bl.dao.FilaDaoImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.r3bl.dao;

import com.r3bl.model.Fila;
import com.r3bl.model.Medico;
import com.r3bl.model.Paciente;
import com.r3bl.model.UserInfo;
import com.r3bl.model.Usuario;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Rafael Soares Pinto <rafaelsp.93@gmail.com>
 */
public class FilaDaoImpl implements FilaDao {

    private static final Logger logger = LoggerFactory.getLogger(UsuarioDaoImpl.class);

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sf) {
        this.sessionFactory = sf;
    }

    @Override
    public void assignarPaciente(Paciente p, Medico m) {
        Session session = this.sessionFactory.getCurrentSession();

        if (!checkAssignado(p.getId_paciente())) {
            Fila f = new Fila();
            f.setMedico(m);
            f.setPaciente(p);
            f.setStatus("1");
            session.persist(f);
        } else {
            logger.info("Esse usurio j tem uma consulta ativa");
        }
    }

    @Override
    public void desassignarPaciente(int id_paciente) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean checkAssignado(int id_paciente) {
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery("from fila where id_paciente= :idPaciente and status = 1");
        query.setParameter("idPaciente", id_paciente);
        if (query.uniqueResult() == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public List<UserInfo> getAssignados(Medico m) {
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery("select new com.r3bl.model.UserInfo(u.id_usuario as id, \n"
                + "            p.nome as nome, \n" + "            p.cpf as identificacao,\n"
                + "                             u.tipo as tipo)\n"
                + "            from paciente p join p.usuario u \n" + "            where p.id_paciente in \n"
                + "                 (select f.paciente  \n" + "            from fila f \n"
                + "            where f.medico.id_medico = :idMedico \n" + "            and f.status = 1)");
        query.setParameter("idMedico", m.getId_medico());

        List<UserInfo> medicoList = query.list();
        return medicoList;
    }

}