From: <fg...@us...> - 2006-12-09 20:16:33
|
Revision: 137 http://svn.sourceforge.net/openutils/?rev=137&view=rev Author: fgiust Date: 2006-12-09 12:16:31 -0800 (Sat, 09 Dec 2006) Log Message: ----------- [maven-scm] copy for tag openutils-bshd5-1.0.3 Added Paths: ----------- tags/openutils-bshd5-1.0.3/ tags/openutils-bshd5-1.0.3/pom.xml tags/openutils-bshd5-1.0.3/src/main/java/it/openutils/hibernate/example/EnhancedExample.java Removed Paths: ------------- tags/openutils-bshd5-1.0.3/pom.xml tags/openutils-bshd5-1.0.3/src/main/java/it/openutils/hibernate/example/EnhancedExample.java Copied: tags/openutils-bshd5-1.0.3 (from rev 109, trunk/openutils-bshd5) Deleted: tags/openutils-bshd5-1.0.3/pom.xml =================================================================== --- trunk/openutils-bshd5/pom.xml 2006-11-13 21:23:57 UTC (rev 109) +++ tags/openutils-bshd5-1.0.3/pom.xml 2006-12-09 20:16:31 UTC (rev 137) @@ -1,46 +0,0 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils</artifactId> - <version>2</version> - <relativePath>../openutils-parent</relativePath> - </parent> - <artifactId>openutils-bshd5</artifactId> - <name>openutils base Spring-Hibernate DAO for java 5.0</name> - <version>1.0.3-SNAPSHOT</version> - <description>openutils base Spring-Hibernate DAO for java 5.0</description> - <dependencies> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-hibernate3</artifactId> - <version>2.0</version> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate</artifactId> - <version>3.2.0.cr5</version> - </dependency> - <dependency> - <groupId>commons-beanutils</groupId> - <artifactId>commons-beanutils</artifactId> - <version>1.7.0</version> - </dependency> - <dependency> - <groupId>commons-lang</groupId> - <artifactId>commons-lang</artifactId> - <version>2.1</version> - </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.0.1</version> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>3.8.1</version> - <optional>true</optional> - </dependency> - </dependencies> -</project> \ No newline at end of file Copied: tags/openutils-bshd5-1.0.3/pom.xml (from rev 136, trunk/openutils-bshd5/pom.xml) =================================================================== --- tags/openutils-bshd5-1.0.3/pom.xml (rev 0) +++ tags/openutils-bshd5-1.0.3/pom.xml 2006-12-09 20:16:31 UTC (rev 137) @@ -0,0 +1,52 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils</artifactId> + <version>2</version> + <relativePath>../openutils-parent</relativePath> + </parent> + <artifactId>openutils-bshd5</artifactId> + <name>openutils base Spring-Hibernate DAO for java 5.0</name> + <version>1.0.3</version> + <description>openutils base Spring-Hibernate DAO for java 5.0</description> + <dependencies> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-hibernate3</artifactId> + <version>2.0</version> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate</artifactId> + <version>3.2.0.cr5</version> + </dependency> + <dependency> + <groupId>commons-beanutils</groupId> + <artifactId>commons-beanutils</artifactId> + <version>1.7.0</version> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.1</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.0.1</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>3.8.1</version> + <optional>true</optional> + </dependency> + </dependencies> + + <scm> + <connection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-bshd5-1.0.3</connection> + <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-bshd5-1.0.3</developerConnection> + <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-bshd5-1.0.3</url> + </scm> +</project> \ No newline at end of file Deleted: tags/openutils-bshd5-1.0.3/src/main/java/it/openutils/hibernate/example/EnhancedExample.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2006-11-13 21:23:57 UTC (rev 109) +++ tags/openutils-bshd5-1.0.3/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2006-12-09 20:16:31 UTC (rev 137) @@ -1,319 +0,0 @@ -package it.openutils.hibernate.example; - -import it.openutils.dao.hibernate.MutableDateRange; - -import java.lang.reflect.InvocationTargetException; -import java.math.BigDecimal; -import java.sql.Timestamp; -import java.util.Calendar; -import java.util.Collection; -import java.util.Date; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.apache.commons.beanutils.PropertyUtils; -import org.apache.commons.lang.ClassUtils; -import org.apache.commons.lang.StringUtils; -import org.hibernate.Criteria; -import org.hibernate.HibernateException; -import org.hibernate.criterion.Restrictions; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.dao.DataRetrievalFailureException; - - -/** - * @author Fabrizio Giustina - * @version $Id: $ - */ -public class EnhancedExample -{ - - private Map<String, FilterMetadata> metadata; - - private EnhancedExample(Criteria crit, Object filter, Map<String, FilterMetadata> metadata) - { - this.metadata = metadata == null ? new HashMap<String, FilterMetadata>(0) : metadata; - fillCriteria(null, crit, filter); - } - - /** - * Logger. - */ - private static Logger log = LoggerFactory.getLogger(EnhancedExample.class); - - /** - * Fills a criteria object calling addCondition() for any non-null property or for any component in collections. - * @param crit Criteria - * @param filter javabean which will be analyzed for non-null properties - * @throws HibernateException exception while building the criteria - */ - public static void create(Criteria crit, Object filter, Map<String, FilterMetadata> metadata) - throws HibernateException - { - new EnhancedExample(crit, filter, metadata); - } - - /** - * Adds contitions to an existing criteria or create sub-criteria for associations. - * @param crit Criteria - * @param propertyName property name in parent bean - * @param value property value - * @throws HibernateException exception while building the criteria - */ - private void addCondition(Criteria crit, String propertyName, Object value, Object parentObject) - throws HibernateException - { - if (isSimpleType(value)) - { - - // don't filter on empty strings! - if (value instanceof String && StringUtils.isBlank((String) value)) - { - return; - } - - FilterMetadata fmd = metadata.get(propertyName); - - if (fmd == null) - { - fmd = FilterMetadata.EQUAL; - } - - String simplePropertyName = StringUtils.contains(propertyName, ".") ? StringUtils.substringAfterLast( - propertyName, - ".") : propertyName; - - fmd.createFilter(crit, simplePropertyName, value); - - } - else if (value instanceof MutableDateRange) // @todo MutableDateRange should be removed, use a FilterMetadata - // for this scope - { - Date from = ((MutableDateRange) value).getFrom(); - Date to = ((MutableDateRange) value).getTo(); - if (from != null && to != null) - { - log.debug("crit.add(Restrictions.between({},{}, {})", new Object[]{propertyName, from, to}); - crit.add(Restrictions.between(propertyName, from, to)); - } - else if (from != null) - { - - log.debug("crit.add(Restrictions.ge({}, {})", propertyName, to); - - crit.add(Restrictions.ge(propertyName, from)); - } - else if (to != null) - { - log.debug("crit.add(Restrictions.le({}, {})", propertyName, to); - crit.add(Restrictions.le(propertyName, to)); - } - } - else - { - if (containsSomething(value)) - { - - // @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()) - { - // collection: the new criteria has already been created, now we only nee to analize content - - for (Object element : ((Collection) value)) - { - String simplePropertyName = StringUtils.contains(propertyName, ".") ? StringUtils - .substringAfterLast(propertyName, ".") : propertyName; - log.debug("crit.createCriteria({})", simplePropertyName); - Criteria childrenCriteria = crit.createCriteria(simplePropertyName); - fillCriteria(propertyName, childrenCriteria, element); - } - } - else - { - String simplePropertyName = StringUtils.contains(propertyName, ".") ? StringUtils - .substringAfterLast(propertyName, ".") : propertyName; - log.debug("crit.createCriteria({})", simplePropertyName); - Criteria childrenCriteria = crit.createCriteria(simplePropertyName); - fillCriteria(propertyName, childrenCriteria, value); - } - } - } - } - - /** - * Check if the bean contains at least a valid property. - * @param bean javabean - * @return <code>true</code> if the bean contains at least a valid property - */ - private boolean containsSomething(Object bean) - { - - if (bean == null) - { - return false; - } - if (isSimpleType(bean)) - { - return true; - } - else if (bean instanceof MutableDateRange) - { - return ((MutableDateRange) bean).isSet(); - } - - if (bean instanceof Collection) - { - - Collection coll = ((Collection) bean); - if (coll.isEmpty()) - { - return false; - } - - if (containsSomething(coll.iterator().next())) - { - return true; - } - } - else if (bean instanceof Map) - { - Map coll = ((Map) bean); - if (coll.isEmpty()) - { - return false; - } - - if (containsSomething(coll.values().iterator().next())) - { - return true; - } - } - - Map<String, Object> properties; - try - { - properties = PropertyUtils.describe(bean); - } - catch (Throwable e) - { - if (e instanceof InvocationTargetException) - { - e = ((InvocationTargetException) e).getTargetException(); - } - - log.error("Unable to build filter, PropertyUtils.describe throws an exception while analizing class " - + ClassUtils.getShortClassName(bean, "NULL"), e); - return false; - } - - for (Map.Entry<String, Object> property : properties.entrySet()) - { - - if (!PropertyUtils.isWriteable(bean, property.getKey())) - { - // skip readonly properties - continue; - } - - Object propertyValue = property.getValue(); - if (propertyValue == null) - { - continue; - } - - if (isSimpleType(propertyValue) || containsSomething(propertyValue)) - { - return true; - } - } - - return false; - } - - /** - * Fills a criteria object calling addCondition() for any non-null property or for any component in collections. - * @param crit Criteria - * @param filter javabean which will be analyzed for non-null properties - * @throws HibernateException exception while building the criteria - */ - private void fillCriteria(String parentPropertyName, Criteria crit, Object filter) throws HibernateException - { - 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)) - { - fillCriteria(parentPropertyName, crit, element); - } - } - - Map<String, Object> properties; - try - { - properties = PropertyUtils.describe(filter); - } - catch (Throwable e) - { - if (e instanceof InvocationTargetException) - { - e = ((InvocationTargetException) e).getTargetException(); - } - - throw new DataRetrievalFailureException( - "Unable to build filter, PropertyUtils.describe throws an exception while analizing class " - + ClassUtils.getShortClassName(filter, "NULL") - + ":" - + e.getClass(), - e); - } - - Iterator<String> iterator = properties.keySet().iterator(); - while (iterator.hasNext()) - { - String propertyName = iterator.next(); - - Object value = properties.get(propertyName); - - // add only non-null values, ignore read-only properties - if (value != null && PropertyUtils.isWriteable(filter, propertyName)) - { - String composedPropertyName = (parentPropertyName == null) ? propertyName : parentPropertyName - + "." - + propertyName; - addCondition(crit, composedPropertyName, value, filter); - } - } - } - - /** - * Check if the given object is a simple java type - * @param object object to check - * @return <code>true</code>if the given object is a simple type - */ - private boolean isSimpleType(Object object) - { - - Class< ? extends Object> objClass = object.getClass(); - - return objClass.isPrimitive() - || objClass.equals(Integer.class) - || objClass.equals(Long.class) - || objClass.equals(Short.class) - || objClass.equals(Boolean.class) - || objClass.equals(String.class) - || objClass.equals(Double.class) - || objClass.equals(Float.class) - || objClass.equals(Date.class) - || objClass.equals(Byte.class) - || objClass.equals(BigDecimal.class) - || objClass.equals(Timestamp.class) - || objClass.equals(Character.class) - || objClass.equals(Calendar.class); - } - -} Copied: tags/openutils-bshd5-1.0.3/src/main/java/it/openutils/hibernate/example/EnhancedExample.java (from rev 134, trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java) =================================================================== --- tags/openutils-bshd5-1.0.3/src/main/java/it/openutils/hibernate/example/EnhancedExample.java (rev 0) +++ tags/openutils-bshd5-1.0.3/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2006-12-09 20:16:31 UTC (rev 137) @@ -0,0 +1,319 @@ +package it.openutils.hibernate.example; + +import it.openutils.dao.hibernate.MutableDateRange; + +import java.lang.reflect.InvocationTargetException; +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.beanutils.PropertyUtils; +import org.apache.commons.lang.ClassUtils; +import org.apache.commons.lang.StringUtils; +import org.hibernate.Criteria; +import org.hibernate.HibernateException; +import org.hibernate.criterion.Restrictions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.dao.DataRetrievalFailureException; + + +/** + * @author Fabrizio Giustina + * @version $Id: $ + */ +public class EnhancedExample +{ + + private Map<String, FilterMetadata> metadata; + + private EnhancedExample(Criteria crit, Object filter, Map<String, FilterMetadata> metadata) + { + this.metadata = metadata == null ? new HashMap<String, FilterMetadata>(0) : metadata; + fillCriteria(null, crit, filter); + } + + /** + * Logger. + */ + private static Logger log = LoggerFactory.getLogger(EnhancedExample.class); + + /** + * Fills a criteria object calling addCondition() for any non-null property or for any component in collections. + * @param crit Criteria + * @param filter javabean which will be analyzed for non-null properties + * @throws HibernateException exception while building the criteria + */ + public static void create(Criteria crit, Object filter, Map<String, FilterMetadata> metadata) + throws HibernateException + { + new EnhancedExample(crit, filter, metadata); + } + + /** + * Adds contitions to an existing criteria or create sub-criteria for associations. + * @param crit Criteria + * @param propertyName property name in parent bean + * @param value property value + * @throws HibernateException exception while building the criteria + */ + private void addCondition(Criteria crit, String propertyName, Object value, Object parentObject) + throws HibernateException + { + if (isSimpleType(value)) + { + + // don't filter on empty strings! + if (value instanceof String && StringUtils.isBlank((String) value)) + { + return; + } + + FilterMetadata fmd = metadata.get(propertyName); + + if (fmd == null) + { + fmd = FilterMetadata.EQUAL; + } + + String simplePropertyName = StringUtils.contains(propertyName, ".") ? StringUtils.substringAfterLast( + propertyName, + ".") : propertyName; + + fmd.createFilter(crit, simplePropertyName, value); + + } + else if (value instanceof MutableDateRange) // @todo MutableDateRange should be removed, use a FilterMetadata + // for this scope + { + Date from = ((MutableDateRange) value).getFrom(); + Date to = ((MutableDateRange) value).getTo(); + if (from != null && to != null) + { + log.debug("crit.add(Restrictions.between({},{}, {})", new Object[]{propertyName, from, to}); + crit.add(Restrictions.between(propertyName, from, to)); + } + else if (from != null) + { + + log.debug("crit.add(Restrictions.ge({}, {})", propertyName, to); + + crit.add(Restrictions.ge(propertyName, from)); + } + else if (to != null) + { + log.debug("crit.add(Restrictions.le({}, {})", propertyName, to); + crit.add(Restrictions.le(propertyName, to)); + } + } + else + { + if (containsSomething(value)) + { + + // @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()) + { + // collection: the new criteria has already been created, now we only nee to analize content + + for (Object element : ((Collection) value)) + { + String simplePropertyName = StringUtils.contains(propertyName, ".") ? StringUtils + .substringAfterLast(propertyName, ".") : propertyName; + log.debug("crit.createCriteria({})", simplePropertyName); + Criteria childrenCriteria = crit.createCriteria(simplePropertyName); + fillCriteria(propertyName, childrenCriteria, element); + } + } + else + { + String simplePropertyName = StringUtils.contains(propertyName, ".") ? StringUtils + .substringAfterLast(propertyName, ".") : propertyName; + log.debug("crit.createCriteria({})", simplePropertyName); + Criteria childrenCriteria = crit.createCriteria(simplePropertyName); + fillCriteria(propertyName, childrenCriteria, value); + } + } + } + } + + /** + * Check if the bean contains at least a valid property. + * @param bean javabean + * @return <code>true</code> if the bean contains at least a valid property + */ + private boolean containsSomething(Object bean) + { + + if (bean == null) + { + return false; + } + if (isSimpleType(bean)) + { + return true; + } + else if (bean instanceof MutableDateRange) + { + return ((MutableDateRange) bean).isSet(); + } + + if (bean instanceof Collection) + { + + Collection coll = ((Collection) bean); + if (coll.isEmpty()) + { + return false; + } + + if (containsSomething(coll.iterator().next())) + { + return true; + } + } + else if (bean instanceof Map) + { + Map coll = ((Map) bean); + if (coll.isEmpty()) + { + return false; + } + + if (containsSomething(coll.values().iterator().next())) + { + return true; + } + } + + Map<String, Object> properties; + try + { + properties = PropertyUtils.describe(bean); + } + catch (Throwable e) + { + if (e instanceof InvocationTargetException) + { + e = ((InvocationTargetException) e).getTargetException(); + } + + log.error("Unable to build filter, PropertyUtils.describe throws an exception while analizing class " + + ClassUtils.getShortClassName(bean, "NULL"), e); + return false; + } + + for (Map.Entry<String, Object> property : properties.entrySet()) + { + + if (!PropertyUtils.isWriteable(bean, property.getKey())) + { + // skip readonly properties + continue; + } + + Object propertyValue = property.getValue(); + if (propertyValue == null) + { + continue; + } + + if (isSimpleType(propertyValue) || containsSomething(propertyValue)) + { + return true; + } + } + + return false; + } + + /** + * Fills a criteria object calling addCondition() for any non-null property or for any component in collections. + * @param crit Criteria + * @param filter javabean which will be analyzed for non-null properties + * @throws HibernateException exception while building the criteria + */ + private void fillCriteria(String parentPropertyName, Criteria crit, Object filter) throws HibernateException + { + 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)) + { + fillCriteria(parentPropertyName, crit, element); + } + } + + Map<String, Object> properties; + try + { + properties = PropertyUtils.describe(filter); + } + catch (Throwable e) + { + if (e instanceof InvocationTargetException) + { + e = ((InvocationTargetException) e).getTargetException(); + } + + throw new DataRetrievalFailureException( + "Unable to build filter, PropertyUtils.describe throws an exception while analizing class " + + ClassUtils.getShortClassName(filter, "NULL") + + ":" + + e.getClass(), + e); + } + + Iterator<String> iterator = properties.keySet().iterator(); + while (iterator.hasNext()) + { + String propertyName = iterator.next(); + + Object value = properties.get(propertyName); + + // add only non-null values, ignore read-only properties + if (value != null && PropertyUtils.isWriteable(filter, propertyName)) + { + String composedPropertyName = (parentPropertyName == null) ? propertyName : parentPropertyName + + "." + + propertyName; + addCondition(crit, composedPropertyName, value, filter); + } + } + } + + /** + * Check if the given object is a simple java type + * @param object object to check + * @return <code>true</code>if the given object is a simple type + */ + private boolean isSimpleType(Object object) + { + + Class< ? extends Object> objClass = object.getClass(); + + return objClass.isPrimitive() + || objClass.equals(Integer.class) + || objClass.equals(Long.class) + || objClass.equals(Short.class) + || objClass.equals(Boolean.class) + || objClass.equals(String.class) + || objClass.equals(Double.class) + || objClass.equals(Float.class) + || objClass.equals(Date.class) + || objClass.equals(Byte.class) + || objClass.equals(BigDecimal.class) + || objClass.equals(Timestamp.class) + || objClass.equals(Character.class) + || Calendar.class.isAssignableFrom(objClass); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |