KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > dao > generic > GenericUserDAO


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: Apr 5, 2003 / 11:43:46 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.dao.generic;
44
45 import java.security.NoSuchAlgorithmException JavaDoc;
46 import java.sql.PreparedStatement JavaDoc;
47 import java.sql.ResultSet JavaDoc;
48 import java.sql.Timestamp JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52
53 import net.jforum.JForumExecutionContext;
54 import net.jforum.dao.DataAccessDriver;
55 import net.jforum.entities.Group;
56 import net.jforum.entities.KarmaStatus;
57 import net.jforum.entities.User;
58 import net.jforum.sso.LoginAuthenticator;
59 import net.jforum.util.preferences.ConfigKeys;
60 import net.jforum.util.preferences.SystemGlobals;
61
62 /**
63  * @author Rafael Steil
64  * @version $Id: GenericUserDAO.java,v 1.15 2006/02/28 01:10:47 rafaelsteil Exp $
65  */

66 public class GenericUserDAO extends AutoKeys implements net.jforum.dao.UserDAO
67 {
68     private static LoginAuthenticator loginAuthenticator;
69     
70     public GenericUserDAO()
71     {
72         String JavaDoc className = SystemGlobals.getValue(ConfigKeys.LOGIN_AUTHENTICATOR);
73
74         try {
75             loginAuthenticator = (LoginAuthenticator)Class.forName(className).newInstance();
76             loginAuthenticator.setUserModel(this);
77         }
78         catch (Exception JavaDoc e) {
79             throw new RuntimeException JavaDoc("Error while trying to instantiate a "
80                     + "login.authenticator instance (" + className + "): " + e);
81         }
82     }
83     
84     /**
85      * @see net.jforum.dao.UserDAO#selectById(int)
86      */

87     public User selectById(int userId) throws Exception JavaDoc
88     {
89         String JavaDoc q = SystemGlobals.getSql("UserModel.selectById");
90         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(q);
91         p.setInt(1, userId);
92         
93         ResultSet JavaDoc rs = p.executeQuery();
94         User u = new User();
95         
96         if (rs.next()) {
97             this.fillUserFromResultSet(u, rs);
98             u.setPrivateMessagesCount(rs.getInt("private_messages"));
99             
100             rs.close();
101             p.close();
102
103             // User groups
104
p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.selectGroups"));
105             p.setInt(1, userId);
106             
107             rs = p.executeQuery();
108             while (rs.next()) {
109                 Group g = new Group();
110                 g.setName(rs.getString("group_name"));
111                 g.setId(rs.getInt("group_id"));
112
113                 u.getGroupsList().add(g);
114             }
115         }
116         
117         rs.close();
118         p.close();
119
120         return u;
121     }
122
123     public User selectByName(String JavaDoc username) throws Exception JavaDoc
124     {
125         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.selectByName"));
126         p.setString(1, username);
127         
128         ResultSet JavaDoc rs = p.executeQuery();
129         User u = null;
130         
131         if (rs.next()) {
132             u = new User();
133             fillUserFromResultSet(u, rs);
134         }
135         
136         rs.close();
137         p.close();
138
139         return u;
140     }
141
142     protected void fillUserFromResultSet(User u, ResultSet JavaDoc rs) throws Exception JavaDoc
143     {
144         u.setAim(rs.getString("user_aim"));
145         u.setAvatar(rs.getString("user_avatar"));
146         u.setGender(rs.getString("gender"));
147         u.setRankId(rs.getInt("rank_id"));
148         u.setThemeId(rs.getInt("themes_id"));
149         u.setPrivateMessagesEnabled(rs.getInt("user_allow_pm") == 1);
150         u.setNotifyOnMessagesEnabled(rs.getInt("user_notify") == 1);
151         u.setViewOnlineEnabled(rs.getInt("user_viewonline") == 1);
152         u.setPassword(rs.getString("user_password"));
153         u.setViewEmailEnabled(rs.getInt("user_viewemail") == 1);
154         u.setViewOnlineEnabled(rs.getInt("user_allow_viewonline") == 1);
155         u.setAvatarEnabled(rs.getInt("user_allowavatar") == 1);
156         u.setBbCodeEnabled(rs.getInt("user_allowbbcode") == 1);
157         u.setHtmlEnabled(rs.getInt("user_allowhtml") == 1);
158         u.setSmiliesEnabled(rs.getInt("user_allowsmilies") == 1);
159         u.setEmail(rs.getString("user_email"));
160         u.setFrom(rs.getString("user_from"));
161         u.setIcq(rs.getString("user_icq"));
162         u.setId(rs.getInt("user_id"));
163         u.setInterests(rs.getString("user_interests"));
164         u.setBiography(rs.getString("user_biography"));
165         u.setLastVisit(rs.getTimestamp("user_lastvisit"));
166         u.setOccupation(rs.getString("user_occ"));
167         u.setTotalPosts(rs.getInt("user_posts"));
168         u.setRegistrationDate(rs.getTimestamp("user_regdate"));
169         u.setSignature(rs.getString("user_sig"));
170         u.setWebSite(rs.getString("user_website"));
171         u.setYim(rs.getString("user_yim"));
172         u.setUsername(rs.getString("username"));
173         u.setAttachSignatureEnabled(rs.getInt("user_attachsig") == 1);
174         u.setMsnm(rs.getString("user_msnm"));
175         u.setLang(rs.getString("user_lang"));
176         u.setActive(rs.getInt("user_active"));
177         u.setKarma(new KarmaStatus(u.getId(), rs.getDouble("user_karma")));
178         u.setNotifyPrivateMessagesEnabled(rs.getInt("user_notify_pm") == 1);
179         u.setDeleted(rs.getInt("deleted"));
180         
181         String JavaDoc actkey = rs.getString("user_actkey");
182         u.setActivationKey(actkey == null || "".equals(actkey) ? null : actkey);
183     }
184
185     /**
186      * @see net.jforum.dao.UserDAO#delete(int)
187      */

188     public void delete(int userId) throws Exception JavaDoc
189     {
190         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.deletedStatus"));
191         p.setInt(1, 1);
192         p.setInt(2, userId);
193         
194         p.executeUpdate();
195         p.close();
196     }
197
198     /**
199      * @see net.jforum.dao.UserDAO#update(net.jforum.User)
200      */

201     public void update(User user) throws Exception JavaDoc
202     {
203         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.update"));
204         
205         p.setString(1, user.getAim());
206         p.setString(2, user.getAvatar());
207         p.setString(3, user.getGender());
208         p.setInt(4, user.getThemeId());
209         p.setInt(5, user.isPrivateMessagesEnabled() ? 1 : 0);
210         p.setInt(6, user.isAvatarEnabled() ? 1 : 0);
211         p.setInt(7, user.isBbCodeEnabled() ? 1 : 0);
212         p.setInt(8, user.isHtmlEnabled() ? 1 : 0);
213         p.setInt(9, user.isSmiliesEnabled() ? 1 : 0);
214         p.setString(10, user.getEmail());
215         p.setString(11, user.getFrom());
216         p.setString(12, user.getIcq());
217         p.setString(13, user.getInterests());
218         p.setString(14, user.getOccupation());
219         p.setString(15, user.getSignature());
220         p.setString(16, user.getWebSite());
221         p.setString(17, user.getYim());
222         p.setString(18, user.getMsnm());
223         p.setString(19, user.getPassword());
224         p.setInt(20, user.isViewEmailEnabled() ? 1 : 0);
225         p.setInt(21, user.isViewOnlineEnabled() ? 1 : 0);
226         p.setInt(22, user.isNotifyOnMessagesEnabled() ? 1 : 0);
227         p.setInt(23, user.getAttachSignatureEnabled() ? 1 : 0);
228         p.setString(24, user.getUsername());
229         p.setString(25, user.getLang());
230         p.setInt(26, user.isNotifyPrivateMessagesEnabled() ? 1 : 0);
231         p.setString(27, user.getBiography());
232         p.setInt(28, user.getId());
233         
234         p.executeUpdate();
235         p.close();
236     }
237
238     /**
239      * @see net.jforum.dao.UserDAO#addNew(net.jforum.User)
240      */

241     public int addNew(User user) throws Exception JavaDoc
242     {
243         PreparedStatement JavaDoc p = this.getStatementForAutoKeys("UserModel.addNew");
244         
245         this.initNewUser(user, p);
246         
247         this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("UserModel.lastGeneratedUserId"));
248         int id = this.executeAutoKeysQuery(p);
249         p.close();
250         
251         this.addToGroup(id, new int[] { SystemGlobals.getIntValue(ConfigKeys.DEFAULT_USER_GROUP) });
252         
253         user.setId(id);
254         return id;
255     }
256
257     protected void initNewUser(User user, PreparedStatement JavaDoc p) throws Exception JavaDoc
258     {
259         p.setString(1, user.getUsername());
260         p.setString(2, user.getPassword());
261         p.setString(3, user.getEmail());
262         p.setTimestamp(4, new Timestamp JavaDoc(System.currentTimeMillis()));
263         p.setString(5, user.getActivationKey());
264     }
265
266     /**
267      * @see net.jforum.dao.UserDAO#addNewWithId(net.jforum.User)
268      */

