From cea4bf11d18bbe9c82d2fbd02e8faeb100dc1db6 Mon Sep 17 00:00:00 2001 From: Ben Alex Date: Wed, 20 Apr 2005 12:06:23 +0000 Subject: [PATCH] Add scrollWithSubclasses() method. --- .../org/acegisecurity/domain/dao/Dao.java | 27 +++++++++++++++++-- .../domain/hibernate/DaoHibernate.java | 20 +++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/domain/src/main/java/org/acegisecurity/domain/dao/Dao.java b/domain/src/main/java/org/acegisecurity/domain/dao/Dao.java index 920d823eea..0eb490b3e7 100644 --- a/domain/src/main/java/org/acegisecurity/domain/dao/Dao.java +++ b/domain/src/main/java/org/acegisecurity/domain/dao/Dao.java @@ -94,7 +94,7 @@ public interface Dao { public void delete(PersistableEntity value); /** - * Return all persistent instances. + * Return all persistent instances, including subclasses. * * @return all persistence instances (an empty List will be * returned if no matches are found) @@ -133,7 +133,8 @@ public interface Dao { * the query by example evaluation. *

* - * @param value parameters to filter on + * @param value parameters to filter on (the class of this object will be + * added to the filter) * @param firstElement the first result (start at zero to obtain all * results) * @param maxElements the maximum number of results desired for this page @@ -148,6 +149,28 @@ public interface Dao { public PaginatedList scroll(PersistableEntity value, int firstElement, int maxElements, String orderByAsc); + /** + * Find persistent instances with properties matching those of the passed + * PersistableEntity, ignoring the class of the passed + * PersistableEntity (useful if you pass a superclass, as you + * want to find all subclass instances which match). + * + * @param value parameters to filter on (the class of this object will NOT + * be added to the filter) + * @param firstElement the first result (start at zero to obtain all + * results) + * @param maxElements the maximum number of results desired for this page + * of the result set + * @param orderByAsc the property name of the + * PersistableEntity that should be used to order the + * results + * + * @return the requested page of the result list (a properly formed + * PaginatedList is returned if no results match) + */ + public PaginatedList scrollWithSubclasses(PersistableEntity value, + int firstElement, int maxElements, String orderByAsc); + /** * Indicates whether the DAO instance provides persistence services for the * specified class. diff --git a/domain/src/main/java/org/acegisecurity/domain/hibernate/DaoHibernate.java b/domain/src/main/java/org/acegisecurity/domain/hibernate/DaoHibernate.java index 80d59cbd83..d3429ad3b3 100644 --- a/domain/src/main/java/org/acegisecurity/domain/hibernate/DaoHibernate.java +++ b/domain/src/main/java/org/acegisecurity/domain/hibernate/DaoHibernate.java @@ -109,12 +109,24 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao, Assert.notNull(value); Assert.hasText(orderByAsc, "An orderByAsc is required (why not use your identity property?)"); + Assert.isInstanceOf(this.supportsClass, value, "Can only scroll with values this DAO supports"); return (PaginatedList) getHibernateTemplate().execute(getFindByValueCallback( - value, firstElement, maxElements, Order.asc(orderByAsc))); + value.getClass(), value, firstElement, maxElements, Order.asc(orderByAsc))); } - public boolean supports(Class clazz) { + public PaginatedList scrollWithSubclasses(PersistableEntity value, int firstElement, + int maxElements, String orderByAsc) { + Assert.notNull(value); + Assert.hasText(orderByAsc, + "An orderByAsc is required (why not use your identity property?)"); + Assert.isInstanceOf(this.supportsClass, value, "Can only scroll with values this DAO supports"); + + return (PaginatedList) getHibernateTemplate().execute(getFindByValueCallback( + this.supportsClass, value, firstElement, maxElements, Order.asc(orderByAsc))); + } + + public boolean supports(Class clazz) { Assert.notNull(clazz); return this.supportsClass.equals(clazz); @@ -187,12 +199,12 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao, * * @return a PaginatedList containing the requested objects */ - private HibernateCallback getFindByValueCallback(final Object bean, + private HibernateCallback getFindByValueCallback(final Class whichClass, final Object bean, final int firstElement, final int count, final Order order) { return new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { - Criteria criteria = session.createCriteria(bean.getClass()); + Criteria criteria = session.createCriteria(whichClass); criteria.addOrder(order);