package ub.scp2.daoImpl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import ub.scp2.dao.LanguajeDao;
import ub.scp2.entities.Languaje;

/**
 * LanguageDaoImpl.java -this class implements the LanguageDao. This is the Data Acces Object of the Language entity
 * 
 *
 */
public class LanguajeDaoImpl implements LanguajeDao, java.io.Serializable {
	SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * Retrieve a language list with all language
	 */
	public List<Languaje> getLanguajeList() {

		 Session session = sessionFactory.getCurrentSession();
		 try{
			 return session.createQuery( "FROM Languaje" ).list();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		return null;
	}
	
	/**
	 * Retrieve a language according to the id 
	 */
	public Languaje getLanguajeById(Integer languajeId){
		 try{
			 Session session = sessionFactory.getCurrentSession();
			 Criteria criteria = session.createCriteria(Languaje.class);
			 criteria.add(Restrictions.eq("languajeId", languajeId));
			 return (Languaje) criteria.uniqueResult();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 return null;
	}

}