269     public void addNewWithId(User user) throws Exception JavaDoc
270     {
271         PreparedStatement JavaDoc p = this.getStatementForAutoKeys("UserModel.addNewWithId");
272         
273         this.initNewUser(user, p);
274         p.setInt(6, user.getId());
275         
276         p.executeUpdate();
277         p.close();
278         
279         this.addToGroup(user.getId(), new int[] { SystemGlobals.getIntValue(ConfigKeys.DEFAULT_USER_GROUP) });
280     }
281
282     /**
283      * @see net.jforum.dao.UserDAO#decrementPosts(int)
284      */

285     public void decrementPosts(int userId) throws Exception JavaDoc
286     {
287         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.decrementPosts"));
288         p.setInt(1, userId);
289         
290         p.executeUpdate();
291         p.close();
292     }
293
294     /**
295      * @see net.jforum.dao.UserDAO#incrementPosts(int)
296      */

297     public void incrementPosts(int userId) throws Exception JavaDoc
298     {
299         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.incrementPosts"));
300         p.setInt(1, userId);
301         
302         p.executeUpdate();
303         p.close();
304     }
305
306     /**
307      * @see net.jforum.dao.UserDAO#incrementRanking(int)
308      */

309     public void setRanking(int userId, int rankingId) throws Exception JavaDoc
310     {
311         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.rankingId"));
312         p.setInt(1, rankingId);
313         p.setInt(2, userId);
314         
315         p.executeUpdate();
316         p.close();
317     }
318
319     /**
320      * @see net.jforum.dao.UserDAO#setActive(int, boolean)
321      */

