Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / dao / kernel / GenericDAOImpl.java
index bc94999e82e44986c1ebc086528278a421b468dd..d76653b7be14998d2cca2b9166f274fbcde0f171 100644 (file)
 package org.splat.dal.dao.kernel;
 
 import java.io.Serializable;
+import java.util.List;
 
+import org.hibernate.Criteria;
+import org.hibernate.criterion.Criterion;
+import org.hibernate.criterion.Order;
+import org.hibernate.criterion.Restrictions;
 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 
 /**
@@ -23,8 +28,8 @@ import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  * @param <PK>
  *            Primary key class
  */
-public abstract class GenericDAOImpl<T, PK extends Serializable> extends HibernateDaoSupport implements
-               GenericDAO<T, PK> {
+public abstract class GenericDAOImpl<T, PK extends Serializable> extends
+               HibernateDaoSupport implements GenericDAO<T, PK> {
        /**
         * Persist the newInstance object into database.
         * 
@@ -49,15 +54,65 @@ public abstract class GenericDAOImpl<T, PK extends Serializable> extends Hiberna
                return (T) getSession().get(getType(), id);
        }
 
-       /** Save changes made to a persistent object.
-        * @param transientObject transient instance of the object to update
+       /**
+        * Retrieve an object that was previously persisted to the database using the given criteria.
+        * 
+        * @param aCondition
+        *            a search condition
+        * @return an object found according to the given criteria
+        */
+       @SuppressWarnings("unchecked")
+       public T findByCriteria(Criterion aCondition) {
+               return (T) getSession().createCriteria(getType()).add(aCondition)
+                               .uniqueResult();
+       }
+
+       /**
+        * Retrieve a list of objects which were previously persisted to the database using the given criteria.
+        * 
+        * @param aCondition
+        *            a search condition
+        * @return a list of objects filtered according to the given criteria
+        */
+       @SuppressWarnings("unchecked")
+       public List<T> getFilteredList(Criterion aCondition) {
+               return getSession().createCriteria(getType()).add(aCondition).list();
+       }
+
+       /**
+        * Retrieve a list of objects which were previously persisted to the database using the given criteria.
+        * 
+        * @param aCondition
+        *            a search condition
+        * @param anOrder
+        *            a result list order. Null is ignored and in such case the result list is unordered.
+        * @return a list of objects filtered according to the given criteria
+        */
+       @SuppressWarnings("unchecked")
+       public List<T> getFilteredList(Criterion aCondition, Order anOrder) {
+               Criteria aCriteria = getSession().createCriteria(getType()).add(
+                               aCondition);
+               if (anOrder != null) {
+                       aCriteria.addOrder(anOrder);
+               }
+               return aCriteria.list();
+       }
+
+       /**
+        * Save changes made to a persistent object.
+        * 
+        * @param transientObject
+        *            transient instance of the object to update
         */
        public void update(T transientObject) {
                getSession().update(transientObject);
        }
 
-       /** Remove an object from persistent storage in the database.
-        * @param persistentObject a persistent object to delete from the database
+       /**
+        * Remove an object from persistent storage in the database.
+        * 
+        * @param persistentObject
+        *            a persistent object to delete from the database
         */
        public void delete(T persistentObject) {
                getSession().delete(persistentObject);
@@ -65,6 +120,7 @@ public abstract class GenericDAOImpl<T, PK extends Serializable> extends Hiberna
 
        /**
         * Get persistent object type.
+        * 
         * @return Persistent object class to be processed by this DAO
         */
        abstract protected Class<T> getType();