Salome HOME
88420adb4de1923016879dc1b3f9fe4be87de114
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / dao / kernel / GenericDAOImpl.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
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 import java.util.Properties;
15
16 import org.hibernate.Criteria;
17 import org.hibernate.criterion.Criterion;
18 import org.hibernate.criterion.Order;
19 import org.hibernate.criterion.Restrictions;
20 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
21
22 /**
23  * Generic DAO implementation.
24  * 
25  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
26  * 
27  * @param <T>
28  *            Persistent object class
29  * @param <PK>
30  *            Primary key class
31  */
32 public abstract class GenericDAOImpl<T, PK extends Serializable> extends
33                 HibernateDaoSupport implements GenericDAO<T, PK> {
34         /**
35          * Persist the newInstance object into database.
36          * 
37          * @param newInstance
38          *            new object as a transient instance
39          * @return new primary key for the created persistent object
40          */
41         @SuppressWarnings("unchecked")
42         public PK create(T newInstance) {
43                 return (PK) getSession().save(newInstance);
44         }
45
46         /**
47          * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
48          * 
49          * @param id
50          *            primary key of an object to read
51          * @return an object found by the given key
52          */
53         @SuppressWarnings("unchecked")
54         public T get(PK id) {
55                 return (T) getSession().get(getType(), id);
56         }
57
58         /**
59          * Retrieve an object that was previously persisted to the database using the given criteria.
60          * 
61          * @param aCondition
62          *            a search condition
63          * @return an object found according to the given criteria
64          */
65         @SuppressWarnings("unchecked")
66         public T findByCriteria(Criterion aCondition) {
67                 return (T) getSession().createCriteria(getType()).add(aCondition)
68                                 .uniqueResult();
69         }
70
71         /**
72          * Retrieve an object that was previously persisted to the database using the given criteria.
73          * 
74          * @param andParams
75          *            a properties values to filter with AND condition
76          * @return an object found according to the given criteria
77          */
78         public T findByCriteria(Properties andParams) {
79                 Criterion aCondition = null;
80                 for (String aName: andParams.stringPropertyNames()) {
81                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
82                 }
83                 return findByCriteria(aCondition);
84         }
85
86         /**
87          * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
88          * 
89          * @return a list of all objects of the considered type T
90          */
91         @SuppressWarnings("unchecked")
92         public List<T> getAll() {
93                 return getSession().createCriteria(getType()).list();
94         }
95
96         /**
97          * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
98          * 
99          * @param anOrder
100          *            a result list order. Null is ignored and in such case the result list is unordered.
101          * @return an ordered list of all objects of the considered type T
102          */
103         @SuppressWarnings("unchecked")
104         public List<T> getAll(Order ... anOrder) {
105                 Criteria aCriteria = getSession().createCriteria(getType());
106                 for (Order order : anOrder) {
107                         if (anOrder != null) {
108                                 aCriteria.addOrder(order);
109                         }
110                 }
111                 return aCriteria.list();
112         }
113
114         /**
115          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
116          * 
117          * @param aCondition
118          *            a search condition
119          * @return a list of objects filtered according to the given criteria
120          */
121         @SuppressWarnings("unchecked")
122         public List<T> getFilteredList(Criterion aCondition) {
123                 return getSession().createCriteria(getType()).add(aCondition).list();
124         }
125
126         /**
127          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
128          * 
129          * @param aCondition
130          *            a search condition
131          * @param anOrder
132          *            a result list order. Null is ignored and in such case the result list is unordered.
133          * @return a list of objects filtered according to the given criteria
134          */
135         @SuppressWarnings("unchecked")
136         public List<T> getFilteredList(Criterion aCondition, Order ... anOrder) {
137                 Criteria aCriteria = getSession().createCriteria(getType()).add(
138                                 aCondition);
139                 for (Order order : anOrder) {
140                         if (anOrder != null) {
141                                 aCriteria.addOrder(order);
142                         }
143                 }
144                 return aCriteria.list();
145         }
146
147         /**
148          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
149          * 
150          * @param andParams
151          *            a properties values to filter with AND condition
152          * @return a list of objects filtered according to the given criteria
153          */
154         public List<T> getFilteredList(Properties andParams) {
155                 return getFilteredList(andParams);
156         }
157
158         /**
159          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
160          * 
161          * @param andParams
162          *            a properties values to filter with AND condition
163          * @param anOrder
164          *            a result list order. Null is ignored and in such case the result list is unordered.
165          * @return a list of objects filtered according to the given criteria
166          */
167         public List<T> getFilteredList(Properties andParams, Order ... anOrder) {
168                 Criterion aCondition = null;
169                 for (String aName: andParams.stringPropertyNames()) {
170                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
171                 }
172                 return getFilteredList(aCondition, anOrder);
173         }
174
175         /**
176          * Save changes made to a persistent object.
177          * 
178          * @param transientObject
179          *            transient instance of the object to update
180          */
181         public void update(T transientObject) {
182                 getSession().update(transientObject);
183         }
184
185         /**
186          * Remove an object from persistent storage in the database.
187          * 
188          * @param persistentObject
189          *            a persistent object to delete from the database
190          */
191         public void delete(T persistentObject) {
192                 getSession().delete(persistentObject);
193         }
194
195         /**
196          * Makes detached object persistent.
197          * 
198          * @param transientObject
199          *            transient instance of the object to be made persistent
200          */
201         public void persist(T transientObject) {
202                 getSession().persist(transientObject);
203         }
204
205         /**
206          * Merge detached object with persistent data.
207          * 
208          * @param transientObject
209          *            transient instance of the object to be merged with persistent data
210          * @return merged persistent object
211          */
212         @SuppressWarnings("unchecked")
213         public T merge(T transientObject) {
214                 return (T) getSession().merge(transientObject);
215         }
216
217         /**
218          * Get persistent object type.
219          * 
220          * @return Persistent object class to be processed by this DAO
221          */
222         abstract protected Class<T> getType();
223 }