|
From: <fg...@us...> - 2006-10-18 12:30:57
|
Revision: 93
http://svn.sourceforge.net/openutils/?rev=93&view=rev
Author: fgiust
Date: 2006-10-18 05:30:41 -0700 (Wed, 18 Oct 2006)
Log Message:
-----------
[maven-scm] copy for tag openutils-bshd5-1.0.2
Added Paths:
-----------
tags/openutils-bshd5-1.0.2/
tags/openutils-bshd5-1.0.2/pom.xml
tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java
tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java
tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/hibernate/example/EnhancedExample.java
Removed Paths:
-------------
tags/openutils-bshd5-1.0.2/pom.xml
tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java
tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java
tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/hibernate/example/EnhancedExample.java
Copied: tags/openutils-bshd5-1.0.2 (from rev 80, trunk/openutils-bshd5)
Deleted: tags/openutils-bshd5-1.0.2/pom.xml
===================================================================
--- trunk/openutils-bshd5/pom.xml 2006-10-08 20:46:20 UTC (rev 80)
+++ tags/openutils-bshd5-1.0.2/pom.xml 2006-10-18 12:30:41 UTC (rev 93)
@@ -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.1-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.cr4</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.2/pom.xml (from rev 92, trunk/openutils-bshd5/pom.xml)
===================================================================
--- tags/openutils-bshd5-1.0.2/pom.xml (rev 0)
+++ tags/openutils-bshd5-1.0.2/pom.xml 2006-10-18 12:30:41 UTC (rev 93)
@@ -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.2</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://svn.sourceforge.net/svnroot/openutils/tags/openutils-bshd5-1.0.2</connection>
+ <developerConnection>scm:svn:https://svn.sourceforge.net/svnroot/openutils/tags/openutils-bshd5-1.0.2</developerConnection>
+ <url>http://svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-bshd5-1.0.2</url>
+ </scm>
+</project>
\ No newline at end of file
Deleted: tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2006-10-08 20:46:20 UTC (rev 80)
+++ tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2006-10-18 12:30:41 UTC (rev 93)
@@ -1,421 +0,0 @@
-package it.openutils.dao.hibernate;
-
-import it.openutils.hibernate.example.EnhancedExample;
-import it.openutils.hibernate.example.FilterMetadata;
-
-import java.io.Serializable;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import org.hibernate.Criteria;
-import org.hibernate.Hibernate;
-import org.hibernate.HibernateException;
-import org.hibernate.ObjectNotFoundException;
-import org.hibernate.Query;
-import org.hibernate.Session;
-import org.hibernate.criterion.CriteriaSpecification;
-import org.hibernate.criterion.Order;
-import org.hibernate.type.Type;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.orm.hibernate3.HibernateCallback;
-import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException;
-import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
-
-
-/**
- * Base Hibernate DAO.
- * @author Fabrizio Giustina
- * @version $Id$
- */
-public abstract class HibernateDAOImpl<T extends Object, K extends Serializable> extends HibernateDaoSupport
- implements
- HibernateDAO<T, K>
-{
-
- /**
- * Logger.
- */
- private static Logger log = LoggerFactory.getLogger(HibernateDAOImpl.class);
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#find(java.lang.String)
- */
- public List<T> find(String query)
- {
- return getHibernateTemplate().find(query);
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#findAll()
- */
- public List<T> findAll()
- {
- return findAll(getDefaultOrder());
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#findAll(org.hibernate.criterion.Order[])
- */
- public List<T> findAll(final Order[] orderProperties)
- {
-
- return (List<T>) getHibernateTemplate().execute(new HibernateCallback()
- {
-
- public Object doInHibernate(Session ses) throws HibernateException
- {
- Criteria crit = ses.createCriteria(getReferenceClass());
- if (null != orderProperties)
- {
- for (int j = 0; j < orderProperties.length; j++)
- {
- crit.addOrder(orderProperties[j]);
- }
-
- }
- return crit.list();
- }
- });
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#find(java.lang.String, java.lang.Object, org.hibernate.type.Type)
- */
- public List<T> find(String query, Object obj, Type type)
- {
- return find(query, new Object[]{obj}, new Type[]{type});
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#find(String, Object[], Type[])
- */
- public List<T> find(final String query, final Object[] obj, final Type[] type)
- {
- return (List<T>) getHibernateTemplate().execute(new HibernateCallback()
- {
-
- public Object doInHibernate(Session ses) throws HibernateException
- {
- // hibernate 3
- return ses.createQuery(query).setParameters(obj, type).list();
- }
- });
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#load(java.io.Serializable)
- */
- public T load(K key)
- {
- T result = (T) getHibernateTemplate().load(getReferenceClass(), key);
- Hibernate.initialize(result);
- return result;
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#load(java.io.Serializable)
- */
- public T loadIfAvailable(K key)
- {
- T result;
- try
- {
- result = (T) getHibernateTemplate().load(getReferenceClass(), key);
- Hibernate.initialize(result);
- }
- catch (ObjectNotFoundException e)
- {
- // during lazy initialization
- return null;
- }
- catch (HibernateObjectRetrievalFailureException e)
- {
- // during load
- if (e.getCause() instanceof ObjectNotFoundException)
- {
- return null;
- }
- throw e;
- }
- return result;
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#saveOrUpdate(null)
- */
- public void saveOrUpdate(final T obj)
- {
- getHibernateTemplate().saveOrUpdate(obj);
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#update(null)
- */
- public void update(T obj)
- {
- getHibernateTemplate().update(obj);
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#delete(null)
- */
- public boolean delete(final K key)
- {
-
- return (Boolean) getHibernateTemplate().execute(new HibernateCallback()
- {
-
- public Object doInHibernate(Session ses) throws HibernateException
- {
- ses.delete(ses.load(getReferenceClass(), key));
- return true;
- }
- });
-
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#refresh(null)
- */
- public void refresh(T obj)
- {
- getHibernateTemplate().refresh(obj);
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#merge(T)
- */
- public T merge(final T obj)
- {
- return (T) getHibernateTemplate().execute(new HibernateCallback()
- {
-
- public Object doInHibernate(Session ses) throws HibernateException
- {
- return ses.merge(obj);
- }
- });
-
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#save(java.lang.Object)
- */
- public K save(T obj)
- {
- return (K) getHibernateTemplate().save(obj);
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#findFilteredFirst(T)
- */
- public T findFilteredFirst(final T filter)
- {
- return getFirstInCollection(findFiltered(filter, 1, 0));
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(T)
- */
- public List<T> findFiltered(final T filter)
- {
- return findFiltered(filter, new HashMap<String, FilterMetadata>(0));
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(java.lang.Object, org.hibernate.criterion.Order[])
- */
- public List<T> findFiltered(T filter, Order[] orderProperties)
- {
- return findFiltered(filter, orderProperties, new HashMap<String, FilterMetadata>(0), Integer.MAX_VALUE, 0);
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(T)
- */
- public List<T> findFiltered(final T filter, final Map<String, FilterMetadata> metadata)
- {
- return findFiltered(filter, metadata, Integer.MAX_VALUE, 0);
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(null, int, int)
- */
- public List<T> findFiltered(final T filter, final int maxResults, final int page)
- {
- return findFiltered(filter, new HashMap<String, FilterMetadata>(0), maxResults, page);
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(null, int, int)
- */
- public List<T> findFiltered(final T filter, final Map<String, FilterMetadata> metadata, final int maxResults,
- final int page)
- {
- return findFiltered(filter, null, metadata, maxResults, page);
- }
-
- /**
- * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(null, int, int)
- */
- public List<T> findFiltered(final T filter, final Order[] customOrder, final Map<String, FilterMetadata> metadata,
- final int maxResults, final int page)
- {
- final Order[] orderProperties = customOrder != null && customOrder.length > 0 ? customOrder : this
- .getDefaultOrder();
-
- return (List<T>) getHibernateTemplate().execute(new HibernateCallback()
- {
-
- public Object doInHibernate(Session ses) throws HibernateException
- {
- Criteria crit = ses.createCriteria(filter.getClass());
- crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
- crit.setMaxResults(maxResults);
- crit.setFirstResult(maxResults * page);
-
- if (null != orderProperties && orderProperties.length > 0)
- {
- for (Order order : orderProperties)
- {
- if (order != null)
- {
- crit.addOrder(order);
- }
- }
- }
-
- EnhancedExample.create(crit, filter, metadata);
-
- return crit.list();
- }
- });
- }
-
- /**
- * Return the specific Object class that will be used for class-specific implementation of this DAO.
- * @return the reference Class
- */
- protected abstract Class<T> getReferenceClass();
-
- /**
- * Return a list of <code>Order</code> object to be used for the default ordering of the collection.
- * @return the property name
- */
- protected Order[] getDefaultOrder()
- {
- return new Order[0];
- }
-
- /**
- * Obtain an instance of Query for a named query string defined in the mapping file.
- * @param name the name of a query defined externally
- * @return Query
- */
- protected List< ? > getNamedQuery(final String name, final int marResults)
- {
- return (List< ? >) getHibernateTemplate().execute(new HibernateCallback()
- {
-
- public Object doInHibernate(Session ses) throws HibernateException
- {
- Query q = ses.getNamedQuery(name);
- q.setMaxResults(marResults);
- return q.list();
- }
- });
- }
-
- /**
- * Obtain an instance of Query for a named query string defined in the mapping file. Use the parameters given.
- * @param name the name of a query defined externally
- * @param params the parameter array
- * @return Query
- */
- protected List< ? > getNamedQuery(final String name, final Serializable[] params, final int marResults)
- {
-
- return (List< ? >) getHibernateTemplate().execute(new HibernateCallback()
- {
-
- public Object doInHibernate(Session ses) throws HibernateException
- {
- Query q = ses.getNamedQuery(name);
- q.setMaxResults(marResults);
- if (null != params)
- {
- for (int i = 0; i < params.length; i++)
- {
- q.setParameter(i, params[i]);
- }
- }
- return q.list();
- }
- });
- }
-
- /**
- * Obtain an instance of Query for a named query string defined in the mapping file. Use the parameters given.
- * @param name the name of a query defined externally
- * @param params the parameter Map
- * @return Query
- */
- protected List< ? > getNamedQuery(final String name, final Map params, final int marResults)
- {
- return (List< ? >) getHibernateTemplate().execute(new HibernateCallback()
- {
-
- public Object doInHibernate(Session ses) throws HibernateException
- {
- Query q = ses.getNamedQuery(name);
- q.setMaxResults(marResults);
- if (params != null)
- {
- for (Iterator i = params.entrySet().iterator(); i.hasNext();)
- {
- Map.Entry entry = (Map.Entry) i.next();
- setParameterValue(q, (String) entry.getKey(), entry.getValue());
- }
- }
- return q.list();
- }
- });
- }
-
- /**
- * Convenience method to set paramers in the query given based on the actual object type in passed in as the value.
- * You may need to add more functionaly to this as desired (or not use this at all).
- * @param query the Query to set
- * @param key the key name
- * @param value the object to set as the parameter
- */
- protected void setParameterValue(Query query, String key, Object value)
- {
- if (null == key || null == value)
- {
- return;
- }
- query.setParameter(key, value);
- }
-
- /**
- * Returns the first object in the collection or null if the collection is null or empty.
- * @param list collection
- * @return first element in the list
- */
- private T getFirstInCollection(Collection<T> list)
- {
- if (list != null && !list.isEmpty())
- {
- Object result = list.iterator().next();
- Hibernate.initialize(result);
- return (T) result;
- }
- return null;
- }
-
-}
\ No newline at end of file
Copied: tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java (from rev 84, trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java)
===================================================================
--- tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java (rev 0)
+++ tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2006-10-18 12:30:41 UTC (rev 93)
@@ -0,0 +1,428 @@
+package it.openutils.dao.hibernate;
+
+import it.openutils.hibernate.example.EnhancedExample;
+import it.openutils.hibernate.example.FilterMetadata;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.hibernate.Criteria;
+import org.hibernate.Hibernate;
+import org.hibernate.HibernateException;
+import org.hibernate.ObjectNotFoundException;
+import org.hibernate.Query;
+import org.hibernate.Session;
+import org.hibernate.criterion.CriteriaSpecification;
+import org.hibernate.criterion.Order;
+import org.hibernate.type.Type;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.orm.hibernate3.HibernateCallback;
+import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException;
+import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
+
+
+/**
+ * Base Hibernate DAO.
+ * @author Fabrizio Giustina
+ * @version $Id$
+ */
+public abstract class HibernateDAOImpl<T extends Object, K extends Serializable> extends HibernateDaoSupport
+ implements
+ HibernateDAO<T, K>
+{
+
+ /**
+ * Logger.
+ */
+ private static Logger log = LoggerFactory.getLogger(HibernateDAOImpl.class);
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#find(java.lang.String)
+ */
+ @SuppressWarnings("unchecked")
+ public List<T> find(String query)
+ {
+ return getHibernateTemplate().find(query);
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#findAll()
+ */
+ public List<T> findAll()
+ {
+ return findAll(getDefaultOrder());
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#findAll(org.hibernate.criterion.Order[])
+ */
+ @SuppressWarnings("unchecked")
+ public List<T> findAll(final Order[] orderProperties)
+ {
+
+ return (List<T>) getHibernateTemplate().execute(new HibernateCallback()
+ {
+
+ public Object doInHibernate(Session ses) throws HibernateException
+ {
+ Criteria crit = ses.createCriteria(getReferenceClass());
+ if (null != orderProperties)
+ {
+ for (int j = 0; j < orderProperties.length; j++)
+ {
+ crit.addOrder(orderProperties[j]);
+ }
+
+ }
+ return crit.list();
+ }
+ });
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#find(java.lang.String, java.lang.Object, org.hibernate.type.Type)
+ */
+ public List<T> find(String query, Object obj, Type type)
+ {
+ return find(query, new Object[]{obj}, new Type[]{type});
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#find(String, Object[], Type[])
+ */
+ @SuppressWarnings("unchecked")
+ public List<T> find(final String query, final Object[] obj, final Type[] type)
+ {
+ return (List<T>) getHibernateTemplate().execute(new HibernateCallback()
+ {
+
+ public Object doInHibernate(Session ses) throws HibernateException
+ {
+ // hibernate 3
+ return ses.createQuery(query).setParameters(obj, type).list();
+ }
+ });
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#load(java.io.Serializable)
+ */
+ @SuppressWarnings("unchecked")
+ public T load(K key)
+ {
+ T result = (T) getHibernateTemplate().load(getReferenceClass(), key);
+ Hibernate.initialize(result);
+ return result;
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#load(java.io.Serializable)
+ */
+ @SuppressWarnings("unchecked")
+ public T loadIfAvailable(K key)
+ {
+ T result;
+ try
+ {
+ result = (T) getHibernateTemplate().load(getReferenceClass(), key);
+ Hibernate.initialize(result);
+ }
+ catch (ObjectNotFoundException e)
+ {
+ // during lazy initialization
+ return null;
+ }
+ catch (HibernateObjectRetrievalFailureException e)
+ {
+ // during load
+ if (e.getCause() instanceof ObjectNotFoundException)
+ {
+ return null;
+ }
+ throw e;
+ }
+ return result;
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#saveOrUpdate(null)
+ */
+ public void saveOrUpdate(final T obj)
+ {
+ getHibernateTemplate().saveOrUpdate(obj);
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#update(null)
+ */
+ public void update(T obj)
+ {
+ getHibernateTemplate().update(obj);
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#delete(null)
+ */
+ public boolean delete(final K key)
+ {
+
+ return (Boolean) getHibernateTemplate().execute(new HibernateCallback()
+ {
+
+ public Object doInHibernate(Session ses) throws HibernateException
+ {
+ ses.delete(ses.load(getReferenceClass(), key));
+ return true;
+ }
+ });
+
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#refresh(null)
+ */
+ public void refresh(T obj)
+ {
+ getHibernateTemplate().refresh(obj);
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#merge(T)
+ */
+ @SuppressWarnings("unchecked")
+ public T merge(final T obj)
+ {
+ return (T) getHibernateTemplate().execute(new HibernateCallback()
+ {
+
+ public Object doInHibernate(Session ses) throws HibernateException
+ {
+ return ses.merge(obj);
+ }
+ });
+
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#save(java.lang.Object)
+ */
+ @SuppressWarnings("unchecked")
+ public K save(T obj)
+ {
+ return (K) getHibernateTemplate().save(obj);
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#findFilteredFirst(T)
+ */
+ public T findFilteredFirst(final T filter)
+ {
+ return getFirstInCollection(findFiltered(filter, 1, 0));
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(T)
+ */
+ public List<T> findFiltered(final T filter)
+ {
+ return findFiltered(filter, new HashMap<String, FilterMetadata>(0));
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(java.lang.Object, org.hibernate.criterion.Order[])
+ */
+ public List<T> findFiltered(T filter, Order[] orderProperties)
+ {
+ return findFiltered(filter, orderProperties, new HashMap<String, FilterMetadata>(0), Integer.MAX_VALUE, 0);
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(T)
+ */
+ public List<T> findFiltered(final T filter, final Map<String, FilterMetadata> metadata)
+ {
+ return findFiltered(filter, metadata, Integer.MAX_VALUE, 0);
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(null, int, int)
+ */
+ public List<T> findFiltered(final T filter, final int maxResults, final int page)
+ {
+ return findFiltered(filter, new HashMap<String, FilterMetadata>(0), maxResults, page);
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(null, int, int)
+ */
+ public List<T> findFiltered(final T filter, final Map<String, FilterMetadata> metadata, final int maxResults,
+ final int page)
+ {
+ return findFiltered(filter, null, metadata, maxResults, page);
+ }
+
+ /**
+ * @see it.openutils.dao.hibernate.HibernateDAO#findFiltered(null, int, int)
+ */
+ @SuppressWarnings("unchecked")
+ public List<T> findFiltered(final T filter, final Order[] customOrder, final Map<String, FilterMetadata> metadata,
+ final int maxResults, final int page)
+ {
+ final Order[] orderProperties = customOrder != null && customOrder.length > 0 ? customOrder : this
+ .getDefaultOrder();
+
+ return (List<T>) getHibernateTemplate().execute(new HibernateCallback()
+ {
+
+ public Object doInHibernate(Session ses) throws HibernateException
+ {
+ Criteria crit = ses.createCriteria(filter.getClass());
+ crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
+ crit.setMaxResults(maxResults);
+ crit.setFirstResult(maxResults * page);
+
+ if (null != orderProperties && orderProperties.length > 0)
+ {
+ for (Order order : orderProperties)
+ {
+ if (order != null)
+ {
+ crit.addOrder(order);
+ }
+ }
+ }
+
+ EnhancedExample.create(crit, filter, metadata);
+
+ return crit.list();
+ }
+ });
+ }
+
+ /**
+ * Return the specific Object class that will be used for class-specific implementation of this DAO.
+ * @return the reference Class
+ */
+ protected abstract Class<T> getReferenceClass();
+
+ /**
+ * Return a list of <code>Order</code> object to be used for the default ordering of the collection.
+ * @return the property name
+ */
+ protected Order[] getDefaultOrder()
+ {
+ return new Order[0];
+ }
+
+ /**
+ * Obtain an instance of Query for a named query string defined in the mapping file.
+ * @param name the name of a query defined externally
+ * @return Query
+ */
+ protected List< ? > getNamedQuery(final String name, final int marResults)
+ {
+ return (List< ? >) getHibernateTemplate().execute(new HibernateCallback()
+ {
+
+ public Object doInHibernate(Session ses) throws HibernateException
+ {
+ Query q = ses.getNamedQuery(name);
+ q.setMaxResults(marResults);
+ return q.list();
+ }
+ });
+ }
+
+ /**
+ * Obtain an instance of Query for a named query string defined in the mapping file. Use the parameters given.
+ * @param name the name of a query defined externally
+ * @param params the parameter array
+ * @return Query
+ */
+ protected List< ? > getNamedQuery(final String name, final Serializable[] params, final int marResults)
+ {
+
+ return (List< ? >) getHibernateTemplate().execute(new HibernateCallback()
+ {
+
+ public Object doInHibernate(Session ses) throws HibernateException
+ {
+ Query q = ses.getNamedQuery(name);
+ q.setMaxResults(marResults);
+ if (null != params)
+ {
+ for (int i = 0; i < params.length; i++)
+ {
+ q.setParameter(i, params[i]);
+ }
+ }
+ return q.list();
+ }
+ });
+ }
+
+ /**
+ * Obtain an instance of Query for a named query string defined in the mapping file. Use the parameters given.
+ * @param name the name of a query defined externally
+ * @param params the parameter Map
+ * @return Query
+ */
+ protected List< ? > getNamedQuery(final String name, final Map<String, Object> params, final int marResults)
+ {
+ return (List< ? >) getHibernateTemplate().execute(new HibernateCallback()
+ {
+
+ public Object doInHibernate(Session ses) throws HibernateException
+ {
+ Query q = ses.getNamedQuery(name);
+ q.setMaxResults(marResults);
+ if (params != null)
+ {
+ for (Map.Entry<String, Object> entry : params.entrySet())
+ {
+ setParameterValue(q, entry.getKey(), entry.getValue());
+ }
+ }
+ return q.list();
+ }
+ });
+ }
+
+ /**
+ * Convenience method to set paramers in the query given based on the actual object type in passed in as the value.
+ * You may need to add more functionaly to this as desired (or not use this at all).
+ * @param query the Query to set
+ * @param key the key name
+ * @param value the object to set as the parameter
+ */
+ protected void setParameterValue(Query query, String key, Object value)
+ {
+ if (null == key || null == value)
+ {
+ return;
+ }
+ query.setParameter(key, value);
+ }
+
+ /**
+ * Returns the first object in the collection or null if the collection is null or empty.
+ * @param list collection
+ * @return first element in the list
+ */
+ @SuppressWarnings("unchecked")
+ private T getFirstInCollection(Collection<T> list)
+ {
+ if (list != null && !list.isEmpty())
+ {
+ Object result = list.iterator().next();
+ Hibernate.initialize(result);
+ return (T) result;
+ }
+ return null;
+ }
+
+}
\ No newline at end of file
Deleted: tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java 2006-10-08 20:46:20 UTC (rev 80)
+++ tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java 2006-10-18 12:30:41 UTC (rev 93)
@@ -1,73 +0,0 @@
-package it.openutils.dao.hibernate;
-
-import java.util.Date;
-
-
-/**
- * @author fgiust
- * @version $Revision$ ($Author$)
- */
-public class MutableDateRange extends Date
-{
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- /**
- * range start.
- */
- private Date from;
-
- /**
- * range end.
- */
- private Date to;
-
- /**
- * Getter for <code>from</code>.
- * @return Returns the from.
- */
- public Date getFrom()
- {
- return this.from;
- }
-
- /**
- * Setter for <code>from</code>.
- * @param from The from to set.
- */
- public void setFrom(Date from)
- {
- this.from = from;
- }
-
- /**
- * Getter for <code>to</code>.
- * @return Returns the to.
- */
- public Date getTo()
- {
- return this.to;
- }
-
- /**
- * Setter for <code>to</code>.
- * @param to The to to set.
- */
- public void setTo(Date to)
- {
- this.to = to;
- }
-
- /**
- * Returns <code>true</code> if at least one date is set.
- * @return <code>true</code> if at least one date is set
- */
- public boolean isSet()
- {
- return this.to != null || this.from != null;
- }
-
-}
Copied: tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java (from rev 86, trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java)
===================================================================
--- tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java (rev 0)
+++ tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java 2006-10-18 12:30:41 UTC (rev 93)
@@ -0,0 +1,74 @@
+package it.openutils.dao.hibernate;
+
+import java.util.Date;
+
+
+/**
+ * @author fgiust
+ * @version $Id: $
+ * @deprecated use FilterMetadata
+ */
+public class MutableDateRange extends Date
+{
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * range start.
+ */
+ private Date from;
+
+ /**
+ * range end.
+ */
+ private Date to;
+
+ /**
+ * Getter for <code>from</code>.
+ * @return Returns the from.
+ */
+ public Date getFrom()
+ {
+ return this.from;
+ }
+
+ /**
+ * Setter for <code>from</code>.
+ * @param from The from to set.
+ */
+ public void setFrom(Date from)
+ {
+ this.from = from;
+ }
+
+ /**
+ * Getter for <code>to</code>.
+ * @return Returns the to.
+ */
+ public Date getTo()
+ {
+ return this.to;
+ }
+
+ /**
+ * Setter for <code>to</code>.
+ * @param to The to to set.
+ */
+ public void setTo(Date to)
+ {
+ this.to = to;
+ }
+
+ /**
+ * Returns <code>true</code> if at least one date is set.
+ * @return <code>true</code> if at least one date is set
+ */
+ public boolean isSet()
+ {
+ return this.to != null || this.from != null;
+ }
+
+}
Deleted: tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/hibernate/example/EnhancedExample.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2006-10-08 20:46:20 UTC (rev 80)
+++ tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2006-10-18 12:30:41 UTC (rev 93)
@@ -1,327 +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.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.hibernate.Criteria;
-import org.hibernate.HibernateException;
-import org.hibernate.criterion.Restrictions;
-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(crit, filter);
- }
-
- /**
- * Logger.
- */
- private static Log log = LogFactory.getLog(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;
- }
-
- // @todo handle nested properties
- FilterMetadata fmd = metadata.get(propertyName);
-
- if (fmd == null)
- {
- fmd = FilterMetadata.EQUAL;
- }
-
- fmd.createFilter(crit, propertyName, value);
-
- }
- else if (value instanceof MutableDateRange)
- {
- Date from = ((MutableDateRange) value).getFrom();
- Date to = ((MutableDateRange) value).getTo();
- if (from != null && to != null)
- {
- if (log.isDebugEnabled())
- {
- log.debug("crit.add(Restrictions.between(" + propertyName + "," + from + ", " + to + ")");
- }
- crit.add(Restrictions.between(propertyName, from, to));
- }
- else if (from != null)
- {
- if (log.isDebugEnabled())
- {
- log.debug("crit.add(Restrictions.ge(" + propertyName + "," + from + ")");
- }
- crit.add(Restrictions.ge(propertyName, from));
- }
- else if (to != null)
- {
- if (log.isDebugEnabled())
- {
- log.debug("crit.add(Restrictions.le(" + propertyName + ", " + to + ")");
- }
- crit.add(Restrictions.le(propertyName, to));
- }
- }
- else
- {
- if (containsSomething(value))
- {
- if (log.isDebugEnabled())
- {
- log.debug("crit.createCriteria(" + propertyName + ")");
- }
-
- // @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
- Iterator iterator = ((Collection) value).iterator();
-
- if (iterator.hasNext())
- {
- Object element = iterator.next();
- Criteria childrenCriteria = crit.createCriteria(propertyName);
- fillCriteria(childrenCriteria, element);
- }
- }
- else
- {
- Criteria childrenCriteria = crit.createCriteria(propertyName);
- fillCriteria(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)
- {
- // log.debug("**COL**");
- Collection coll = ((Collection) bean);
- if (coll.isEmpty())
- {
- return false;
- }
-
- if (containsSomething(coll.iterator().next()))
- {
- return true;
- }
- }
- else if (bean instanceof Map)
- {
- // log.debug("**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;
- }
- }
-
- if (log.isDebugEnabled())
- {
- // log.debug(ClassUtils.getShortClassName(bean.getClass()) + " is empty");
- }
-
- 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(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
- Iterator iterator = ((Collection) filter).iterator();
- while (iterator.hasNext())
- {
- Object element = iterator.next();
- fillCriteria(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))
- {
- addCondition(crit, propertyName, 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.2/src/main/java/it/openutils/hibernate/example/EnhancedExample.java (from rev 91, trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java)
===================================================================
--- tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/hibernate/example/EnhancedExample.java (rev 0)
+++ tags/openutils-bshd5-1.0.2/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2006-10-18 12:30:41 UTC (rev 93)
@@ -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)
+ || objClass.equals(Calendar.class);
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|