Salome HOME
SaveOrUpdate is introduced in DAO and is used for user creation.
[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.criterion.Criterion;
18 import org.hibernate.criterion.Order;
19 import org.hibernate.criterion.Restrictions;
20 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
21
22 /**
23  * Generic DAO implementation.
24  * 
25  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
26  * 
27  * @param <T>
28  *            Persistent object class
29  * @param <PK>
30  *            Primary key class
31  */
32 public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
33                 HibernateDaoSupport implements GenericDAO<T, PK> {
34         /**
35          * Unchecked warning specification.
36          */
37         private static final String UNCHECKED = "unchecked";
38         /**
39          * Persist the newInstance object into database.
40          * 
41          * @param newInstance
42          *            new object as a transient instance
43          * @return new primary key for the created persistent object
44          */
45         @SuppressWarnings(UNCHECKED)
46         public PK create(final T newInstance) {
47                 return (PK) getSession().save(newInstance);
48         }
49
50         /**
51          * Persist the newInstance object into database.
52          * 
53          * @param newInstance
54          *            new object as a transient instance
55          */
56         @SuppressWarnings(UNCHECKED)
57         public void saveOrUpdate(final T newInstance) {
58                 getSession().saveOrUpdate(newInstance);
59         }
60
61         /**
62          * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
63          * 
64          * @param id
65          *            primary key of an object to read
66          * @return an object found by the given key
67          */
68         @SuppressWarnings(UNCHECKED)
69         public T get(final PK id) {
70                 return (T) getSession().get(getType(), id);
71         }
72
73         /**
74          * Retrieve an object that was previously persisted to the database using the given criteria.
75          * 
76          * @param aCondition
77          *            a search condition
78          * @return an object found according to the given criteria
79          */
80         @SuppressWarnings(UNCHECKED)
81         public T findByCriteria(final Criterion aCondition) {
82                 return (T) getSession().createCriteria(getType()).add(aCondition)
83                                 .uniqueResult();
84         }
85
86         /**
87          * Retrieve an object that was previously persisted to the database using the given criteria.
88          * 
89          * @param andParams
90          *            a properties values to filter with AND condition
91          * @return an object found according to the given criteria
92          */
93         public T findByCriteria(final Properties andParams) {
94                 Criterion aCondition = null;
95                 for (String aName: andParams.stringPropertyNames()) {
96                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
97                 }
98                 return findByCriteria(aCondition);
99         }
100
101         /**
102          * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
103          * 
104          * @return a list of all objects of the considered type T
105          */
106         @SuppressWarnings(UNCHECKED)
107         public List<T> getAll() {
108                 return getSession().createCriteria(getType()).list();
109         }
110
111         /**
112          * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
113          * 
114          * @param anOrder
115          *            a result list order. Null is ignored and in such case the result list is unordered.
116          * @return an ordered list of all objects of the considered type T
117          */
118         @SuppressWarnings(UNCHECKED)
119         public List<T> getAll(final Order ... anOrder) {
120                 Criteria aCriteria = getSession().createCriteria(getType());
121                 for (Order order : anOrder) {
122                         if (anOrder != null) {
123                                 aCriteria.addOrder(order);
124                         }
125                 }
126                 return aCriteria.list();
127         }
128
129         /**
130          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
131          * 
132          * @param aCondition
133          *            a search condition
134          * @return a list of objects filtered according to the given criteria
135          */
136         @SuppressWarnings(UNCHECKED)
137         public List<T> getFilteredList(final Criterion aCondition) {
138                 return getSession().createCriteria(getType()).add(aCondition).list();
139         }
140
141         /**
142          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
143          * 
144          * @param aCondition
145          *            a search condition
146          * @param anOrder
147          *            a result list order. Null is ignored and in such case the result list is unordered.
148          * @return a list of objects filtered according to the given criteria
149          */
150         @SuppressWarnings(UNCHECKED)
151         public List<T> getFilteredList(final Criterion aCondition, final Order ... anOrder) {
152                 Criteria aCriteria = getSession().createCriteria(getType()).add(
153                                 aCondition);
154                 for (Order order : anOrder) {
155                         if (anOrder != null) {
156                                 aCriteria.addOrder(order);
157                         }
158                 }
159                 return aCriteria.list();
160         }
161
162         /**
163          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
164          * 
165          * @param andParams
166          *            a properties values to filter with AND condition
167          * @return a list of objects filtered according to the given criteria
168          */
169         public List<T> getFilteredList(final Properties andParams) {
170                 return getFilteredList(andParams);
171         }
172
173         /**
174          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
175          * 
176          * @param andParams
177          *            a properties values to filter with AND 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         public List<T> getFilteredList(final Properties andParams, final Order ... anOrder) {
183                 Criterion aCondition = null;
184                 for (String aName: andParams.stringPropertyNames()) {
185                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
186                 }
187                 return getFilteredList(aCondition, anOrder);
188         }
189
190         /**
191          * Save changes made to a persistent object.
192          * 
193          * @param transientObject
194          *            transient instance of the object to update
195          */
196         public void update(final T transientObject) {
197                 getSession().update(transientObject);
198         }
199
200         /**
201          * Remove an object from persistent storage in the database.
202          * 
203          * @param persistentObject
204          *            a persistent object to delete from the database
205          */
206         public void delete(final T persistentObject) {
207                 getSession().delete(persistentObject);
208         }
209
210         /**
211          * Makes detached object persistent.
212          * 
213          * @param transientObject
214          *            transient instance of the object to be made persistent
215          */
216         public void persist(final T transientObject) {
217                 getSession().persist(transientObject);
218         }
219
220         /**
221          * Merge detached object with persistent data.
222          * 
223          * @param transientObject
224          *            transient instance of the object to be merged with persistent data
225          * @return merged persistent object
226          */
227         @SuppressWarnings(UNCHECKED)
228         public T merge(final T transientObject) {
229                 return (T) getSession().merge(transientObject);
230         }
231
232         /**
233          * Synchronize the session data with the database.
234          */
235         public void flush() {
236                 getSession().flush();
237         }
238
239         /**
240          * Get persistent object type.
241          * 
242          * @return Persistent object class to be processed by this DAO
243          */
244         abstract protected Class<T> getType();
245 }