package org.sakaiproject.wontovlab.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sakaiproject.wontovlab.dao.MaterialDao;
import org.sakaiproject.wontovlab.model.Drug;
import org.sakaiproject.wontovlab.model.Equipment;
import org.sakaiproject.wontovlab.model.Material;
import org.sakaiproject.wontovlab.model.Solution;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Implementations of any specialized DAO methods from the specialized DAO 
 * that allows the developer to extend the functionality of the generic dao package
 * @author Sakai App Builder -AZ
 */
public class MaterialDaoImpl 
	extends HibernateDaoSupport 
		implements MaterialDao {

	private static Log log = LogFactory.getLog(MaterialDaoImpl.class);

	public void init() {
		log.debug("init");
	}
	
	public List findAllMaterials() {
		return getHibernateTemplate().find("from Material");
	}
	
//Tive que fazer isso pois ao fazer um select em Material (description), ao procurar nos filhos, ele procurava
//somente no primeiro filho que aparecia no mapeamento HBM em joined-subclass, e nao ia para o segundo
//Entao faço o select nos dois filhos e o que retornar 1 eu retorno
	public List findMaterialByDescription(String material) {
		List l1 = findEquipmentByDescription(material); 
		List l2 = findDrugByDescription(material);
		List l3 = findSolutionByDescription(material);
		
		if (l1.size() == 1) {
			return l1;
		} else {
			if (l2.size() == 1)
				return l2;
			else
				return l3;
		}
		
	}
	
	
	
	public List findEquipmentByDescription(String description) {
		return getHibernateTemplate().find("from Equipment e where e.description=? order by e.description", description);
	}
	public List findDrugByDescription(String description) {
		return getHibernateTemplate().find("from Drug d where d.description=? order by d.description", description);
	}
	public List findSolutionByDescription(String description) {
		return getHibernateTemplate().find("from Solution s where s.description=? order by s.description", description);
	}
	
	public List findAllEquipments() {
		return getHibernateTemplate().find("from Equipment e order by e.description");
	}
	
	public List findAllDrugs() {
		return getHibernateTemplate().find("from Drug d order by d.description");
	}

	public List findAllSolutions() {
		return getHibernateTemplate().find("from Solution s order by s.description");
	}
	
	
	
	
	
	
	
	
	public boolean saveMaterial(Material l) {
		getHibernateTemplate().saveOrUpdate(l);
		return true;
	}
	
	public boolean saveDrug(Drug l) {
		getHibernateTemplate().saveOrUpdate(l);
		return true;
	}
	
	public boolean saveEquipment(Equipment l) {
		getHibernateTemplate().saveOrUpdate(l);
		return true;
	}
	
	public boolean saveSolution(Solution l) {
		getHibernateTemplate().saveOrUpdate(l);
		return true;
	}
	
	
	public boolean deleteDrug(Drug l){
		getHibernateTemplate().delete(l);
		return true;
	}

	public boolean deleteEquipment(Equipment l){
		getHibernateTemplate().delete(l);
		return true;
	}
	
	public boolean deleteSolution(Solution l){
		getHibernateTemplate().delete(l);
		return true;
	}
}