322     public void setActive(int userId, boolean active) throws Exception JavaDoc
323     {
324         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.activeStatus"));
325         p.setInt(1, active ? 1 : 0);
326         p.setInt(2, userId);
327         
328         p.executeUpdate();
329         p.close();
330     }
331
332     /**
333      * @see net.jforum.dao.UserDAO#undelete(int)
334      */

335     public void undelete(int userId) throws Exception JavaDoc
336     {
337         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.deletedStatus"));
338         p.setInt(1, 0);
339         p.setInt(2, userId);
340         
341         p.executeUpdate();
342         p.close();
343     }
344
345     /**
346      * @see net.jforum.dao.UserDAO#selectAll()
347      */

348     public List JavaDoc selectAll() throws Exception JavaDoc
349     {
350         return selectAll(0, 0);
351     }
352
353     /**
354      * @see net.jforum.dao.UserDAO#selectAll(int, int)
355      */

356     public List JavaDoc selectAll(int startFrom, int count) throws Exception JavaDoc
357     {
358         PreparedStatement JavaDoc p;
359         
360         if (count > 0) {
361             p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.selectAllByLimit"));
362             p.setInt(1, startFrom);
363             p.setInt(2, count);
364         }
365         else {
366             p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.selectAll"));
367         }
368         
369         ResultSet JavaDoc rs = p.executeQuery();
370         List JavaDoc list = this.processSelectAll(rs);
371         rs.close();
372         p.close();
373         
374         return list;
375     }
376     
377     /**
378      * @see net.jforum.dao.UserDAO#selectAllWithKarma()
379      */

