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