package com.alcedomoreno.sirme.core.dao.common;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.alcedomoreno.sirme.core.dao.ApplicationsDaoImpl;
import com.google.common.base.Preconditions;

/**
 * Clase que implementa todos los métodos genéricos
 * @param <T> tipo con el que se extenderá la clase
 */
@SuppressWarnings("unchecked")
public class AbstractHibernateDao<T extends Serializable> extends HibernateDaoSupport implements Operations<T> {

	///////////////////////////////////////////////////////////////
	//                         Attributes                        //
	///////////////////////////////////////////////////////////////

	private static Logger log = LoggerFactory.getLogger( ApplicationsDaoImpl.class );
	private static final String CLASS_NAME = "ApplicationsDaoImpl";

	private Class<T> clazz;

	@Autowired
    private SessionFactory sessionFactory;

	///////////////////////////////////////////////////////////////
	//                      End of Attributes                    //
	///////////////////////////////////////////////////////////////

	@Autowired
	public void init(SessionFactory factory) {
	    setSessionFactory(factory);
	}

	protected final void setClazz(final Class<T> clazzToSet) {
        clazz = Preconditions.checkNotNull(clazzToSet);
    }
	@Override
	public T findOne(Serializable id) {
		return (T) getCurrentSession().get(clazz, id);
	}
	@Override
	public List<T> findAll() {

		return getCurrentSession().createQuery("from " + clazz.getName()).list();
	}
	@Override
	public void create(T entity) {
		Preconditions.checkNotNull(entity);
        // getCurrentSession().persist(entity);
		getCurrentSession().saveOrUpdate(entity);
	}
	@Override
	public T update(T entity) {
		Preconditions.checkNotNull(entity);
        return (T) getCurrentSession().merge(entity);
	}
	@Override
	public void delete(T entity) {
		Preconditions.checkNotNull(entity);
		getCurrentSession().delete(entity);
	}
	@Override
	public void deleteById(Integer entityId) {
		final T entity = findOne(entityId);
        Preconditions.checkState(entity != null);
        delete(entity);
	}
    protected final Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }
	@Override
	public void evict(T entity) {
		Preconditions.checkNotNull(entity);
		getCurrentSession().evict(entity);
	}


	@Override
	public long count() {
		return (Long) getCurrentSession().createCriteria( clazz.getName() )
				.setProjection( Projections.rowCount() ).uniqueResult();
	}
	
	@Override
	public List<T> findAll(int rowId, int size) {

		Query query = getCurrentSession().createQuery("FROM " + clazz.getName());
        query.setFirstResult( rowId );
        query.setMaxResults( size );

		return query.list();
	}
}

@@ -571,17 +571,18 @@
 os los m
-%C3%A9
+%C3%83%C2%A9
 todos ge
@@ -586,9 +586,10 @@
  gen
-%C3%A9
+%C3%83%C2%A9
 rico
@@ -635,9 +635,10 @@
 nder
-%C3%A1
+%C3%83%C2%A1
  la 
@@ -2704,12 +2704,15 @@
 lic 
-long
+Integer
  cou
@@ -2732,12 +2732,15 @@
 rn (
-Long
+Integer
 ) ge

count return Integer