From: <gca...@us...> - 2009-09-06 07:54:09
|
Revision: 1432 http://openutils.svn.sourceforge.net/openutils/?rev=1432&view=rev Author: gcatania Date: 2009-09-06 07:54:02 +0000 (Sun, 06 Sep 2009) Log Message: ----------- BSHD-1 - improve generics usage 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 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 2009-09-05 19:16:40 UTC (rev 1431) +++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java 2009-09-06 07:54:02 UTC (rev 1432) @@ -17,7 +17,7 @@ * @param <T> Persistence class * @param <K> Object Key */ -public interface HibernateDAO<T extends Object, K extends Serializable> +public interface HibernateDAO<T, K extends Serializable> { /** @@ -46,7 +46,7 @@ * @param criteria Additional Criterion conditions * @return a list of all instances */ - List<T> findAll(final Order[] orderProperties, List<Criterion> criteria); + List<T> findAll(final Order[] orderProperties, List< ? extends Criterion> criteria); /** * Execute a query. @@ -115,7 +115,8 @@ * @param page result page (first result is maxResults * page) * @return list of objects */ - List<T> findFiltered(final T filter, Map<String, FilterMetadata> metadata, final int maxResults, final int page); + List<T> findFiltered(final T filter, Map<String, ? extends FilterMetadata> metadata, final int maxResults, + final int page); /** * Return all objects related to the implementation of this DAO filtered using properties of the provided instance. @@ -126,8 +127,8 @@ * @param page result page (first result is maxResults * page) * @return list of objects */ - List<T> findFiltered(final T filter, final Order[] customOrder, final Map<String, FilterMetadata> metadata, - final int maxResults, final int page); + List<T> findFiltered(final T filter, final Order[] customOrder, + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page); /** * Return all objects related to the implementation of this DAO filtered using properties of the provided instance. @@ -139,8 +140,9 @@ * @param additionalCriteria additional criteria * @return list of objects */ - List<T> findFiltered(final T filter, final Order[] customOrder, final Map<String, FilterMetadata> metadata, - final int maxResults, final int page, List<Criterion> additionalCriteria); + List<T> findFiltered(final T filter, final Order[] customOrder, + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page, + List< ? extends Criterion> additionalCriteria); /** * Return properties from all objects related to the implementation of this DAO filtered using properties of the @@ -155,8 +157,8 @@ * @return list of properties from all objects */ List< ? > findFilteredProperties(final T filter, final Order[] customOrder, - final Map<String, FilterMetadata> metadata, final int maxResults, final int page, - List<Criterion> additionalCriteria, List<String> properties); + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page, + List< ? extends Criterion> additionalCriteria, List<String> properties); /** * Return all objects related to the implementation of this DAO filtered using properties of the provided instance. @@ -179,7 +181,7 @@ * @param metadata filter metadata * @return list of objects */ - List<T> findFiltered(final T filter, Map<String, FilterMetadata> metadata); + List<T> findFiltered(final T filter, Map<String, ? extends FilterMetadata> metadata); /** * Return the first object related to the implementation of this DAO filtered using properties of the provided @@ -204,7 +206,7 @@ * @param criteria additional criterion * @return first object in the collection */ - T findFilteredFirst(final T filter, final List<Criterion> criteria); + T findFilteredFirst(final T filter, final List< ? extends Criterion> criteria); /** * Used by the base DAO classes but here for your modification. Remove a persistent instance from the datastore. The 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 2009-09-05 19:16:40 UTC (rev 1431) +++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2009-09-06 07:54:02 UTC (rev 1432) @@ -4,9 +4,8 @@ import it.openutils.hibernate.example.FilterMetadata; import java.io.Serializable; -import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -36,7 +35,7 @@ * @param <T> Persistence class * @param <K> Object Key */ -public abstract class HibernateDAOImpl<T extends Object, K extends Serializable> extends HibernateDaoSupport +public abstract class HibernateDAOImpl<T, K extends Serializable> extends HibernateDaoSupport implements HibernateDAO<T, K> { @@ -58,7 +57,7 @@ * @param referenceClass the class for the bean managed by this DAO */ @SuppressWarnings({"unchecked" }) - public HibernateDAOImpl(Class referenceClass) + public HibernateDAOImpl(final Class referenceClass) { super(); this.referenceClass = referenceClass; @@ -68,7 +67,7 @@ * {@inheritDoc} */ @SuppressWarnings("unchecked") - public List<T> find(String query) + public List<T> find(final String query) { return getHibernateTemplate().find(query); } @@ -86,19 +85,19 @@ */ public List<T> findAll(final Order[] orderProperties) { - return getThis().findAll(orderProperties, new ArrayList<Criterion>()); + return getThis().findAll(orderProperties, Collections.<Criterion> emptyList()); } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") - public List<T> findAll(final Order[] orderProperties, final List<Criterion> criteria) + public List<T> findAll(final Order[] orderProperties, final List< ? extends Criterion> criteria) { return (List<T>) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { Criteria crit = ses.createCriteria(getReferenceClass()); if (null != orderProperties) @@ -124,7 +123,7 @@ /** * {@inheritDoc} */ - public List<T> find(String query, Object obj, Type type) + public List<T> find(final String query, final Object obj, final Type type) { return find(query, new Object[]{obj }, new Type[]{type }); } @@ -138,7 +137,7 @@ return (List<T>) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { // hibernate 3 return ses.createQuery(query).setParameters(obj, type).list(); @@ -150,7 +149,7 @@ * {@inheritDoc} */ @SuppressWarnings("unchecked") - public T load(K key) + public T load(final K key) { T result = (T) getHibernateTemplate().load(getReferenceClass(), key); Hibernate.initialize(result); @@ -161,7 +160,7 @@ * {@inheritDoc} */ @SuppressWarnings("unchecked") - public T loadIfAvailable(K key) + public T loadIfAvailable(final K key) { T result; try @@ -184,7 +183,7 @@ * {@inheritDoc} */ @SuppressWarnings("unchecked") - public T get(K key) + public T get(final K key) { return (T) getHibernateTemplate().get(getReferenceClass(), key); } @@ -200,7 +199,7 @@ /** * {@inheritDoc} */ - public void update(T obj) + public void update(final T obj) { getHibernateTemplate().update(obj); } @@ -214,7 +213,7 @@ return (Boolean) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { ses.delete(ses.load(getReferenceClass(), key)); return true; @@ -226,7 +225,7 @@ /** * {@inheritDoc} */ - public void refresh(T obj) + public void refresh(final T obj) { getHibernateTemplate().refresh(obj); } @@ -234,7 +233,7 @@ /** * {@inheritDoc} */ - public void evict(T obj) + public void evict(final T obj) { getHibernateTemplate().evict(obj); } @@ -248,7 +247,7 @@ return (T) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { return ses.merge(obj); } @@ -260,7 +259,7 @@ * {@inheritDoc} */ @SuppressWarnings("unchecked") - public K save(T obj) + public K save(final T obj) { return (K) getHibernateTemplate().save(obj); } @@ -281,11 +280,10 @@ return getFirstInCollection(findFiltered(filter, order, getDefaultFilterMetadata(), 1, 0)); } - /** * {@inheritDoc} */ - public T findFilteredFirst(final T filter, List<Criterion> criteria) + public T findFilteredFirst(final T filter, final List< ? extends Criterion> criteria) { return getFirstInCollection(findFiltered(filter, null, getDefaultFilterMetadata(), 1, 0, criteria)); } @@ -301,7 +299,7 @@ /** * {@inheritDoc} */ - public List<T> findFiltered(T filter, Order[] orderProperties) + public List<T> findFiltered(final T filter, final Order[] orderProperties) { return findFiltered(filter, orderProperties, getDefaultFilterMetadata(), Integer.MAX_VALUE, 0); } @@ -309,7 +307,7 @@ /** * {@inheritDoc} */ - public List<T> findFiltered(final T filter, final Map<String, FilterMetadata> metadata) + public List<T> findFiltered(final T filter, final Map<String, ? extends FilterMetadata> metadata) { return findFiltered(filter, metadata, Integer.MAX_VALUE, 0); } @@ -325,8 +323,8 @@ /** * {@inheritDoc} */ - public List<T> findFiltered(final T filter, final Map<String, FilterMetadata> metadata, final int maxResults, - final int page) + public List<T> findFiltered(final T filter, final Map<String, ? extends FilterMetadata> metadata, + final int maxResults, final int page) { return findFiltered(filter, null, metadata, maxResults, page); } @@ -334,18 +332,25 @@ /** * {@inheritDoc} */ - public List<T> findFiltered(final T filter, final Order[] customOrder, final Map<String, FilterMetadata> metadata, - final int maxResults, final int page) + public List<T> findFiltered(final T filter, final Order[] customOrder, + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page) { - return getThis().findFiltered(filter, customOrder, metadata, maxResults, page, new ArrayList<Criterion>()); + return getThis().findFiltered( + filter, + customOrder, + metadata, + maxResults, + page, + Collections.<Criterion> emptyList()); } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") - public List<T> findFiltered(T filter, Order[] customOrder, Map<String, FilterMetadata> metadata, int maxResults, - int page, List<Criterion> additionalCriteria) + public List<T> findFiltered(final T filter, final Order[] customOrder, + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page, + final List< ? extends Criterion> additionalCriteria) { final Order[] orderProperties = customOrder != null && customOrder.length > 0 ? customOrder : this .getDefaultOrder(); @@ -365,8 +370,8 @@ * {@inheritDoc} */ public List< ? > findFilteredProperties(final T filter, final Order[] customOrder, - final Map<String, FilterMetadata> metadata, final int maxResults, final int page, - List<Criterion> additionalCriteria, List<String> properties) + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page, + final List< ? extends Criterion> additionalCriteria, final List<String> properties) { final Order[] orderProperties = customOrder != null && customOrder.length > 0 ? customOrder : this .getDefaultOrder(); @@ -391,7 +396,7 @@ return referenceClass; } - public void setReferenceClass(Class<T> referenceClass) + public void setReferenceClass(final Class<T> referenceClass) { this.referenceClass = referenceClass; } @@ -417,7 +422,7 @@ return (List< ? >) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { Query q = ses.getNamedQuery(name); if (maxResults != null) @@ -443,12 +448,12 @@ * @param maxResults max number of results * @return Query */ - protected List< ? > findByNamedQuery(final String name, final Map<String, Object> params, final Integer maxResults) + protected List< ? > findByNamedQuery(final String name, final Map<String, ? > params, final Integer maxResults) { return (List< ? >) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { Query q = ses.getNamedQuery(name); if (maxResults != null) @@ -458,7 +463,7 @@ if (params != null) { - for (Map.Entry<String, Object> entry : params.entrySet()) + for (Map.Entry<String, ? > entry : params.entrySet()) { setParameterValue(q, entry.getKey(), entry.getValue()); } @@ -477,7 +482,7 @@ * @deprecated use the better named <code>findByNamedQuery</code> method */ @Deprecated - protected List< ? > getNamedQuery(final String name, final Serializable[] params, int maxResults) + protected List< ? > getNamedQuery(final String name, final Serializable[] params, final int maxResults) { return findByNamedQuery(name, params, maxResults > 0 ? maxResults : Integer.MAX_VALUE); } @@ -491,7 +496,7 @@ * @deprecated use the better named <code>findByNamedQuery</code> method */ @Deprecated - protected List< ? > getNamedQuery(final String name, final Map<String, Object> params, int maxResults) + protected List< ? > getNamedQuery(final String name, final Map<String, ? > params, final int maxResults) { return findByNamedQuery(name, params, maxResults > 0 ? maxResults : Integer.MAX_VALUE); } @@ -503,7 +508,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 void setParameterValue(final Query query, final String key, final Object value) { if (null == key || null == value) { @@ -517,9 +522,9 @@ * set any default filter, subclasses may override this. * @return map of property name - filter metadata */ - protected Map<String, FilterMetadata> getDefaultFilterMetadata() + protected Map<String, ? extends FilterMetadata> getDefaultFilterMetadata() { - return new HashMap<String, FilterMetadata>(0); + return Collections.emptyMap(); } /** @@ -527,14 +532,13 @@ * @param list collection * @return first element in the list */ - @SuppressWarnings("unchecked") - private T getFirstInCollection(Collection<T> list) + private T getFirstInCollection(final Collection< ? extends T> list) { if (list != null && !list.isEmpty()) { - Object result = list.iterator().next(); + T result = list.iterator().next(); Hibernate.initialize(result); - return (T) result; + return result; } return null; } @@ -543,7 +547,7 @@ * Sets the aopenabled. * @param aopenabled the aopenabled to set */ - public void setAopenabled(boolean aopenabled) + public void setAopenabled(final boolean aopenabled) { this.aopenabled = aopenabled; } @@ -601,7 +605,7 @@ /** * */ - private final Map<String, FilterMetadata> metadata; + private final Map<String, ? extends FilterMetadata> metadata; /** * @@ -613,7 +617,7 @@ */ private final Order[] orderProperties; - private List<Criterion> additionalCriteria; + private List< ? extends Criterion> additionalCriteria; /** * @param filter @@ -623,13 +627,13 @@ * @param orderProperties */ private HibernateCallbackForExecution( - T filter, - int page, - int maxResults, - Map<String, FilterMetadata> metadata, - Order[] orderProperties, - List<Criterion> additionalCriteria, - List<String> properties) + final T filter, + final int page, + final int maxResults, + final Map<String, ? extends FilterMetadata> metadata, + final Order[] orderProperties, + final List< ? extends Criterion> additionalCriteria, + final List<String> properties) { this.filter = filter; this.page = page; @@ -640,7 +644,7 @@ this.properties = properties; } - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { Criteria crit = ses.createCriteria(filter.getClass()); crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); 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 2009-09-05 19:16:40 UTC (rev 1431) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2009-09-06 07:54:02 UTC (rev 1432) @@ -5,8 +5,8 @@ import java.sql.Timestamp; import java.util.Calendar; import java.util.Collection; +import java.util.Collections; import java.util.Date; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -34,11 +34,14 @@ */ private static Logger log = LoggerFactory.getLogger(EnhancedExample.class); - private Map<String, FilterMetadata> metadata; + private Map<String, ? extends FilterMetadata> metadata; - private EnhancedExample(Criteria crit, Object filter, Map<String, FilterMetadata> metadata) + private EnhancedExample( + final Criteria crit, + final Object filter, + final Map<String, ? extends FilterMetadata> metadata) { - this.metadata = metadata == null ? new HashMap<String, FilterMetadata>(0) : metadata; + this.metadata = metadata == null ? Collections.<String, FilterMetadata> emptyMap() : metadata; fillCriteria(null, crit, filter); } @@ -49,8 +52,8 @@ * @param metadata Map of property names - filter metadata * @throws HibernateException exception while building the criteria */ - public static void create(Criteria crit, Object filter, Map<String, FilterMetadata> metadata) - throws HibernateException + public static void create(final Criteria crit, final Object filter, + final Map<String, ? extends FilterMetadata> metadata) throws HibernateException { new EnhancedExample(crit, filter, metadata); } @@ -62,8 +65,8 @@ * @param value property value * @throws HibernateException exception while building the criteria */ - private void addCondition(Criteria crit, String propertyName, Object value, Object parentObject) - throws HibernateException + private void addCondition(final Criteria crit, final String propertyName, final Object value, + final Object parentObject) throws HibernateException { String simplePropertyName = StringUtils.contains(propertyName, ".") ? StringUtils.substringAfterLast( @@ -95,9 +98,9 @@ { // @todo handle multiple associations in lists? // see http://opensource2.atlassian.com/projects/hibernate/browse/HHH-879 - if ((value instanceof Set || value instanceof List) && !((Collection< ? >) value).isEmpty()) + if ((value instanceof Set< ? > || value instanceof List< ? >) && !((Collection< ? >) value).isEmpty()) { - // collection: the new criteria has already been created, now we only nee to analize content + // collection: the new criteria has already been created, now we only need to analize content for (Object element : ((Collection< ? >) value)) { @@ -107,7 +110,7 @@ fillCriteria(propertyName, childrenCriteria, element); } } - else if ((value instanceof Map) && !((Map< ? , ? >) value).isEmpty()) + else if ((value instanceof Map< ? , ? >) && !((Map< ? , ? >) value).isEmpty()) { FilterMetadata fmd = metadata.get(propertyName); @@ -139,7 +142,7 @@ * @return <code>true</code> if the bean contains at least a valid property */ @SuppressWarnings("unchecked") - private boolean containsSomething(Object bean) + private boolean containsSomething(final Object bean) { if (bean == null) @@ -151,7 +154,7 @@ return true; } - if (bean instanceof Collection) + if (bean instanceof Collection< ? >) { Collection< ? > coll = ((Collection< ? >) bean); @@ -165,7 +168,7 @@ return true; } } - else if (bean instanceof Map) + else if (bean instanceof Map< ? , ? >) { Map< ? , ? > coll = ((Map< ? , ? >) bean); if (coll.isEmpty()) @@ -227,9 +230,10 @@ * @throws HibernateException exception while building the criteria */ @SuppressWarnings("unchecked") - private void fillCriteria(String parentPropertyName, Criteria crit, Object filter) throws HibernateException + private void fillCriteria(final String parentPropertyName, final Criteria crit, final Object filter) + throws HibernateException { - if ((filter instanceof Set || filter instanceof List) && !((Collection< ? >) filter).isEmpty()) + if ((filter instanceof Set< ? > || filter instanceof List< ? >) && !((Collection< ? >) filter).isEmpty()) { // collection: the new criteria has already been created, now we only need to analize content for (Object element : ((Collection< ? >) filter)) @@ -281,7 +285,7 @@ * @param object object to check * @return <code>true</code>if the given object is a simple type */ - private boolean isSimpleType(Object object) + private boolean isSimpleType(final Object object) { Class< ? extends Object> objClass = object.getClass(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gca...@us...> - 2010-06-07 08:33:04
|
Revision: 2624 http://openutils.svn.sourceforge.net/openutils/?rev=2624&view=rev Author: gcatania Date: 2010-06-07 08:32:57 +0000 (Mon, 07 Jun 2010) Log Message: ----------- compile fixes, adding license BSHD-5 Modified Paths: -------------- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 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 2010-06-07 07:11:35 UTC (rev 2623) +++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2010-06-07 08:32:57 UTC (rev 2624) @@ -36,7 +36,6 @@ import java.util.Map; import org.aopalliance.aop.AspectException; -import org.apache.commons.collections.CollectionUtils; import org.hibernate.Criteria; import org.hibernate.Hibernate; import org.hibernate.HibernateException; @@ -53,6 +52,7 @@ import org.springframework.aop.framework.AopContext; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; +import org.springframework.util.CollectionUtils; /** @@ -732,14 +732,14 @@ } } // EnhancedExample.create(crit, filter, metadata); - if (CollectionUtils.isNotEmpty(additionalCriteria)) + if (!CollectionUtils.isEmpty(additionalCriteria)) { for (Criterion criterion : additionalCriteria) { crit.add(criterion); } } - if (CollectionUtils.isNotEmpty(properties)) + if (!CollectionUtils.isEmpty(properties)) { ProjectionList projectionList = Projections.projectionList(); 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 2010-06-07 07:11:35 UTC (rev 2623) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2010-06-07 08:32:57 UTC (rev 2624) @@ -1,3 +1,28 @@ +/** + * + * openutils base Spring-Hibernate DAO for java 5.0 (http://www.openmindlab.com/lab/products/bshd5.html) + * + * Copyright(C) null-2010, Openmind S.r.l. http://www.openmindonline.it + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-2.1.html + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + package it.openutils.hibernate.example; import org.hibernate.Criteria; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |