KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > dao > UserDAO


1 /*
2  * $Id: UserDAO.java,v 1.8 2005/01/17 21:36:10 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.dao;
27
28 import java.io.Serializable JavaDoc;
29 import java.util.List JavaDoc;
30
31 import net.sf.hibernate.Criteria;
32 import net.sf.hibernate.HibernateException;
33 import net.sf.hibernate.Query;
34 import net.sf.hibernate.Session;
35 import net.sf.hibernate.expression.Expression;
36 import net.sf.hibernate.expression.MatchMode;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 import com.j2biz.blogunity.BlogunityManager;
42 import com.j2biz.blogunity.exception.BlogunityException;
43 import com.j2biz.blogunity.i18n.I18N;
44 import com.j2biz.blogunity.i18n.I18NStatusFactory;
45 import com.j2biz.blogunity.pojo.SystemConfiguration;
46 import com.j2biz.blogunity.pojo.User;
47 import com.j2biz.blogunity.util.HibernateUtil;
48 import com.j2biz.blogunity.util.PasswordEncryptionService;
49
50 /**
51  * @author michelson
52  * @version $$
53  * @since 0.1
54  *
55  *
56  */

57 public class UserDAO extends AbstractDAO {
58     /**
59      * Logger for this class
60      */

61     private static final Log log = LogFactory.getLog(UserDAO.class);
62
63     /**
64      *
65      */

66     public UserDAO() {
67         super();
68     }
69
70     /**
71      * Returns user-instance with the given ID.
72      *
73      * @param id
74      * @return
75      * @throws BlogException
76      */

77     public User getUserByID(long id) throws BlogunityException {
78         return getUserByID(new Long JavaDoc(id));
79     }
80
81     /**
82      * Returns user-instance with the given ID.
83      *
84      * @param id
85      * @return
86      * @throws BlogException
87      */

88     public User getUserByID(Long JavaDoc id) throws BlogunityException {
89         User user = null;
90         try {
91             user = (User) getByID(User.class, id);
92         } catch (HibernateException ex) {
93             log.error("getUserByID(Long)", ex);
94             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_ID,
95                     "User", ex));
96         }
97
98         return user;
99     }
100
101     /**
102      * Returns user-instance with the given nickname and password.
103      *
104      * @param nickname
105      * @param password
106      * @return
107      * @throws BlogException
108      */

109     public User getUserByNameAndPassword(String JavaDoc nickname, String JavaDoc password)
110             throws BlogunityException {
111
112         Session session = HibernateUtil.getSession();
113         try {
114             // TODO mark this query cacheable?
115
// TODO check if user isActive!
116

117             // encode password, if password-encryption is enabled!
118
boolean isEncryptionEnbaled = false;
119             SystemConfiguration config = BlogunityManager.getSystemConfiguration();
120             if (config != null) isEncryptionEnbaled = config.isPasswordEncriptionEnabled();
121
122             String JavaDoc encodedPass = password;
123             if (isEncryptionEnbaled) {
124                 try {
125                     encodedPass = PasswordEncryptionService.encrypt(password, BlogunityManager
126                             .getSystemConfiguration().getPasswordEncryptionType());
127                 } catch (Throwable JavaDoc t) {
128                     throw new BlogunityException(I18NStatusFactory.createUnknown(t));
129                 }
130             }
131
132             Query query = session
133                     .createQuery("from User u where u.nickname = :nickname and u.password = :password");
134             query.setString("nickname", nickname);
135             query.setString("password", encodedPass);
136
137             return (User) query.uniqueResult();
138
139         } catch (HibernateException ex) {
140             log.error("getUserByNameAndPassword(String, String)", ex);
141             throw new BlogunityException(I18NStatusFactory.create(
142                     I18N.ERRORS.USER_BY_NICKNAME_AND_PASS_NOT_FOUND, ex));
143         }
144     }
145
146     /**
147      * Returns user-instance with the given nickname.
148      *
149      * @param nickname
150      * @return
151      * @throws BlogException
152      */

153     public User getUserByName(String JavaDoc nickname) throws BlogunityException {
154
155         Session session = HibernateUtil.getSession();
156         try {
157             // TODO mark this query cacheable?
158
// TODO check if user isActive!
159
Query query = session.createQuery("from User u where u.nickname = :nickname");
160             query.setString("nickname", nickname);
161
162             return (User) query.uniqueResult();
163
164         } catch (HibernateException ex) {
165             log.error("getUserByName(String)", ex);
166             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_NAME,
167                     "User", ex));
168         }
169
170     }
171
172     /**
173      * Persists given user-instance into database.
174      *
175      * @param user
176      * @return
177      * @throws BlogException
178      */

