Salome HOME
DAO's method getFilteredList is improved so that it is possible to do subqueries...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / dao / kernel / AbstractGenericDAOImpl.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 AbstractGenericDAOImpl<T, PK extends Serializable>
33                 extends HibernateDaoSupport implements GenericDAO<T, PK> {
34         /**
35          * Unchecked warning specification.
36          */
37         private static final String UNCHECKED = "unchecked";
38
39         /**
40          * Persist the newInstance object into database.
41          * 
42          * @param newInstance
43          *            new object as a transient instance
44          * @return new primary key for the created persistent object
45          */
46         @SuppressWarnings(UNCHECKED)
47         public PK create(final T newInstance) {
48                 return (PK) getSession().save(newInstance);
49         }
50
51         /**
52          * Persist the newInstance object into database.
53          * 
54          * @param newInstance
55          *            new object as a transient instance
56          */
57         @SuppressWarnings(UNCHECKED)
58         public void saveOrUpdate(final T newInstance) {
59                 getSession().saveOrUpdate(newInstance);
60         }
61
62         /**
63          * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
64          * 
65          * @param id
66          *            primary key of an object to read
67          * @return an object found by the given key
68          */
69         @SuppressWarnings(UNCHECKED)
70         public T get(final PK id) {
71                 return (T) getSession().get(getType(), id);
72         }
73
74         /**
75          * Retrieve an object that was previously persisted to the database using the given criteria.
76          * 
77          * @param aCondition
78          *            a search condition
79          * @return an object found according to the given criteria
80          */
81         @SuppressWarnings(UNCHECKED)
82         public T findByCriteria(final Criterion aCondition) {
83                 return (T) getSession().createCriteria(getType()).add(aCondition)
84                                 .uniqueResult();
85         }
86
87         /**
88          * Retrieve an object that was previously persisted to the database using the given criteria.
89          * 
90          * @param andParams
91          *            a properties values to filter with AND condition
92          * @return an object found according to the given criteria
93          */
94         public T findByCriteria(final Properties andParams) {
95                 Criterion aCondition = null;
96                 for (String aName : andParams.stringPropertyNames()) {
97                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
98                                         andParams.get(aName)));
99                 }
100                 return findByCriteria(aCondition);
101         }
102
103         /**
104          * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
105          * 
106          * @return a list of all objects of the considered type T
107          */
108         @SuppressWarnings(UNCHECKED)
109         public List<T> getAll() {
110                 return getSession().createCriteria(getType()).list();
111         }
112
113         /**
114          * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
115          * 
116          * @param anOrder
117          *            a result list order. Null is ignored and in such case the result list is unordered.
118          * @return an ordered list of all objects of the considered type T
119          */
120         @SuppressWarnings(UNCHECKED)
121         public List<T> getAll(final Order... anOrder) {
122                 Criteria aCriteria = getSession().createCriteria(getType());
123                 for (Order order : anOrder) {
124                         if (order != null) {
125                                 aCriteria.addOrder(order);
126                         }
127                 }
128                 return aCriteria.list();
129         }
130
131         /**
132          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
133          * 
134          * @param aCondition
135          *            a search condition
136          * @return a list of objects filtered according to the given criteria
137          */
138         @SuppressWarnings(UNCHECKED)
139         public List<T> getFilteredList(final Criterion aCondition) {
140                 return getSession().createCriteria(getType()).add(aCondition).list();
141         }
142
143         /**
144          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
145          * 
146          * @param aCondition
147          *            a search condition
148          * @param anOrder
149          *            a result list order. Null is ignored and in such case the result list is unordered.
150          * @return a list of objects filtered according to the given criteria
151          */
152         @SuppressWarnings(UNCHECKED)
153         public List<T> getFilteredList(final Criterion aCondition,
154                         final Order... anOrder) {
155                 Criteria aCriteria = getSession().createCriteria(getType()).add(
156                                 aCondition);
157                 for (Order order : anOrder) {
158                         if (order != null) {
159                                 aCriteria.addOrder(order);
160                         }
161                 }
162                 return aCriteria.list();
163         }
164
165         /**
166          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
167          * 
168          * @param joinField
169          *            a field containing object to apply the condition
170          * 
171          * @param aCondition
172          *            a search condition
173          * @return a list of objects filtered according to the given criteria
174          */
175         public List<T> getFilteredList(final String joinField,
176                         final Criterion aCondition) {
177                 return getFilteredList(joinField, aCondition, (Order) null);
178         }
179
180         /**
181          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
182          * 
183          * @param joinField
184          *            a field containing object to apply the condition
185          * 
186          * @param aCondition
187          *            a search condition
188          * @param anOrder
189          *            a result list order. Null is ignored and in such case the result list is unordered.
190          * @return a list of objects filtered according to the given criteria
191          */
192         @SuppressWarnings(UNCHECKED)
193         public List<T> getFilteredList(final String joinField,
194                         final Criterion aCondition, final Order... anOrder) {
195                 Criteria aCriteria = getSession().createCriteria(getType());
196                 for (Order order : anOrder) {
197                         if (order != null) {
198                                 aCriteria.addOrder(order);
199                         }
200                 }
201                 return aCriteria.createCriteria(joinField).add(aCondition).list();
202         }
203
204         /**
205          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
206          * 
207          * @param andParams
208          *            a properties values to filter with AND condition
209          * @return a list of objects filtered according to the given criteria
210          */
211         public List<T> getFilteredList(final Properties andParams) {
212                 return getFilteredList(andParams, (Order) null);
213         }
214
215         /**
216          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
217          * 
218          * @param andParams
219          *            a properties values to filter with AND condition
220          * @param anOrder
221          *            a result list order. Null is ignored and in such case the result list is unordered.
222          * @return a list of objects filtered according to the given criteria
223          */
224         public List<T> getFilteredList(final Properties andParams,
225                         final Order... anOrder) {
226                 Criterion aCondition = null;
227                 for (String aName : andParams.stringPropertyNames()) {
228                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
229                                         andParams.get(aName)));
230                 }
231                 return getFilteredList(aCondition, anOrder);
232         }
233
234         /**
235          * Save changes made to a persistent object.
236          * 
237          * @param transientObject
238          *            transient instance of the object to update
239          */
240         public void update(final T transientObject) {
241                 getSession().update(transientObject);
242         }
243
244         /**
245          * Remove an object from persistent storage in the database.
246          * 
247          * @param persistentObject
248          *            a persistent object to delete from the database
249          */
250         public void delete(final T persistentObject) {
251                 getSession().delete(persistentObject);
252         }
253
254         /**
255          * Makes detached object persistent.
256          * 
257          * @param transientObject
258          *            transient instance of the object to be made persistent
259          */
260         public void persist(final T transientObject) {
261                 getSession().persist(transientObject);
262         }
263
264         /**
265          * Merge detached object with persistent data.
266          * 
267          * @param transientObject
268          *            transient instance of the object to be merged with persistent data
269          * @return merged persistent object
270          */
271         @SuppressWarnings(UNCHECKED)
272         public T merge(final T transientObject) {
273                 return (T) getSession().merge(transientObject);
274         }
275
276         /**
277          * Synchronize the session data with the database.
278          */
279         public void flush() {
280                 getSession().flush();
281         }
282
283         /**
284          * Get persistent object type.
285          * 
286          * @return Persistent object class to be processed by this DAO
287          */
288         abstract protected Class<T> getType();
289 }