Salome HOME
296df7384492598f41d8dcee70e50c1e9936f908
[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.Session;
19 import org.hibernate.criterion.Criterion;
20 import org.hibernate.criterion.DetachedCriteria;
21 import org.hibernate.criterion.Order;
22 import org.hibernate.criterion.Restrictions;
23 import org.splat.common.Constants;
24 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
25
26 /**
27  * Generic DAO implementation.
28  * 
29  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
30  * 
31  * @param <T>
32  *            Persistent object class
33  * @param <PK>
34  *            Primary key class
35  */
36 public abstract class AbstractGenericDAOImpl<T, PK extends Serializable>
37                 extends HibernateDaoSupport implements GenericDAO<T, PK> {
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          * @see Session#save(Object)
46          */
47         @SuppressWarnings(Constants.UNCHECKED)
48         public PK create(final T newInstance) {
49                 return (PK) getSession().save(newInstance);
50         }
51
52         /**
53          * Persist the newInstance object into database.
54          * 
55          * @param newInstance
56          *            new object as a transient instance
57          * @see Session#saveOrUpdate(Object)
58          */
59         @SuppressWarnings(Constants.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          * @see Session#get(Class, Serializable)
71          */
72         @SuppressWarnings(Constants.UNCHECKED)
73         public T get(final PK id) {
74                 return (T) getSession().get(getType(), id);
75         }
76
77         /**
78          * Retrieve an object that was previously persisted to the database using the given criteria.
79          * 
80          * @param aCondition
81          *            a search condition
82          * @return an object found according to the given criteria
83          */
84         @SuppressWarnings(Constants.UNCHECKED)
85         public T findByCriteria(final Criterion aCondition) {
86                 return (T) getSession().createCriteria(getType()).add(aCondition)
87                                 .uniqueResult();
88         }
89
90         /**
91          * Retrieve an object that was previously persisted to the database using the given criteria.
92          * 
93          * @param andParams
94          *            a properties values to filter with AND condition
95          * @return an object found according to the given criteria
96          */
97         public T findByCriteria(final Properties andParams) {
98                 Criterion aCondition = null;
99                 for (String aName : andParams.stringPropertyNames()) {
100                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
101                                         andParams.get(aName)));
102                 }
103                 return findByCriteria(aCondition);
104         }
105
106         /**
107          * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
108          * 
109          * @return a list of all objects of the considered type T
110          */
111         @SuppressWarnings(Constants.UNCHECKED)
112         public List<T> getAll() {
113                 return getSession().createCriteria(getType()).list();
114         }
115
116         /**
117          * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
118          * 
119          * @param anOrder
120          *            a result list order. Null is ignored and in such case the result list is unordered.
121          * @return an ordered list of all objects of the considered type T
122          */
123         @SuppressWarnings(Constants.UNCHECKED)
124         public List<T> getAll(final Order... anOrder) {
125                 Criteria aCriteria = getSession().createCriteria(getType());
126                 for (Order order : anOrder) {
127                         if (order != null) {
128                                 aCriteria.addOrder(order);
129                         }
130                 }
131                 return aCriteria.list();
132         }
133
134         /**
135          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
136          * 
137          * @param aDetachedCriteria
138          *            search criteria
139          * @return a list of objects filtered according to the given criteria
140          */
141         @SuppressWarnings(Constants.UNCHECKED)
142         public List<T> getFilteredList(final DetachedCriteria aDetachedCriteria) {
143                 return aDetachedCriteria.getExecutableCriteria(getSession()).list();
144         }
145
146         /**
147          * Retrieve a list of DTO objects using the given criteria.
148          * 
149          * @param <DTO>
150          *            the class of returned DTOs
151          * @param aDetachedCriteria
152          *            search criteria
153          * @return a list of DTO objects filtered according to the given criteria
154          */
155         @SuppressWarnings(Constants.UNCHECKED)
156         public <DTO> List<DTO> getFilteredDTOList(
157                         final DetachedCriteria aDetachedCriteria) {
158                 return aDetachedCriteria.getExecutableCriteria(getSession()).list();
159         }
160
161         /**
162          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
163          * 
164          * @param aCondition
165          *            a search condition
166          * @return a list of objects filtered according to the given criteria
167          */
168         @SuppressWarnings(Constants.UNCHECKED)
169         public List<T> getFilteredList(final Criterion aCondition) {
170                 return getSession().createCriteria(getType()).add(aCondition).list();
171         }
172
173         /**
174          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
175          * 
176          * @param aCondition
177          *            a search condition
178          * @param anOrder
179          *            a result list order. Null is ignored and in such case the result list is unordered.
180          * @return a list of objects filtered according to the given criteria
181          */
182         @SuppressWarnings(Constants.UNCHECKED)
183         public List<T> getFilteredList(final Criterion aCondition,
184                         final Order... anOrder) {
185                 Criteria aCriteria = getSession().createCriteria(getType()).add(
186                                 aCondition);
187                 for (Order order : anOrder) {
188                         if (order != null) {
189                                 aCriteria.addOrder(order);
190                         }
191                 }
192                 return aCriteria.list();
193         }
194
195         /**
196          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
197          * 
198          * @param joinField
199          *            a field containing object to apply the condition
200          * 
201          * @param aCondition
202          *            a search condition
203          * @return a list of objects filtered according to the given criteria
204          */
205         public List<T> getFilteredList(final String joinField,
206                         final Criterion aCondition) {
207                 return getFilteredList(joinField, aCondition, (Order) null);
208         }
209
210         /**
211          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
212          * 
213          * @param joinField
214          *            a field containing object to apply the condition
215          * 
216          * @param aCondition
217          *            a search condition
218          * @param anOrder
219          *            a result list order. Null is ignored and in such case the result list is unordered.
220          * @return a list of objects filtered according to the given criteria
221          */
222         @SuppressWarnings(Constants.UNCHECKED)
223         public List<T> getFilteredList(final String joinField,
224                         final Criterion aCondition, final Order... anOrder) {
225                 Criteria aCriteria = getSession().createCriteria(getType());
226                 for (Order order : anOrder) {
227                         if (order != null) {
228                                 aCriteria.addOrder(order);
229                         }
230                 }
231                 return aCriteria.createCriteria(joinField).add(aCondition).list();
232         }
233
234         /**
235          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
236          * 
237          * @param andParams
238          *            a properties values to filter with AND condition
239          * @return a list of objects filtered according to the given criteria
240          */
241         public List<T> getFilteredList(final Properties andParams) {
242                 return getFilteredList(andParams, (Order) null);
243         }
244
245         /**
246          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
247          * 
248          * @param andParams
249          *            a properties values to filter with AND condition
250          * @param anOrder
251          *            a result list order. Null is ignored and in such case the result list is unordered.
252          * @return a list of objects filtered according to the given criteria
253          */
254         public List<T> getFilteredList(final Properties andParams,
255                         final Order... anOrder) {
256                 Criterion aCondition = null;
257                 for (String aName : andParams.stringPropertyNames()) {
258                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
259                                         andParams.get(aName)));
260                 }
261                 return getFilteredList(aCondition, anOrder);
262         }
263
264         /**
265          * Save changes made to a persistent object.
266          * 
267          * @param transientObject
268          *            transient instance of the object to update
269          */
270         public void update(final T transientObject) {
271                 getSession().update(transientObject);
272         }
273
274         /**
275          * Refresh a persistent object.
276          * 
277          * @param transientObject
278          *            transient instance of the object to refresh
279          * @see Session#refresh(Object)
280          */
281         public void refresh(final T transientObject) {
282                 getSession().refresh(transientObject);
283         }
284
285         /**
286          * Load a persistent object.
287          * 
288          * @param transientObject
289          *            transient instance of the object to load
290          * @param id
291          *            object primary key
292          * @see Session#load(Object, Serializable)
293          */
294         public void load(final T transientObject, final PK id) {
295                 getSession().load(transientObject, id);
296         }
297
298         /**
299          * Lock a persistent object.
300          * 
301          * @param transientObject
302          *            transient instance of the object to lock
303          * @param lockOptions
304          *            lock options
305          * @see Session#refresh(Object, LockOptions)
306          */
307         public void refresh(final T transientObject, final LockOptions lockOptions) {
308                 getSession().refresh(transientObject, lockOptions);
309         }
310
311         /**
312          * Remove an object from persistent storage in the database.
313          * 
314          * @param persistentObject
315          *            a persistent object to delete from the database
316          * @see Session#delete(Object)
317          */
318         public void delete(final T persistentObject) {
319                 getSession().delete(persistentObject);
320         }
321
322         /**
323          * Make a transient instance persistent. This operation cascades to <BR>
324          * associated instances if the association is mapped with cascade="persist".
325          * 
326          * @param transientObject
327          *            transient instance of the object to be made persistent
328          * @see Session#persist(Object)
329          */
330         public void persist(final T transientObject) {
331                 getSession().persist(transientObject);
332         }
333
334         /**
335          * Copy the state of the given object onto the persistent object with the <BR>
336          * same identifier. If there is no persistent instance currently associated<BR>
337          * with the session, it will be loaded. Return the persistent instance. If <BR>
338          * the given instance is unsaved, save a copy of and return it as a newly <BR>
339          * persistent instance. The given instance does not become associated with <BR>
340          * the session. This operation cascades to associated instances if the <BR>
341          * association is mapped with cascade="merge".
342          * 
343          * @param transientObject
344          *            transient instance of the object to be merged with persistent data
345          * @return merged persistent object
346          * @see Session#merge(Object)
347          */
348         @SuppressWarnings(Constants.UNCHECKED)
349         public T merge(final T transientObject) {
350                 return (T) getSession().merge(transientObject);
351         }
352
353         /**
354          * Remove this instance from the session cache. Changes to the instance will<BR>
355          * not be synchronized with the database. This operation cascades to <BR>
356          * associated instances if the association is mapped with cascade="evict".
357          * 
358          * @param persistentObject
359          *            the object to be removed from session cache
360          * @see Session#evict(Object)
361          */
362         public void evict(final T persistentObject) {
363                 getSession().evict(persistentObject);
364         }
365
366         /**
367          * Synchronize the session data with the database.
368          * 
369          * @see Session#flush()
370          */
371         public void flush() {
372                 getSession().flush();
373         }
374
375         /**
376          * Get persistent object type.
377          * 
378          * @return Persistent object class to be processed by this DAO
379          */
380         abstract protected Class<T> getType();
381 }