32043b59d502f725897248dc6cdcd5ca7f4da454
[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.6 2003/09/18 21:39:42 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, Order order){
83                 return list(offset, defaultLimit, null, order);
84         }
85                 
86         public List list(final int offset, final Expression expression){
87                 return list(offset, defaultLimit, expression);
88         }       
89         
90         public List list(final int offset, final int limit, 
91                 final Expression expression) {
92                 return list(offset, limit, expression, null);
93         }
94         
95         /**
96          * Load a list of Objects from the database
97          * @param offset an offset of the list
98          * @param limit the limit number of Objects to be loaded
99          * @param expression a Expression object which describes the 
100          * constraints of the objects in the list
101          * @param order a Order object which describes the order of 
102          * the list
103          * @see Expression
104          * @see Order
105          * @return a list of Objects
106          */
107         public List list(final int offset, final int limit, 
108                 final Expression expression, final Order order) {
109                 try {
110                         Session session = null;
111                         Transaction transaction = null;
112                         try {
113                                 session = sessionHolder.currentSession();
114                                 transaction = session.beginTransaction();
115                                 Criteria criteria = session.createCriteria(objectClass);
116                                 if(expression != null){
117                                         criteria = criteria.add(expression);
118                                 }       
119                                 if(order != null){
120                                         criteria.addOrder(order);
121                                 }
122                                 criteria.setFirstResult(offset)
123                                         .setMaxResults(limit);
124                                 List returnList = criteria.list();
125                                 transaction.commit();
126                                 return returnList;
127                         } catch (HibernateException e) {
128                                 if(transaction != null){
129                                         transaction.rollback();
130                                 }
131                                 throw new StorageServiceFailure(e);
132                         } finally {
133                                 if (session != null) {
134                                         sessionHolder.closeSession();
135                                 }
136                         }
137                 } catch (Exception e){
138                         throw new StorageServiceFailure(e);
139                 }
140         }
141         
142         /**
143          * Load a Object with the given unique identifier
144          * @param id the identifer of the Object to be loaded
145          * @return the Object according to the id
146          */
147         public Object load(final Integer id){
148                 try {
149                         Session session = null;
150                         Transaction transaction = null;
151                         try {
152                                 session = sessionHolder.currentSession();
153                                 transaction = session.beginTransaction();
154                                 Object returnObject = session.load(objectClass, id);
155                                 initializeLazyCollections(returnObject);
156                                 transaction.commit();
157                                 return returnObject;
158                         } catch (HibernateException e) {
159                                 if(transaction != null){
160                                         transaction.rollback();
161                                 }
162                                 throw new StorageServiceFailure(e);
163                         }       finally {
164                                 if(session != null){
165                                         sessionHolder.closeSession();
166                                 }
167                         }
168                 } catch (Exception e) {
169                         throw new StorageServiceFailure(e);
170                 }
171         }
172                 
173         /**
174          * Save a new Object in the database
175          * @param newObject the Object to be saved
176          * @return
177          */
178         public Integer save(final Object newObject){
179                 try {
180                         Session session = null;
181                         Transaction transaction = null;
182                         try {
183                                 session = sessionHolder.currentSession();
184                                 //transaction = session.beginTransaction();
185                                 Integer newid = (Integer)session.save(newObject);
186                                 //transaction.commit();
187                                 return newid;
188                         } catch (HibernateException e) {
189                                 if(transaction != null){
190                                         transaction.rollback();
191                                 }
192                                 throw new StorageServiceFailure(e);
193                         }       finally {
194                                 if(session != null){
195                                         sessionHolder.closeSession();
196                                 }
197                         }
198                 } catch (Exception e) {
199                         throw new StorageServiceFailure(e);
200                 }                       
201         }
202         
203         /**
204          * Update a given Object
205          * @param toUpdate the Object to be updated
206          */
207         public void update(final Object toUpdate){
208                 try {
209                         Session session = null;
210                         Transaction transaction = null;
211                         try {
212                                 session = sessionHolder.currentSession();
213                                 transaction = session.beginTransaction();
214                                 session.update(toUpdate);
215                                 transaction.commit();
216                         } catch (HibernateException e) {
217                                 if(transaction != null){
218                                         transaction.rollback();
219                                 }
220                                 throw new StorageServiceFailure(e);
221                         }       finally {
222                                 if(session != null){
223                                         sessionHolder.closeSession();
224                                 }
225                         }
226                 } catch (Exception e) {
227                         throw new StorageServiceFailure(e);
228                 }                                       
229         }
230         
231         /**
232          * Delete a given Object from the database
233          * @param toDelete the Object to be deleted
234          */
235         public void delete(final Object toDelete){
236                 try {
237                         Session session = null;
238                         Transaction transaction = null;
239                         try {
240                                 session = sessionHolder.currentSession();
241                                 transaction = session.beginTransaction();
242                                 session.delete(toDelete);
243                                 transaction.commit();
244                         } catch (HibernateException e) {
245                                 if(transaction != null){
246                                         transaction.rollback();
247                                 }
248                                 throw new StorageServiceFailure(e);
249                         }       finally {
250                                 if(session != null){
251                                         sessionHolder.closeSession();
252                                 }
253                         }
254                 } catch (Exception e) {
255                         throw new StorageServiceFailure(e);
256                 }                               
257         }
258         
259         /**
260          * Initialize all the lazy loaded collections 
261          * of an object loaded from the database
262          * @param object the object to be initialized
263          */
264         protected abstract void initializeLazyCollections(
265                 final Object object) throws HibernateException;
266 }