Salome HOME
Simulation context type selection is fixed in study search. Lucene is no more used...
[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.LockOptions;
18 import org.hibernate.criterion.Criterion;
19 import org.hibernate.criterion.DetachedCriteria;
20 import org.hibernate.criterion.Order;
21 import org.hibernate.criterion.Restrictions;
22 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
23
24 /**
25  * Generic DAO implementation.
26  * 
27  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
28  * 
29  * @param <T>
30  *            Persistent object class
31  * @param <PK>
32  *            Primary key class
33  */
34 public abstract class AbstractGenericDAOImpl<T, PK extends Serializable>
35                 extends HibernateDaoSupport implements GenericDAO<T, PK> {
36         /**
37          * Unchecked warning specification.
38          */
39         private static final String UNCHECKED = "unchecked";
40
41         /**
42          * Persist the newInstance object into database.
43          * 
44          * @param newInstance
45          *            new object as a transient instance
46          * @return new primary key for the created persistent object
47          */
48         @SuppressWarnings(UNCHECKED)
49         public PK create(final T newInstance) {
50                 return (PK) getSession().save(newInstance);
51         }
52
53         /**
54          * Persist the newInstance object into database.
55          * 
56          * @param newInstance
57          *            new object as a transient instance
58          */
59         @SuppressWarnings(UNCHECKED)
60         public void saveOrUpdate(final T newInstance) {
61                 getSession().saveOrUpdate(newInstance);
62         }
63
64         /**
65          * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
66          * 
67          * @param id
68          *            primary key of an object to read
69          * @return an object found by the given key
70          */
71         @SuppressWarnings(UNCHECKED)
72         public T get(final PK id) {
73                 return (T) getSession().get(getType(), id);
74         }
75
76         /**
77          * Retrieve an object that was previously persisted to the database using the given criteria.
78          * 
79          * @param aCondition
80          *            a search condition
81          * @return an object found according to the given criteria
82          */
83         @SuppressWarnings(UNCHECKED)
84         public T findByCriteria(final Criterion aCondition) {
85                 return (T) getSession().createCriteria(getType()).add(aCondition)
86                                 .uniqueResult();
87         }
88
89         /**
90          * Retrieve an object that was previously persisted to the database using the given criteria.
91          * 
92          * @param andParams
93          *            a properties values to filter with AND condition
94          * @return an object found according to the given criteria
95          */
96         public T findByCriteria(final Properties andParams) {
97                 Criterion aCondition = null;
98                 for (String aName : andParams.stringPropertyNames()) {
99                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
100                                         andParams.get(aName)));
101                 }
102                 return findByCriteria(aCondition);
103         }
104
105         /**
106          * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
107          * 
108          * @return a list of all objects of the considered type T
109          */
110         @SuppressWarnings(UNCHECKED)
111         public List<T> getAll() {
112                 return getSession().createCriteria(getType()).list();
113         }
114
115         /**
116          * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
117          * 
118          * @param anOrder
119          *            a result list order. Null is ignored and in such case the result list is unordered.
120          * @return an ordered list of all objects of the considered type T
121          */
122         @SuppressWarnings(UNCHECKED)
123         public List<T> getAll(final Order... anOrder) {
124                 Criteria aCriteria = getSession().createCriteria(getType());
125                 for (Order order : anOrder) {
126                         if (order != null) {
127                                 aCriteria.addOrder(order);
128                         }
129                 }
130                 return aCriteria.list();
131         }
132
133         /**
134          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
135          * 
136          * @param aDetachedCriteria
137          *            search criteria
138          * @return a list of objects filtered according to the given criteria
139          */
140         @SuppressWarnings(UNCHECKED)
141         public List<T> getFilteredList(final DetachedCriteria aDetachedCriteria) {
142                 return aDetachedCriteria.getExecutableCriteria(getSession()).list();
143         }
144
145         /**
146          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
147          * 
148          * @param aCondition
149          *            a search condition
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                 return getSession().createCriteria(getType()).add(aCondition).list();
155         }
156
157         /**
158          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
159          * 
160          * @param aCondition
161          *            a search condition
162          * @param anOrder
163          *            a result list order. Null is ignored and in such case the result list is unordered.
164          * @return a list of objects filtered according to the given criteria
165          */
166         @SuppressWarnings(UNCHECKED)
167         public List<T> getFilteredList(final Criterion aCondition,
168                         final Order... anOrder) {
169                 Criteria aCriteria = getSession().createCriteria(getType()).add(
170                                 aCondition);
171                 for (Order order : anOrder) {
172                         if (order != null) {
173                                 aCriteria.addOrder(order);
174                         }
175                 }
176                 return aCriteria.list();
177         }
178
179         /**
180          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
181          * 
182          * @param joinField
183          *            a field containing object to apply the condition
184          * 
185          * @param aCondition
186          *            a search condition
187          * @return a list of objects filtered according to the given criteria
188          */
189         public List<T> getFilteredList(final String joinField,
190                         final Criterion aCondition) {
191                 return getFilteredList(joinField, aCondition, (Order) null);
192         }
193
194         /**
195          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
196          * 
197          * @param joinField
198          *            a field containing object to apply the condition
199          * 
200          * @param aCondition
201          *            a search condition
202          * @param anOrder
203          *            a result list order. Null is ignored and in such case the result list is unordered.
204          * @return a list of objects filtered according to the given criteria
205          */
206         @SuppressWarnings(UNCHECKED)
207         public List<T> getFilteredList(final String joinField,
208                         final Criterion aCondition, final Order... anOrder) {
209                 Criteria aCriteria = getSession().createCriteria(getType());
210                 for (Order order : anOrder) {
211                         if (order != null) {
212                                 aCriteria.addOrder(order);
213                         }
214                 }
215                 return aCriteria.createCriteria(joinField).add(aCondition).list();
216         }
217
218         /**
219          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
220          * 
221          * @param andParams
222          *            a properties values to filter with AND condition
223          * @return a list of objects filtered according to the given criteria
224          */
225         public List<T> getFilteredList(final Properties andParams) {
226                 return getFilteredList(andParams, (Order) null);
227         }
228
229         /**
230          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
231          * 
232          * @param andParams
233          *            a properties values to filter with AND condition
234          * @param anOrder
235          *            a result list order. Null is ignored and in such case the result list is unordered.
236          * @return a list of objects filtered according to the given criteria
237          */
238         public List<T> getFilteredList(final Properties andParams,
239                         final Order... anOrder) {
240                 Criterion aCondition = null;
241                 for (String aName : andParams.stringPropertyNames()) {
242                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
243                                         andParams.get(aName)));
244                 }
245                 return getFilteredList(aCondition, anOrder);
246         }
247
248         /**
249          * Save changes made to a persistent object.
250          * 
251          * @param transientObject
252          *            transient instance of the object to update
253          */
254         public void update(final T transientObject) {
255                 getSession().update(transientObject);
256         }
257
258         /**
259          * Refresh a persistent object.
260          * 
261          * @param transientObject
262          *            transient instance of the object to refresh
263          */
264         public void refresh(final T transientObject) {
265                 getSession().refresh(transientObject);
266         }
267
268         /**
269          * Load a persistent object.
270          * 
271          * @param transientObject
272          *            transient instance of the object to load
273          * @param id
274          *            object primary key
275          */
276         public void load(final T transientObject, final PK id) {
277                 getSession().load(transientObject, id);
278         }
279
280         /**
281          * Lock a persistent object.
282          * 
283          * @param transientObject
284          *            transient instance of the object to lock
285          * @param lockOptions
286          *            lock options
287          */
288         public void refresh(final T transientObject, final LockOptions lockOptions) {
289                 getSession().refresh(transientObject, lockOptions);
290         }
291
292         /**
293          * Remove an object from persistent storage in the database.
294          * 
295          * @param persistentObject
296          *            a persistent object to delete from the database
297          */
298         public void delete(final T persistentObject) {
299                 getSession().delete(persistentObject);
300         }
301
302         /**
303          * Makes detached object persistent.
304          * 
305          * @param transientObject
306          *            transient instance of the object to be made persistent
307          */
308         public void persist(final T transientObject) {
309                 getSession().persist(transientObject);
310         }
311
312         /**
313          * Merge detached object with persistent data.
314          * 
315          * @param transientObject
316          *            transient instance of the object to be merged with persistent data
317          * @return merged persistent object
318          */
319         @SuppressWarnings(UNCHECKED)
320         public T merge(final T transientObject) {
321                 return (T) getSession().merge(transientObject);
322         }
323
324         /**
325          * Synchronize the session data with the database.
326          */
327         public void flush() {
328                 getSession().flush();
329         }
330
331         /**
332          * Get persistent object type.
333          * 
334          * @return Persistent object class to be processed by this DAO
335          */
336         abstract protected Class<T> getType();
337 }