380     public List JavaDoc selectAllWithKarma() throws Exception JavaDoc
381     {
382         return this.selectAllWithKarma(0, 0);
383     }
384     
385     /**
386      * @see net.jforum.dao.UserDAO#selectAllWithKarma(int, int)
387      */

388     public List JavaDoc selectAllWithKarma(int startFrom, int count) throws Exception JavaDoc
389     {
390         return this.loadKarma( this.selectAll(startFrom, count) );
391     }
392     
393     protected List JavaDoc processSelectAll(ResultSet JavaDoc rs) throws Exception JavaDoc
394     {
395         List JavaDoc list = new ArrayList JavaDoc();
396
397         while (rs.next()) {
398             User u = new User();
399             
400             u.setEmail(rs.getString("user_email"));
401             u.setId(rs.getInt("user_id"));
402             u.setTotalPosts(rs.getInt("user_posts"));
403             u.setRegistrationDate(rs.getTimestamp("user_regdate"));
404             u.setUsername(rs.getString("username"));
405             u.setDeleted(rs.getInt("deleted"));
406             KarmaStatus karma = new KarmaStatus();
407             karma.setKarmaPoints(rs.getInt("user_karma"));
408             u.setKarma( karma );
409             u.setFrom(rs.getString("user_from"));
410             u.setWebSite(rs.getString("user_website"));
411             u.setViewEmailEnabled(rs.getInt("user_viewemail") == 1);
412             
413             list.add(u);
414         }
415         
416         return list;
417     }
418     
419     /**
420      * @see net.jforum.dao.UserDAO#selectAllByGroup(int, int, int)
421      */

422     public List JavaDoc selectAllByGroup(int groupId, int start, int count) throws Exception JavaDoc
423     {
424         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.selectAllByGroup"));
425         p.setInt(1, groupId);
426         p.setInt(2, start);
427         p.setInt(3, count);
428         
429         ResultSet JavaDoc rs = p.executeQuery();
430         List JavaDoc l = this.processSelectAll(rs);
431         rs.close();
432         
433         return l;
434     }
435
436     /**
437      * @see net.jforum.dao.UserDAO#getLastUserInfo()
438      */

439     public User getLastUserInfo() throws Exception JavaDoc
440     {
441         User u = new User();
442         
443         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.lastUserRegistered"));
444         ResultSet JavaDoc rs = p.executeQuery();
445         rs.next();
446         
447         u.setUsername(rs.getString("username"));
448         u.setId(rs.getInt("user_id"));
449         
450         rs.close();
451         p.close();
452         
453         return u;
454     }
455
456     /**
457      * @see net.jforum.dao.UserDAO#getTotalUsers()
458      */

459     public int getTotalUsers() throws Exception JavaDoc
460     {
461         return this.getTotalUsersCommon(JForumExecutionContext.getConnection().prepareStatement(
462                 SystemGlobals.getSql("UserModel.totalUsers")));
463     }
464     
465     /**
466      * @see net.jforum.dao.UserDAO#getTotalUsersByGroup(int)
467      */

468     public int getTotalUsersByGroup(int groupId) throws Exception JavaDoc
469     {
470         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
471                 SystemGlobals.getSql("UserModel.totalUsersByGroup"));
472         p.setInt(1, groupId);
473         
474         return this.getTotalUsersCommon(p);
475     }
476     
477     protected int getTotalUsersCommon(PreparedStatement JavaDoc p) throws Exception JavaDoc
478     {
479         ResultSet JavaDoc rs = p.executeQuery();
480         rs.next();
481         
482         int total = rs.getInt(1);
483         
484         rs.close();
485         p.close();
486         
487         return total;
488     }
489
490     /**
491      * @see net.jforum.dao.UserDAO#isDeleted(int user_id)
492      */