179     public Serializable JavaDoc createUser(User user) throws BlogunityException {
180         try {
181             return create(user);
182         } catch (HibernateException e) {
183             log.error("createUser(User)", e);
184             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE, "User", e));
185         }
186     }
187
188     /**
189      * Deletes database-entry equals to the given user-instance from database.
190      *
191      * @param user
192      * @throws BlogException
193      */

194     public void deleteUser(User user) throws BlogunityException {
195         try {
196             delete(user);
197         } catch (HibernateException e) {
198             log.error("deleteUser(User)", e);
199             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE, "User", e));
200         }
201     }
202
203     /**
204      * Updates database-entry equals to the given user-instance.
205      *
206      * @param user
207      * @throws BlogException
208      */

209     public void updateUser(User user) throws BlogunityException {
210         try {
211             update(user);
212         } catch (HibernateException e) {
213             log.error("updateUser(User)", e);
214             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UPDATE, "User", e));
215         }
216     }
217
218     /**
219      * Refreshes user-instance saved within Hibernate-Session.
220      *
221      * @param user
222      * @throws BlogException
223      */

224     public void attachToSession(User user) throws BlogunityException {
225
226         Session session = HibernateUtil.getSession();
227         try {
228
229             // FIXME maybe this is not a best solution for attaching
230
// user-instance to current hibernate session!
231

232             // session.lock(user, LockMode.NONE);
233
// session.saveOrUpdate(user);
234
session.refresh(user);
235
236         } catch (HibernateException e) {
237             log.error("attachToSession(user)", e);
238             throw new BlogunityException(I18NStatusFactory.create(
239                     I18N.ERRORS.USER_UNABLE_ATTACH_TO_SESSION, e));
240
241         }
242     }
243
244     /**
245      * Returns #count - users ordered by registration date.
246      *
247      * @param count
248      * @return
249      * @throws BlogException
250      */

251     public List JavaDoc getLastRegisteredUsers(int count) throws BlogunityException {
252         Session session = HibernateUtil.getSession();
253         try {
254             Query q = session.createQuery("from User u order by u.registerTime desc");
255             q.setFirstResult(0);
256             q.setMaxResults(count);
257
258             List JavaDoc returnList = q.list();
259             return returnList;
260         } catch (HibernateException e) {
261             log.error("getLastRegisteredUsers(int)", e);
262             throw new BlogunityException(I18NStatusFactory.create(
263                     I18N.ERRORS.USER_FETCH_LAST_REGISTERED, e));
264         }
265     }
266
267     /**
268      * Returns a list of all persistent user-objects.
269      *
270      * @return
271      * @throws BlogunityException
272      */

273     public List JavaDoc getAllUsers() throws BlogunityException {
274         Session session = HibernateUtil.getSession();
275         try {
276             Query q = session.createQuery("from User u order by u.registerTime desc");
277             return q.list();
278         } catch (HibernateException e) {
279             log.error("getAllUsers()", e);
280             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.USER_FETCH_ALL, e));
281         }
282     }
283
284     /**
285      * Returns list of the users, one of whose friends is the given user.
286      *
287      * @param u
288      * @return
289      * @throws BlogException
290      */

291     public List JavaDoc getFriendOfList(User u) throws BlogunityException {
292         Session session = HibernateUtil.getSession();
293         try {
294             Criteria criteria = session.createCriteria(User.class).createCriteria("friends").add(
295                     Expression.eq("id", u.getId()));
296
297             List JavaDoc returnList = criteria.list();
298             return returnList;
299
300         } catch (HibernateException e) {
301             log.error("getFriendOfList(User)", e);
302             throw new BlogunityException(I18NStatusFactory.create(
303                     I18N.ERRORS.USER_FETCH_FRIEND_OF_LIST, e));
304         }
305     }
306
307     /**
308      * Returns list of the users, whose nickname is like the given one (similar
309      * to sql's LIKE).
310      *
311      * @param likeNickname
312      * @return list of the users.
313      * @throws BlogException
314      */

315     public List JavaDoc getUsersWithNicknameLike(String JavaDoc likeNickname) throws BlogunityException {
316         Session session = HibernateUtil.getSession();
317         try {
318
319             Criteria criteria = session.createCriteria(User.class);
320             criteria.add(Expression.like("nickname", likeNickname, MatchMode.ANYWHERE));
321             return criteria.list();
322
323         } catch (HibernateException e) {
324             log.error("getUsersWithNicknameLike(likeNickname)", e);
325             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.LIKE_NAME_NOT_FOUND,
326                     new String JavaDoc[]{"users", likeNickname}, e));
327         }
328     }
329 }
Popular Tags