package es.dalocar.software.daos;

// Generated 29-oct-2009 22:34:39 by Hibernate Tools 3.2.4.GA

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import es.dalocar.software.beans.Cliente;

/**
 * Home object for domain model class Cliente.
 * 
 * @see es.dalocar.software.beans.Cliente
 * @author Hibernate Tools
 */
public class ClienteDAO extends HibernateDaoSupport {

	private static final Logger log = Logger.getLogger(ClienteDAO.class);

	public void persist(Cliente transientInstance) {
		log.debug("persisting Cliente instance");
		try {
			getHibernateTemplate().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void save(Cliente transientInstance) {
		log.debug("persisting Cliente instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void update(Cliente transientInstance) {
		log.debug("persisting Cliente instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Cliente instance) {
		log.debug("attaching dirty Cliente instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Cliente instance) {
		log.debug("attaching clean Cliente instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Cliente persistentInstance) {
		log.debug("deleting Cliente instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Cliente merge(Cliente detachedInstance) {
		log.debug("merging Cliente instance");
		try {
			Cliente result = (Cliente) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Cliente findById(java.lang.Integer id) {
		log.debug("getting Cliente instance with id: " + id);
		try {
			Cliente instance = (Cliente) getHibernateTemplate().get(
					"es.dalocar.software.beans.Cliente", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("getting all Clientes");
		try {
			List results = getHibernateTemplate().find(" from Cliente");
			return results;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Cliente instance) {
		log.debug("finding Cliente instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			/*
			 * getHibernateTemplate().createCriteria(
			 * "es.dalocar.software.beans.Cliente").add(
			 * Example.create(instance)).list();
			 * log.debug("find by example successful, result size: " +
			 * results.size());
			 */
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List searchCliente(Integer Identificador, java.lang.String Cif,
			java.lang.String Nombre) {

		DetachedCriteria c = DetachedCriteria.forClass(Cliente.class);

		if (Identificador.intValue() != 0)
			c.add(Restrictions.eq("Identificador", Identificador));

		if (!Cif.equals(""))
			c.add(Restrictions.ilike("Cif", "%" + Cif + "%"));

		if (!Nombre.equals(""))
			c.add(Restrictions.ilike("Nombre", "%" + Nombre + "%"));

		return getHibernateTemplate().findByCriteria(c);

	}

	public Integer getMaxId() {
		try {
			List results = getHibernateTemplate().find("select max(identificador) from Cliente c");
			Integer result = (Integer)results.get(0);
			return result;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

}
