Salome HOME
Modifications done to respect PMD rules. Versioning a document is fixed. Validation...
[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          * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
52          * 
53          * @param id
54          *            primary key of an object to read
55          * @return an object found by the given key
56          */
57         @SuppressWarnings(UNCHECKED)
58         public T get(final PK id) {
59                 return (T) getSession().get(getType(), id);
60         }
61
62         /**
63          * Retrieve an object that was previously persisted to the database using the given criteria.
64          * 
65          * @param aCondition
66          *            a search condition
67          * @return an object found according to the given criteria
68          */
69         @SuppressWarnings(UNCHECKED)
70         public T findByCriteria(final Criterion aCondition) {
71                 return (T) getSession().createCriteria(getType()).add(aCondition)
72                                 .uniqueResult();
73         }
74
75         /**
76          * Retrieve an object that was previously persisted to the database using the given criteria.
77          * 
78          * @param andParams
79          *            a properties values to filter with AND condition
80          * @return an object found according to the given criteria
81          */
82         public T findByCriteria(final Properties andParams) {
83                 Criterion aCondition = null;
84                 for (String aName: andParams.stringPropertyNames()) {
85                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
86                 }
87                 return findByCriteria(aCondition);
88         }
89
90         /**
91          * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
92          * 
93          * @return a list of all objects of the considered type T
94          */
95         @SuppressWarnings(UNCHECKED)
96         public List<T> getAll() {
97                 return getSession().createCriteria(getType()).list();
98         }
99
100         /**
101          * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
102          * 
103          * @param anOrder
104          *            a result list order. Null is ignored and in such case the result list is unordered.
105          * @return an ordered list of all objects of the considered type T
106          */
107         @SuppressWarnings(UNCHECKED)
108         public List<T> getAll(final Order ... anOrder) {
109                 Criteria aCriteria = getSession().createCriteria(getType());
110                 for (Order order : anOrder) {
111                         if (anOrder != null) {
112                                 aCriteria.addOrder(order);
113                         }
114                 }
115                 return aCriteria.list();
116         }
117
118         /**
119          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
120          * 
121          * @param aCondition
122          *            a search condition
123          * @return a list of objects filtered according to the given criteria
124          */
125         @SuppressWarnings(UNCHECKED)
126         public List<T> getFilteredList(final Criterion aCondition) {
127                 return getSession().createCriteria(getType()).add(aCondition).list();
128         }
129
130         /**
131          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
132          * 
133          * @param aCondition
134          *            a search condition
135          * @param anOrder
136          *            a result list order. Null is ignored and in such case the result list is unordered.
137          * @return a list of objects filtered according to the given criteria
138          */
139         @SuppressWarnings(UNCHECKED)
140         public List<T> getFilteredList(final Criterion aCondition, final Order ... anOrder) {
141                 Criteria aCriteria = getSession().createCriteria(getType()).add(
142                                 aCondition);
143                 for (Order order : anOrder) {
144                         if (anOrder != null) {
145                                 aCriteria.addOrder(order);
146                         }
147                 }
148                 return aCriteria.list();
149         }
150
151         /**
152          * Retrieve a list of objects which were previously persisted to the database using the given criteria.
153          * 
154          * @param andParams
155          *            a properties values to filter with AND condition
156          * @return a list of objects filtered according to the given criteria
157          */
158         public List<T> getFilteredList(final Properties andParams) {
159                 return getFilteredList(andParams);
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          * @param anOrder
168          *            a result list order. Null is ignored and in such case the result list is unordered.
169          * @return a list of objects filtered according to the given criteria
170          */
171         public List<T> getFilteredList(final Properties andParams, final Order ... anOrder) {
172                 Criterion aCondition = null;
173                 for (String aName: andParams.stringPropertyNames()) {
174                         aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
175                 }
176                 return getFilteredList(aCondition, anOrder);
177         }
178
179         /**
180          * Save changes made to a persistent object.
181          * 
182          * @param transientObject
183          *            transient instance of the object to update
184          */
185         public void update(final T transientObject) {
186                 getSession().update(transientObject);
187         }
188
189         /**
190          * Remove an object from persistent storage in the database.
191          * 
192          * @param persistentObject
193          *            a persistent object to delete from the database
194          */
195         public void delete(final T persistentObject) {
196                 getSession().delete(persistentObject);
197         }
198
199         /**
200          * Makes detached object persistent.
201          * 
202          * @param transientObject
203          *            transient instance of the object to be made persistent
204          */
205         public void persist(final T transientObject) {
206                 getSession().persist(transientObject);
207         }
208
209         /**
210          * Merge detached object with persistent data.
211          * 
212          * @param transientObject
213          *            transient instance of the object to be merged with persistent data
214          * @return merged persistent object
215          */
216         @SuppressWarnings(UNCHECKED)
217         public T merge(final T transientObject) {
218                 return (T) getSession().merge(transientObject);
219         }
220
221         /**
222          * Synchronize the session data with the database.
223          */
224         public void flush() {
225                 getSession().flush();
226         }
227
228         /**
229          * Get persistent object type.
230          * 
231          * @return Persistent object class to be processed by this DAO
232          */
233         abstract protected Class<T> getType();
234 }