Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
924 | dev | 1 | package ak.hostadmiral.util; |
919 | dev | 2 | |
1021 | dev | 3 | import java.util.Iterator; |
1015 | dev | 4 | import java.util.Collection; |
1020 | dev | 5 | import java.util.Map; |
1021 | dev | 6 | import java.util.List; |
7 | import java.util.ArrayList; |
||
8 | import java.sql.Connection; |
||
9 | import java.sql.PreparedStatement; |
||
10 | import java.sql.ResultSet; |
||
11 | import java.sql.SQLException; |
||
919 | dev | 12 | import net.sf.hibernate.*; |
13 | import net.sf.hibernate.cfg.*; |
||
1020 | dev | 14 | import net.sf.hibernate.type.Type; |
919 | dev | 15 | |
16 | public class HibernateUtil |
||
17 | { |
||
1015 | dev | 18 | public static final int DATABASE_VERSION = 1; |
19 | |||
919 | dev | 20 | private static Configuration configuration; |
21 | private static SessionFactory sessionFactory; |
||
22 | private static final ThreadLocal hibernateBean = new ThreadLocal(); |
||
1015 | dev | 23 | private static boolean validated = false; |
919 | dev | 24 | |
1015 | dev | 25 | private static void validate() |
26 | throws HibernateException, ModelException |
||
27 | { |
||
28 | synchronized(HibernateUtil.class) { |
||
29 | if(validated) return; |
||
30 | |||
31 | Collection versions = currentSession().find("from DatabaseVersion"); |
||
32 | |||
33 | if(versions == null || versions.size() == 0) |
||
34 | throw new ModelException("Database structure version not found"); |
||
35 | |||
36 | if(versions.size() > 1) |
||
37 | throw new ModelException("Too much entries in database structure version table"); |
||
38 | |||
39 | int version = ((DatabaseVersion)versions.iterator().next()).getVersion(); |
||
40 | if(version != DATABASE_VERSION) |
||
41 | throw new ModelException("Expected database structure version " |
||
42 | + DATABASE_VERSION + ", found " + version); |
||
43 | |||
44 | validated = true; |
||
45 | } |
||
46 | } |
||
47 | |||
919 | dev | 48 | public static Configuration getConfiguration() |
49 | throws HibernateException |
||
50 | { |
||
51 | if(configuration == null) |
||
52 | configuration = new Configuration(); |
||
53 | |||
54 | return configuration; |
||
55 | } |
||
56 | |||
57 | public static SessionFactory getSessionFactory() |
||
58 | throws HibernateException |
||
59 | { |
||
60 | if(sessionFactory == null) |
||
61 | sessionFactory = getConfiguration().configure().buildSessionFactory(); |
||
62 | |||
63 | return sessionFactory; |
||
64 | } |
||
65 | |||
66 | private static HibernateBean currentBean() |
||
67 | throws HibernateException |
||
68 | { |
||
69 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
70 | |||
71 | if(hb == null) { |
||
72 | hb = new HibernateBean(); |
||
73 | hb.session = getSessionFactory().openSession(); |
||
74 | hibernateBean.set(hb); |
||
75 | } |
||
76 | return hb; |
||
77 | } |
||
78 | |||
79 | public static Session currentSession() |
||
80 | throws HibernateException |
||
81 | { |
||
82 | return currentBean().session; |
||
83 | } |
||
84 | |||
85 | public static void closeSession() |
||
86 | throws HibernateException, ModelException |
||
87 | { |
||
88 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
89 | |||
90 | if(hb == null) |
||
91 | throw new ModelException("No session found for this thread"); |
||
92 | |||
93 | hibernateBean.set(null); |
||
94 | hb.session.close(); |
||
95 | } |
||
96 | |||
97 | public static void beginTransaction() |
||
98 | throws HibernateException, ModelException |
||
99 | { |
||
100 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
101 | |||
102 | if(hb != null && hb.transaction != null) |
||
103 | throw new ModelException("Transaction is already open"); |
||
104 | |||
105 | currentBean().transaction = currentSession().beginTransaction(); |
||
1015 | dev | 106 | |
107 | // validate database structure version |
||
108 | if(!validated) // just try to speed up by avoiding synchronization |
||
109 | validate(); |
||
919 | dev | 110 | } |
111 | |||
112 | public static boolean isTransactionOpen() |
||
113 | throws HibernateException, ModelException |
||
114 | { |
||
115 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
116 | |||
117 | return (hb != null) && (hb.transaction != null); |
||
118 | } |
||
119 | |||
120 | public static void commitTransaction() |
||
121 | throws HibernateException, ModelException |
||
122 | { |
||
123 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
124 | |||
125 | if(hb == null || hb.transaction == null) |
||
126 | throw new ModelException("No open transaction"); |
||
127 | |||
128 | hb.transaction.commit(); |
||
129 | hb.transaction = null; |
||
130 | } |
||
131 | |||
132 | public static void rollbackTransaction() |
||
133 | throws HibernateException, ModelException |
||
134 | { |
||
135 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
136 | |||
137 | if(hb == null || hb.transaction == null) |
||
138 | throw new ModelException("No open transaction"); |
||
139 | |||
140 | hb.transaction.rollback(); |
||
141 | hb.transaction = null; |
||
142 | } |
||
143 | |||
1021 | dev | 144 | public static List sqlQuery(String query, Object[] values) |
1020 | dev | 145 | throws ModelException |
146 | { |
||
1021 | dev | 147 | Connection con; |
148 | PreparedStatement stmt; |
||
1020 | dev | 149 | |
1021 | dev | 150 | try { |
151 | con = currentSession().connection(); |
||
152 | } |
||
153 | catch(HibernateException ex) { |
||
154 | throw new ModelException(ex); |
||
155 | } |
||
1020 | dev | 156 | |
1021 | dev | 157 | try { |
158 | stmt = con.prepareStatement(query); |
||
159 | } |
||
160 | catch(SQLException ex) { |
||
161 | throw new ModelException(ex); |
||
162 | } |
||
163 | |||
164 | try { |
||
165 | if(values != null) { |
||
166 | for(int i = 0; i < values.length; i++) |
||
167 | stmt.setObject(i+1, values[i]); |
||
1020 | dev | 168 | } |
169 | |||
1021 | dev | 170 | List res = new ArrayList(); |
171 | ResultSet rs = stmt.executeQuery(); |
||
172 | while(rs.next()) { |
||
173 | res.add(rs.getObject(1)); |
||
174 | } |
||
175 | return res; |
||
176 | } |
||
177 | catch(SQLException ex) |
||
178 | { |
||
179 | throw new ModelException(ex); |
||
180 | } |
||
181 | finally { |
||
182 | try { |
||
183 | stmt.close(); |
||
184 | } |
||
185 | catch(SQLException ex) { |
||
186 | ex.printStackTrace(); |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | public static List pageableList(int pageSize, int pageNumber, |
||
192 | String query, Class classToLoad, String[] returnAliases, Class[] returnClasses, |
||
193 | Object[] values, Type[] types) |
||
194 | throws ModelException |
||
195 | { |
||
196 | try { |
||
197 | Query hq = currentSession().createSQLQuery( |
||
1020 | dev | 198 | query, returnAliases, returnClasses); |
199 | |||
200 | if(values != null && types != null) { |
||
201 | for(int i = 0; i < values.length; i++) |
||
202 | hq.setParameter(i, values[i], types[i]); |
||
203 | } |
||
204 | |||
205 | if(pageSize > 0) { |
||
206 | hq.setFirstResult(pageSize * pageNumber); |
||
207 | hq.setMaxResults(pageSize); |
||
208 | } |
||
209 | |||
1021 | dev | 210 | return selectClassColumn(classToLoad, hq.list()); |
211 | // FIXME: really no other way in Hibernate? |
||
1020 | dev | 212 | } |
213 | catch(HibernateException ex) |
||
214 | { |
||
215 | throw new ModelException(ex); |
||
216 | } |
||
217 | } |
||
218 | |||
1021 | dev | 219 | protected static List selectClassColumn(Class classToLoad, List list) |
220 | throws ModelException |
||
221 | { |
||
222 | List res = new ArrayList(); |
||
223 | |||
224 | if(list == null || list.size() == 0) return res; |
||
225 | |||
226 | Object o = list.get(0); |
||
227 | if(o == null) |
||
228 | throw new ModelException( |
||
229 | "First element in the list is null, cannot determine it type"); |
||
230 | |||
231 | if(!(o instanceof Object[])) { |
||
232 | if(classToLoad.isInstance(o)) |
||
233 | return list; |
||
234 | else |
||
235 | throw new ModelException("First element in the list is instance of " |
||
236 | + o.getClass().getName() + ", which is not java.lang.Object[] or " |
||
237 | + classToLoad.getName()); |
||
238 | } |
||
239 | |||
240 | Object[] oa = (Object[])o; |
||
241 | int pos = -1; |
||
242 | |||
243 | for(int i = 0; i < oa.length; i++) { |
||
244 | if(classToLoad.isInstance(oa[i])) { |
||
245 | if(pos < 0) |
||
246 | pos = i; |
||
247 | else |
||
248 | throw new ModelException("First row of the list has several elements of type " |
||
249 | + classToLoad.getName()); |
||
250 | } |
||
251 | } |
||
252 | if(pos < 0) { |
||
253 | throw new ModelException("First row of the list has no elements of type " |
||
254 | + classToLoad.getName()); |
||
255 | } |
||
256 | |||
257 | for(Iterator i = list.iterator(); i.hasNext(); ) { |
||
258 | Object[] e = (Object[])i.next(); |
||
259 | res.add(e[pos]); |
||
260 | } |
||
261 | |||
262 | return res; |
||
263 | } |
||
264 | |||
1020 | dev | 265 | public static String formOrderClause(Integer[] sortingKeys, Map fieldMap) |
266 | throws ModelException |
||
267 | { |
||
268 | if(sortingKeys == null || sortingKeys.length == 0) return ""; |
||
269 | |||
270 | StringBuffer buf = new StringBuffer(" order by "); |
||
271 | |||
272 | for(int i = 0; i < sortingKeys.length; i++) { |
||
273 | if(i > 0) buf.append(","); |
||
274 | |||
275 | String field = (String)fieldMap.get(sortingKeys[i]); |
||
276 | if(field == null) |
||
277 | throw new ModelException( |
||
278 | "Field for sorting key " + sortingKeys[i] + " not found"); |
||
279 | |||
280 | buf.append(field); |
||
281 | } |
||
282 | |||
283 | return buf.toString(); |
||
284 | } |
||
285 | |||
919 | dev | 286 | static class HibernateBean |
287 | { |
||
288 | public Session session; |
||
289 | public Transaction transaction; |
||
290 | } |
||
291 | } |