View Javadoc

1   // Created on 13.12.2003
2   package com.pnpconsult.zeiterfassung.helper;
3   
4   import java.io.Serializable;
5   import java.util.Date;
6   import java.util.IdentityHashMap;
7   import java.util.List;
8   import java.util.Map;
9   import java.util.SortedSet;
10  import java.util.TreeSet;
11  
12  import jface.util.factory.FactoryException;
13  import net.sf.hibernate.HibernateException;
14  import net.sf.hibernate.Session;
15  import net.sf.hibernate.Transaction;
16  import net.sf.hibernate.UnresolvableObjectException;
17  import net.sf.hibernate.type.Type;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import com.pnpconsult.zeiterfassung.model.TimestampedObject;
23  import com.pnpconsult.zeiterfassung.util.SingletonManager;
24  
25  /***
26   * @author <a href="mailto:powerpete@users.sf.net">M. Petersen</a>
27   * @version $Id: AbstractObjectManager.java,v 1.7 2004/05/23 16:59:42 powerpete Exp $
28   */
29  public class AbstractObjectManager extends SingletonManager
30  {
31  	private static final Log LOG = LogFactory
32  		.getLog(AbstractObjectManager.class);
33  
34  	private Map txSessions = new IdentityHashMap();
35  
36  	protected AbstractObjectManager()
37  	{}
38  
39  	protected Session openSession() throws HibernateException, FactoryException
40  	{
41  		return SessionManager.Factory.getInstance().openSession();
42  	}
43  
44  	public Object load(Class clazz, Serializable key)
45  		throws HibernateException, FactoryException
46  	{
47  		Session session = openSession();
48  		try
49  		{
50  			return session.load(clazz, key);
51  		}
52  		catch (UnresolvableObjectException e)
53  		{
54  			if (e.getPersistentClass() != clazz)
55  			{
56  				throw e;
57  			}
58  			return null;
59  		}
60  		catch (HibernateException e)
61  		{
62  			throw e;
63  		}
64  		finally
65  		{
66  			session.close();
67  		}
68  	}
69  
70  	protected SortedSet loadAllSorted(Class clazz) throws HibernateException,
71  		FactoryException
72  	{
73  		return new TreeSet(find(clazz, null));
74  	}
75  
76  	protected List find(Class clazz, String clause) throws HibernateException,
77  		FactoryException
78  	{
79  		Session session = openSession();
80  		try
81  		{
82  			String query = "from " + clazz.getName();
83  			if (clause != null)
84  			{
85  				query += " " + clause;
86  			}
87  			return session.find(query);
88  		}
89  		catch (HibernateException e)
90  		{
91  			throw e;
92  		}
93  		finally
94  		{
95  			session.close();
96  		}
97  	}
98  
99  	/***
100 	 * Searches the database using the given {@link Class}. The claus is a 
101 	 * Hibernate Query without the class name and should not start with a 
102 	 * leading whitespace.
103 	 * 
104 	 * @param clazz     The type of the return values.
105 	 * @param clause    The Hibernate Query to refine the search.
106 	 * @param objects   The values of the placeholders inside the query.
107 	 * @param types     The types of the placeholder values inside the query.
108 	 * 
109 	 * @return A {@link List} containing the search results.
110 	 * 
111 	 * @throws HibernateException If an Hibernate error occured.
112 	 * @throws FactoryException If the SessionManager could not be used.
113 	 */
114 	protected List find(
115 		Class clazz,
116 		String clause,
117 		Object[] objects,
118 		Type[] types) throws HibernateException, FactoryException
119 	{
120 		Session session = openSession();
121 		try
122 		{
123 			return session.find(
124 				"from " + clazz.getName() + " " + clause,
125 				objects,
126 				types);
127 		}
128 		catch (HibernateException e)
129 		{
130 			throw e;
131 		}
132 		finally
133 		{
134 			session.close();
135 		}
136 	}
137 
138 	protected boolean exists(Class clazz, Serializable key)
139 		throws HibernateException, FactoryException
140 	{
141 		Session session = openSession();
142 		try
143 		{
144 			session.load(clazz, key);
145 			return true;
146 		}
147 		catch (UnresolvableObjectException e)
148 		{
149 			if (e.getPersistentClass() == clazz)
150 			{
151 				return false;
152 			}
153 			throw e;
154 		}
155 		catch (HibernateException e)
156 		{
157 			throw e;
158 		}
159 		finally
160 		{
161 			session.close();
162 		}
163 	}
164 
165 	public Serializable save(Object obj, Serializable key)
166 		throws HibernateException, FactoryException
167 	{
168 		Session session = openSession();
169 		Transaction tx = session.beginTransaction();
170 		try
171 		{
172 			checkTimestamps(obj);
173 			if (key == null)
174 			{
175 				key = session.save(obj);
176 			}
177 			else
178 			{
179 				session.save(obj, key);
180 			}
181 			tx.commit();
182 		}
183 		catch (HibernateException e)
184 		{
185 			tx.rollback();
186 			throw e;
187 		}
188 		finally
189 		{
190 			session.close();
191 		}
192 		return key;
193 	}
194 
195 	protected static void checkTimestamps(Object obj)
196 	{
197 		if (obj instanceof TimestampedObject)
198 		{
199 			TimestampedObject t = (TimestampedObject) obj;
200 			if (t.getDateCreated() == null)
201 			{
202 				t.setDateCreated(new Date());
203 			}
204 			t.setDateModified(new Date());
205 		}
206 	}
207 
208 	public Object open(Class clazz, Serializable key)
209 		throws HibernateException, FactoryException
210 	{
211 		Session session = openSession();
212 		Transaction tx = session.beginTransaction();
213 		try
214 		{
215 			Object obj = session.load(clazz, key);
216 			txSessions.put(obj, new TxSession(session, tx));
217 			return obj;
218 		}
219 		catch (UnresolvableObjectException e)
220 		{
221             tx.rollback();
222             session.close();
223 			if (e.getPersistentClass() == clazz)
224 			{
225 				return null;
226 			}
227 			throw e;
228 		}
229 		catch (HibernateException e)
230 		{
231             tx.rollback();
232             session.close();
233 			throw e;
234 		}
235 	}
236 
237 	public void close(Object obj) throws HibernateException
238 	{
239 		TxSession txSession = (TxSession) txSessions.get(obj);
240 		if (txSession == null)
241 		{
242 			return;
243 		}
244 		try
245 		{
246 			txSession.session.save(obj);
247 			txSession.tx.commit();
248 		}
249 		catch (HibernateException e)
250 		{
251 		    LOG.fatal("Error while closing object " + obj, e);
252 			txSession.tx.rollback();
253 			throw e;
254 		}
255 		finally
256 		{
257 			txSession.session.close();
258 		}
259 	}
260 
261 	public void delete(Object obj) throws HibernateException
262 	{
263 		if (obj == null)
264 		{
265 			return;
266 		}
267 		TxSession txSession = (TxSession) txSessions.get(obj);
268 		if (txSession == null)
269 		{
270 			return;
271 		}
272 		try
273 		{
274 			txSession.session.delete(obj);
275 			txSession.tx.commit();
276 		}
277 		catch (HibernateException e)
278 		{
279 			txSession.tx.rollback();
280 			throw e;
281 		}
282 		finally
283 		{
284 			txSession.session.close();
285 		}
286 	}
287 
288 	public void dispose(Object obj)
289 	{
290 		TxSession txSession = (TxSession) txSessions.get(obj);
291 		if (txSession == null)
292 		{
293 			return;
294 		}
295 		try
296 		{
297 			txSession.tx.rollback();
298 		}
299 		catch (HibernateException e)
300 		{
301 			LOG.fatal("Error while rolling back " + obj, e);
302 		}
303 		finally
304 		{
305 			try
306 			{
307 				txSession.session.close();
308 			}
309 			catch (HibernateException e1)
310 			{
311 				LOG.fatal("Error while closing session", e1);
312 			}
313 		}
314 	}
315 
316 	private class TxSession
317 	{
318 		Session session;
319 
320 		Transaction tx;
321 
322 		public TxSession(Session session, Transaction tx)
323 		{
324 			this.session = session;
325 			this.tx = tx;
326 		}
327 	}
328 }