From: <gca...@us...> - 2012-06-29 15:10:29
|
Revision: 4076 http://openutils.svn.sourceforge.net/openutils/?rev=4076&view=rev Author: gcatania Date: 2012-06-29 15:10:22 +0000 (Fri, 29 Jun 2012) Log Message: ----------- BSHD-15 source code refactor and cleanup Modified Paths: -------------- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java Added Paths: ----------- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/selectors/ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java 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 2012-06-29 14:26:07 UTC (rev 4075) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2012-06-29 15:10:22 UTC (rev 4076) @@ -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); } } @@ -322,11 +319,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); @@ -364,128 +356,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 Added: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java (rev 0) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2012-06-29 15:10:22 UTC (rev 4076) @@ -0,0 +1,125 @@ +/** + * Copyright (c) Energeya LLC. All rights reserved. http://www.energeya.com + */ +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; + } + +} Property changes on: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native 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 2012-06-29 14:26:07 UTC (rev 4075) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2012-06-29 15:10:22 UTC (rev 4076) @@ -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); } } @@ -185,15 +183,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 +195,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; - } } } Added: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java (rev 0) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java 2012-06-29 15:10:22 UTC (rev 4076) @@ -0,0 +1,44 @@ +/** + * Copyright (c) Energeya LLC. All rights reserved. http://www.energeya.com + */ +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); + } + +} Property changes on: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/selectors/ExcludeBackrefPropertySelector.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |