/**
 * 
 */
package org.sistema.hibernate.criteria.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.sistema.hibernate.criteria.HibernateSession;
import org.sistema.hibernate.criteria.models.ProductType;

/**
 * implementation of productTypeDAOInterface
 * 
 * @author Eugenia Pérez
 * @email eugenia_perez@cuatrovientos.org
 *
 */
public class ProductTypeDAO extends GenericDAO {

	/**
	 * Retrieves all product types between a range of two ids.
	 * 
	 * @return List of products
	 */
	public List<ProductType> selectIdRange(Long firstId, Long lastId) {
		session = HibernateSession.getSession();

		List<ProductType> productTypes = session
				.createCriteria(ProductType.class)
				.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
				.add(Restrictions.between("id", firstId, lastId)).list();

		session.close();
		return productTypes;
	}

	/**
	 * Retrieves all products begining with a given prefix
	 * 
	 * @return List of products
	 */
	public List<ProductType> selectNamePrefix(String prefix) {
		session = HibernateSession.getSession();

		List<ProductType> productTypes = session
				.createCriteria(ProductType.class)
				.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
				.add(Restrictions.like("name", prefix + "%")).list();

		session.close();
		return productTypes;
	}

	/**
	 * Selects the type of product for a specific product name.
	 * @param productName
	 * @return
	 */
	public List<ProductType> selectProductName(String productName) {
		session = HibernateSession.getSession();

		List<ProductType> productTypes = session
				.createCriteria(ProductType.class).createAlias("products", "p")
				.add(Restrictions.like("p.name", "%" + productName + "%"))
				.list();

		session.close();
		return productTypes;
	}

}
