Rev 1042 | 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.UserLogin; |
||
17 | import ak.hostadmiral.core.model.UserManager; |
||
18 | import ak.hostadmiral.core.model.store.UserStore; |
||
19 | |||
20 | public class UserHibernate |
||
21 | implements UserStore |
||
22 | { |
||
23 | public UserHibernate() |
||
24 | throws ModelStoreException |
||
25 | { |
||
26 | initSortKeys(); |
||
27 | register(); |
||
28 | } |
||
29 | |||
30 | public User get(Long id) |
||
31 | throws ModelStoreException |
||
32 | { |
||
33 | try { |
||
34 | return (User)HibernateUtil.currentSession().load(User.class, id); |
||
35 | } |
||
36 | catch(HibernateException ex) { |
||
37 | throw new ModelStoreException(ex); |
||
38 | } |
||
39 | } |
||
40 | |||
41 | public boolean loginExists(User user, String login) |
||
42 | throws ModelStoreException |
||
43 | { |
||
44 | try { |
||
45 | if(user.getId() == null) |
||
46 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
47 | "select count(*) from User u where login = ?", |
||
48 | login, Hibernate.STRING) |
||
49 | .next()).intValue() > 0; |
||
50 | else |
||
51 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
52 | "select count(*) from User u where login = ? and u != ?", |
||
53 | new Object[] { login, user }, |
||
54 | new Type[] { Hibernate.STRING, Hibernate.entity(User.class) } ) |
||
55 | .next()).intValue() > 0; |
||
56 | } |
||
57 | catch(HibernateException ex) { |
||
58 | throw new ModelStoreException(ex); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | public User findForLogin(String login) |
||
63 | throws ModelStoreException |
||
64 | { |
||
65 | try { |
||
66 | List list = HibernateUtil.currentSession().find( |
||
67 | "select u from User u left join fetch u.boss where u.login = ? and u.enabled = ?", |
||
68 | new Object[] { login, Boolean.TRUE }, |
||
69 | new Type[] { Hibernate.STRING, Hibernate.BOOLEAN } ); |
||
70 | |||
71 | if(list.size() == 0) |
||
72 | return null; |
||
73 | else |
||
74 | return (User)list.get(0); |
||
75 | } |
||
76 | catch(HibernateException ex) { |
||
77 | throw new ModelStoreException(ex); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | public void save(User user) |
||
82 | throws ModelStoreException |
||
83 | { |
||
84 | try { |
||
85 | HibernateUtil.currentSession().saveOrUpdate(user); |
||
86 | } |
||
87 | catch(HibernateException ex) { |
||
88 | throw new ModelStoreException(ex); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | public void delete(User user) |
||
93 | throws ModelStoreException |
||
94 | { |
||
95 | try { |
||
96 | HibernateUtil.currentSession().delete(user); |
||
97 | } |
||
98 | catch(HibernateException ex) { |
||
99 | throw new ModelStoreException(ex); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public Collection listAllUsers(CollectionInfo info, int rowsPerPage, int pageNumber, |
||
104 | Integer[] sortingKeys) |
||
105 | throws ModelStoreException |
||
106 | { |
||
107 | try { |
||
108 | if(info != null) { |
||
109 | info.init(((Integer)HibernateUtil.currentSession().iterate( |
||
110 | "select count(*) from User").next()).intValue(), |
||
111 | pageNumber, rowsPerPage); |
||
112 | } |
||
113 | |||
114 | return HibernateUtil.pageableList(rowsPerPage, pageNumber, |
||
115 | "select u from User u left join fetch u.boss" |
||
116 | + HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null); |
||
117 | } |
||
118 | catch(HibernateException ex) { |
||
119 | throw new ModelStoreException(ex); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | public Collection listUsers(CollectionInfo info, int rowsPerPage, int pageNumber, |
||
124 | Integer[] sortingKeys, User user) |
||
125 | throws ModelStoreException |
||
126 | { |
||
127 | try { |
||
128 | if(info != null) { |
||
129 | info.init(((Integer)HibernateUtil.currentSession().iterate( |
||
130 | "select count(*) from User u where u = ? or u.boss = ?", |
||
131 | new Object[] { user, user}, |
||
132 | new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } |
||
133 | ).next()).intValue(), |
||
134 | pageNumber, rowsPerPage); |
||
135 | } |
||
136 | |||
137 | return HibernateUtil.pageableList(rowsPerPage, pageNumber, |
||
138 | "select u from User u left join fetch u.boss where u = ? or u.boss = ?" |
||
139 | + HibernateUtil.formOrderClause(sortingKeys, sortKeys), |
||
140 | new Object[] { user, user}, |
||
141 | new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } ); |
||
142 | } |
||
143 | catch(HibernateException ex) { |
||
144 | throw new ModelStoreException(ex); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | public void saveUserLogin(UserLogin userLogin) |
||
149 | throws ModelStoreException |
||
150 | { |
||
151 | try { |
||
152 | HibernateUtil.currentSession().saveOrUpdate(userLogin); |
||
153 | } |
||
154 | catch(HibernateException ex) { |
||
155 | throw new ModelStoreException(ex); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | public Collection listFailedLogins() |
||
160 | throws ModelStoreException |
||
161 | { |
||
162 | try { |
||
163 | return HibernateUtil.currentSession().find( |
||
164 | "select l from UserLogin l left join fetch l.user where l.success = ?", |
||
165 | Boolean.FALSE, Hibernate.BOOLEAN); |
||
166 | } |
||
167 | catch(HibernateException ex) { |
||
168 | throw new ModelStoreException(ex); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | public Collection listSubusers(User user) |
||
173 | throws ModelStoreException |
||
174 | { |
||
175 | try { |
||
176 | return HibernateUtil.currentSession().find( |
||
177 | "select u from User u where u.boss = ?", |
||
178 | user, Hibernate.entity(User.class) ); |
||
179 | } |
||
180 | catch(HibernateException ex) { |
||
181 | throw new ModelStoreException(ex); |
||
182 | } |
||
183 | } |
||
184 | |||
1055 | dev | 185 | public Collection listUserLogins(CollectionInfo info, int rowsPerPage, int pageNumber, |
186 | Integer[] sortingKeys, User user) |
||
187 | throws ModelStoreException |
||
188 | { |
||
189 | try { |
||
190 | if(info != null) { |
||
191 | info.init(((Integer)HibernateUtil.currentSession().iterate( |
||
192 | "select count(*) from UserLogin where usr = ?", |
||
193 | new Object[] { user }, |
||
194 | new Type[] { Hibernate.entity(User.class) } |
||
195 | ).next()).intValue(), |
||
196 | pageNumber, rowsPerPage); |
||
197 | } |
||
198 | |||
199 | return HibernateUtil.pageableList(rowsPerPage, pageNumber, |
||
200 | "select l from UserLogin l where usr = ?" |
||
201 | + HibernateUtil.formOrderClause(sortingKeys, sortKeysLogins), |
||
202 | new Object[] { user }, |
||
203 | new Type[] { Hibernate.entity(User.class) } ); |
||
204 | } |
||
205 | catch(HibernateException ex) { |
||
206 | throw new ModelStoreException(ex); |
||
207 | } |
||
208 | } |
||
209 | |||
1041 | dev | 210 | protected static Map sortKeys = new HashMap(); |
1055 | dev | 211 | protected static Map sortKeysLogins = new HashMap(); |
1041 | dev | 212 | private static boolean sortKeysInitialized = false; |
213 | |||
214 | private static void initSortKeys() |
||
215 | { |
||
216 | if(!sortKeysInitialized) { |
||
217 | sortKeys.put(UserManager.SORT_LOGIN, "u.login"); |
||
1055 | dev | 218 | sortKeysLogins.put(UserManager.SORT_LOGINS_TIME, "l.loginTime"); |
219 | sortKeysLogins.put(UserManager.SORT_LOGINS_TIME_REVERSE, "l.loginTime desc"); |
||
1041 | dev | 220 | sortKeysInitialized = true; |
221 | } |
||
222 | } |
||
223 | |||
224 | private static boolean registered = false; |
||
225 | protected static void register() |
||
226 | throws ModelStoreException |
||
227 | { |
||
228 | synchronized(UserHibernate.class) { |
||
229 | if(registered) return; |
||
230 | |||
231 | registered = true; |
||
232 | try { |
||
233 | HibernateUtil.getConfiguration().addResource( |
||
234 | "ak/hostadmiral/core/model/User.hbm.xml"); |
||
235 | HibernateUtil.getConfiguration().addResource( |
||
236 | "ak/hostadmiral/core/model/UserLogin.hbm.xml"); |
||
237 | HibernateUtil.getConfiguration().addResource( |
||
238 | "ak/hostadmiral/core/model/PasswordStoreAbstract.hbm.xml"); |
||
239 | } |
||
240 | catch(Exception ex) { |
||
241 | throw new ModelStoreException(ex); |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | } |