Rev 1041 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1041 | dev | 1 | package ak.hostadmiral.core.model.store.hibernate; |
2 | |||
3 | import java.util.Collection; |
||
4 | import java.util.List; |
||
5 | import java.util.Map; |
||
6 | import java.util.HashMap; |
||
7 | |||
8 | import net.sf.hibernate.Hibernate; |
||
9 | import net.sf.hibernate.HibernateException; |
||
10 | import net.sf.hibernate.type.Type; |
||
11 | |||
12 | import ak.hostadmiral.util.CollectionInfo; |
||
13 | import ak.hostadmiral.util.ModelStoreException; |
||
1042 | dev | 14 | import ak.hostadmiral.util.hibernate.HibernateUtil; |
1041 | dev | 15 | import ak.hostadmiral.core.model.User; |
16 | import ak.hostadmiral.core.model.SystemUser; |
||
17 | import ak.hostadmiral.core.model.SystemUserManager; |
||
18 | import ak.hostadmiral.core.model.store.SystemUserStore; |
||
19 | |||
20 | public class SystemUserHibernate |
||
21 | implements SystemUserStore |
||
22 | { |
||
23 | public SystemUserHibernate() |
||
24 | throws ModelStoreException |
||
25 | { |
||
26 | initSortKeys(); |
||
27 | register(); |
||
28 | } |
||
29 | |||
30 | public SystemUser get(Long id) |
||
31 | throws ModelStoreException |
||
32 | { |
||
33 | try { |
||
34 | return (SystemUser)HibernateUtil.currentSession().load(SystemUser.class, id); |
||
35 | } |
||
36 | catch(HibernateException ex) |
||
37 | { |
||
38 | throw new ModelStoreException(ex); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | public boolean nameExists(SystemUser user, String name) |
||
43 | throws ModelStoreException |
||
44 | { |
||
45 | try { |
||
46 | if(user.getId() == null) |
||
47 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
48 | "select count(*) from SystemUser u where name = ?", |
||
49 | name, Hibernate.STRING) |
||
50 | .next()).intValue() > 0; |
||
51 | else |
||
52 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
53 | "select count(*) from SystemUser u where name = ? and u != ?", |
||
54 | new Object[] { name, user }, |
||
55 | new Type[] { Hibernate.STRING, Hibernate.entity(SystemUser.class) } ) |
||
56 | .next()).intValue() > 0; |
||
57 | } |
||
58 | catch(HibernateException ex) |
||
59 | { |
||
60 | throw new ModelStoreException(ex); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | public boolean uidExists(SystemUser user, Integer uid) |
||
65 | throws ModelStoreException |
||
66 | { |
||
67 | try { |
||
68 | if(user.getId() == null) |
||
69 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
70 | "select count(*) from SystemUser u where uid = ?", |
||
71 | uid, Hibernate.INTEGER) |
||
72 | .next()).intValue() > 0; |
||
73 | else |
||
74 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
75 | "select count(*) from SystemUser u where uid = ? and u != ?", |
||
76 | new Object[] { uid, user }, |
||
77 | new Type[] { Hibernate.INTEGER, Hibernate.entity(SystemUser.class) } ) |
||
78 | .next()).intValue() > 0; |
||
79 | } |
||
80 | catch(HibernateException ex) |
||
81 | { |
||
82 | throw new ModelStoreException(ex); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | public SystemUser findForName(String name) |
||
87 | throws ModelStoreException |
||
88 | { |
||
89 | try { |
||
90 | List list = HibernateUtil.currentSession().find( |
||
91 | "select u from SystemUser u left join fetch u.owner where u.name=?", |
||
92 | name, Hibernate.STRING); |
||
93 | |||
94 | if(list.size() == 0) |
||
95 | return null; |
||
96 | else |
||
97 | return (SystemUser)list.get(0); |
||
98 | } |
||
99 | catch(HibernateException ex) |
||
100 | { |
||
101 | throw new ModelStoreException(ex); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | public SystemUser findForUid(Integer uid) |
||
106 | throws ModelStoreException |
||
107 | { |
||
108 | try { |
||
109 | List list = HibernateUtil.currentSession().find( |
||
110 | "select u from SystemUser u left join fetch u.owner where u.uid=?", |
||
111 | uid, Hibernate.INTEGER); |
||
112 | |||
113 | if(list.size() == 0) |
||
114 | return null; |
||
115 | else |
||
116 | return (SystemUser)list.get(0); |
||
117 | } |
||
118 | catch(HibernateException ex) |
||
119 | { |
||
120 | throw new ModelStoreException(ex); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | public void save(SystemUser systemUser) |
||
125 | throws ModelStoreException |
||
126 | { |
||
127 | try { |
||
128 | HibernateUtil.currentSession().saveOrUpdate(systemUser); |
||
129 | } |
||
130 | catch(HibernateException ex) |
||
131 | { |
||
132 | throw new ModelStoreException(ex); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | public void delete(SystemUser systemUser) |
||
137 | throws ModelStoreException |
||
138 | { |
||
139 | try { |
||
140 | HibernateUtil.currentSession().delete(systemUser); |
||
141 | } |
||
142 | catch(HibernateException ex) |
||
143 | { |
||
144 | throw new ModelStoreException(ex); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | public Collection listAllSystemUsers(CollectionInfo info, int rowsPerPage, int pageNumber, |
||
149 | Integer[] sortingKeys) |
||
150 | throws ModelStoreException |
||
151 | { |
||
152 | try { |
||
153 | if(info != null) { |
||
154 | info.init(((Integer)HibernateUtil.currentSession().iterate( |
||
155 | "select count(*) from SystemUser").next()).intValue(), |
||
156 | pageNumber, rowsPerPage); |
||
157 | } |
||
158 | |||
159 | return HibernateUtil.pageableList(rowsPerPage, pageNumber, |
||
160 | "select u from SystemUser u left join fetch u.owner" |
||
161 | + HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null); |
||
162 | } |
||
163 | catch(HibernateException ex) |
||
164 | { |
||
165 | throw new ModelStoreException(ex); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | public Collection listSystemUsers(CollectionInfo info, int rowsPerPage, int pageNumber, |
||
170 | Integer[] sortingKeys, User user) |
||
171 | throws ModelStoreException |
||
172 | { |
||
173 | try { |
||
174 | if(info != null) { |
||
175 | info.init(((Integer)HibernateUtil.currentSession().iterate( |
||
176 | "select count(*) from SystemUser where owner is null or owner = ?", |
||
177 | user, Hibernate.entity(User.class)) |
||
178 | .next()).intValue(), |
||
179 | pageNumber, rowsPerPage); |
||
180 | } |
||
181 | |||
182 | return HibernateUtil.pageableList(rowsPerPage, pageNumber, |
||
183 | "select u from SystemUser u left join u.owner o" |
||
184 | + " where u.owner is null or u.owner = ?" |
||
185 | + HibernateUtil.formOrderClause(sortingKeys, sortKeys), |
||
186 | new Object[] { user }, new Type[] { Hibernate.entity(User.class) } ); |
||
187 | } |
||
188 | catch(HibernateException ex) |
||
189 | { |
||
190 | throw new ModelStoreException(ex); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | public int countSystemUsersAvailable(User user) |
||
195 | throws ModelStoreException |
||
196 | { |
||
197 | try { |
||
198 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
199 | "select count(*) from SystemUser u left join u.owner o where o is null or o=?", |
||
200 | user, Hibernate.entity(User.class)).next()).intValue(); |
||
201 | } |
||
202 | catch(HibernateException ex) |
||
203 | { |
||
204 | throw new ModelStoreException(ex); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | public Collection listOwnSystemUsers(User user) |
||
209 | throws ModelStoreException |
||
210 | { |
||
211 | try { |
||
212 | return HibernateUtil.currentSession().find( |
||
213 | "select u from SystemUser u where u.owner = ?", |
||
214 | user, Hibernate.entity(User.class) ); |
||
215 | } |
||
216 | catch(HibernateException ex) |
||
217 | { |
||
218 | throw new ModelStoreException(ex); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | protected static Map sortKeys = new HashMap(); |
||
223 | private static boolean sortKeysInitialized = false; |
||
224 | |||
225 | private static void initSortKeys() |
||
226 | { |
||
227 | if(!sortKeysInitialized) { |
||
228 | sortKeys.put(SystemUserManager.SORT_UID, "u.uid"); |
||
229 | sortKeys.put(SystemUserManager.SORT_NAME, "u.name"); |
||
230 | sortKeysInitialized = true; |
||
231 | } |
||
232 | } |
||
233 | |||
234 | private static boolean registered = false; |
||
235 | protected static void register() |
||
236 | throws ModelStoreException |
||
237 | { |
||
238 | synchronized(SystemUserManager.class) { |
||
239 | if(registered) return; |
||
240 | |||
241 | registered = true; |
||
242 | try { |
||
243 | HibernateUtil.getConfiguration().addResource( |
||
244 | "ak/hostadmiral/core/model/SystemUser.hbm.xml"); |
||
245 | } |
||
246 | catch(Exception ex) { |
||
247 | throw new ModelStoreException(ex); |
||
248 | } |
||
249 | } |
||
250 | } |
||
251 | } |