493     public boolean isDeleted(int userId) throws Exception JavaDoc
494     {
495         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.isDeleted"));
496         p.setInt(1, userId);
497         
498         int deleted = 0;
499         
500         ResultSet JavaDoc rs = p.executeQuery();
501         if (rs.next()) {
502             deleted = rs.getInt("deleted");
503         }
504         
505         rs.close();
506         p.close();
507         
508         return deleted == 1;
509     }
510     
511     /**
512      * @see net.jforum.dao.UserDAO#isUsernameRegistered(java.lang.String)
513      */

514     public boolean isUsernameRegistered(String JavaDoc username) throws Exception JavaDoc
515     {
516         boolean status = false;
517         
518         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.isUsernameRegistered"));
519         p.setString(1, username);
520         
521         ResultSet JavaDoc rs = p.executeQuery();
522         if (rs.next() && rs.getInt("registered") > 0) {
523             status = true;
524         }
525         
526         rs.close();
527         p.close();
528         
529         return status;
530     }
531
532     /**
533      * @see net.jforum.dao.UserDAO#validateLogin(java.lang.String, java.lang.String)
534      */

535     public User validateLogin(String JavaDoc username, String JavaDoc password) throws NoSuchAlgorithmException JavaDoc, Exception JavaDoc
536     {
537         return loginAuthenticator.validateLogin(username, password, null);
538     }
539
540     /**
541      * @see net.jforum.dao.UserDAO#addToGroup(int, int[])
542      */

543     public void addToGroup(int userId, int[] groupId) throws Exception JavaDoc
544     {
545         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.addToGroup"));
546         p.setInt(1, userId);
547         
548         for (int i = 0; i < groupId.length; i++) {
549             p.setInt(2, groupId[i]);
550             p.executeUpdate();
551         }
552         
553         p.close();
554     }
555
556     /**
557      * @see net.jforum.dao.UserDAO#removeFromGroup(int, int[])
558      */

559     public void removeFromGroup(int userId, int[] groupId) throws Exception JavaDoc
560     {
561         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.removeFromGroup"));
562         p.setInt(1, userId);
563         
564         for (int i = 0; i < groupId.length; i++) {
565             p.setInt(2, groupId[i]);
566             p.executeUpdate();
567         }
568         
569         p.close();
570     }
571
572     /**
573      * @see net.jforum.dao.UserDAO#saveNewPassword(java.lang.String, java.lang.String)
574      */

575     public void saveNewPassword(String JavaDoc password, String JavaDoc email) throws Exception JavaDoc
576     {
577         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.saveNewPassword"));
578         p.setString(1, password);
579         p.setString(2, email);
580         p.executeUpdate();
581         p.close();
582     }
583     
584     /**
585      * @see net.jforum.dao.UserDAO#validateLostPasswordHash(java.lang.String, java.lang.String)
586      */

587     public boolean validateLostPasswordHash(String JavaDoc email, String JavaDoc hash) throws Exception JavaDoc
588     {
589         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
590                 SystemGlobals.getSql("UserModel.validateLostPasswordHash"));
591         p.setString(1, hash);
592         p.setString(2, email);
593         
594         boolean status = false;
595         
596         ResultSet JavaDoc rs = p.executeQuery();
597         if (rs.next() && rs.getInt("valid") > 0) {
598             status = true;
599             
600             this.writeLostPasswordHash(email, "");
601         }
602         
603         rs.close();
604         p.close();
605         
606         return status;
607     }
608     
609     /**
610      * @see net.jforum.dao.UserDAO#writeLostPasswordHash(java.lang.String, java.lang.String)
611      */

612     public void writeLostPasswordHash(String JavaDoc email, String JavaDoc hash) throws Exception JavaDoc
613     {
614         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.writeLostPasswordHash"));
615         p.setString(1, hash);
616         p.setString(2, email);
617         p.executeUpdate();
618         p.close();
619     }
620     
621     /**
622      * @see net.jforum.dao.UserDAO#getUsernameByEmail(java.lang.String)
623      */

