Salome HOME
Copyrights update 2015.
[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 first found object in the database using the given criteria.
148          * 
149          * @param aDetachedCriteria
150          *            search criteria
151          * @return a first found object filtered according to the given criteria
152          */
153         @SuppressWarnings(Constants.UNCHECKED)
154         public T getFirstResult(final DetachedCriteria aDetachedCriteria) {
155                 return (T) aDetachedCriteria.getExecutableCriteria(getSession())
156                                 .setMaxResults(1).uniqueResult();
157         }
158
159         /**
160          * Retrieve a list of DTO objects using the given criteria.
161          * 
162          * @param <DTO>
163          *            the class of returned DTOs
164          * @param aDetachedCriteria
165          *            search criteria
166          * @return a list of DTO objects filtered according to the given criteria
167          */
168         @SuppressWarnings(Constants.UNCHECKED)
169         public <DTO> List<DTO> getFilteredDTOList(
170                         final DetachedCriteria aDetachedCriteria) {
171                 return aDetachedCriteria.getExecutableCriteria(getSession()).list();
172         }
173
174         /**
175          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
176          * 
177          * @param aCondition
178          *            a search condition
179          * @return a list of objects filtered according to the given criteria
180          */
181         @SuppressWarnings(Constants.UNCHECKED)
182         public List<T> getFilteredList(final Criterion aCondition) {
183                 return getSession().createCriteria(getType()).add(aCondition).list();
184         }
185
186         /**
187          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
188          * 
189          * @param aCondition
190          *            a search condition
191          * @param anOrder
192          *            a result list order. Null is ignored and in such case the result list is unordered.
193          * @return a list of objects filtered according to the given criteria
194          */
195         @SuppressWarnings(Constants.UNCHECKED)
196         public List<T> getFilteredList(final Criterion aCondition,
197                         final Order... anOrder) {
198                 Criteria aCriteria = getSession().createCriteria(getType()).add(
199                                 aCondition);
200                 for (Order order : anOrder) {
201                         if (order != null) {
202                                 aCriteria.addOrder(order);
203                         }
204                 }
205                 return aCriteria.list();
206         }
207
208         /**
209          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
210          * 
211          * @param joinField
212          *            a field containing object to apply the condition
213          * 
214          * @param aCondition
215          *            a search condition
216          * @return a list of objects filtered according to the given criteria
217          */
218         public List<T> getFilteredList(final String joinField,
219                         final Criterion aCondition) {
220                 return getFilteredList(joinField, aCondition, (Order) null);
221         }
222
223         /**
224          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
225          * 
226          * @param joinField
227          *            a field containing object to apply the condition
228          * 
229          * @param aCondition
230          *            a search condition
231          * @param anOrder
232          *            a result list order. Null is ignored and in such case the result list is unordered.
233          * @return a list of objects filtered according to the given criteria
234          */
235         @SuppressWarnings(Constants.UNCHECKED)
236         public List<T> getFilteredList(final String joinField,
237                         final Criterion aCondition, final Order... anOrder) {
238                 Criteria aCriteria = getSession().createCriteria(getType());
239                 for (Order order : anOrder) {
240                         if (order != null) {
241                                 aCriteria.addOrder(order);
242                         }
243                 }
244                 return aCriteria.createCriteria(joinField).add(aCondition).list();
245         }
246
247         /**
248          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
249          * 
250          * @param andParams
251          *            a properties values to filter with AND condition
252          * @return a list of objects filtered according to the given criteria
253          */
254         public List<T> getFilteredList(final Properties andParams) {
255                 return getFilteredList(andParams, (Order) null);
256         }
257
258         /**
259          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
260          * 
261          * @param andParams
262          *            a properties values to filter with AND condition
263          * @param anOrder
264          *            a result list order. Null is ignored and in such case the result list is unordered.
265          * @return a list of objects filtered according to the given criteria
266          */
267         public List<T> getFilteredList(final Properties andParams,
268                         final Order... anOrder) {
269                 Criterion aCondition = null;
270                 for (String aName : andParams.stringPropertyNames()) {
271                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
272                                         andParams.get(aName)));
273                 }
274                 return getFilteredList(aCondition, anOrder);
275         }
276
277         /**
278          * Update the persistent instance with the identifier of the given detached <BR>
279          * instance. If there is a persistent instance with the same identifier, an <BR>
280          * exception is thrown. This operation cascades to associated instances if <BR>
281          * the association is mapped with cascade="save-update".
282          * 
283          * @param detachedObject
284          *            transient instance of the object to update
285          * @see Session#update(Object)
286          */
287         public void update(final T detachedObject) {
288                 getSession().update(detachedObject);
289         }
290
291         /**
292          * Re-read the state of the given instance from the underlying database. <BR>
293          * It is inadvisable to use this to implement long-running sessions that <BR>
294          * span many business tasks. This method is, however, useful in certain <BR>
295          * special circumstances. For example
296          * <ul>
297          * <li>where a database trigger alters the object state upon insert or update</li>
298          * <li>after executing direct SQL (eg. a mass update) in the same session</li>
299          * <li>after inserting a Blob or Clob</li>
300          * </ul>
301          * 
302          * @param anObject
303          *            a persistent or detached instance to refresh
304          * @see Session#refresh(Object)
305          */
306         public void refresh(final T anObject) {
307                 getSession().refresh(anObject);
308         }
309
310         /**
311          * Load a persistent object.
312          * 
313          * @param transientObject
314          *            transient instance of the object to load
315          * @param id
316          *            object primary key
317          * @see Session#load(Object, Serializable)
318          */
319         public void load(final T transientObject, final PK id) {
320                 getSession().load(transientObject, id);
321         }
322
323         /**
324          * Lock a persistent object.
325          * 
326          * @param transientObject
327          *            transient instance of the object to lock
328          * @param lockOptions
329          *            lock options
330          * @see Session#refresh(Object, LockOptions)
331          */
332         public void refresh(final T transientObject, final LockOptions lockOptions) {
333                 getSession().refresh(transientObject, lockOptions);
334         }
335
336         /**
337          * Remove an object from persistent storage in the database.
338          * 
339          * @param persistentObject
340          *            a persistent object to delete from the database
341          * @see Session#delete(Object)
342          */
343         public void delete(final T persistentObject) {
344                 getSession().delete(persistentObject);
345         }
346
347         /**
348          * Make a transient instance persistent. This operation cascades to <BR>
349          * associated instances if the association is mapped with cascade="persist".
350          * 
351          * @param transientObject
352          *            transient instance of the object to be made persistent
353          * @see Session#persist(Object)
354          */
355         public void persist(final T transientObject) {
356                 getSession().persist(transientObject);
357         }
358
359         /**
360          * Copy the state of the given object onto the persistent object with the <BR>
361          * same identifier. If there is no persistent instance currently associated<BR>
362          * with the session, it will be loaded. Return the persistent instance. If <BR>
363          * the given instance is unsaved, save a copy of and return it as a newly <BR>
364          * persistent instance. The given instance does not become associated with <BR>
365          * the session. This operation cascades to associated instances if the <BR>
366          * association is mapped with cascade="merge".
367          * 
368          * @param transientObject
369          *            transient instance of the object to be merged with persistent data
370          * @return merged persistent object
371          * @see Session#merge(Object)
372          */
373         @SuppressWarnings(Constants.UNCHECKED)
374         public T merge(final T transientObject) {
375                 return (T) getSession().merge(transientObject);
376         }
377
378         /**
379          * Remove this instance from the session cache. Changes to the instance will<BR>
380          * not be synchronized with the database. This operation cascades to <BR>
381          * associated instances if the association is mapped with cascade="evict".
382          * 
383          * @param persistentObject
384          *            the object to be removed from session cache
385          * @see Session#evict(Object)
386          */
387         public void evict(final T persistentObject) {
388                 getSession().evict(persistentObject);
389         }
390
391         /**
392          * Synchronize the session data with the database.
393          * 
394          * @see Session#flush()
395          */
396         public void flush() {
397                 getSession().flush();
398         }
399
400         /**
401          * Get persistent object type.
402          * 
403          * @return Persistent object class to be processed by this DAO
404          */
405         abstract protected Class<T> getType();
406 }