Salome HOME
d76653b7be14998d2cca2b9166f274fbcde0f171
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / dao / kernel / GenericDAOImpl.java
1 /*****************************************************************************
2  * Company         EURIWARE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   08.10.2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package org.splat.dal.dao.kernel;
11
12 import java.io.Serializable;
13 import java.util.List;
14
15 import org.hibernate.Criteria;
16 import org.hibernate.criterion.Criterion;
17 import org.hibernate.criterion.Order;
18 import org.hibernate.criterion.Restrictions;
19 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
20
21 /**
22  * Generic DAO implementation.
23  * 
24  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
25  * 
26  * @param <T>
27  *            Persistent object class
28  * @param <PK>
29  *            Primary key class
30  */
31 public abstract class GenericDAOImpl<T, PK extends Serializable> extends
32                 HibernateDaoSupport implements GenericDAO<T, PK> {
33         /**
34          * Persist the newInstance object into database.
35          * 
36          * @param newInstance
37          *            new object as a transient instance
38          * @return new primary key for the created persistent object
39          */
40         @SuppressWarnings("unchecked")
41         public PK create(T newInstance) {
42                 return (PK) getSession().save(newInstance);
43         }
44
45         /**
46          * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
47          * 
48          * @param id
49          *            primary key of an object to read
50          * @return an object found by the given key
51          */
52         @SuppressWarnings("unchecked")
53         public T get(PK id) {
54                 return (T) getSession().get(getType(), id);
55         }
56
57         /**
58          * Retrieve an object that was previously persisted to the database using the given criteria.
59          * 
60          * @param aCondition
61          *            a search condition
62          * @return an object found according to the given criteria
63          */
64         @SuppressWarnings("unchecked")
65         public T findByCriteria(Criterion aCondition) {
66                 return (T) getSession().createCriteria(getType()).add(aCondition)
67                                 .uniqueResult();
68         }
69
70         /**
71          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
72          * 
73          * @param aCondition
74          *            a search condition
75          * @return a list of objects filtered according to the given criteria
76          */
77         @SuppressWarnings("unchecked")
78         public List<T> getFilteredList(Criterion aCondition) {
79                 return getSession().createCriteria(getType()).add(aCondition).list();
80         }
81
82         /**
83          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
84          * 
85          * @param aCondition
86          *            a search condition
87          * @param anOrder
88          *            a result list order. Null is ignored and in such case the result list is unordered.
89          * @return a list of objects filtered according to the given criteria
90          */
91         @SuppressWarnings("unchecked")
92         public List<T> getFilteredList(Criterion aCondition, Order anOrder) {
93                 Criteria aCriteria = getSession().createCriteria(getType()).add(
94                                 aCondition);
95                 if (anOrder != null) {
96                         aCriteria.addOrder(anOrder);
97                 }
98                 return aCriteria.list();
99         }
100
101         /**
102          * Save changes made to a persistent object.
103          * 
104          * @param transientObject
105          *            transient instance of the object to update
106          */
107         public void update(T transientObject) {
108                 getSession().update(transientObject);
109         }
110
111         /**
112          * Remove an object from persistent storage in the database.
113          * 
114          * @param persistentObject
115          *            a persistent object to delete from the database
116          */
117         public void delete(T persistentObject) {
118                 getSession().delete(persistentObject);
119         }
120
121         /**
122          * Get persistent object type.
123          * 
124          * @return Persistent object class to be processed by this DAO
125          */
126         abstract protected Class<T> getType();
127 }