From: <gca...@us...> - 2012-06-29 16:11:56
|
Revision: 4079 http://openutils.svn.sourceforge.net/openutils/?rev=4079&view=rev Author: gcatania Date: 2012-06-29 16:11:44 +0000 (Fri, 29 Jun 2012) Log Message: ----------- Merged revisions 4075-4078 via svnmerge from https://openutils.svn.sourceforge.net/svnroot/openutils/trunk/openutils-bshd5 ........ r4075 | gcatania | 2012-06-29 16:26:07 +0200 (Fri, 29 Jun 2012) | 1 line cleanup unit tests ........ r4076 | gcatania | 2012-06-29 17:10:22 +0200 (Fri, 29 Jun 2012) | 1 line BSHD-15 source code refactor and cleanup ........ r4077 | gcatania | 2012-06-29 18:03:05 +0200 (Fri, 29 Jun 2012) | 1 line BSHD-2 adding tests for filter metadata, refactoring ........ r4078 | gcatania | 2012-06-29 18:05:56 +0200 (Fri, 29 Jun 2012) | 1 line licenses ........ Revision Links: -------------- http://openutils.svn.sourceforge.net/openutils/?rev=4075&view=rev http://openutils.svn.sourceforge.net/openutils/?rev=4076&view=rev http://openutils.svn.sourceforge.net/openutils/?rev=4077&view=rev http://openutils.svn.sourceforge.net/openutils/?rev=4078&view=rev Modified Paths: -------------- branches/openutils-bshd5-backport/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTree.java branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOPersistenceTest.java Added Paths: ----------- branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/selectors/ branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/EntityBuilder.java branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOFilterMetadataTest.java Removed Paths: ------------- branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java Property Changed: ---------------- branches/openutils-bshd5-backport/ branches/openutils-bshd5-backport/pom.xml branches/openutils-bshd5-backport/src/ Property changes on: branches/openutils-bshd5-backport ___________________________________________________________________ Modified: svnmerge-integrated - /trunk/openutils-bshd5:1-4040,4042-4056,4058-4073 + /trunk/openutils-bshd5:1-4040,4042-4056,4058-4078 Modified: svn:mergeinfo - /trunk/openutils-bshd5:4045-4047*,4057,4059,4069-4070*,4073* + /trunk/openutils-bshd5:4045-4047*,4057,4059,4069-4070*,4073*,4075-4078* Property changes on: branches/openutils-bshd5-backport/pom.xml ___________________________________________________________________ Modified: svn:mergeinfo - /trunk/openutils-bshd5/pom.xml:4045-4047,4059,4069-4070,4073 + /trunk/openutils-bshd5/pom.xml:4045-4047,4059,4069-4070,4073,4075-4078 Property changes on: branches/openutils-bshd5-backport/src ___________________________________________________________________ Modified: svn:mergeinfo - /trunk/openutils-bshd5/src:4045-4047,4057,4069-4070,4073 + /trunk/openutils-bshd5/src:4045-4047,4057,4069-4070,4073,4075-4078 Modified: branches/openutils-bshd5-backport/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java =================================================================== --- branches/openutils-bshd5-backport/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2012-06-29 16:05:56 UTC (rev 4078) +++ branches/openutils-bshd5-backport/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2012-06-29 16:11:44 UTC (rev 4079) @@ -38,7 +38,6 @@ import org.aopalliance.aop.AspectException; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.hibernate.Criteria; import org.hibernate.Hibernate; @@ -167,7 +166,7 @@ */ public List<T> find(String query, Object paramValue, Type paramType) { - return getThis().find(query, new Object[]{paramValue }, new Type[]{paramType }); + return getThis().find(query, new Object[]{paramValue}, new Type[]{paramType}); } /** @@ -724,7 +723,7 @@ * @author gcatania */ @SuppressWarnings("deprecation") - private class LegacySupportCallback<R> extends ExampleTreeCallback<R> + private class LegacySupportCallback<R> extends BaseCallback<R> { private final T rootEntity; @@ -741,7 +740,7 @@ List< ? extends Criterion> additionalCriteria, Order... orders) { - super(new ExampleTree(rootEntity), maxResults, page, orders); + super(maxResults, page, orders); this.rootEntity = rootEntity; this.filterMetadata = metadata; this.additionalCriteria = additionalCriteria; @@ -750,11 +749,7 @@ @Override protected Criteria createCriteria(Session session) { - Criteria crit = super.createCriteria(session); - if (MapUtils.isNotEmpty(filterMetadata)) - { - new FilterMetadataSupport(rootEntity, filterMetadata).appendTo(crit, session); - } + Criteria crit = new FilterMetadataSupport(rootEntity, filterMetadata).create(session); if (additionalCriteria != null) { for (Criterion c : additionalCriteria) @@ -794,8 +789,12 @@ protected Criteria createCriteria(Session session) { Criteria crit = super.createCriteria(session); - if (!CollectionUtils.isEmpty(properties)) + if (CollectionUtils.isEmpty(properties)) { + crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); + } + else + { ProjectionList projectionList = Projections.projectionList(); for (String property : properties) { @@ -814,7 +813,7 @@ * @author gcatania * @param R the result class */ -class ExampleTreeCallback<R> implements HibernateCallback +abstract class BaseCallback<R> implements HibernateCallback { private final Order[] orders; @@ -823,26 +822,23 @@ private final int page; - private final ExampleTree exampleTree; - - protected ExampleTreeCallback(ExampleTree exampleTree, int maxResults, int page, Order... orders) + protected BaseCallback(int maxResults, int page, Order... orders) { - this.exampleTree = exampleTree; this.maxResults = maxResults; this.page = page; this.orders = orders; } /** - * internal method that creates the query criteria. Subclasses may override but should call {@code super()} + * internal method that creates the query criteria. * @param session the hibernate session * @return the hibernate criteria */ - protected Criteria createCriteria(Session session) + protected abstract Criteria createCriteria(Session session); + + public final List<R> doInHibernate(Session session) throws HibernateException, SQLException { - Criteria crit = exampleTree.create(session); - - crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); + Criteria crit = createCriteria(session); crit.setMaxResults(maxResults); crit.setFirstResult(maxResults * page); if (orders != null) @@ -852,13 +848,35 @@ crit.addOrder(o); } } + return crit.list(); + } +} - return crit; + +/** + * @author gcatania + * @param R the result class + */ +class ExampleTreeCallback<R> extends BaseCallback<R> +{ + + private final ExampleTree exampleTree; + + protected ExampleTreeCallback(ExampleTree exampleTree, int maxResults, int page, Order... orders) + { + super(maxResults, page, orders); + this.exampleTree = exampleTree; } - public List<R> doInHibernate(Session session) throws HibernateException, SQLException + /** + * internal method that creates the query criteria. Subclasses may override. + * @param session the hibernate session + * @return the hibernate criteria + */ + @Override + protected Criteria createCriteria(Session session) { - return createCriteria(session).list(); + return exampleTree.create(session); } } @@ -868,9 +886,12 @@ private final List<String> properties; - protected ExampleTreePropertiesCallback(ExampleTree exampleTree, int maxResults, int page, - - List<String> properties, Order... orders) + protected ExampleTreePropertiesCallback( + ExampleTree exampleTree, + int maxResults, + int page, + List<String> properties, + Order... orders) { super(exampleTree, maxResults, page, orders); this.properties = properties; @@ -880,8 +901,12 @@ protected Criteria createCriteria(Session session) { Criteria crit = super.createCriteria(session); - if (!CollectionUtils.isEmpty(properties)) + if (CollectionUtils.isEmpty(properties)) { + crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); + } + else + { ProjectionList projectionList = Projections.projectionList(); for (String property : properties) { Modified: branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTree.java =================================================================== --- branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2012-06-29 16:05:56 UTC (rev 4078) +++ branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2012-06-29 16:11:44 UTC (rev 4079) @@ -25,11 +25,10 @@ package it.openutils.hibernate.example; +import it.openutils.hibernate.selectors.ExcludeBackrefPropertySelector; + import java.io.Serializable; -import java.lang.reflect.Array; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -37,7 +36,6 @@ import java.util.Map; import java.util.Set; -import org.apache.commons.lang3.StringUtils; import org.hibernate.Criteria; import org.hibernate.EntityMode; import org.hibernate.Hibernate; @@ -50,7 +48,6 @@ import org.hibernate.criterion.Restrictions; import org.hibernate.engine.SessionImplementor; import org.hibernate.metadata.ClassMetadata; -import org.hibernate.property.BackrefPropertyAccessor; import org.hibernate.type.Type; @@ -67,7 +64,7 @@ private Character escapeCharacter; - private PropertySelector selector = new ExcludeBackrefPropertySelector(); // BSHD-15 + private PropertySelector selector = new ExcludeBackrefPropertySelector(ExampleTreePropertySelectorSupport.NOT_NULL); // BSHD-15 private MatchMode matchMode; @@ -260,7 +257,7 @@ private void createSubExamples(Criteria crit, Object entity, String[] walkedProperties) { - String associationPath = getAssociationPath(walkedProperties); + String associationPath = ExampleTreeUtils.getPath(walkedProperties); crit.add(example(entity, associationPath)); for (Criterion c : getAdditionalConditions(associationPath)) { @@ -281,7 +278,7 @@ continue; } String propertyName = names[i]; - if (alreadyWalked(walkedProperties, propertyName)) + if (ExampleTreeUtils.alreadyWalked(walkedProperties, propertyName)) { continue; } @@ -289,7 +286,7 @@ Object propertyValue = classMetadata.getPropertyValue(entity, propertyName, entityMode); if (propertyType.isCollectionType()) { - propertyValue = getValueFromCollection(propertyValue); + propertyValue = ExampleTreeUtils.getValueFromCollection(propertyValue); } if (propertyValue == null) { @@ -298,7 +295,7 @@ } Criteria subCrit = crit.createCriteria(propertyName); - String[] subProperties = append(walkedProperties, propertyName); + String[] subProperties = ExampleTreeUtils.append(walkedProperties, propertyName); createSubExamples(subCrit, propertyValue, subProperties); } } @@ -323,11 +320,6 @@ } } - private String getAssociationPath(String[] walkedProperties) - { - return walkedProperties.length > 0 ? StringUtils.join(walkedProperties, '.') : StringUtils.EMPTY; - } - private Example example(Object entity, String associationPath) { Example ex = Example.create(entity); @@ -365,128 +357,13 @@ return result; } - /** - * check the property with the input name was already walked in the input path - * @param path the current path - * @param propertyName the property name about to be walked - * @return true if the property with the input name was already walked in the input path - */ - private boolean alreadyWalked(String[] walkedProperties, String propertyName) - { - if (walkedProperties.length <= 2) - { - return false; - } - String parent = walkedProperties[walkedProperties.length - 1]; - boolean lastWasChild = false; - for (int i = walkedProperties.length - 2; i > 0; i--) - { - String currPropertyName = walkedProperties[i]; - if (currPropertyName.equals(propertyName)) - { - lastWasChild = true; - continue; - } - if (lastWasChild) - { - if (currPropertyName.equals(parent)) - { - return true; - } - else - { - lastWasChild = false; - } - } - } - return false; - } - - // see http://opensource2.atlassian.com/projects/hibernate/browse/HHH-879 - private Object getValueFromCollection(Object collectionValue) - { - if (collectionValue != null) - { - if (collectionValue instanceof Collection< ? >) - { - Collection< ? > coll = (Collection< ? >) collectionValue; - int size = coll.size(); - if (size == 1) - { - return coll.iterator().next(); - } - if (size > 1) - { - throw new IllegalArgumentException("More than one element in filter collection is unsupported."); - } - } - Class< ? extends Object> clazz = collectionValue.getClass(); - if (clazz.isArray()) - { - int length = Array.getLength(collectionValue); - if (length == 1) - { - return Array.get(collectionValue, 0); - } - if (length > 1) - { - throw new IllegalArgumentException("More than one element in filter array is unsupported."); - } - } - // TODO other cases? - } - return null; - } - - private String[] append(String[] propertyNames, String propertyName) - { - String[] result = Arrays.copyOf(propertyNames, propertyNames.length + 1); - result[propertyNames.length] = propertyName; - return result; - } } - } /** - * support for BSHD-15 - * @author gcatania - * @version $Id$ - */ -class ExcludeBackrefPropertySelector implements PropertySelector -{ - - private static final long serialVersionUID = -2803322309158823550L; - - private final PropertySelector selector; - - public ExcludeBackrefPropertySelector(PropertySelector selector) - { - this.selector = selector; - } - - public ExcludeBackrefPropertySelector() - { - selector = ExampleTreePropertySelectorSupport.NOT_NULL; - } - - public boolean include(Object propertyValue, String propertyName, Type type) - { - if (BackrefPropertyAccessor.UNKNOWN.equals(propertyValue)) - { - return false; - } - return selector.include(propertyValue, propertyName, type); - } - -} - - -/** * workaround to {@link Example} not exposing internal property selectors * @author gcatania - * @version $Id$ */ @SuppressWarnings({"serial", "static-method"}) class ExampleTreePropertySelectorSupport Copied: branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java (from rev 4078, trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java) =================================================================== --- branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java (rev 0) +++ branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2012-06-29 16:11:44 UTC (rev 4079) @@ -0,0 +1,147 @@ +/** + * + * openutils base Spring-Hibernate DAO (http://www.openmindlab.com/lab/products/bshd5.html) + * + * Copyright(C) 2005-2012, 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 java.lang.reflect.Array; +import java.util.Arrays; +import java.util.Collection; + +import org.apache.commons.lang3.StringUtils; + + +/** + * @author gcatania + * @version $Id$ + */ +final class ExampleTreeUtils +{ + + private ExampleTreeUtils() + { + } + + /** + * check the property with the input name was already walked in the input path + * @param path the current path + * @param propertyName the property name about to be walked + * @return true if the property with the input name was already walked in the input path + */ + public static boolean alreadyWalked(String[] walkedProperties, String propertyName) + { + if (walkedProperties.length <= 2) + { + return false; + } + String parent = walkedProperties[walkedProperties.length - 1]; + boolean lastWasChild = false; + for (int i = walkedProperties.length - 2; i > 0; i--) + { + String currPropertyName = walkedProperties[i]; + if (currPropertyName.equals(propertyName)) + { + lastWasChild = true; + continue; + } + if (lastWasChild) + { + if (currPropertyName.equals(parent)) + { + return true; + } + else + { + lastWasChild = false; + } + } + } + return false; + } + + /** + * retrieves a value from a collection + * @param collectionValue the collection + * @return a value + * @see http://opensource2.atlassian.com/projects/hibernate/browse/HHH-879 + * @throws IllegalArgumentException if the input collection contains more than one value + */ + public static Object getValueFromCollection(Object collectionValue) throws IllegalArgumentException + { + if (collectionValue != null) + { + if (collectionValue instanceof Collection< ? >) + { + Collection< ? > coll = (Collection< ? >) collectionValue; + int size = coll.size(); + if (size == 1) + { + return coll.iterator().next(); + } + if (size > 1) + { + throw new IllegalArgumentException("More than one element in filter collection is unsupported."); + } + } + Class< ? extends Object> clazz = collectionValue.getClass(); + if (clazz.isArray()) + { + int length = Array.getLength(collectionValue); + if (length == 1) + { + return Array.get(collectionValue, 0); + } + if (length > 1) + { + throw new IllegalArgumentException("More than one element in filter array is unsupported."); + } + } + // TODO other cases? + } + return null; + } + + /** + * @param strings an array of strings + * @param s the string to append + * @return a new array containing the input string array plus the input string at the end + */ + public static String[] append(String[] strings, String s) + { + String[] result = Arrays.copyOf(strings, strings.length + 1); + result[strings.length] = s; + return result; + } + + /** + * constructs the association path from an array of property names + * @param propertyNames the walked properties + * @return the association path + */ + public static String getPath(String[] propertyNames) + { + return propertyNames.length > 0 ? StringUtils.join(propertyNames, '.') : StringUtils.EMPTY; + } + +} Modified: branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java =================================================================== --- branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2012-06-29 16:05:56 UTC (rev 4078) +++ branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2012-06-29 16:11:44 UTC (rev 4079) @@ -25,15 +25,13 @@ package it.openutils.hibernate.example; -import java.lang.reflect.Array; -import java.util.Arrays; -import java.util.Collection; +import it.openutils.hibernate.selectors.ExcludeBackrefPropertySelector; + import java.util.HashMap; import java.util.Map; import java.util.Set; import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang3.StringUtils; import org.hibernate.Criteria; import org.hibernate.EntityMode; import org.hibernate.Hibernate; @@ -117,7 +115,7 @@ private void createSubExamples(Criteria crit, Object entity, String[] walkedProperties) { - String path = getPath(walkedProperties); + String path = ExampleTreeUtils.getPath(walkedProperties); Map<String, FilterMetadata> currFilterMetadata = getFilterMetadata(path); crit.add(example(entity, currFilterMetadata.keySet())); ClassMetadata classMetadata = sessionFactory.getClassMetadata(Hibernate.getClass(entity)); @@ -126,7 +124,7 @@ for (int i = 0; i < types.length; i++) { String propertyName = names[i]; - if (alreadyWalked(walkedProperties, propertyName)) + if (ExampleTreeUtils.alreadyWalked(walkedProperties, propertyName)) { continue; } @@ -147,7 +145,7 @@ if (propertyType.isCollectionType()) { - propertyValue = getValueFromCollection(propertyValue); + propertyValue = ExampleTreeUtils.getValueFromCollection(propertyValue); } if (propertyValue == null) { @@ -156,7 +154,7 @@ } Criteria subCrit = crit.createCriteria(propertyName); - String[] subProperties = append(walkedProperties, propertyName); + String[] subProperties = ExampleTreeUtils.append(walkedProperties, propertyName); createSubExamples(subCrit, propertyValue, subProperties); } } @@ -174,10 +172,12 @@ { continue; } - String leftover = key.substring(path.length() + 1); - // skip subproperties + // need to take into account leading dot for subproperties + int trimLength = path.isEmpty() ? 0 : path.length() + 1; + String leftover = key.substring(trimLength); if (leftover.contains(".")) { + // skip subproperties continue; } result.put(leftover, filterMetadata.get(key)); @@ -185,15 +185,10 @@ return result; } - private String getPath(String[] walkedProperties) - { - return walkedProperties.length > 0 ? StringUtils.join(walkedProperties, '.') : StringUtils.EMPTY; - } - private Example example(Object entity, Set<String> propertiesToExclude) { Example ex = Example.create(entity); - ex.setPropertySelector(new ExcludeBackrefPropertySelector()); // BSHD-15 + ex.setPropertySelector(new ExcludeBackrefPropertySelector(ExampleTreePropertySelectorSupport.NOT_NULL)); // BSHD-15 for (String propertyName : propertiesToExclude) { // skip properties handled by filterMetadata @@ -202,84 +197,6 @@ return ex; } - /** - * check the property with the input name was already walked in the input path - * @param path the current path - * @param propertyName the property name about to be walked - * @return true if the property with the input name was already walked in the input path - */ - private boolean alreadyWalked(String[] walkedProperties, String propertyName) - { - if (walkedProperties.length <= 2) - { - return false; - } - String parent = walkedProperties[walkedProperties.length - 1]; - boolean lastWasChild = false; - for (int i = walkedProperties.length - 2; i > 0; i--) - { - String currPropertyName = walkedProperties[i]; - if (currPropertyName.equals(propertyName)) - { - lastWasChild = true; - continue; - } - if (lastWasChild) - { - if (currPropertyName.equals(parent)) - { - return true; - } - else - { - lastWasChild = false; - } - } - } - return false; - } - - private Object getValueFromCollection(Object collectionValue) - { - if (collectionValue != null) - { - if (collectionValue instanceof Collection< ? >) - { - Collection< ? > coll = (Collection< ? >) collectionValue; - int size = coll.size(); - if (size == 1) - { - return coll.iterator().next(); - } - if (size > 1) - { - throw new IllegalArgumentException("More than one element in filter collection is unsupported."); - } - } - Class< ? extends Object> clazz = collectionValue.getClass(); - if (clazz.isArray()) - { - int length = Array.getLength(collectionValue); - if (length == 1) - { - return Array.get(collectionValue, 0); - } - if (length > 1) - { - throw new IllegalArgumentException("More than one element in filter array is unsupported."); - } - } - // TODO other cases? - } - return null; - } - - private String[] append(String[] propertyNames, String propertyName) - { - String[] result = Arrays.copyOf(propertyNames, propertyNames.length + 1); - result[propertyNames.length] = propertyName; - return result; - } } } Deleted: branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java 2012-06-29 16:05:56 UTC (rev 4078) +++ branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java 2012-06-29 16:11:44 UTC (rev 4079) @@ -1,66 +0,0 @@ -/** - * - * openutils base Spring-Hibernate DAO (http://www.openmindlab.com/lab/products/bshd5.html) - * - * Copyright(C) 2005-2012, 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.selectors; - -import java.io.Serializable; - -import org.hibernate.criterion.Example.PropertySelector; -import org.hibernate.property.BackrefPropertyAccessor; -import org.hibernate.type.Type; - - -/** - * utility selector to avoid class cast exceptions on {@link BackrefPropertyAccessor.UNKNOWN} - * @see BSHD-15 - * @author gcatania - * @version $Id$ - */ -public class ExcludeBackrefPropertySelector implements PropertySelector, Serializable -{ - - private static final long serialVersionUID = -2803322309158823550L; - - private final PropertySelector selector; - - public ExcludeBackrefPropertySelector(PropertySelector selector) - { - if (selector == null) - { - throw new NullPointerException("Null selector."); - } - this.selector = selector; - } - - public boolean include(Object propertyValue, String propertyName, Type type) - { - if (BackrefPropertyAccessor.UNKNOWN.equals(propertyValue)) - { - return false; - } - return selector.include(propertyValue, propertyName, type); - } - -} Copied: branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java (from rev 4078, trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java) =================================================================== --- branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java (rev 0) +++ branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java 2012-06-29 16:11:44 UTC (rev 4079) @@ -0,0 +1,66 @@ +/** + * + * openutils base Spring-Hibernate DAO (http://www.openmindlab.com/lab/products/bshd5.html) + * + * Copyright(C) 2005-2012, 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.selectors; + +import java.io.Serializable; + +import org.hibernate.criterion.Example.PropertySelector; +import org.hibernate.property.BackrefPropertyAccessor; +import org.hibernate.type.Type; + + +/** + * utility selector to avoid class cast exceptions on {@link BackrefPropertyAccessor.UNKNOWN} + * @see BSHD-15 + * @author gcatania + * @version $Id$ + */ +public class ExcludeBackrefPropertySelector implements PropertySelector, Serializable +{ + + private static final long serialVersionUID = -2803322309158823550L; + + private final PropertySelector selector; + + public ExcludeBackrefPropertySelector(PropertySelector selector) + { + if (selector == null) + { + throw new NullPointerException("Null selector."); + } + this.selector = selector; + } + + public boolean include(Object propertyValue, String propertyName, Type type) + { + if (BackrefPropertyAccessor.UNKNOWN.equals(propertyValue)) + { + return false; + } + return selector.include(propertyValue, propertyName, type); + } + +} Copied: branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/EntityBuilder.java (from rev 4078, trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/EntityBuilder.java) =================================================================== --- branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/EntityBuilder.java (rev 0) +++ branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/EntityBuilder.java 2012-06-29 16:11:44 UTC (rev 4079) @@ -0,0 +1,180 @@ +/** + * + * openutils base Spring-Hibernate DAO (http://www.openmindlab.com/lab/products/bshd5.html) + * + * Copyright(C) 2005-2012, 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.test; + +import it.openutils.hibernate.test.model.Address; +import it.openutils.hibernate.test.model.Car; +import it.openutils.hibernate.test.model.CarMaker; +import it.openutils.hibernate.test.model.CarModel; +import it.openutils.hibernate.test.model.CurrencyAmount; +import it.openutils.hibernate.test.model.Designer; +import it.openutils.hibernate.test.model.FullName; +import it.openutils.hibernate.test.model.Owner; +import it.openutils.hibernate.test.model.Person; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.GregorianCalendar; +import java.util.List; + + +/** + * @author gcatania + * @version $Id$ + */ +public final class EntityBuilder +{ + + private EntityBuilder() + { + } + + public static Person alice() + { + FullName fullName = new FullName("Alice", "McBeal"); + Calendar birthDate = new GregorianCalendar(1970, Calendar.MARCH, 7); + Address address = new Address("Long road", 15, "Smalltown", "MI", 14352); + Person p = new Person(); + p.setName(fullName); + p.setBirthDate(birthDate); + p.setFiscalAddress(address); + p.setCurrentAddress(address); + return p; + } + + public static Owner bob() + { + FullName fullName = new FullName("Bob", "Kelso"); + Calendar birthDate = new GregorianCalendar(1950, Calendar.MARCH, 7); + Address address = new Address("Sacred Heart Lane", 3, "Smalltown", "CA", 11243); + Owner o = new Owner(); + o.setName(fullName); + o.setBirthDate(birthDate); + o.setFiscalAddress(address); + o.setCurrentAddress(address); + return o; + } + + public static Owner chuck() + { + FullName fullName = new FullName("Chuck", "Palahniuk"); + Calendar birthDate = new GregorianCalendar(1962, Calendar.FEBRUARY, 21); + Address address = new Address("Awesome Street", 2, "Pasco", "WA", 13121); + Owner p = new Owner(); + p.setName(fullName); + p.setBirthDate(birthDate); + p.setFiscalAddress(address); + p.setCurrentAddress(address); + return p; + } + + public static CarMaker toyota() + { + CarMaker toyota = new CarMaker(); + toyota.setCode("TYT"); + toyota.setName("Toyota"); + toyota.setCapitalization(new CurrencyAmount(12000, "YEN")); + return toyota; + } + + public static CarMaker fiat() + { + CarMaker fiat = new CarMaker(); + fiat.setCode("FIA"); + fiat.setName("Fabbrica Italiana Automobili Torino"); + fiat.setCapitalization(new CurrencyAmount(80000, "EUR")); + return fiat; + } + + public static CarModel prius(CarMaker toyota) + { + CarModel prius = new CarModel(); + prius.setName("Prius"); + prius.setMake(toyota); + prius.setYear(Integer.valueOf(2008)); + + List<CarModel> toyotaModels = toyota.getModels(); + if (toyotaModels == null) + { + toyotaModels = new ArrayList<CarModel>(); + } + toyotaModels.add(prius); + toyota.setModels(toyotaModels); + return prius; + } + + public static CarModel prius() + { + return prius(toyota()); + } + + public static Designer priusDesigner(CarModel prius) + { + FullName fullName = new FullName("Ken", "Shiro"); + Calendar birthDate = new GregorianCalendar(1981, Calendar.OCTOBER, 16); + Address address = new Address("Khan avenue", 6, "Nagato", "TK", 99867); + Designer p = new Designer(); + p.setName(fullName); + p.setBirthDate(birthDate); + p.setFiscalAddress(address); + p.setCurrentAddress(address); + p.setDepartment("design"); + p.setEmployer(prius.getMake()); + p.setDesignedModels(Collections.singleton(prius)); + p.setGrossAnnualSalary(new CurrencyAmount(60000, "YEN")); + p.setHipsterFactor(97); + return p; + } + + public static Car bobsPrius(Owner bob, CarModel prius) + { + Car bobsPrius = new Car(); + bobsPrius.setModel(prius); + bobsPrius.setRegistrationDate(new GregorianCalendar(2010, Calendar.OCTOBER, 28)); + bobsPrius.setMarketValue(new CurrencyAmount(1700, "USD")); + bobsPrius.setOwner(bob); + bob.setCars(Collections.singleton(bobsPrius)); + return bobsPrius; + } + + public static Car bobsPrius() + { + return bobsPrius(bob(), prius()); + } + + public static Car chucksPrius(Owner chuck, CarModel prius) + { + Car chucksPrius = new Car(); + chucksPrius.setModel(prius); + chucksPrius.setRegistrationDate(new GregorianCalendar(2011, Calendar.DECEMBER, 13)); + chucksPrius.setMarketValue(new CurrencyAmount(5400, "USD")); + chucksPrius.setOwner(chuck); + chuck.setCars(Collections.singleton(chucksPrius)); + return chucksPrius; + } + +} Copied: branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOFilterMetadataTest.java (from rev 4078, trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOFilterMetadataTest.java) =================================================================== --- branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOFilterMetadataTest.java (rev 0) +++ branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOFilterMetadataTest.java 2012-06-29 16:11:44 UTC (rev 4079) @@ -0,0 +1,155 @@ +/** + * + * openutils base Spring-Hibernate DAO (http://www.openmindlab.com/lab/products/bshd5.html) + * + * Copyright(C) 2005-2012, 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.test; + +import static it.openutils.hibernate.test.EntityBuilder.bobsPrius; +import static it.openutils.hibernate.test.EntityBuilder.chuck; +import static it.openutils.hibernate.test.EntityBuilder.chucksPrius; +import static it.openutils.hibernate.test.EntityBuilder.prius; +import static it.openutils.hibernate.test.EntityBuilder.toyota; +import it.openutils.hibernate.example.FilterMetadata; +import it.openutils.hibernate.test.dao.CarDAO; +import it.openutils.hibernate.test.dao.CarMakerDAO; +import it.openutils.hibernate.test.dao.PersonDAO; +import it.openutils.hibernate.test.dao.StickerDAO; +import it.openutils.hibernate.test.model.Car; +import it.openutils.hibernate.test.model.Sticker; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.hibernate.Criteria; +import org.hibernate.criterion.Restrictions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests; +import org.testng.Assert; +import org.testng.annotations.Test; + + +/** + * @author gcatania + */ +@SuppressWarnings("deprecation") +@ContextConfiguration(locations = "/spring-tests.xml") +public class HibernateDAOFilterMetadataTest extends AbstractTransactionalTestNGSpringContextTests +{ + + @Autowired + private PersonDAO personDAO; + + @Autowired + private CarMakerDAO carMakerDAO; + + @Autowired + private CarDAO carDAO; + + @Autowired + private StickerDAO stickerDAO; + + @Test + public void testFindFilterMetadataBasic() + { + Sticker st1 = new Sticker(); + st1.setName("Warning! Baby on board!"); + st1.setHeight(20d); + st1.setWidth(10d); + Sticker st2 = new Sticker(); + st2.setName("Objects in the mirror are losing"); + st2.setHeight(5d); + st2.setWidth(10d); + Sticker st3 = new Sticker(); + st3.setName("(tribal tattoo sticker)"); + st3.setHeight(35d); + st3.setWidth(18d); + + Car chucksPrius = chucksPrius(chuck(), prius(toyota())); + chucksPrius.setStickers(Arrays.asList(st1, st2, st3)); + carDAO.save(chucksPrius); + carDAO.evict(chucksPrius); + + FilterMetadata fm = new FilterMetadata() + { + + public void createFilter(Criteria criteria, String propertyName, Object propertyValue) + { + criteria.add(Restrictions.gt(propertyName, 18d)); + criteria.add(Restrictions.lt(propertyName, 22d)); + } + }; + List<Sticker> found = stickerDAO.findFiltered(new Sticker(), Collections.singletonMap("height", fm)); + Assert.assertEquals(found.size(), 1); + Assert.assertEquals(found.get(0), st1); + } + + @Test + public void testFindFilterMetadataOnChildProperty() + { + Sticker st1 = new Sticker(); + st1.setName("Warning! Baby on board!"); + st1.setHeight(20d); + st1.setWidth(10d); + Sticker st2 = new Sticker(); + st2.setName("Objects in the mirror are losing"); + st2.setHeight(5d); + st2.setWidth(10d); + Sticker st3 = new Sticker(); + st3.setName("(tribal tattoo sticker)"); + st3.setHeight(35d); + st3.setWidth(18d); + + Car chucksPrius = chucksPrius(chuck(), prius(toyota())); + chucksPrius.setStickers(Arrays.asList(st1, st2, st3)); + + Car bobsPrius = bobsPrius(); + bobsPrius.setStickers(Arrays.asList(st2.clone())); + + carDAO.save(chucksPrius); + carDAO.evict(chucksPrius); + carDAO.save(bobsPrius); + carDAO.evict(bobsPrius); + + FilterMetadata fm = new FilterMetadata() + { + + public void createFilter(Criteria criteria, String propertyName, Object propertyValue) + { + criteria.add(Restrictions.gt(propertyName, 18d)); + criteria.add(Restrictions.lt(propertyName, 22d)); + } + }; + Car filter = new Car(); + filter.setStickers(Collections.singletonList(new Sticker())); + List<Car> found = carDAO.findFiltered(filter, Collections.singletonMap("stickers.height", fm)); + Assert.assertEquals(found.size(), 1); + Car actual = found.get(0); + Assert.assertEquals(actual.getStickers().size(), 3); + // full fledged equality fails because of Object.equals() on PersistentBag + Assert.assertEquals(actual.getOwner().getName(), chucksPrius.getOwner().getName()); + } + +} Modified: branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOPersistenceTest.java =================================================================== --- branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOPersistenceTest.java 2012-06-29 16:05:56 UTC (rev 4078) +++ branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOPersistenceTest.java 2012-06-29 16:11:44 UTC (rev 4079) @@ -25,6 +25,15 @@ package it.openutils.hibernate.test; +import static it.openutils.hibernate.test.EntityBuilder.alice; +import static it.openutils.hibernate.test.EntityBuilder.bob; +import static it.openutils.hibernate.test.EntityBuilder.bobsPrius; +import static it.openutils.hibernate.test.EntityBuilder.chuck; +import static it.openutils.hibernate.test.EntityBuilder.chucksPrius; +import static it.openutils.hibernate.test.EntityBuilder.fiat; +import static it.openutils.hibernate.test.EntityBuilder.prius; +import static it.openutils.hibernate.test.EntityBuilder.priusDesigner; +import static it.openutils.hibernate.test.EntityBuilder.toyota; import it.openutils.hibernate.test.dao.CarDAO; import it.openutils.hibernate.test.dao.CarMakerDAO; import it.openutils.hibernate.test.dao.PersonDAO; @@ -40,11 +49,8 @@ import it.openutils.hibernate.test.model.Person; import it.openutils.hibernate.test.model.Sticker; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Calendar; import java.util.Collections; -import java.util.GregorianCalendar; import java.util.List; import org.hibernate.criterion.Example; @@ -80,120 +86,6 @@ @Autowired private StickerDAO stickerDAO; - private static Person alice() - { - FullName fullName = new FullName("Alice", "McBeal"); - Calendar birthDate = new GregorianCalendar(1970, Calendar.MARCH, 7); - Address address = new Address("Long road", 15, "Smalltown", "MI", 14352); - Person p = new Person(); - p.setName(fullName); - p.setBirthDate(birthDate); - p.setFiscalAddress(address); - p.setCurrentAddress(address); - return p; - } - - private static Owner bob() - { - FullName fullName = new FullName("Bob", "Kelso"); - Calendar birthDate = new GregorianCalendar(1950, Calendar.MARCH, 7); - Address address = new Address("Sacred Heart Lane", 3, "Smalltown", "CA", 11243); - Owner o = new Owner(); - o.setName(fullName); - o.setBirthDate(birthDate); - o.setFiscalAddress(address); - o.setCurrentAddress(address); - return o; - } - - private static Owner chuck() - { - FullName fullName = new FullName("Chuck", "Palahniuk"); - Calendar birthDate = new GregorianCalendar(1962, Calendar.FEBRUARY, 21); - Address address = new Address("Awesome Street", 2, "Pasco", "WA", 13121); - Owner p = new Owner(); - p.setName(fullName); - p.setBirthDate(birthDate); - p.setFiscalAddress(address); - p.setCurrentAddress(address); - return p; - } - - private static CarMaker toyota() - { - CarMaker toyota = new CarMaker(); - toyota.setCode("TYT"); - toyota.setName("Toyota"); - toyota.setCapitalization(new CurrencyAmount(12000, "YEN")); - return toyota; - } - - private static CarMaker fiat() - { - CarMaker fiat = new CarMaker(); - fiat.setCode("FIA"); - fiat.setName("Fabbrica Italiana Automobili Torino"); - fiat.setCapitalization(new CurrencyAmount(80000, "EUR")); - return fiat; - } - - private static CarModel prius(CarMaker toyota) - { - CarModel prius = new CarModel(); - prius.setName("Prius"); - prius.setMake(toyota); - prius.setYear(Integer.valueOf(2008)); - - List<CarModel> toyotaModels = toyota.getModels(); - if (toyotaModels == null) - { - toyotaModels = new ArrayList<CarModel>(); - } - toyotaModels.add(prius); - toyota.setModels(toyotaModels); - return prius; - } - - private static Designer priusDesigner(CarModel prius) - { - FullName fullName = new FullName("Ken", "Shiro"); - Calendar birthDate = new GregorianCalendar(1981, Calendar.OCTOBER, 16); - Address address = new Address("Khan avenue", 6, "Nagato", "TK", 99867); - Designer p = new Designer(); - p.setName(fullName); - p.setBirthDate(birthDate); - p.setFiscalAddress(address); - p.setCurrentAddress(address); - p.setDepartment("design"); - p.setEmployer(prius.getMake()); - p.setDesignedModels(Collections.singleton(prius)); - p.setGrossAnnualSalary(new CurrencyAmount(60000, "YEN")); - p.setHipsterFactor(97); - return p; - } - - private static Car bobsPrius(Owner bob, CarModel prius) - { - Car bobsPrius = new Car(); - bobsPrius.setModel(prius); - bobsPrius.setRegistrationDate(new GregorianCalendar(2010, Calendar.OCTOBER, 28)); - bobsPrius.setMarketValue(new CurrencyAmount(1700, "USD")); - bobsPrius.setOwner(bob); - bob.setCars(Collections.singleton(bobsPrius)); - return bobsPrius; - } - - private static Car chucksPrius(Owner chuck, CarModel prius) - { - Car chucksPrius = new Car(); - chucksPrius.setModel(prius); - chucksPrius.setRegistrationDate(new GregorianCalendar(2011, Calendar.DECEMBER, 13)); - chucksPrius.setMarketValue(new CurrencyAmount(5400, "USD")); - chucksPrius.setOwner(chuck); - chuck.setCars(Collections.singleton(chucksPrius)); - return chucksPrius; - } - /** * basic save/evict/get test. */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |