package com.palmnest.dao.hibernate;

import com.palmnest.dao.LookupDao;
import com.palmnest.model.Role;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Hibernate implementation of LookupDao.
 *
 * @author <a href="mailto:chasel.java@gmail.com">Chasel Li</a>
 */
@Slf4j
@Repository
public class LookupDaoHibernate implements LookupDao {


	private final SessionFactory sessionFactory;

	/**
	 * Initialize LookupDaoHibernate with Hibernate SessionFactory.
	 *
	 * @param sessionFactory
	 */
	@Autowired
	public LookupDaoHibernate(final SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<Role> getRoles() {
		log.debug("Retrieving all role names...");
		Session session = sessionFactory.getCurrentSession();
		return session.createCriteria(Role.class).list();
	}
}
