rebuilding head
[mir.git] / source / mir / core / service / storage / StorageService.java
1 /*
2  * StorageService.java
3  * 
4  * Copyright (C) 2001, 2002, 2003 The Mir-coders group
5  *
6  * This file is part of Mir.
7  *
8  * Mir is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Mir is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Mir; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * In addition, as a special exception, The Mir-coders gives permission to link
23  * the code of this program with  any library licensed under the Apache Software License,
24  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
25  * (or with modified versions of the above that use the same license as the above),
26  * and distribute linked combinations including the two.  You must obey the
27  * GNU General Public License in all respects for all of the code used other than
28  * the above mentioned libraries.  If you modify this file, you may extend this
29  * exception to your version of the file, but you are not obligated to do so.
30  * If you do not wish to do so, delete this exception statement from your version.
31  */
32  
33 package mir.core.service.storage;
34
35 import java.util.Iterator;
36 import java.util.List;
37
38 import mir.config.MirPropertiesConfiguration;
39 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
40 import net.sf.hibernate.Criteria;
41 import net.sf.hibernate.HibernateException;
42 import net.sf.hibernate.Session;
43 import net.sf.hibernate.SessionFactory;
44 import net.sf.hibernate.Transaction;
45 import net.sf.hibernate.expression.Expression;
46 import net.sf.hibernate.expression.Order;
47
48 /**
49  * 
50  * StorageService
51  * @author idefix
52  * @version $Id: StorageService.java,v 1.8 2003/12/20 20:27:09 idfx Exp $
53  */
54 public abstract class StorageService {
55         private final int defaultLimit;
56         private Class objectClass;
57         private SessionHolder sessionHolder;
58         protected MirPropertiesConfiguration _configuration;
59
60         protected StorageService(final Class objectClass, 
61                 final SessionFactory factory){
62                 this.objectClass = objectClass;
63                 sessionHolder = new SessionHolder(factory);
64                 int limit = 30;
65                 try {
66                         _configuration = MirPropertiesConfiguration.instance();
67                         limit = 
68                                 _configuration.getInt("ServletModule.Default.ListSize");
69                 } catch (PropertiesConfigExc e) {
70                         e.printStackTrace();
71                 }
72                 defaultLimit = limit;
73         }
74         
75         public List list(final int offset, final int limit){
76                 return list(offset, limit, null);
77         }
78         
79         public List list(final int offset){
80                 return list(offset, defaultLimit, null);
81         }
82
83         public List list(final int offset, Order order){
84                 return list(offset, defaultLimit, null, order);
85         }
86                 
87         public List list(final int offset, final Expression expression){
88                 return list(offset, defaultLimit, expression);
89         }       
90   
91   public List list(final int offset, final Expression expression,
92     final Order order){
93     return list(offset, defaultLimit, expression, order);
94   }
95         
96         public List list(final int offset, final int limit, 
97                 final Expression expression) {
98                 return list(offset, limit, expression, null);
99         }
100         
101         /**
102          * Load a list of Objects from the database
103          * @param offset an offset of the list
104          * @param limit the limit number of Objects to be loaded
105          * @param expression a Expression object which describes the 
106          * constraints of the objects in the list
107          * @param order a Order object which describes the order of 
108          * the list
109          * @see Expression
110          * @see Order
111          * @return a list of Objects
112          */
113         public List list(final int offset, final int limit, 
114                 final Expression expression, final Order order) {
115                 try {
116                         Session session = null;
117                         Transaction transaction = null;
118                         try {
119                                 session = sessionHolder.currentSession();
120                                 transaction = session.beginTransaction();
121                                 Criteria criteria = session.createCriteria(objectClass);
122                                 if(expression != null){
123                                         criteria = criteria.add(expression);
124                                 }       
125                                 if(order != null){
126                                         criteria.addOrder(order);
127                                 }
128                                 criteria.setFirstResult(offset)
129                                         .setMaxResults(limit);
130                                 List returnList = criteria.list();
131                                 for(Iterator iterator = returnList.iterator(); iterator.hasNext();){
132                                         initializeLazyCollections(iterator.next());
133                                 }
134                                 transaction.commit();
135                                 return returnList;
136                         } catch (HibernateException e) {
137                                 if(transaction != null){
138                                         transaction.rollback();
139                                 }
140                                 throw new StorageServiceFailure(e);
141                         } finally {
142                                 if (session != null) {
143                                         sessionHolder.closeSession();
144                                 }
145                         }
146                 } catch (Exception e){
147                         throw new StorageServiceFailure(e);
148                 }
149         }
150
151         /**
152          * Load a list of Objects from the database
153          * @return a list of Objects
154          */
155         public List list() {
156                 try {
157                         Session session = null;
158                         Transaction transaction = null;
159                         try {
160                                 session = sessionHolder.currentSession();
161                                 transaction = session.beginTransaction();
162                                 Criteria criteria = session.createCriteria(objectClass);
163                                 List returnList = criteria.list();
164                                 for(Iterator iterator = returnList.iterator(); iterator.hasNext();){
165                                         initializeLazyCollections(iterator.next());
166                                 }
167                                 transaction.commit();
168                                 return returnList;
169                         } catch (HibernateException e) {
170                                 if(transaction != null){
171                                         transaction.rollback();
172                                 }
173                                 throw new StorageServiceFailure(e);
174                         } finally {
175                                 if (session != null) {
176                                         sessionHolder.closeSession();
177                                 }
178                         }
179                 } catch (Exception e){
180                         throw new StorageServiceFailure(e);
181                 }
182         }
183
184         
185         /**
186          * Load a Object with the given unique identifier
187          * @param id the identifer of the Object to be loaded
188          * @return the Object according to the id
189          */
190         public Object load(final Integer id){
191                 try {
192                         Session session = null;
193                         Transaction transaction = null;
194                         try {
195                                 session = sessionHolder.currentSession();
196                                 transaction = session.beginTransaction();
197                                 Object returnObject = session.load(objectClass, id);
198                                 initializeLazyCollections(returnObject);
199                                 transaction.commit();
200                                 return returnObject;
201                         } catch (HibernateException e) {
202                                 if(transaction != null){
203                                         transaction.rollback();
204                                 }
205                                 throw new StorageServiceFailure(e);
206                         }       finally {
207                                 if(session != null){
208                                         sessionHolder.closeSession();
209                                 }
210                         }
211                 } catch (Exception e) {
212                         throw new StorageServiceFailure(e);
213                 }
214         }
215                 
216         /**
217          * Save a new Object in the database
218          * @param newObject the Object to be saved
219          * @return
220          */
221         public Integer save(final Object newObject){
222                 try {
223                         Session session = null;
224                         Transaction transaction = null;
225                         try {
226                                 session = sessionHolder.currentSession();
227                                 transaction = session.beginTransaction();
228                                 Integer newid = (Integer)session.save(newObject);
229                                 transaction.commit();
230                                 return newid;
231                         } catch (HibernateException e) {
232                                 if(transaction != null){
233                                         transaction.rollback();
234                                 }
235                                 throw new StorageServiceFailure(e);
236                         }       finally {
237                                 if(session != null){
238                                         sessionHolder.closeSession();
239                                 }
240                         }
241                 } catch (Exception e) {
242                         throw new StorageServiceFailure(e);
243                 }                       
244         }
245         
246         /**
247          * Update a given Object
248          * @param toUpdate the Object to be updated
249          */
250         public void update(final Object toUpdate){
251                 try {
252                         Session session = null;
253                         Transaction transaction = null;
254                         try {
255                                 session = sessionHolder.currentSession();
256                                 transaction = session.beginTransaction();
257                                 session.update(toUpdate);
258                                 transaction.commit();
259                         } catch (HibernateException e) {
260                                 if(transaction != null){
261                                         transaction.rollback();
262                                 }
263                                 throw new StorageServiceFailure(e);
264                         }       finally {
265                                 if(session != null){
266                                         sessionHolder.closeSession();
267                                 }
268                         }
269                 } catch (Exception e) {
270                         throw new StorageServiceFailure(e);
271                 }                                       
272         }
273         
274         /**
275          * Delete a given Object from the database
276          * @param toDelete the Object to be deleted
277          */
278         public void delete(final Object toDelete){
279                 try {
280                         Session session = null;
281                         Transaction transaction = null;
282                         try {
283                                 session = sessionHolder.currentSession();
284                                 transaction = session.beginTransaction();
285                                 session.delete(toDelete);
286                                 transaction.commit();
287                         } catch (HibernateException e) {
288                                 if(transaction != null){
289                                         transaction.rollback();
290                                 }
291                                 throw new StorageServiceFailure(e);
292                         }       finally {
293                                 if(session != null){
294                                         sessionHolder.closeSession();
295                                 }
296                         }
297                 } catch (Exception e) {
298                         throw new StorageServiceFailure(e);
299                 }                               
300         }
301         
302         /**
303          * Initialize all the lazy loaded collections 
304          * of an object loaded from the database
305          * @param object the object to be initialized
306          */
307         protected abstract void initializeLazyCollections(
308                 final Object object) throws HibernateException;
309 }