|
From: <gca...@us...> - 2011-11-30 15:11:43
|
Revision: 3712
http://openutils.svn.sourceforge.net/openutils/?rev=3712&view=rev
Author: gcatania
Date: 2011-11-30 15:11:36 +0000 (Wed, 30 Nov 2011)
Log Message:
-----------
BSHD-2 exposed ExampleTree methods, deprecated use of FilterMetadata
Modified Paths:
--------------
trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java
trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java
trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java
trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java
trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java 2011-11-30 12:05:20 UTC (rev 3711)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java 2011-11-30 15:11:36 UTC (rev 3712)
@@ -25,6 +25,7 @@
package it.openutils.dao.hibernate;
+import it.openutils.hibernate.example.ExampleTree;
import it.openutils.hibernate.example.FilterMetadata;
import java.io.Serializable;
@@ -125,13 +126,53 @@
List<T> findFiltered(T filter, int maxResults, int page);
/**
+ * Retrieve the entities handled by this DAO that match the input example tree
+ * @param exampleTree the example tree criterion to match
+ * @return a list of distinct entity instances (never null)
+ */
+ List<T> findFiltered(ExampleTree exampleTree);
+
+ /**
+ * Retrieve the entities handled by this DAO that match the input example tree
+ * @param exampleTree the example tree criterion to match
+ * @param orders the orders to apply with respect to entity class properties
+ * @return a list of distinct entity instances (never null)
+ */
+ List<T> findFiltered(ExampleTree exampleTree, Order... orders);
+
+ /**
+ * Retrieve the entities handled by this DAO that match the input example tree
+ * @param exampleTree the example tree criterion to match
+ * @param maxResults the maximum number of results to be fetched
+ * @param page the zero-based page number to use when displaying paginated results (the first entity returned is the
+ * one at position <code>maxResults * page</code> in the complete list of results), or <code>0</code> for no
+ * pagination
+ * @return a list of distinct entity instances (never null)
+ */
+ List<T> findFiltered(ExampleTree exampleTree, int maxResults, int page);
+
+ /**
+ * Retrieve the entities handled by this DAO that match the input example tree
+ * @param exampleTree the example tree criterion to match
+ * @param maxResults the maximum number of results to be fetched
+ * @param page the zero-based page number to use when displaying paginated results (the first entity returned is the
+ * one at position <code>maxResults * page</code> in the complete list of results), or <code>0</code> for no
+ * pagination
+ * @param orders the orders to apply with respect to entity class properties
+ * @return a list of distinct entity instances (never null)
+ */
+ List<T> findFiltered(ExampleTree exampleTree, int maxResults, int page, Order... orders);
+
+ /**
* Retrieve the entities handled by this DAO whose property values match, via <code>equals()</code> or via a
* specified <code>FilterMetadata</code> object, <code>filter</code>'s non-null property values.
* @param filter an instance of this DAO's entity class to be used as filter
* @param metadata a map that matches names of entity class properties to <code>FilterMetadata</code> modifiers,
* that will be used for comparing values of the corresponding property
* @return a list of distinct entity instances (never null)
+ * @deprecated use of {@link FilterMetadata} has been deprecated
*/
+ @Deprecated
List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata);
/**
@@ -145,7 +186,9 @@
* one at position <code>maxResults * page</code> in the complete list of results), or <code>0</code> for no
* pagination
* @return a list of distinct entity instances (never null)
+ * @deprecated use of {@link FilterMetadata} has been deprecated
*/
+ @Deprecated
List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page);
/**
@@ -161,7 +204,9 @@
* pagination
* @param orders the orders to apply with respect to entity class properties
* @return a list of distinct entity instances (never null)
+ * @deprecated use of {@link FilterMetadata} has been deprecated
*/
+ @Deprecated
List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page,
Order... orders);
@@ -179,7 +224,9 @@
* @param criteria a list of additional Hibernate criteria
* @param orders the orders to apply with respect to entity class properties
* @return a list of distinct entity instances (never null)
+ * @deprecated use of {@link FilterMetadata} has been deprecated
*/
+ @Deprecated
List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page,
List< ? extends Criterion> criteria, Order... orders);
@@ -197,7 +244,9 @@
* @param properties the names of the properties to return
* @param orders the orders to apply with respect to entity class properties
* @return a list of distinct entity instances (never null)
+ * @deprecated use of {@link FilterMetadata} has been deprecated
*/
+ @Deprecated
List< ? > findFilteredProperties(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults,
int page, List< ? extends Criterion> criteria, List<String> properties, Order... orders);
@@ -210,6 +259,14 @@
T findFilteredFirst(T filter);
/**
+ * Retrieve the first entity instance that matches the input example tree
+ * @param exampleTree the example tree criterion to match
+ * @return the first matching instance of the entity class managed by this DAO, or <code>null</code> if none found
+ * @see #findFiltered(ExampleTree)
+ */
+ T findFilteredFirst(ExampleTree exampleTree);
+
+ /**
* Retrieve the first entity instance that matches the input <code>filter</code>, if existing.
* @param filter an instance of this DAO's entity class to be used as filter
* @param orders the orders to apply with respect to entity class properties
@@ -219,6 +276,15 @@
T findFilteredFirst(T filter, Order... orders);
/**
+ * Retrieve the first entity instance that matches the input example tree
+ * @param exampleTree the example tree criterion to match
+ * @param orders the orders to apply with respect to entity class properties
+ * @return the first matching instance of the entity class managed by this DAO, or <code>null</code> if none found
+ * @see #findFiltered(ExampleTree, Order...)
+ */
+ T findFilteredFirst(ExampleTree exampleTree, final Order... orders);
+
+ /**
* Retrieve the first entity instance that matches the input <code>filter</code> and the additional input
* <code>criteria</code>, if existing.
* @param filter an instance of this DAO's entity class to be used as filter
@@ -337,7 +403,7 @@
* one at position <code>maxResults * page</code> in the complete list of results), or <code>0</code> for no
* pagination
* @return a list of distinct entity instances (never null)
- * @deprecated superseded by {@link #findFiltered(Object, Map, int, int, Order...)}
+ * @deprecated use of {@link FilterMetadata} has been deprecated
*/
@Deprecated
List<T> findFiltered(T filter, Order[] orders, Map<String, ? extends FilterMetadata> metadata, int maxResults,
@@ -357,7 +423,7 @@
* pagination
* @param criteria a list of additional Hibernate criteria
* @return a list of distinct entity instances (never null)
- * @deprecated superseded by {@link #findFiltered(Object, Map, int, int, List, Order...)}
+ * @deprecated use of {@link FilterMetadata} has been deprecated
*/
@Deprecated
List<T> findFiltered(T filter, Order[] orders, Map<String, ? extends FilterMetadata> metadata, int maxResults,
@@ -377,7 +443,7 @@
* @param criteria a list of additional Hibernate criteria
* @param properties the names of the properties to return
* @return a list of distinct entity instances (never null)
- * @deprecated superseded by {@link #findFilteredProperties(Object, Map, int, int, List, List, Order...)}
+ * @deprecated use of {@link FilterMetadata} has been deprecated
*/
@Deprecated
List< ? > findFilteredProperties(T filter, Order[] orders, Map<String, ? extends FilterMetadata> metadata,
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2011-11-30 12:05:20 UTC (rev 3711)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2011-11-30 15:11:36 UTC (rev 3712)
@@ -38,6 +38,8 @@
import java.util.Map;
import org.aopalliance.aop.AspectException;
+import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
@@ -188,46 +190,53 @@
*/
public List<T> findFiltered(T filter)
{
- return getThis().findFiltered(
- filter,
- getDefaultFilterMetadata(),
- Integer.MAX_VALUE,
- 0,
- Collections.<Criterion> emptyList(),
- getDefaultOrder());
+ return getThis().findFiltered(new ExampleTree(filter), Integer.MAX_VALUE, 0, getDefaultOrder());
}
/**
* {@inheritDoc}
*/
+ public List<T> findFiltered(ExampleTree exampleTree)
+ {
+ return getThis().findFiltered(exampleTree, Integer.MAX_VALUE, 0, getDefaultOrder());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public List<T> findFiltered(T filter, Order... orders)
{
- return getThis().findFiltered(
- filter,
- getDefaultFilterMetadata(),
- Integer.MAX_VALUE,
- 0,
- Collections.<Criterion> emptyList(),
- orders);
+ return getThis().findFiltered(new ExampleTree(filter), Integer.MAX_VALUE, 0, orders);
}
/**
* {@inheritDoc}
*/
+ public List<T> findFiltered(ExampleTree exampleTree, Order... orders)
+ {
+ return getThis().findFiltered(exampleTree, Integer.MAX_VALUE, 0, orders);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public List<T> findFiltered(T filter, int maxResults, int page)
{
- return getThis().findFiltered(
- filter,
- getDefaultFilterMetadata(),
- maxResults,
- page,
- Collections.<Criterion> emptyList(),
- getDefaultOrder());
+ return getThis().findFiltered(new ExampleTree(filter), maxResults, page, getDefaultOrder());
}
/**
* {@inheritDoc}
*/
+ public List<T> findFiltered(ExampleTree exampleTree, int maxResults, int page)
+ {
+ return getThis().findFiltered(exampleTree, maxResults, page, getDefaultOrder());
+ }
+
+ /**
+ * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
+ */
+ @Deprecated
public List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata)
{
return getThis().findFiltered(
@@ -240,8 +249,9 @@
}
/**
- * {@inheritDoc}
+ * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
*/
+ @Deprecated
public List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page)
{
return getThis().findFiltered(
@@ -258,41 +268,41 @@
*/
public T findFilteredFirst(T filter)
{
- return getFirstInCollection(getThis().findFiltered(
- filter,
- getDefaultFilterMetadata(),
- 1,
- 0,
- Collections.<Criterion> emptyList(),
- getDefaultOrder()));
+ return getFirstInCollection(findFiltered(filter));
}
/**
* {@inheritDoc}
*/
+ public T findFilteredFirst(ExampleTree exampleTree)
+ {
+ return getFirstInCollection(findFiltered(exampleTree));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public T findFilteredFirst(T filter, final Order... orders)
{
- return getFirstInCollection(getThis().findFiltered(
- filter,
- getDefaultFilterMetadata(),
- 1,
- 0,
- Collections.<Criterion> emptyList(),
- orders));
+ return getFirstInCollection(findFiltered(filter, orders));
}
/**
* {@inheritDoc}
*/
+ public T findFilteredFirst(ExampleTree exampleTree, final Order... orders)
+ {
+ return getFirstInCollection(findFiltered(exampleTree, orders));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public T findFilteredFirst(T filter, List< ? extends Criterion> criteria)
{
- return getFirstInCollection(getThis().findFiltered(
- filter,
- getDefaultFilterMetadata(),
- 1,
- 0,
- criteria,
- getDefaultOrder()));
+ ExampleTree exampleTree = new ExampleTree(filter);
+ appendToRoot(exampleTree, criteria);
+ return getFirstInCollection(getThis().findFiltered(exampleTree, Integer.MAX_VALUE, 0, getDefaultOrder()));
}
/**
@@ -364,7 +374,6 @@
*/
public boolean delete(final K key)
{
-
return getHibernateTemplate().execute(new HibernateCallback<Boolean>()
{
@@ -374,7 +383,6 @@
return true;
}
});
-
}
/**
@@ -396,15 +404,15 @@
/**
* {@inheritDoc}
*/
- @SuppressWarnings("unchecked")
public T merge(final T obj)
{
- return (T) getHibernateTemplate().execute(new HibernateCallback()
+ return getHibernateTemplate().execute(new HibernateCallback<T>()
{
- public Object doInHibernate(final Session ses) throws HibernateException
+ @SuppressWarnings("unchecked")
+ public T doInHibernate(final Session ses) throws HibernateException
{
- return ses.merge(obj);
+ return (T) ses.merge(obj);
}
});
@@ -422,6 +430,15 @@
/**
* {@inheritDoc}
*/
+ public List<T> findFiltered(ExampleTree exampleTree, int maxResults, int page, Order... orders)
+ {
+ return getHibernateTemplate().execute(new ExampleTreeCallback(exampleTree, orders, maxResults, page));
+ }
+
+ /**
+ * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
+ */
+ @Deprecated
public List<T> findFiltered(T filter, Order[] customOrder, Map<String, ? extends FilterMetadata> metadata,
int maxResults, int page)
{
@@ -435,8 +452,9 @@
}
/**
- * {@inheritDoc}
+ * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
*/
+ @Deprecated
public List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page,
Order... orders)
{
@@ -444,23 +462,38 @@
}
/**
- * {@inheritDoc}
+ * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
*/
+ @Deprecated
public List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page,
List< ? extends Criterion> criteria, Order... orders)
{
- if (metadata == null || metadata.isEmpty())
+ HibernateCallback<List<T>> callback;
+ if (MapUtils.isNotEmpty(metadata))
{
- return getHibernateTemplate().execute(new ExampleTreeCallback(orders, criteria, maxResults, page, filter));
+ // backwards compatibility
+ callback = new HibernateCallbackForExecution(
+ filter,
+ page,
+ maxResults,
+ metadata,
+ orders,
+ criteria,
+ Collections.<String> emptyList());
}
- return getHibernateTemplate().execute(
- new HibernateCallbackForExecution(filter, page, maxResults, metadata, orders, criteria, Collections
- .<String> emptyList()));
+ else
+ {
+ ExampleTree exampleTree = new ExampleTree(filter);
+ appendToRoot(exampleTree, criteria);
+ callback = new ExampleTreeCallback(exampleTree, orders, maxResults, page);
+ }
+ return getHibernateTemplate().execute(callback);
}
/**
- * {@inheritDoc}
+ * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
*/
+ @Deprecated
public List< ? > findFilteredProperties(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults,
int page, List< ? extends Criterion> criteria, List<String> properties, Order... orders)
{
@@ -469,7 +502,7 @@
}
/**
- * {@inheritDoc}
+ * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
*/
@Deprecated
public List<T> findFiltered(T filter, Order[] orders, Map<String, ? extends FilterMetadata> metadata,
@@ -479,7 +512,7 @@
}
/**
- * {@inheritDoc}
+ * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
*/
@Deprecated
public List< ? > findFilteredProperties(T filter, Order[] orders, Map<String, ? extends FilterMetadata> metadata,
@@ -619,7 +652,7 @@
* @param key the key name
* @param value the object to set as the parameter
*/
- protected void setParameterValue(Query query, String key, Object value)
+ protected static void setParameterValue(Query query, String key, Object value)
{
if (null == key || null == value)
{
@@ -655,6 +688,22 @@
}
/**
+ * appends the input criterions to the input example tree as root entity criterions
+ * @param exampleTree the example tree
+ * @param criterions the criterions to append
+ */
+ protected static void appendToRoot(ExampleTree exampleTree, List< ? extends Criterion> criterions)
+ {
+ if (criterions != null)
+ {
+ for (Criterion crit : criterions)
+ {
+ exampleTree.add(StringUtils.EMPTY, crit);
+ }
+ }
+ }
+
+ /**
* @return This is needed as for http://opensource.atlassian.com/projects/spring/browse/SPR-2226
*/
@SuppressWarnings("unchecked")
@@ -687,41 +736,34 @@
private final Order[] orders;
- private final List< ? extends Criterion> criteria;
-
private final int page;
private final int maxResults;
- private final T filter;
+ private final ExampleTree exampleTree;
- private ExampleTreeCallback(
- Order[] orders,
- List< ? extends Criterion> criteria,
- int maxResults,
- int page,
- T filter)
+ private ExampleTreeCallback(ExampleTree exampleTree, Order[] orders, int maxResults, int page)
{
+ this.exampleTree = exampleTree;
this.orders = orders;
- this.criteria = criteria;
this.page = page;
this.maxResults = maxResults;
- this.filter = filter;
}
@SuppressWarnings("unchecked")
public List<T> doInHibernate(Session session) throws HibernateException, SQLException
{
- Criteria crit = new ExampleTree(filter).create(session);
+ Criteria crit = exampleTree.create(session);
+
+ // backwards compatibility
+ Map<String, ? extends FilterMetadata> filterMetadata = getDefaultFilterMetadata();
+ if (MapUtils.isNotEmpty(filterMetadata))
+ {
+ new FilterMetadataSupport(exampleTree.getRootEntity(), filterMetadata).appendTo(crit, session);
+ }
+
crit.setMaxResults(maxResults);
crit.setFirstResult(maxResults * page);
- if (criteria != null)
- {
- for (Criterion c : criteria)
- {
- crit.add(c);
- }
- }
if (orders != null)
{
for (Order o : orders)
@@ -734,9 +776,11 @@
}
/**
+ * @deprecated callback implementation that uses EnhancedExample, deprecated in favor of ExampleTreeCallback
* @author carone
* @version $Id$
*/
+ @Deprecated
private final class HibernateCallbackForExecution implements HibernateCallback<List<T>>
{
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2011-11-30 12:05:20 UTC (rev 3711)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2011-11-30 15:11:36 UTC (rev 3712)
@@ -48,9 +48,12 @@
/**
+ * @deprecated this example implementation does not take into account hibernate metadata, see BSHD-5. Use ExampleTree
+ * instead
* @author Fabrizio Giustina
* @version $Id: $
*/
+@Deprecated
public final class EnhancedExample
{
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2011-11-30 12:05:20 UTC (rev 3711)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2011-11-30 15:11:36 UTC (rev 3712)
@@ -45,8 +45,8 @@
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
+import org.hibernate.criterion.Example.PropertySelector;
import org.hibernate.criterion.MatchMode;
-import org.hibernate.criterion.Example.PropertySelector;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.type.Type;
@@ -244,6 +244,14 @@
return this;
}
+ /**
+ * @return the rootEntity
+ */
+ public Object getRootEntity()
+ {
+ return rootEntity;
+ }
+
private class ExampleTreeWalker implements Serializable
{
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2011-11-30 12:05:20 UTC (rev 3711)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2011-11-30 15:11:36 UTC (rev 3712)
@@ -32,6 +32,7 @@
import java.util.Map;
import java.util.Set;
+import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.EntityMode;
@@ -106,7 +107,10 @@
public Criteria walk(Criteria rootCriteria, Object rootEntity)
{
- createSubExamples(rootCriteria, rootEntity, new String[0]);
+ if (MapUtils.isNotEmpty(filterMetadata))
+ {
+ createSubExamples(rootCriteria, rootEntity, new String[0]);
+ }
return rootCriteria;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|