better exception handling
[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.List;
36
37 import mir.config.MirPropertiesConfiguration;
38 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
39 import net.sf.hibernate.Criteria;
40 import net.sf.hibernate.HibernateException;
41 import net.sf.hibernate.Session;
42 import net.sf.hibernate.SessionFactory;
43 import net.sf.hibernate.Transaction;
44 import net.sf.hibernate.expression.Expression;
45 import net.sf.hibernate.expression.Order;
46
47 /**
48  * 
49  * StorageService
50  * @author idefix
51  * @version $Id: StorageService.java,v 1.5 2003/09/10 20:57:17 idfx Exp $
52  */
53 public abstract class StorageService {
54         private final int defaultLimit;
55         private Class objectClass;
56         private SessionHolder sessionHolder;
57         protected MirPropertiesConfiguration _configuration;
58
59         protected StorageService(final Class objectClass, 
60                 final SessionFactory factory){
61                 this.objectClass = objectClass;
62                 sessionHolder = new SessionHolder(factory);
63                 int limit = 30;
64                 try {
65                         _configuration = MirPropertiesConfiguration.instance();
66                         limit = 
67                                 _configuration.getInt("ServletModule.Default.ListSize");
68                 } catch (PropertiesConfigExc e) {
69                         e.printStackTrace();
70                 }
71                 defaultLimit = limit;
72         }
73         
74         public List list(final int offset, final int limit){
75                 return list(offset, limit, null);
76         }
77         
78         public List list(final int offset){
79                 return list(offset, defaultLimit, null);
80         }
81                 
82         public List list(final int offset, final Expression expression){
83                 return list(offset, defaultLimit, expression);
84         }       
85         
86         public List list(final int offset, final int limit, 
87                 final Expression expression) {
88                 return list(offset, limit, expression, null);
89         }
90         
91         /**
92          * Load a list of Objects from the database
93          * @param offset an offset of the list
94          * @param limit the limit number of Objects to be loaded
95          * @param expression a Expression object which describes the 
96          * constraints of the objects in the list
97          * @param order a Order object which describes the order of 
98          * the list
99          * @see Expression
100          * @see Order
101          * @return a list of Objects
102          */
103         public List list(final int offset, final int limit, 
104                 final Expression expression, final Order order) {
105                 try {
106                         Session session = null;
107                         Transaction transaction = null;
108                         try {
109                                 session = sessionHolder.currentSession();
110                                 transaction = session.beginTransaction();
111                                 Criteria criteria = session.createCriteria(objectClass);
112                                 if(expression != null){
113                                         criteria = criteria.add(expression);
114                                 }       
115                                 if(order != null){
116                                         criteria.addOrder(order);
117                                 }
118                                 criteria.setFirstResult(offset)
119                                         .setMaxResults(limit);
120                                 List returnList = criteria.list();
121                                 transaction.commit();
122                                 return returnList;
123                         } catch (HibernateException e) {
124                                 if(transaction != null){
125                                         transaction.rollback();
126                                 }
127                                 throw new StorageServiceFailure(e);
128                         } finally {
129                                 if (session != null) {
130                                         sessionHolder.closeSession();
131                                 }
132                         }
133                 } catch (Exception e){
134                         throw new StorageServiceFailure(e);
135                 }
136         }
137         
138         /**
139          * Load a Object with the given unique identifier
140          * @param id the identifer of the Object to be loaded
141          * @return the Object according to the id
142          */
143         public Object load(final Integer id){
144                 try {
145                         Session session = null;
146                         Transaction transaction = null;
147                         try {
148                                 session = sessionHolder.currentSession();
149                                 transaction = session.beginTransaction();
150                                 Object returnObject = session.load(objectClass, id);
151                                 initializeLazyCollections(returnObject);
152                                 transaction.commit();
153                                 return returnObject;
154                         } catch (HibernateException e) {
155                                 if(transaction != null){
156                                         transaction.rollback();
157                                 }
158                                 throw new StorageServiceFailure(e);
159                         }       finally {
160                                 if(session != null){
161                                         sessionHolder.closeSession();
162                                 }
163                         }
164                 } catch (Exception e) {
165                         throw new StorageServiceFailure(e);
166                 }
167         }
168                 
169         /**
170          * Save a new Object in the database
171          * @param newObject the Object to be saved
172          * @return
173          */
174         public Integer save(final Object newObject){
175                 try {
176                         Session session = null;
177                         Transaction transaction = null;
178                         try {
179                                 session = sessionHolder.currentSession();
180                                 transaction = session.beginTransaction();
181                                 Integer newid = (Integer)session.save(newObject);
182                                 transaction.commit();
183                                 return newid;
184                         } catch (HibernateException e) {
185                                 if(transaction != null){
186                                         transaction.rollback();
187                                 }
188                                 throw new StorageServiceFailure(e);
189                         }       finally {
190                                 if(session != null){
191                                         sessionHolder.closeSession();
192                                 }
193                         }
194                 } catch (Exception e) {
195                         throw new StorageServiceFailure(e);
196                 }                       
197         }
198         
199         /**
200          * Update a given Object
201          * @param toUpdate the Object to be updated
202          */
203         public void update(final Object toUpdate){
204                 try {
205                         Session session = null;
206                         Transaction transaction = null;
207                         try {
208                                 session = sessionHolder.currentSession();
209                                 transaction = session.beginTransaction();
210                                 session.update(toUpdate);
211                                 transaction.commit();
212                         } catch (HibernateException e) {
213                                 if(transaction != null){
214                                         transaction.rollback();
215                                 }
216                                 throw new StorageServiceFailure(e);
217                         }       finally {
218                                 if(session != null){
219                                         sessionHolder.closeSession();
220                                 }
221                         }
222                 } catch (Exception e) {
223                         throw new StorageServiceFailure(e);
224                 }                                       
225         }
226         
227         /**
228          * Delete a given Object from the database
229          * @param toDelete the Object to be deleted
230          */
231         public void delete(final Object toDelete){
232                 try {
233                         Session session = null;
234                         Transaction transaction = null;
235                         try {
236                                 session = sessionHolder.currentSession();
237                                 transaction = session.beginTransaction();
238                                 session.delete(toDelete);
239                                 transaction.commit();
240                         } catch (HibernateException e) {
241                                 if(transaction != null){
242                                         transaction.rollback();
243                                 }
244                                 throw new StorageServiceFailure(e);
245                         }       finally {
246                                 if(session != null){
247                                         sessionHolder.closeSession();
248                                 }
249                         }
250                 } catch (Exception e) {
251                         throw new StorageServiceFailure(e);
252                 }                               
253         }
254         
255         /**
256          * Initialize all the lazy loaded collections 
257          * of an object loaded from the database
258          * @param object the object to be initialized
259          */
260         protected abstract void initializeLazyCollections(
261                 final Object object) throws HibernateException;
262 }