Salome HOME
Creation of a new study from an existing one is implemented.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / dao / kernel / AbstractGenericDAOImpl.java
index b1276d265a3a88ed8444973f45a544b72398a2f8..296df7384492598f41d8dcee70e50c1e9936f908 100644 (file)
@@ -14,9 +14,13 @@ import java.util.List;
 import java.util.Properties;
 
 import org.hibernate.Criteria;
+import org.hibernate.LockOptions;
+import org.hibernate.Session;
 import org.hibernate.criterion.Criterion;
+import org.hibernate.criterion.DetachedCriteria;
 import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Restrictions;
+import org.splat.common.Constants;
 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 
 /**
@@ -29,32 +33,43 @@ import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  * @param <PK>
  *            Primary key class
  */
-public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
-               HibernateDaoSupport implements GenericDAO<T, PK> {
-       /**
-        * Unchecked warning specification.
-        */
-       private static final String UNCHECKED = "unchecked";
+public abstract class AbstractGenericDAOImpl<T, PK extends Serializable>
+               extends HibernateDaoSupport implements GenericDAO<T, PK> {
+
        /**
         * Persist the newInstance object into database.
         * 
         * @param newInstance
         *            new object as a transient instance
         * @return new primary key for the created persistent object
+        * @see Session#save(Object)
         */
-       @SuppressWarnings(UNCHECKED)
+       @SuppressWarnings(Constants.UNCHECKED)
        public PK create(final T newInstance) {
                return (PK) getSession().save(newInstance);
        }
 
+       /**
+        * Persist the newInstance object into database.
+        * 
+        * @param newInstance
+        *            new object as a transient instance
+        * @see Session#saveOrUpdate(Object)
+        */
+       @SuppressWarnings(Constants.UNCHECKED)
+       public void saveOrUpdate(final T newInstance) {
+               getSession().saveOrUpdate(newInstance);
+       }
+
        /**
         * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
         * 
         * @param id
         *            primary key of an object to read
         * @return an object found by the given key
+        * @see Session#get(Class, Serializable)
         */
-       @SuppressWarnings(UNCHECKED)
+       @SuppressWarnings(Constants.UNCHECKED)
        public T get(final PK id) {
                return (T) getSession().get(getType(), id);
        }
@@ -66,7 +81,7 @@ public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
         *            a search condition
         * @return an object found according to the given criteria
         */
-       @SuppressWarnings(UNCHECKED)
+       @SuppressWarnings(Constants.UNCHECKED)
        public T findByCriteria(final Criterion aCondition) {
                return (T) getSession().createCriteria(getType()).add(aCondition)
                                .uniqueResult();
@@ -81,8 +96,9 @@ public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
         */
        public T findByCriteria(final Properties andParams) {
                Criterion aCondition = null;
-               for (String aName: andParams.stringPropertyNames()) {
-                       aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
+               for (String aName : andParams.stringPropertyNames()) {
+                       aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
+                                       andParams.get(aName)));
                }
                return findByCriteria(aCondition);
        }
@@ -92,7 +108,7 @@ public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
         * 
         * @return a list of all objects of the considered type T
         */
-       @SuppressWarnings(UNCHECKED)
+       @SuppressWarnings(Constants.UNCHECKED)
        public List<T> getAll() {
                return getSession().createCriteria(getType()).list();
        }
@@ -104,17 +120,44 @@ public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
         *            a result list order. Null is ignored and in such case the result list is unordered.
         * @return an ordered list of all objects of the considered type T
         */
-       @SuppressWarnings(UNCHECKED)
-       public List<T> getAll(final Order ... anOrder) {
+       @SuppressWarnings(Constants.UNCHECKED)
+       public List<T> getAll(final Order... anOrder) {
                Criteria aCriteria = getSession().createCriteria(getType());
                for (Order order : anOrder) {
-                       if (anOrder != null) {
+                       if (order != null) {
                                aCriteria.addOrder(order);
                        }
                }
                return aCriteria.list();
        }
 
+       /**
+        * Retrieve a list of objects which were previously persisted to the database using the given criteria.
+        * 
+        * @param aDetachedCriteria
+        *            search criteria
+        * @return a list of objects filtered according to the given criteria
+        */
+       @SuppressWarnings(Constants.UNCHECKED)
+       public List<T> getFilteredList(final DetachedCriteria aDetachedCriteria) {
+               return aDetachedCriteria.getExecutableCriteria(getSession()).list();
+       }
+
+       /**
+        * Retrieve a list of DTO objects using the given criteria.
+        * 
+        * @param <DTO>
+        *            the class of returned DTOs
+        * @param aDetachedCriteria
+        *            search criteria
+        * @return a list of DTO objects filtered according to the given criteria
+        */
+       @SuppressWarnings(Constants.UNCHECKED)
+       public <DTO> List<DTO> getFilteredDTOList(
+                       final DetachedCriteria aDetachedCriteria) {
+               return aDetachedCriteria.getExecutableCriteria(getSession()).list();
+       }
+
        /**
         * Retrieve a list of objects which were previously persisted to the database using the given criteria.
         * 
@@ -122,7 +165,7 @@ public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
         *            a search condition
         * @return a list of objects filtered according to the given criteria
         */
-       @SuppressWarnings(UNCHECKED)
+       @SuppressWarnings(Constants.UNCHECKED)
        public List<T> getFilteredList(final Criterion aCondition) {
                return getSession().createCriteria(getType()).add(aCondition).list();
        }
@@ -136,18 +179,58 @@ public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
         *            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(final Criterion aCondition, final Order ... anOrder) {
+       @SuppressWarnings(Constants.UNCHECKED)
+       public List<T> getFilteredList(final Criterion aCondition,
+                       final Order... anOrder) {
                Criteria aCriteria = getSession().createCriteria(getType()).add(
                                aCondition);
                for (Order order : anOrder) {
-                       if (anOrder != null) {
+                       if (order != null) {
                                aCriteria.addOrder(order);
                        }
                }
                return aCriteria.list();
        }
 
+       /**
+        * Retrieve a list of objects which were previously persisted to the database using the given criteria.
+        * 
+        * @param joinField
+        *            a field containing object to apply the condition
+        * 
+        * @param aCondition
+        *            a search condition
+        * @return a list of objects filtered according to the given criteria
+        */
+       public List<T> getFilteredList(final String joinField,
+                       final Criterion aCondition) {
+               return getFilteredList(joinField, aCondition, (Order) null);
+       }
+
+       /**
+        * Retrieve a list of objects which were previously persisted to the database using the given criteria.
+        * 
+        * @param joinField
+        *            a field containing object to apply the condition
+        * 
+        * @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(Constants.UNCHECKED)
+       public List<T> getFilteredList(final String joinField,
+                       final Criterion aCondition, final Order... anOrder) {
+               Criteria aCriteria = getSession().createCriteria(getType());
+               for (Order order : anOrder) {
+                       if (order != null) {
+                               aCriteria.addOrder(order);
+                       }
+               }
+               return aCriteria.createCriteria(joinField).add(aCondition).list();
+       }
+
        /**
         * Retrieve a list of objects which were previously persisted to the database using the given criteria.
         * 
@@ -156,7 +239,7 @@ public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
         * @return a list of objects filtered according to the given criteria
         */
        public List<T> getFilteredList(final Properties andParams) {
-               return getFilteredList(andParams);
+               return getFilteredList(andParams, (Order) null);
        }
 
        /**
@@ -168,10 +251,12 @@ public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
         *            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
         */
-       public List<T> getFilteredList(final Properties andParams, final Order ... anOrder) {
+       public List<T> getFilteredList(final Properties andParams,
+                       final Order... anOrder) {
                Criterion aCondition = null;
-               for (String aName: andParams.stringPropertyNames()) {
-                       aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
+               for (String aName : andParams.stringPropertyNames()) {
+                       aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
+                                       andParams.get(aName)));
                }
                return getFilteredList(aCondition, anOrder);
        }
@@ -186,40 +271,102 @@ public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
                getSession().update(transientObject);
        }
 
+       /**
+        * Refresh a persistent object.
+        * 
+        * @param transientObject
+        *            transient instance of the object to refresh
+        * @see Session#refresh(Object)
+        */
+       public void refresh(final T transientObject) {
+               getSession().refresh(transientObject);
+       }
+
+       /**
+        * Load a persistent object.
+        * 
+        * @param transientObject
+        *            transient instance of the object to load
+        * @param id
+        *            object primary key
+        * @see Session#load(Object, Serializable)
+        */
+       public void load(final T transientObject, final PK id) {
+               getSession().load(transientObject, id);
+       }
+
+       /**
+        * Lock a persistent object.
+        * 
+        * @param transientObject
+        *            transient instance of the object to lock
+        * @param lockOptions
+        *            lock options
+        * @see Session#refresh(Object, LockOptions)
+        */
+       public void refresh(final T transientObject, final LockOptions lockOptions) {
+               getSession().refresh(transientObject, lockOptions);
+       }
+
        /**
         * Remove an object from persistent storage in the database.
         * 
         * @param persistentObject
         *            a persistent object to delete from the database
+        * @see Session#delete(Object)
         */
        public void delete(final T persistentObject) {
                getSession().delete(persistentObject);
        }
 
        /**
-        * Makes detached object persistent.
+        * Make a transient instance persistent. This operation cascades to <BR>
+        * associated instances if the association is mapped with cascade="persist".
         * 
         * @param transientObject
         *            transient instance of the object to be made persistent
+        * @see Session#persist(Object)
         */
        public void persist(final T transientObject) {
                getSession().persist(transientObject);
        }
 
        /**
-        * Merge detached object with persistent data.
+        * Copy the state of the given object onto the persistent object with the <BR>
+        * same identifier. If there is no persistent instance currently associated<BR>
+        * with the session, it will be loaded. Return the persistent instance. If <BR>
+        * the given instance is unsaved, save a copy of and return it as a newly <BR>
+        * persistent instance. The given instance does not become associated with <BR>
+        * the session. This operation cascades to associated instances if the <BR>
+        * association is mapped with cascade="merge".
         * 
         * @param transientObject
         *            transient instance of the object to be merged with persistent data
         * @return merged persistent object
+        * @see Session#merge(Object)
         */
-       @SuppressWarnings(UNCHECKED)
+       @SuppressWarnings(Constants.UNCHECKED)
        public T merge(final T transientObject) {
                return (T) getSession().merge(transientObject);
        }
 
+       /**
+        * Remove this instance from the session cache. Changes to the instance will<BR>
+        * not be synchronized with the database. This operation cascades to <BR>
+        * associated instances if the association is mapped with cascade="evict".
+        * 
+        * @param persistentObject
+        *            the object to be removed from session cache
+        * @see Session#evict(Object)
+        */
+       public void evict(final T persistentObject) {
+               getSession().evict(persistentObject);
+       }
+
        /**
         * Synchronize the session data with the database.
+        * 
+        * @see Session#flush()
         */
        public void flush() {
                getSession().flush();