package com.br.uepb.dao;

import java.util.List;

import org.hibernate.Session;

import com.br.uepb.model.LoginDomain;
import com.br.uepb.model.PacienteDomain;

import conexaoBD.HibernateUtil;

public class LoginDAO {
	
	private Session sessaoAtual;
	
	public LoginDAO(){
		
	}
	
	/**
	 * Salva ou atualiza login
	 * @param login Sem o ID: Salvar, com ID: atualiza
	 */
	public void salvaLogin(LoginDomain login){
		SessaoAtual().beginTransaction();
		if(ehNovoUsuario(login)){
			SessaoAtual().save(login);
		}
		else{
			SessaoAtual().update(login);
		}
		SessaoAtual().getTransaction().commit();
		SessaoAtual().close();
	}
	
	public void excluiLogin(LoginDomain login){
		SessaoAtual().delete(login);
		SessaoAtual().close();
	}
	
	public LoginDomain obtemLogin(int idLogin){
		LoginDomain login = (LoginDomain)SessaoAtual().get(LoginDomain.class, idLogin);
		SessaoAtual().close();
		return login;
	}
	
	public LoginDomain obtemLogin(String usuario, String senha){
		LoginDomain login = (LoginDomain)SessaoAtual().createQuery("from LoginDomain"
				+ " where login ='" + usuario + "' AND senha ='" + senha + "'").list().get(0);
		
		return login;
	}
	
	public boolean jaExisteUsuario(String usuario){
		LoginDomain login = (LoginDomain)SessaoAtual().createQuery("from LoginDomain"
				+ " where login ='" + usuario + "'").list().get(0);
		if(login == null) return false;
		else return true;
	}
	
	@SuppressWarnings("unchecked")
	public List<LoginDomain> listaLogins(){
		
		List<LoginDomain> logins = 
				(List<LoginDomain>)SessaoAtual().createQuery("from LoginDomain").list();
		
		SessaoAtual().close();
		return logins;
	}
	
	private Session SessaoAtual(){
		if (sessaoAtual == null || !sessaoAtual.isOpen()){
			sessaoAtual = HibernateUtil.getSessionFactory().openSession();
		}
		return sessaoAtual;
	}
	
	private boolean ehNovoUsuario(LoginDomain login){
		if(login.getId() > 0){
			return false;
		}
		else{
			return true;
		}
	}

}

@@ -1142,32 +1142,64 @@
 urn login;%0A%09%7D%0A%09%0A
+%09@SuppressWarnings(%22unchecked%22)%0A
 %09public boolean 
@@ -1229,24 +1229,29 @@
 usuario)%7B%0A%09%09
+List%3C
 LoginDomain 
@@ -1245,34 +1245,46 @@
 %3CLoginDomain
+%3E
  login
+s
  = 
-(
+%0A%09%09%09%09(List%3C
 LoginDomain)
@@ -1274,32 +1274,33 @@
 List%3CLoginDomain
+%3E
 )SessaoAtual().c
@@ -1332,56 +1332,16 @@
 ain%22
-%0A%09%09%09%09+ %22 where login ='%22 + usuario + %22'%22
 ).list()
 .get
@@ -1332,31 +1332,24 @@
 ain%22).list()
-.get(0)
 ;%0A%09%09if(login
@@ -1348,24 +1348,25 @@
 if(login
+s
  == null
 ) return
@@ -1357,18 +1357,42 @@
  == null
-)
  
+%7C%7C logins.isEmpty())%7B%0A%09%09%09
 return f
@@ -1397,16 +1397,20 @@
  false;%0A
+%09%09%7D%0A
 %09%09else r

Ajuste no método jaExisteUsuario