package de.terrestris.shogun2.dao;

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

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import de.terrestris.shogun2.model.PersistentObject;

/**
 * @author Nils Bühner
 * 
 */
public abstract class GenericHibernateDao<E extends PersistentObject, ID extends Serializable> {

	/**
	 * Represents the class of the entity
	 */
	private Class<E> clazz;

	protected GenericHibernateDao(Class<E> clazz) {
		this.clazz = clazz;
	}

	/**
	 * Hibernate SessionFactory
	 */
	@Autowired
	private SessionFactory sessionFactory;

	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	@SuppressWarnings("unchecked")
	public E findById(ID id) {
		return (E) getSession().get(clazz, id);
	}

	/**
	 * Returns all Entities by calling findByCriteria(), i.e. without arguments.
	 * 
	 * @see GenericHibernateDao#findByCriteria(Criterion...)
	 * @return All entities
	 */
	public List<E> findAll() {
		return findByCriteria();
	}

	public void saveOrUpdate(E e) {
		e.setModified(DateTime.now());
		getSession().saveOrUpdate(e);
	}

	public void delete(E e) {
		getSession().delete(e);
	}

	/**
	 * Gets the results, that match a variable number of passed criterions. Call
	 * this method without arguments to find all entities.
	 * 
	 * @param criterion
	 *            A variable number of hibernate criterions
	 * @return Entities matching the passed hibernate criterions
	 */
	@SuppressWarnings("unchecked")
	public List<E> findByCriteria(Criterion... criterion) {
		Criteria criteria = getSession().createCriteria(clazz);
		for (Criterion c : criterion) {
			if (c != null) {
				criteria.add(c);
			}
		}
		return criteria.list();
	}

}

@@ -218,24 +218,106 @@
 .Criterion;%0A
+import org.hibernate.criterion.Order;%0Aimport org.hibernate.criterion.Projections;%0A
 import org.j
@@ -335,16 +335,16 @@
 teTime;%0A
-
 import o
@@ -451,16 +451,66 @@
 tObject;
+%0Aimport de.terrestris.shogun2.paging.PagingResult;
 %0A%0A/**%0A *
@@ -2008,10 +2008,1330 @@
 ();%0A%09%7D%0A%0A
+%09/**%0A%09 * Gets the results, that match a variable number of passed criterions,%0A%09 * considering the paging- and sort-info at the same time.%0A%09 * %0A%09 * @param firstResult%0A%09 *            Starting index for the paging request.%0A%09 * @param maxResults%0A%09 *            Max number of result size.%0A%09 * @param criterion%0A%09 *            A variable number of hibernate criterions%0A%09 * @return%0A%09 */%0A%09@SuppressWarnings(%22unchecked%22)%0A%09public PagingResult%3CE%3E findByCriteriaWithPaging(Integer firstResult,%0A%09%09%09Integer maxResults, List%3COrder%3E sorters, Criterion... criterion) %7B%0A%09%09Criteria criteria = getSession().createCriteria(clazz);%0A%0A%09%09// add paging info%0A%09%09if (maxResults != null && firstResult != null) %7B%0A%09%09%09criteria.setMaxResults(maxResults);%0A%09%09%09criteria.setFirstResult(firstResult);%0A%09%09%7D%0A%0A%09%09// add sort info%0A%09%09if (sorters != null) %7B%0A%09%09%09for (Order sortInfo : sorters) %7B%0A%09%09%09%09criteria.addOrder(sortInfo);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09for (Criterion c : criterion) %7B%0A%09%09%09if (c != null) %7B%0A%09%09%09%09criteria.add(c);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return new PagingResult%3CE%3E(criteria.list(), getTotalCount());%0A%09%7D%0A%0A%09/**%0A%09 * Returns the total count of db entries for the current type.%0A%09 * %0A%09 * @return%0A%09 */%0A%09private Number getTotalCount() %7B%0A%09%09Criteria criteria = getSession().createCriteria(clazz);%0A%09%09criteria.setProjection(Projections.rowCount());%0A%09%09return (Long) criteria.uniqueResult();%0A%09%7D%0A
 %7D%0A

Add a method for paging requests to the genericDao