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.5 2003/09/10 20:57:17 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, final Expression expression){
83 return list(offset, defaultLimit, expression);
86 public List list(final int offset, final int limit,
87 final Expression expression) {
88 return list(offset, limit, expression, null);
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
101 * @return a list of Objects
103 public List list(final int offset, final int limit,
104 final Expression expression, final Order order) {
106 Session session = null;
107 Transaction transaction = null;
109 session = sessionHolder.currentSession();
110 transaction = session.beginTransaction();
111 Criteria criteria = session.createCriteria(objectClass);
112 if(expression != null){
113 criteria = criteria.add(expression);
116 criteria.addOrder(order);
118 criteria.setFirstResult(offset)
119 .setMaxResults(limit);
120 List returnList = criteria.list();
121 transaction.commit();
123 } catch (HibernateException e) {
124 if(transaction != null){
125 transaction.rollback();
127 throw new StorageServiceFailure(e);
129 if (session != null) {
130 sessionHolder.closeSession();
133 } catch (Exception e){
134 throw new StorageServiceFailure(e);
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
143 public Object load(final Integer id){
145 Session session = null;
146 Transaction transaction = null;
148 session = sessionHolder.currentSession();
149 transaction = session.beginTransaction();
150 Object returnObject = session.load(objectClass, id);
151 initializeLazyCollections(returnObject);
152 transaction.commit();
154 } catch (HibernateException e) {
155 if(transaction != null){
156 transaction.rollback();
158 throw new StorageServiceFailure(e);
161 sessionHolder.closeSession();
164 } catch (Exception e) {
165 throw new StorageServiceFailure(e);
170 * Save a new Object in the database
171 * @param newObject the Object to be saved
174 public Integer save(final Object newObject){
176 Session session = null;
177 Transaction transaction = null;
179 session = sessionHolder.currentSession();
180 transaction = session.beginTransaction();
181 Integer newid = (Integer)session.save(newObject);
182 transaction.commit();
184 } catch (HibernateException e) {
185 if(transaction != null){
186 transaction.rollback();
188 throw new StorageServiceFailure(e);
191 sessionHolder.closeSession();
194 } catch (Exception e) {
195 throw new StorageServiceFailure(e);
200 * Update a given Object
201 * @param toUpdate the Object to be updated
203 public void update(final Object toUpdate){
205 Session session = null;
206 Transaction transaction = null;
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();
216 throw new StorageServiceFailure(e);
219 sessionHolder.closeSession();
222 } catch (Exception e) {
223 throw new StorageServiceFailure(e);
228 * Delete a given Object from the database
229 * @param toDelete the Object to be deleted
231 public void delete(final Object toDelete){
233 Session session = null;
234 Transaction transaction = null;
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();
244 throw new StorageServiceFailure(e);
247 sessionHolder.closeSession();
250 } catch (Exception e) {
251 throw new StorageServiceFailure(e);
256 * Initialize all the lazy loaded collections
257 * of an object loaded from the database
258 * @param object the object to be initialized
260 protected abstract void initializeLazyCollections(
261 final Object object) throws HibernateException;