package org.sistema.hibernate.hql.dao;

import java.util.List;

import org.hibernate.Query;
import org.sistema.hibernate.hql.HibernateSession;
import org.sistema.hibernate.hql.models.Product;

/**
 * implementation of ProductDAO
 * 
 * @author Eugenia Pérez
 * @email eugenia_perez@cuatrovientos.org
 *
 */
public class ProductDAO extends GenericDAO<Product> {

	/**
	 * Selects all products of given name HQL style
	 * 
	 * @return List of products
	 */
	public List<Product> selectName(String name) {
		session = HibernateSession.getSession();

		Query query = session.createQuery("From Product p where p.name= :name");

		query.setParameter("name", name);

		List<Product> products = query.list();

		session.close();
		
		return products;
	}

	

	/**
	 * Selects all products of given type HQL with implicit join
	 * 
	 * @return List of products
	 */
	public List<Product> selectType(Long idType) {
		session = HibernateSession.getSession();
		
		Query query = session
				.createQuery("FROM Product p WHERE p.productType.id=?0");

		query.setParameter("0", idType);

		List<Product> products = (List<Product>) query.list();
		
		session.close();
		
		return products;
	}
	

	/**
	 * Selects all products of given type name HQL with explicit join
	 * 
	 * @return List of products
	 */
	public List<Product> selectProductsByTypeName(String typeName) {
		session = HibernateSession.getSession();

		Query query = session
				.createQuery("SELECT p FROM Product p INNER JOIN p.productType t where t.name=?0");

		query.setParameter("0", typeName);

		List<Product> products = query.list();

		session.close();
		
		return products;
	}
}
