4 * Copyright (C) 2001, 2002, 2003 The Mir-coders group
6 * This file is part of Mir.
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.
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.
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
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.
33 package mir.core.service.storage;
35 import java.util.Iterator;
36 import java.util.List;
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;
52 * @version $Id: StorageService.java,v 1.8 2003/12/20 20:27:09 idfx Exp $
54 public abstract class StorageService {
55 private final int defaultLimit;
56 private Class objectClass;
57 private SessionHolder sessionHolder;
58 protected MirPropertiesConfiguration _configuration;
60 protected StorageService(final Class objectClass,
61 final SessionFactory factory){
62 this.objectClass = objectClass;
63 sessionHolder = new SessionHolder(factory);
66 _configuration = MirPropertiesConfiguration.instance();
68 _configuration.getInt("ServletModule.Default.ListSize");
69 } catch (PropertiesConfigExc e) {
75 public List list(final int offset, final int limit){
76 return list(offset, limit, null);
79 public List list(final int offset){
80 return list(offset, defaultLimit, null);
83 public List list(final int offset, Order order){
84 return list(offset, defaultLimit, null, order);
87 public List list(final int offset, final Expression expression){
88 return list(offset, defaultLimit, expression);
91 public List list(final int offset, final Expression expression,
93 return list(offset, defaultLimit, expression, order);
96 public List list(final int offset, final int limit,
97 final Expression expression) {
98 return list(offset, limit, expression, null);
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
111 * @return a list of Objects
113 public List list(final int offset, final int limit,
114 final Expression expression, final Order order) {
116 Session session = null;
117 Transaction transaction = null;
119 session = sessionHolder.currentSession();
120 transaction = session.beginTransaction();
121 Criteria criteria = session.createCriteria(objectClass);
122 if(expression != null){
123 criteria = criteria.add(expression);
126 criteria.addOrder(order);
128 criteria.setFirstResult(offset)
129 .setMaxResults(limit);
130 List returnList = criteria.list();
131 for(Iterator iterator = returnList.iterator(); iterator.hasNext();){
132 initializeLazyCollections(iterator.next());
134 transaction.commit();
136 } catch (HibernateException e) {
137 if(transaction != null){
138 transaction.rollback();
140 throw new StorageServiceFailure(e);
142 if (session != null) {
143 sessionHolder.closeSession();
146 } catch (Exception e){
147 throw new StorageServiceFailure(e);
152 * Load a list of Objects from the database
153 * @return a list of Objects
157 Session session = null;
158 Transaction transaction = null;
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());
167 transaction.commit();
169 } catch (HibernateException e) {
170 if(transaction != null){
171 transaction.rollback();
173 throw new StorageServiceFailure(e);
175 if (session != null) {
176 sessionHolder.closeSession();
179 } catch (Exception e){
180 throw new StorageServiceFailure(e);
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
190 public Object load(final Integer id){
192 Session session = null;
193 Transaction transaction = null;
195 session = sessionHolder.currentSession();
196 transaction = session.beginTransaction();
197 Object returnObject = session.load(objectClass, id);
198 initializeLazyCollections(returnObject);
199 transaction.commit();
201 } catch (HibernateException e) {
202 if(transaction != null){
203 transaction.rollback();
205 throw new StorageServiceFailure(e);
208 sessionHolder.closeSession();
211 } catch (Exception e) {
212 throw new StorageServiceFailure(e);
217 * Save a new Object in the database
218 * @param newObject the Object to be saved
221 public Integer save(final Object newObject){
223 Session session = null;
224 Transaction transaction = null;
226 session = sessionHolder.currentSession();
227 transaction = session.beginTransaction();
228 Integer newid = (Integer)session.save(newObject);
229 transaction.commit();
231 } catch (HibernateException e) {
232 if(transaction != null){
233 transaction.rollback();
235 throw new StorageServiceFailure(e);
238 sessionHolder.closeSession();
241 } catch (Exception e) {
242 throw new StorageServiceFailure(e);
247 * Update a given Object
248 * @param toUpdate the Object to be updated
250 public void update(final Object toUpdate){
252 Session session = null;
253 Transaction transaction = null;
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();
263 throw new StorageServiceFailure(e);
266 sessionHolder.closeSession();
269 } catch (Exception e) {
270 throw new StorageServiceFailure(e);
275 * Delete a given Object from the database
276 * @param toDelete the Object to be deleted
278 public void delete(final Object toDelete){
280 Session session = null;
281 Transaction transaction = null;
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();
291 throw new StorageServiceFailure(e);
294 sessionHolder.closeSession();
297 } catch (Exception e) {
298 throw new StorageServiceFailure(e);
303 * Initialize all the lazy loaded collections
304 * of an object loaded from the database
305 * @param object the object to be initialized
307 protected abstract void initializeLazyCollections(
308 final Object object) throws HibernateException;