Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
924 | dev | 1 | package ak.hostadmiral.util; |
919 | dev | 2 | |
1015 | dev | 3 | import java.util.Collection; |
1020 | dev | 4 | import java.util.Map; |
1015 | dev | 5 | |
919 | dev | 6 | import net.sf.hibernate.*; |
7 | import net.sf.hibernate.cfg.*; |
||
1020 | dev | 8 | import net.sf.hibernate.type.Type; |
919 | dev | 9 | |
10 | public class HibernateUtil |
||
11 | { |
||
1015 | dev | 12 | public static final int DATABASE_VERSION = 1; |
13 | |||
919 | dev | 14 | private static Configuration configuration; |
15 | private static SessionFactory sessionFactory; |
||
16 | private static final ThreadLocal hibernateBean = new ThreadLocal(); |
||
1015 | dev | 17 | private static boolean validated = false; |
919 | dev | 18 | |
1015 | dev | 19 | private static void validate() |
20 | throws HibernateException, ModelException |
||
21 | { |
||
22 | synchronized(HibernateUtil.class) { |
||
23 | if(validated) return; |
||
24 | |||
25 | Collection versions = currentSession().find("from DatabaseVersion"); |
||
26 | |||
27 | if(versions == null || versions.size() == 0) |
||
28 | throw new ModelException("Database structure version not found"); |
||
29 | |||
30 | if(versions.size() > 1) |
||
31 | throw new ModelException("Too much entries in database structure version table"); |
||
32 | |||
33 | int version = ((DatabaseVersion)versions.iterator().next()).getVersion(); |
||
34 | if(version != DATABASE_VERSION) |
||
35 | throw new ModelException("Expected database structure version " |
||
36 | + DATABASE_VERSION + ", found " + version); |
||
37 | |||
38 | validated = true; |
||
39 | } |
||
40 | } |
||
41 | |||
919 | dev | 42 | public static Configuration getConfiguration() |
43 | throws HibernateException |
||
44 | { |
||
45 | if(configuration == null) |
||
46 | configuration = new Configuration(); |
||
47 | |||
48 | return configuration; |
||
49 | } |
||
50 | |||
51 | public static SessionFactory getSessionFactory() |
||
52 | throws HibernateException |
||
53 | { |
||
54 | if(sessionFactory == null) |
||
55 | sessionFactory = getConfiguration().configure().buildSessionFactory(); |
||
56 | |||
57 | return sessionFactory; |
||
58 | } |
||
59 | |||
60 | private static HibernateBean currentBean() |
||
61 | throws HibernateException |
||
62 | { |
||
63 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
64 | |||
65 | if(hb == null) { |
||
66 | hb = new HibernateBean(); |
||
67 | hb.session = getSessionFactory().openSession(); |
||
68 | hibernateBean.set(hb); |
||
69 | } |
||
70 | return hb; |
||
71 | } |
||
72 | |||
73 | public static Session currentSession() |
||
74 | throws HibernateException |
||
75 | { |
||
76 | return currentBean().session; |
||
77 | } |
||
78 | |||
79 | public static void closeSession() |
||
80 | throws HibernateException, ModelException |
||
81 | { |
||
82 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
83 | |||
84 | if(hb == null) |
||
85 | throw new ModelException("No session found for this thread"); |
||
86 | |||
87 | hibernateBean.set(null); |
||
88 | hb.session.close(); |
||
89 | } |
||
90 | |||
91 | public static void beginTransaction() |
||
92 | throws HibernateException, ModelException |
||
93 | { |
||
94 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
95 | |||
96 | if(hb != null && hb.transaction != null) |
||
97 | throw new ModelException("Transaction is already open"); |
||
98 | |||
99 | currentBean().transaction = currentSession().beginTransaction(); |
||
1015 | dev | 100 | |
101 | // validate database structure version |
||
102 | if(!validated) // just try to speed up by avoiding synchronization |
||
103 | validate(); |
||
919 | dev | 104 | } |
105 | |||
106 | public static boolean isTransactionOpen() |
||
107 | throws HibernateException, ModelException |
||
108 | { |
||
109 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
110 | |||
111 | return (hb != null) && (hb.transaction != null); |
||
112 | } |
||
113 | |||
114 | public static void commitTransaction() |
||
115 | throws HibernateException, ModelException |
||
116 | { |
||
117 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
118 | |||
119 | if(hb == null || hb.transaction == null) |
||
120 | throw new ModelException("No open transaction"); |
||
121 | |||
122 | hb.transaction.commit(); |
||
123 | hb.transaction = null; |
||
124 | } |
||
125 | |||
126 | public static void rollbackTransaction() |
||
127 | throws HibernateException, ModelException |
||
128 | { |
||
129 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
130 | |||
131 | if(hb == null || hb.transaction == null) |
||
132 | throw new ModelException("No open transaction"); |
||
133 | |||
134 | hb.transaction.rollback(); |
||
135 | hb.transaction = null; |
||
136 | } |
||
137 | |||
1020 | dev | 138 | public static Collection pageableList(CollectionInfo info, int pageSize, int pageNumber, |
139 | String query, String[] returnAliases, Class[] returnClasses, |
||
140 | Object[] values, Type[] types) |
||
141 | throws ModelException |
||
142 | { |
||
143 | try { |
||
144 | // find the collection size if needed |
||
145 | if(info != null) { |
||
146 | Query sizeq = HibernateUtil.currentSession().createSQLQuery( |
||
147 | "select count(*) as {c} from " + query, |
||
148 | "c", Integer.class); |
||
149 | |||
150 | if(values != null && types != null) { |
||
151 | for(int i = 0; i < values.length; i++) |
||
152 | sizeq.setParameter(i, values[i], types[i]); |
||
153 | } |
||
154 | |||
155 | info.setSize(((Integer)sizeq.iterate().next()).intValue()); |
||
156 | } |
||
157 | |||
158 | // find the collection |
||
159 | Query hq = HibernateUtil.currentSession().createSQLQuery( |
||
160 | query, returnAliases, returnClasses); |
||
161 | |||
162 | if(values != null && types != null) { |
||
163 | for(int i = 0; i < values.length; i++) |
||
164 | hq.setParameter(i, values[i], types[i]); |
||
165 | } |
||
166 | |||
167 | if(pageSize > 0) { |
||
168 | hq.setFirstResult(pageSize * pageNumber); |
||
169 | hq.setMaxResults(pageSize); |
||
170 | } |
||
171 | |||
172 | return hq.list(); |
||
173 | } |
||
174 | catch(HibernateException ex) |
||
175 | { |
||
176 | throw new ModelException(ex); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | public static String formOrderClause(Integer[] sortingKeys, Map fieldMap) |
||
181 | throws ModelException |
||
182 | { |
||
183 | if(sortingKeys == null || sortingKeys.length == 0) return ""; |
||
184 | |||
185 | StringBuffer buf = new StringBuffer(" order by "); |
||
186 | |||
187 | for(int i = 0; i < sortingKeys.length; i++) { |
||
188 | if(i > 0) buf.append(","); |
||
189 | |||
190 | String field = (String)fieldMap.get(sortingKeys[i]); |
||
191 | if(field == null) |
||
192 | throw new ModelException( |
||
193 | "Field for sorting key " + sortingKeys[i] + " not found"); |
||
194 | |||
195 | buf.append(field); |
||
196 | } |
||
197 | |||
198 | return buf.toString(); |
||
199 | } |
||
200 | |||
919 | dev | 201 | static class HibernateBean |
202 | { |
||
203 | public Session session; |
||
204 | public Transaction transaction; |
||
205 | } |
||
206 | } |