]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/dal/dao/kernel/AbstractGenericDAOImpl.java
Salome HOME
Fix of checkin and removeDocument.
[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          * Update the persistent instance with the identifier of the given detached <BR>
266          * instance. If there is a persistent instance with the same identifier, an <BR>
267          * exception is thrown. This operation cascades to associated instances if <BR>
268          * the association is mapped with cascade="save-update".
269          * 
270          * @param detachedObject
271          *            transient instance of the object to update
272          * @see Session#update(Object)
273          */
274         public void update(final T detachedObject) {
275                 getSession().update(detachedObject);
276         }
277
278         /**
279          * Re-read the state of the given instance from the underlying database. <BR>
280          * It is inadvisable to use this to implement long-running sessions that <BR>
281          * span many business tasks. This method is, however, useful in certain <BR>
282          * special circumstances. For example
283          * <ul>
284          * <li>where a database trigger alters the object state upon insert or update</li>
285          * <li>after executing direct SQL (eg. a mass update) in the same session</li>
286          * <li>after inserting a Blob or Clob</li>
287          * </ul>
288          * 
289          * @param anObject
290          *            a persistent or detached instance to refresh
291          * @see Session#refresh(Object)
292          */
293         public void refresh(final T anObject) {
294                 getSession().refresh(anObject);
295         }
296
297         /**
298          * Load a persistent object.
299          * 
300          * @param transientObject
301          *            transient instance of the object to load
302          * @param id
303          *            object primary key
304          * @see Session#load(Object, Serializable)
305          */
306         public void load(final T transientObject, final PK id) {
307                 getSession().load(transientObject, id);
308         }
309
310         /**
311          * Lock a persistent object.
312          * 
313          * @param transientObject
314          *            transient instance of the object to lock
315          * @param lockOptions
316          *            lock options
317          * @see Session#refresh(Object, LockOptions)
318          */
319         public void refresh(final T transientObject, final LockOptions lockOptions) {
320                 getSession().refresh(transientObject, lockOptions);
321         }
322
323         /**
324          * Remove an object from persistent storage in the database.
325          * 
326          * @param persistentObject
327          *            a persistent object to delete from the database
328          * @see Session#delete(Object)
329          */
330         public void delete(final T persistentObject) {
331                 getSession().delete(persistentObject);
332         }
333
334         /**
335          * Make a transient instance persistent. This operation cascades to <BR>
336          * associated instances if the association is mapped with cascade="persist".
337          * 
338          * @param transientObject
339          *            transient instance of the object to be made persistent
340          * @see Session#persist(Object)
341          */
342         public void persist(final T transientObject) {
343                 getSession().persist(transientObject);
344         }
345
346         /**
347          * Copy the state of the given object onto the persistent object with the <BR>
348          * same identifier. If there is no persistent instance currently associated<BR>
349          * with the session, it will be loaded. Return the persistent instance. If <BR>
350          * the given instance is unsaved, save a copy of and return it as a newly <BR>
351          * persistent instance. The given instance does not become associated with <BR>
352          * the session. This operation cascades to associated instances if the <BR>
353          * association is mapped with cascade="merge".
354          * 
355          * @param transientObject
356          *            transient instance of the object to be merged with persistent data
357          * @return merged persistent object
358          * @see Session#merge(Object)
359          */
360         @SuppressWarnings(Constants.UNCHECKED)
361         public T merge(final T transientObject) {
362                 return (T) getSession().merge(transientObject);
363         }
364
365         /**
366          * Remove this instance from the session cache. Changes to the instance will<BR>
367          * not be synchronized with the database. This operation cascades to <BR>
368          * associated instances if the association is mapped with cascade="evict".
369          * 
370          * @param persistentObject
371          *            the object to be removed from session cache
372          * @see Session#evict(Object)
373          */
374         public void evict(final T persistentObject) {
375                 getSession().evict(persistentObject);
376         }
377
378         /**
379          * Synchronize the session data with the database.
380          * 
381          * @see Session#flush()
382          */
383         public void flush() {
384                 getSession().flush();
385         }
386
387         /**
388          * Get persistent object type.
389          * 
390          * @return Persistent object class to be processed by this DAO
391          */
392         abstract protected Class<T> getType();
393 }