624     public String JavaDoc getUsernameByEmail(String JavaDoc email) throws Exception JavaDoc
625     {
626         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.getUsernameByEmail"));
627         p.setString(1, email);
628         
629         String JavaDoc username = "";
630         
631         ResultSet JavaDoc rs = p.executeQuery();
632         if (rs.next()) {
633             username = rs.getString("username");
634         }
635         
636         rs.close();
637         p.close();
638         
639         return username;
640     }
641
642     /**
643      * @see net.jforum.dao.UserDAO#findByName(java.lang.String, boolean)
644      */

645     public List JavaDoc findByName(String JavaDoc input, boolean exactMatch) throws Exception JavaDoc
646     {
647         List JavaDoc namesList = new ArrayList JavaDoc();
648         
649         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.findByName"));
650         p.setString(1, exactMatch ? input : "%" + input + "%");
651     
652         ResultSet JavaDoc rs = p.executeQuery();
653         while (rs.next()) {
654             User u = new User();
655             
656             u.setId(rs.getInt("user_id"));
657             u.setUsername(rs.getString("username"));
658             u.setEmail(rs.getString("user_email"));
659             u.setDeleted(rs.getInt("deleted"));
660
661             namesList.add(u);
662         }
663         
664         rs.close();
665         p.close();
666         
667         return namesList;
668     }
669
670     /**
671      * @see net.jforum.dao.UserDAO#validateActivationKeyHash(int, java.lang.String)
672      */

673     public boolean validateActivationKeyHash(int userId , String JavaDoc hash) throws Exception JavaDoc
674     {
675         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.validateActivationKeyHash"));
676         p.setString(1, hash);
677         p.setInt(2, userId);
678
679         boolean status = false;
680
681         ResultSet JavaDoc rs = p.executeQuery();
682         if (rs.next() && rs.getInt("valid") == 1) {
683             status = true;
684         }
685
686         rs.close();
687         p.close();
688
689         return status;
690     }
691
692     /**
693      * @see net.jforum.dao.UserDAO#writeUserActive(int)
694      */

695     public void writeUserActive(int userId) throws Exception JavaDoc
696     {
697         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.writeUserActive"));
698         p.setInt(1, userId);
699         p.executeUpdate();
700         p.close();
701     }
702     
703     /**
704      * @see net.jforum.dao.UserDAO#updateUsername(int, String)
705      */

706     public void updateUsername(int userId, String JavaDoc username) throws Exception JavaDoc
707     {
708         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.updateUsername"));
709         p.setString(1, username);
710         p.setInt(2, userId);
711         p.executeUpdate();
712         p.close();
713     }
714     
715     /**
716      * @see net.jforum.dao.UserDAO#hasUsernameChanged(int, java.lang.String)
717      */

718     public boolean hasUsernameChanged(int userId, String JavaDoc usernameToCheck) throws Exception JavaDoc
719     {
720         boolean status = false;
721         
722         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.getUsername"));
723         p.setString(1, usernameToCheck);
724         p.setInt(2, userId);
725         
726         String JavaDoc dbUsername = null;
727         
728         ResultSet JavaDoc rs = p.executeQuery();
729         if (rs.next()) {
730             dbUsername = rs.getString("username");
731         }
732         
733         if (!usernameToCheck.equals(dbUsername)) {
734             status = true;
735         }
736         
737         rs.close();
738         p.close();
739         
740         return status;
741     }
742     
743     /**
744      * Load KarmaStatus from a list of users.
745      * @param users
746      * @return
747      * @throws Exception
748      */

749     protected List JavaDoc loadKarma(List JavaDoc users) throws Exception JavaDoc{
750         List JavaDoc result = new ArrayList JavaDoc(users.size());
751         
752         User user = null;
753         Iterator JavaDoc iter = users.iterator();
754         while (iter.hasNext()) {
755             user = (User) iter.next();
756             //load Karma
757
DataAccessDriver.getInstance().newKarmaDAO().getUserTotalKarma(user);
758             result.add(user);
759         }
760         return result;
761     }
762 }
763
Popular Tags