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.RoleDao;
import ub.scp2.entities.Role;
import ub.scp2.entities.Subject;
import ub.scp2.entities.Task;

/**
 * RoleDaoImpl.java -this class implements the RoleDao. This is the Data Acces Object of the Role entity
 * 
 *
 */
public class RoleDaoImpl implements RoleDao, java.io.Serializable{
	SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	/**
	 * Retrieve a language list with all roles
	 */
	public List<Role> getRoleList() {

		 Session session = sessionFactory.getCurrentSession();
		 try{
			 return session.createQuery( "FROM Role r WHERE r.id in (1,2)" ).list();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		return null;
	}

	/**
	 * Retrieve a role according to the id 
	 */
	public Role getRoleById(Integer roleId) {
		 try{
			 Session session = sessionFactory.getCurrentSession();
			 Criteria criteria = session.createCriteria(Role.class);
			 criteria.add(Restrictions.eq("roleId", roleId));
			 return (Role) criteria.uniqueResult();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 return null;
	}

}
