package es.dalocar.software.daos;

// Generated 03-may-2010 23:15:26 by Hibernate Tools 3.2.4.GA

import java.util.List;

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

import es.dalocar.software.beans.Amortizacion;
import es.dalocar.software.beans.LineaAmortizacion;

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

	private static final Logger log = Logger.getLogger(LineaAmortizacionDAO.class);
	private static final boolean DEBUG = log.isDebugEnabled();



	

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

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

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

	public void delete(LineaAmortizacion persistentInstance) {
		log.debug("deleting Lineaamortizacion instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	/**
	 * Elimina las lineas de una factura para guardarlas posteriormente
	 * @param factura
	 */
	public void deleteFromAmortizacion(Amortizacion factura) {
		if (DEBUG)
			log.debug("deleting LineaFactura instance from Factura");
		try {
			getHibernateTemplate().deleteAll(findByAmortizacion(factura));
			if (DEBUG)
				log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	/**
	 * Persiste las lineas de una factura
	 * @param factura
	 */
	public void saveLineasFactura(Amortizacion factura) {
		if (DEBUG)
			log.debug("saving all LineaFactura instances from Factura");
		try {
			deleteFromAmortizacion(factura);
			getHibernateTemplate().saveOrUpdateAll(factura.getLineaamortizacions());
			if (DEBUG)
				log.debug("saved All  successful");
		} catch (RuntimeException re) {
			log.error("saveLineasFacturas failed", re);
			throw re;
		}
	}

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

	public LineaAmortizacion findById(
			es.dalocar.software.beans.LineaAmortizacionId id) {
		log.debug("getting Lineaamortizacion instance with id: " + id);
		try {
			LineaAmortizacion instance = (LineaAmortizacion)getHibernateTemplate().get(
							"es.dalocar.software.beans.Lineaamortizacion", 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 findByExample(LineaAmortizacion instance) {
		if (DEBUG)
			log.debug("finding LineaFactura instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			if (DEBUG)
				log.debug("find by example successful, result size: "
						+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/**
	 * @param factura
	 * @return
	 */
	public List findByAmortizacion(es.dalocar.software.beans.Amortizacion amortizacion) {

		if (DEBUG)
			log.debug("finding LineaFactura instance by example");
		try {
		
			List results = getHibernateTemplate().find("from LineaFactura where " +
					"id.numerofactura=" + amortizacion.getId().getNumero()+ " " +
					"and id.anyofactura=" + amortizacion.getId().getAnyo() + " order by id.numerolinea" );			
			if (DEBUG)
				log.debug("find by query successful, result size: "
						+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
}
