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.List;
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;
51 * @version $Id: StorageService.java,v 1.6 2003/09/18 21:39:42 idfx Exp $
53 public abstract class StorageService {
54 private final int defaultLimit;
55 private Class objectClass;
56 private SessionHolder sessionHolder;
57 protected MirPropertiesConfiguration _configuration;
59 protected StorageService(final Class objectClass,
60 final SessionFactory factory){
61 this.objectClass = objectClass;
62 sessionHolder = new SessionHolder(factory);
65 _configuration = MirPropertiesConfiguration.instance();
67 _configuration.getInt("ServletModule.Default.ListSize");
68 } catch (PropertiesConfigExc e) {
74 public List list(final int offset, final int limit){
75 return list(offset, limit, null);
78 public List list(final int offset){
79 return list(offset, defaultLimit, null);
82 public List list(final int offset, Order order){
83 return list(offset, defaultLimit, null, order);
86 public List list(final int offset, final Expression expression){
87 return list(offset, defaultLimit, expression);
90 public List list(final int offset, final int limit,
91 final Expression expression) {
92 return list(offset, limit, expression, null);
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
105 * @return a list of Objects
107 public List list(final int offset, final int limit,
108 final Expression expression, final Order order) {
110 Session session = null;
111 Transaction transaction = null;
113 session = sessionHolder.currentSession();
114 transaction = session.beginTransaction();
115 Criteria criteria = session.createCriteria(objectClass);
116 if(expression != null){
117 criteria = criteria.add(expression);
120 criteria.addOrder(order);
122 criteria.setFirstResult(offset)
123 .setMaxResults(limit);
124 List returnList = criteria.list();
125 transaction.commit();
127 } catch (HibernateException e) {
128 if(transaction != null){
129 transaction.rollback();
131 throw new StorageServiceFailure(e);
133 if (session != null) {
134 sessionHolder.closeSession();
137 } catch (Exception e){
138 throw new StorageServiceFailure(e);
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
147 public Object load(final Integer id){
149 Session session = null;
150 Transaction transaction = null;
152 session = sessionHolder.currentSession();
153 transaction = session.beginTransaction();
154 Object returnObject = session.load(objectClass, id);
155 initializeLazyCollections(returnObject);
156 transaction.commit();
158 } catch (HibernateException e) {
159 if(transaction != null){
160 transaction.rollback();
162 throw new StorageServiceFailure(e);
165 sessionHolder.closeSession();
168 } catch (Exception e) {
169 throw new StorageServiceFailure(e);
174 * Save a new Object in the database
175 * @param newObject the Object to be saved
178 public Integer save(final Object newObject){
180 Session session = null;
181 Transaction transaction = null;
183 session = sessionHolder.currentSession();
184 //transaction = session.beginTransaction();
185 Integer newid = (Integer)session.save(newObject);
186 //transaction.commit();
188 } catch (HibernateException e) {
189 if(transaction != null){
190 transaction.rollback();
192 throw new StorageServiceFailure(e);
195 sessionHolder.closeSession();
198 } catch (Exception e) {
199 throw new StorageServiceFailure(e);
204 * Update a given Object
205 * @param toUpdate the Object to be updated
207 public void update(final Object toUpdate){
209 Session session = null;
210 Transaction transaction = null;
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();
220 throw new StorageServiceFailure(e);
223 sessionHolder.closeSession();
226 } catch (Exception e) {
227 throw new StorageServiceFailure(e);
232 * Delete a given Object from the database
233 * @param toDelete the Object to be deleted
235 public void delete(final Object toDelete){
237 Session session = null;
238 Transaction transaction = null;
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();
248 throw new StorageServiceFailure(e);
251 sessionHolder.closeSession();
254 } catch (Exception e) {
255 throw new StorageServiceFailure(e);
260 * Initialize all the lazy loaded collections
261 * of an object loaded from the database
262 * @param object the object to be initialized
264 protected abstract void initializeLazyCollections(
265 final Object object) throws HibernateException;