1
2
3 package com.pnpconsult.zeiterfassung.helper;
4
5 import java.io.Serializable;
6 import java.util.Collections;
7 import java.util.List;
8
9 import net.sf.hibernate.Hibernate;
10 import net.sf.hibernate.HibernateException;
11 import net.sf.hibernate.NonUniqueObjectException;
12 import net.sf.hibernate.Session;
13 import net.sf.hibernate.Transaction;
14 import net.sf.hibernate.type.Type;
15
16 /***
17 * @author <a href="mailto:powerpete@users.sf.net">Moritz Petersen</a>
18 * @version $Id: AbstractObjectManager2.java,v 1.4 2004/11/06 15:11:46 powerpete Exp $
19 */
20 public class AbstractObjectManager2
21 {
22 protected List find(Class clazz) throws HibernateException
23 {
24 return SessionLocator.getSession().find(asQuery(clazz, null));
25 }
26
27 /***
28 * Sorts the given list by its natural order (i.e. by using the Comparable
29 * interface).
30 *
31 * @param list The list containing Comparable instances.
32 *
33 * @return The sorted list (not neccessary, as the list itself is manipulated).
34 */
35 protected List sort(List list)
36 {
37 Collections.sort(list);
38 return list;
39 }
40
41 protected List find(Class clazz, String query, Object obj, Type type)
42 throws HibernateException
43 {
44 return SessionLocator.getSession().find(
45 asQuery(clazz, query),
46 obj,
47 type);
48 }
49
50 private String asQuery(Class clazz, String query)
51 {
52 StringBuffer sbuf = new StringBuffer();
53 sbuf.append("from ").append(clazz.getName());
54 if (query != null)
55 {
56 if (!query.startsWith(" "))
57 ;
58 {
59 sbuf.append(" ");
60 }
61 sbuf = sbuf.append(query);
62 }
63 return sbuf.toString();
64 }
65
66 protected List loadAll(Class clazz) throws HibernateException
67 {
68 return sort(find(clazz));
69 }
70
71 protected List loadAllNotArchived(Class clazz) throws HibernateException
72 {
73 return sort(find(
74 clazz,
75 " as c where c.archived = ?",
76 Boolean.FALSE,
77 Hibernate.BOOLEAN));
78 }
79
80 public void delete(Class clazz, Serializable key) throws HibernateException
81 {
82 Session session = SessionLocator.getSession();
83 session.delete(session.load(clazz, key));
84 }
85
86 public Object load(Class clazz, Serializable key) throws HibernateException
87 {
88 return SessionLocator.getSession().load(clazz, key);
89 }
90
91 public Serializable save(Object obj) throws HibernateException
92 {
93 return save(obj, null);
94 }
95
96 public Serializable save(Object obj, Serializable key)
97 throws HibernateException
98 {
99 Session session = SessionLocator.getSession();
100 Transaction tx = session.beginTransaction();
101 try
102 {
103 if (key == null)
104 {
105 key = session.save(obj);
106 }
107 else
108 {
109 session.save(obj, key);
110 }
111 tx.commit();
112 return key;
113 }
114 catch (HibernateException e)
115 {
116 tx.rollback();
117 throw e;
118 }
119 }
120
121 public void update(Object obj) throws HibernateException
122 {
123 Session session = SessionLocator.getSession();
124 Transaction tx = session.beginTransaction();
125 try
126 {
127
128 try
129 {
130 tx.commit();
131 }
132 catch (NonUniqueObjectException e)
133 {
134 session.saveOrUpdateCopy(obj);
135 tx.commit();
136 }
137 }
138 catch (HibernateException e)
139 {
140 tx.rollback();
141 throw e;
142 }
143 }
144
145 protected List find(
146 Class clazz,
147 String query,
148 Object[] objects,
149 Type[] types) throws HibernateException
150 {
151 return SessionLocator.getSession().find(
152 asQuery(clazz, query),
153 objects,
154 types);
155 }
156 }