KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > dao > UserDAO


1 /*
2  * $$Id: UserDAO.java,v 1.3 2005/06/07 12:32:29 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 package org.jresearch.gossip.dao;
26
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.security.SecureRandom JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Calendar JavaDoc;
35
36 import org.jresearch.gossip.IConst;
37 import org.jresearch.gossip.am.ban.BanMap;
38 import org.jresearch.gossip.beans.Ban;
39 import org.jresearch.gossip.beans.NamedValue;
40 import org.jresearch.gossip.beans.user.Sender;
41 import org.jresearch.gossip.beans.user.User;
42 import org.jresearch.gossip.beans.user.UserInfo;
43 import org.jresearch.gossip.beans.user.UserSettings;
44 import org.jresearch.gossip.dao.drivers.DbDriver;
45 import org.jresearch.gossip.dao.drivers.keygen.IKeyGenConst;
46 import org.jresearch.gossip.dao.drivers.keygen.KeyGenerator;
47 import org.jresearch.gossip.dao.drivers.keygen.KeyGeneratorFactory;
48 import org.jresearch.gossip.forms.ListForm;
49 import org.jresearch.gossip.forms.ProfileForm;
50 import org.jresearch.gossip.list.Mapping;
51 import org.jresearch.gossip.list.RecordsData;
52 import org.jresearch.gossip.util.MD5Digest;
53
54 /**
55  * DOCUMENT ME!
56  *
57  * @author Bel
58  */

59 public class UserDAO extends DAO {
60
61     private static UserDAO instance;
62
63     private static Object JavaDoc lock = new Object JavaDoc();
64
65     private final DbDriver dbDriver;
66
67     private final KeyGenerator keyGen;
68
69     private UserDAO() {
70         try {
71             this.dbDriver = DbDriver.getInstance();
72             this.keyGen = KeyGeneratorFactory
73                     .getKeyGenerator(IKeyGenConst.DEFAULT_KEYGEN);
74         } catch (SQLException JavaDoc ex) {
75             throw new RuntimeException JavaDoc("UserDAO not initialized", ex);
76         }
77     }
78
79     /**
80      * DOCUMENT ME!
81      *
82      * @return DOCUMENT ME!
83      */

84     public static UserDAO getInstance() {
85         if (instance == null) {
86             synchronized (lock) {
87                 if (instance == null) {
88                     instance = new UserDAO();
89                 }
90             }
91         }
92         return instance;
93     }
94
95     /**
96      * DOCUMENT ME!
97      *
98      * @param profile
99      * DOCUMENT ME!
100      *
101      * @return DOCUMENT ME!
102      *
103      * @throws SQLException
104      * DOCUMENT ME!
105      */

106     public boolean addUser(ProfileForm profile) throws SQLException JavaDoc {
107         Connection JavaDoc connection = this.dataSource.getConnection();
108         PreparedStatement JavaDoc st = null;
109         ResultSet JavaDoc rs = null;
110         boolean notExist = !isUserExist(profile.getLogin());
111
112         try {
113             if (notExist) {
114                 int uid = ((Integer JavaDoc) keyGen.generateKey(
115                         IKeyGenConst.KEY_NAMES[IKeyGenConst.KEY_USER],
116                         connection)).intValue();
117                 Calendar JavaDoc cl = Calendar.getInstance();
118                 cl.set(Integer.parseInt(profile.getDOB_year()), Integer
119                         .parseInt(profile.getDOB_month()), Integer
120                         .parseInt(profile.getDOB_day()));
121                 st = connection.prepareStatement(dbDriver.getQueries()
122                         .getUserQueries().getSql_ADD_USER());
123                 st.setString(1, profile.getLogin());
124                 st.setString(2, MD5Digest.digest(profile.getLogin(), profile
125                         .getPassword2()));
126                 st.setString(3, profile.getEmail());
127                 st.setString(4, profile.getHomePage());
128                 st.setString(5, profile.getIcq());
129                 st.setDate(6, new java.sql.Date JavaDoc(cl.getTime().getTime()));
130                 st.setString(7, profile.getPlace());
131                 st.setString(8, profile.getOccupation());
132                 st.setString(9, profile.getSignature());
133                 st.setInt(10, Integer.parseInt(profile.getMessPerPage()));
134                 st.setInt(11, Integer.parseInt(profile.getAutoLogin()));
135                 st.setInt(12, Integer.parseInt(profile.getShowEmail()));
136                 st.setDate(13, new java.sql.Date JavaDoc(0L));
137                 st.setInt(14, uid);
138
139                 st.executeUpdate();
140             }
141         } finally {
142             if (rs != null) {
143                 rs.close();
144             }
145
146             if (st != null) {
147                 st.close();
148             }
149
150             connection.close();
151         }
152
153         return notExist;
154     }
155
156     /**
157      *
158      * @param login
159      * @param email
160      * @param confirmcode
161      * @return
162      * @throws SQLException
163      */

164     public boolean addPendingUser(String JavaDoc login, String JavaDoc email, String JavaDoc confirmcode)
165             throws SQLException JavaDoc {
166         Connection JavaDoc connection = this.dataSource.getConnection();
167         PreparedStatement JavaDoc st = null;
168         ResultSet JavaDoc rs = null;
169         boolean notExist = !isUserExist(login)
170                 && !checkPendingUser(login, confirmcode);
171
172         try {
173             if (notExist) {
174                 st = connection.prepareStatement(dbDriver.getQueries()
175                         .getUserQueries().getSql_ADD_PENDING_USER());
176                 st.setString(1, login);
177                 st.setString(2, email);
178                 st.setString(3, confirmcode);
179                 st.setTimestamp(4, new java.sql.Timestamp JavaDoc(
180                         (new java.util.Date JavaDoc()).getTime()));
181
182                 st.executeUpdate();
183             }
184         } finally {
185             if (rs != null) {
186                 rs.close();
187             }
188
189             if (st != null) {
190                 st.close();
191             }
192
193             connection.close();
194         }
195
196         return notExist;
197     }
198
199     /**
200      * DOCUMENT ME!
201      *
202      * @param pass
203      * DOCUMENT ME!
204      * @param login
205      * DOCUMENT ME!
206      *
207      * @throws SQLException
208      * DOCUMENT ME!
209      */

210     public void changePassword(String JavaDoc pass, String JavaDoc login) throws SQLException JavaDoc {
211         Connection JavaDoc connection = this.dataSource.getConnection();
212         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
213                 .getQueries().getUserQueries().getSql_CHANGE_PASSWORD());
214
215         try {
216             st.setString(1, MD5Digest.digest(login, pass));
217             st.setString(2, login);
218             st.execute();
219         } finally {
220             st.close();
221             connection.close();
222         }
223     }
224
225     /**
226      * DOCUMENT ME!
227      *
228      * @return
229      * @throws SQLException
230      * DOCUMENT ME!
231      */

232     public int countUsers() throws SQLException JavaDoc {
233         Connection JavaDoc connection = this.dataSource.getConnection();
234         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
235                 .getQueries().getUserQueries().getSql_COUNT_USERS());
236         ResultSet JavaDoc rs = null;
237         int count = 0;
238
239         try {
240             rs = (ResultSet JavaDoc) st.executeQuery();
241
242             if (rs.next()) {
243                 count = rs.getInt(1);
244             }
245         } finally {
246             if (rs != null) {
247                 rs.close();
248             }
249
250             st.close();
251             connection.close();
252         }
253
254         return count;
255     }
256
257     /**
258      * @param login
259      * @param confirmcode
260      * @return
261      * @throws SQLException
262      */

263     public boolean checkPendingUser(String JavaDoc login, String JavaDoc confirmcode)
264             throws SQLException JavaDoc {
265         Connection JavaDoc connection = this.dataSource.getConnection();
266         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
267                 .getQueries().getUserQueries().getSql_CHECK_PENDING_USER());
268         ResultSet JavaDoc rs = null;
269         boolean success = false;
270
271         try {
272             st.setString(1, login);
273             st.setString(2, confirmcode);
274             rs = (ResultSet JavaDoc) st.executeQuery();
275
276             if (rs.next()) {
277                 success = true;
278             }
279         } finally {
280             if (rs != null) {
281                 rs.close();
282             }
283
284             st.close();
285             connection.close();
286         }
287
288         return success;
289     }
290
291     /**
292      * DOCUMENT ME!
293      *
294      * @param uid
295      *
296      * @throws SQLException
297      * DOCUMENT ME!
298      */

299     public void deleteUser(String JavaDoc uid) throws SQLException JavaDoc {
300         Connection JavaDoc connection = this.dataSource.getConnection();
301         PreparedStatement JavaDoc st = null;
302
303         try {
304             st = connection.prepareStatement(dbDriver.getQueries()
305                     .getUserQueries().getSql_GET_USER_BY_ID());
306             st.setInt(1, Integer.parseInt(uid));
307
308             User user = new User();
309             fillUser(st, user, false);
310             st = connection.prepareStatement(dbDriver.getQueries()
311                     .getUserQueries().getSql_DELETE_USER());
312             st.setInt(1, Integer.parseInt(uid));
313             st.execute();
314             st = connection.prepareStatement(dbDriver.getQueries()
315                     .getForumQueries().getSql_CLEAN_USER_MOD());
316             st.setString(1, user.getName());
317             st.execute();
318             st = connection.prepareStatement(dbDriver.getQueries()
319                     .getForumQueries().getSql_CLEAN_USER_SUBSCR());
320             st.setString(1, user.getName());
321             st.execute();
322             st = connection.prepareStatement(dbDriver.getQueries()
323                     .getForumQueries().getSql_MARK_USER_MESS());
324             st.setString(1, "<" + user.getName() + ">");
325             st.setString(2, user.getName());
326             st.execute();
327         } finally {
328             st.close();
329             connection.close();
330         }
331     }
332
333     /**
334      * @param login
335      * @return
336      * @throws SQLException
337      */

338     public boolean deletePendingUser(String JavaDoc login) throws SQLException JavaDoc {
339         Connection JavaDoc connection = this.dataSource.getConnection();
340         PreparedStatement JavaDoc st = null;
341
342         try {
343             st = connection.prepareStatement(dbDriver.getQueries()
344                     .getUserQueries().getSql_DELETE_PENDING_USER());
345             st.setString(1, login);
346             st.execute();
347             return true;
348         } finally {
349             st.close();
350             connection.close();
351         }
352     }
353
354     /**
355      * @param expirationTimeout
356      * @return
357      * @throws SQLException
358      */

359     public boolean deletePendingUser(long expirationTimeout)
360             throws SQLException JavaDoc {
361         Connection JavaDoc connection = this.dataSource.getConnection();
362         PreparedStatement JavaDoc st = null;
363
364         try {
365             st = connection.prepareStatement(dbDriver.getQueries()
366                     .getUserQueries().getSql_DELETE_EXPIRED_PENDING_USER());
367             st.setTimestamp(1, new java.sql.Timestamp JavaDoc(expirationTimeout));
368             st.execute();
369             return true;
370         } finally {
371             st.close();
372             connection.close();
373         }
374     }
375
376     /**
377      * DOCUMENT ME!
378      *
379      * @param st
380      * DOCUMENT ME!
381      * @param bean
382      * DOCUMENT ME!
383      *
384      * @throws SQLException
385      * DOCUMENT ME!
386      */

387     public void fillUser(PreparedStatement JavaDoc st, User bean) throws SQLException JavaDoc {
388         fillUser(st, bean, true);
389     }
390
391     /**
392      * DOCUMENT ME!
393      *
394      * @param st
395      * DOCUMENT ME!
396      * @param bean
397      * DOCUMENT ME!
398      * @param fullinfo
399      * DOCUMENT ME!
400      *
401      * @throws SQLException
402      * DOCUMENT ME!
403      */

404     public void fillUser(PreparedStatement JavaDoc st, User bean, boolean fullinfo)
405             throws SQLException JavaDoc {
406         ResultSet JavaDoc rs = null;
407
408         try {
409             rs = (ResultSet JavaDoc) st.executeQuery();
410
411             if (rs.next()) {
412                 UserInfo info = new UserInfo();
413                 UserSettings settings = new UserSettings();
414                 bean.setId(rs.getInt("id"));
415                 bean.setName(rs.getString("user_name"));
416                 info.setEmail(rs.getString("user_mail"));
417                 bean.setStatus(rs.getInt("user_status"));
418                 bean.setIntime(rs.getTimestamp("last_intime"));
419
420                 if (fullinfo) {
421                     settings.setAutologin(rs.getBoolean("auto_login"));
422                     bean.setPassword(rs.getString("user_pass"));
423                     settings.setMes_per_page(rs.getInt("mes_per_page"));
424                 }
425
426                 info.setCity(rs.getString("user_city"));
427                 info.setIcq(rs.getString("user_icq"));
428                 info.setBirthday(rs.getDate("user_dob"));
429                 info.setHomepage(rs.getString("user_hp"));
430                 info.setOccupation(rs.getString("user_occupation"));
431                 settings.setShow_user_mail(rs.getBoolean("show_user_mail"));
432                 settings.setSignature(rs.getString("user_signature"));
433                 bean.setInfo(info);
434                 bean.setSettings(settings);
435             }
436         } finally {
437             if (rs != null) {
438                 rs.close();
439             }
440         }
441     }
442
443     /**
444      * DOCUMENT ME!
445      *
446      * @param user
447      * DOCUMENT ME!
448      * @param recordsData
449      * @param dform
450      *
451      * @throws InstantiationException
452      * DOCUMENT ME!
453      * @throws IllegalAccessException
454      * DOCUMENT ME!
455      * @throws InvocationTargetException
456      * DOCUMENT ME!
457      * @throws NoSuchMethodException
458      * DOCUMENT ME!
459      * @throws SQLException
460      * DOCUMENT ME!
461      */

462     public void fillUserList(User user, RecordsData recordsData, ListForm lform)
463             throws InstantiationException JavaDoc, IllegalAccessException JavaDoc,
464             InvocationTargetException JavaDoc, NoSuchMethodException JavaDoc, SQLException JavaDoc {
465         Connection JavaDoc connection = this.dataSource.getConnection();
466         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
467                 .getQueries().getUserQueries().getSql_GET_USERS());
468         ResultSet JavaDoc rs = null;
469
470         try {
471             int blokSize = user.getSettings().getMes_per_page() * 2;
472             int currBlok = Integer.parseInt(lform.getBlock());
473             st.setInt(1, currBlok);
474             st.setInt(2, dbDriver.getLastRowIdx(currBlok, blokSize));
475             rs = (ResultSet JavaDoc) st.executeQuery();
476             recordsData.fillRecords(rs, Mapping.getInstance().UserMapping,
477                     User.class);
478             recordsData.setBlockSize(blokSize);
479             recordsData.setCurrBlock(currBlok);
480             recordsData.setRecordsCount(countUsers());
481         } finally {
482             if (rs != null) {
483                 rs.close();
484             }
485
486             st.close();
487             connection.close();
488         }
489     }
490
491     public String JavaDoc generatePassword() {
492         SecureRandom JavaDoc random = new SecureRandom JavaDoc();
493         String JavaDoc charset = IConst.VALUES.PASSWORD_DICTIONARY;
494         int length = charset.length();
495         StringBuffer JavaDoc newpass = new StringBuffer JavaDoc();
496
497         for (int i = 0; i < IConst.VALUES.DEFAULT_PASSWORD_LENGTH; i++) {
498             newpass.append(charset.charAt(random.nextInt(length)));
499         }
500
501         return newpass.toString();
502     }
503
504     /**
505      * DOCUMENT ME!
506      *
507      * @param username
508      * DOCUMENT ME!
509      * @param password
510      * DOCUMENT ME!
511      *
512      * @return DOCUMENT ME!
513      *
514      * @throws SQLException
515      * DOCUMENT ME!
516      */

517     public User getUser(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc {
518         Connection JavaDoc connection = this.dataSource.getConnection();
519         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
520                 .getQueries().getUserQueries().getSql_GET_USER());
521         User bean = new User();
522         st.setString(1, username);
523         st.setString(2, MD5Digest.digest(username, password));
524
525         try {
526             fillUser(st, bean);
527         } finally {
528             st.close();
529             connection.close();
530         }
531
532         return bean;
533     }
534
535     /**
536      * DOCUMENT ME!
537      *
538      * @return DOCUMENT ME!
539      *
540      * @throws SQLException
541      * DOCUMENT ME!
542      */

543     public ArrayList JavaDoc getUserEmails() throws SQLException JavaDoc {
544         Connection JavaDoc connection = this.dataSource.getConnection();
545         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
546                 .getQueries().getUserQueries().getSql_GET_USER_EMAILS());
547         ResultSet JavaDoc rs = null;
548         ArrayList JavaDoc list = new ArrayList JavaDoc();
549
550         try {
551             rs = (ResultSet JavaDoc) st.executeQuery();
552
553             while (rs.next()) {
554                 NamedValue nv = new NamedValue();
555                 nv.setName(rs.getString("user_name"));
556                 nv.setValue(rs.getString("user_mail"));
557                 list.add(nv);
558             }
559         } finally {
560             if (rs != null) {
561                 rs.close();
562             }
563
564             st.close();
565             connection.close();
566         }
567
568         return list;
569     }
570
571     /**
572      * DOCUMENT ME!
573      *
574      * @param username
575      * DOCUMENT ME!
576      * @param password
577      * DOCUMENT ME!
578      *
579      * @return DOCUMENT ME!
580      *
581      * @throws SQLException
582      * DOCUMENT ME!
583      */

584     public User getUserEncoded(String JavaDoc username, String JavaDoc password)
585             throws SQLException JavaDoc {
586         Connection JavaDoc connection = this.dataSource.getConnection();
587         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
588                 .getQueries().getUserQueries().getSql_GET_USER_ENCODED());
589         User bean = new User();
590         st.setString(1, username);
591         st.setString(2, password);
592
593         try {
594             fillUser(st, bean);
595         } finally {
596             st.close();
597             connection.close();
598         }
599
600         return bean;
601     }
602
603     /**
604      * @param username
605      * @return
606      * @throws SQLException
607      */

608     public User getUser(String JavaDoc username) throws SQLException JavaDoc {
609         Connection JavaDoc connection = this.dataSource.getConnection();
610         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
611                 .getQueries().getUserQueries().getSql_GET_USER_INFO_FULL());
612         User bean = new User();
613
614         try {
615             st.setString(1, username);
616             fillUser(st, bean);
617         } finally {
618             st.close();
619             connection.close();
620         }
621
622         return bean;
623     }
624
625     /**
626      * DOCUMENT ME!
627      *
628      * @param username
629      * DOCUMENT ME!
630      *
631      * @return DOCUMENT ME!
632      *
633      * @throws SQLException
634      * DOCUMENT ME!
635      */

636     public User getUserInfo(String JavaDoc username) throws SQLException JavaDoc {
637         Connection JavaDoc connection = this.dataSource.getConnection();
638         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
639                 .getQueries().getUserQueries().getSql_GET_USER_INFO_FULL());
640         User bean = new User();
641
642         try {
643             st.setString(1, username);
644             fillUser(st, bean, false);
645         } finally {
646             st.close();
647             connection.close();
648         }
649
650         return bean;
651     }
652
653     /**
654      * DOCUMENT ME!
655      *
656      * @param username
657      * DOCUMENT ME!
658      *
659      * @return
660      * @throws SQLException
661      * DOCUMENT ME!
662      */

663     public Sender getSenderInfo(String JavaDoc username) throws SQLException JavaDoc {
664         Connection JavaDoc connection = this.dataSource.getConnection();
665         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
666                 .getQueries().getUserQueries().getSql_GET_USER_INFO());
667         ResultSet JavaDoc rs = null;
668         Sender bean = new Sender();
669
670         try {
671             st.setString(1, username);
672             rs = (ResultSet JavaDoc) st.executeQuery();
673
674             if (rs.next()) {
675                 UserInfo info = new UserInfo();
676                 UserSettings settings = new UserSettings();
677                 bean.setName(rs.getString("user_name"));
678                 info.setCity(rs.getString("user_city"));
679                 settings.setSignature(rs.getString("user_signature"));
680                 bean.setTotalMess(rs.getInt("tot_mes"));
681                 bean.setStatus(rs.getInt("user_status"));
682                 bean.setSettings(settings);
683                 bean.setInfo(info);
684             }
685         } finally {
686             if (rs != null) {
687                 rs.close();
688             }
689
690             st.close();
691             connection.close();
692         }
693
694         return bean;
695     }
696
697     /**
698      * DOCUMENT ME!
699      *
700      * @param id
701      * DOCUMENT ME!
702      *
703      * @return DOCUMENT ME!
704      *
705      * @throws SQLException
706      * DOCUMENT ME!
707      */

708     public String JavaDoc getUserName(int id) throws SQLException JavaDoc {
709         Connection JavaDoc connection = this.dataSource.getConnection();
710         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
711                 .getQueries().getUserQueries().getSql_GET_USER_INFO_BY_ID());
712         ResultSet JavaDoc rs = null;
713         String JavaDoc name = null;
714
715         try {
716             st.setInt(1, id);
717             rs = (ResultSet JavaDoc) st.executeQuery();
718
719             if (rs.next()) {
720                 name = rs.getString("user_name");
721             }
722         } finally {
723             if (rs != null) {
724                 rs.close();
725             }
726
727             st.close();
728             connection.close();
729         }
730
731         return name;
732     }
733
734     /**
735      * DOCUMENT ME!
736      *
737      * @param login
738      *
739      * @return
740      * @throws SQLException
741      * DOCUMENT ME!
742      */

743     public boolean isUserExist(String JavaDoc login) throws SQLException JavaDoc {
744         Connection JavaDoc connection = this.dataSource.getConnection();
745         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
746                 .getQueries().getUserQueries().getSql_CHECK_USER());
747         ResultSet JavaDoc rs = null;
748         boolean success = false;
749
750         try {
751             st.setString(1, login);
752             rs = (ResultSet JavaDoc) st.executeQuery();
753
754             if (rs.next()) {
755                 success = true;
756             }
757         } finally {
758             if (rs != null) {
759                 rs.close();
760             }
761
762             st.close();
763             connection.close();
764         }
765
766         return success;
767     }
768
769     /**
770      * DOCUMENT ME!
771      *
772      * @param email
773      * @param login
774      * DOCUMENT ME!
775      *
776      * @return
777      * @throws SQLException
778      * DOCUMENT ME!
779      */

780     public User setNewPassword(String JavaDoc email, String JavaDoc login) throws SQLException JavaDoc {
781         Connection JavaDoc connection = this.dataSource.getConnection();
782         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
783                 .getQueries().getUserQueries().getSql_CHECK_USER_WITH_EMAIL());
784         ResultSet JavaDoc rs = null;
785         String JavaDoc newpass = null;
786         User user = new User();
787
788         try {
789             st.setString(1, email);
790             st.setString(2, login);
791             rs = (ResultSet JavaDoc) st.executeQuery();
792             fillUser(st, user, false);
793
794             if (user.getStatus() > 0) {
795                 newpass = generatePassword();
796                 st = connection.prepareStatement(dbDriver.getQueries()
797                         .getUserQueries().getSql_CHANGE_PASSWORD());
798                 st.setString(1, MD5Digest.digest(login, newpass));
799                 st.setString(2, login);
800                 st.execute();
801                 user.setPassword(newpass);
802             }
803         } finally {
804             if (rs != null) {
805                 rs.close();
806             }
807
808             st.close();
809             connection.close();
810         }
811
812         return user;
813     }
814
815     /**
816      * DOCUMENT ME!
817      *
818      * @param login
819      * @param status
820      *
821      * @throws SQLException
822      * DOCUMENT ME!
823      */

824     public void setUserStatus(String JavaDoc login, int status) throws SQLException JavaDoc {
825         Connection JavaDoc connection = this.dataSource.getConnection();
826
827         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
828                 .getQueries().getUserQueries().getSql_UPDATE_USER_STATUS());
829
830         try {
831             st.setInt(1, status);
832             st.setString(2, login);
833             st.execute();
834
835             if (status < 7) {
836                 st = connection.prepareStatement(dbDriver.getQueries()
837                         .getForumQueries().getSql_CLEAN_USER_MOD());
838                 st.setString(1, login);
839                 st.execute();
840             }
841         } finally {
842             st.close();
843             connection.close();
844         }
845     }
846
847     /**
848      * DOCUMENT ME!
849      *
850      * @param uid
851      * DOCUMENT ME!
852      *
853      * @return DOCUMENT ME!
854      *
855      * @throws SQLException
856      * DOCUMENT ME!
857      */

858     public User getUserInfoShort(int uid) throws SQLException JavaDoc {
859         Connection JavaDoc connection = this.dataSource.getConnection();
860         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
861                 .getQueries().getUserQueries().getSql_GET_USER_INFO_BY_ID());
862         ResultSet JavaDoc rs = null;
863         User bean = new User();
864
865         try {
866             st.setInt(1, uid);
867             rs = st.executeQuery();
868
869             if (rs.next()) {
870                 bean.setName(rs.getString("user_name"));
871                 bean.setStatus(rs.getInt("user_status"));
872             }
873         } finally {
874             if (rs != null) {
875                 rs.close();
876             }
877
878             st.close();
879             connection.close();
880         }
881
882         return bean;
883     }
884
885     /**
886      * DOCUMENT ME!
887      *
888      * @param profile
889      * DOCUMENT ME!
890      * @param login
891      * DOCUMENT ME!
892      *
893      * @return DOCUMENT ME!
894      *
895      * @throws SQLException
896      * DOCUMENT ME!
897      */

898     public boolean updateUser(ProfileForm profile, String JavaDoc login)
899             throws SQLException JavaDoc {
900         Connection JavaDoc connection = this.dataSource.getConnection();
901         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
902                 .getQueries().getUserQueries().getSql_CHECK_USER());
903         ResultSet JavaDoc rs = null;
904
905         try {
906             st.setString(1, profile.getLogin());
907             rs = (ResultSet JavaDoc) st.executeQuery();
908
909             if (rs.next()) {
910                 return false;
911             } else {
912                 Calendar JavaDoc cl = Calendar.getInstance();
913                 cl.set(Integer.parseInt(profile.getDOB_year()), Integer
914                         .parseInt(profile.getDOB_month()), Integer
915                         .parseInt(profile.getDOB_day()));
916                 st = connection.prepareStatement(dbDriver.getQueries()
917                         .getUserQueries().getSql_UPDATE_USER());
918
919                 st.setString(1, profile.getEmail());
920                 st.setString(2, profile.getHomePage());
921                 st.setString(3, profile.getIcq());
922                 st.setDate(4, new java.sql.Date JavaDoc(cl.getTime().getTime()));
923                 st.setString(5, profile.getPlace());
924                 st.setString(6, profile.getOccupation());
925                 st.setString(7, profile.getSignature());
926                 st.setInt(8, Integer.parseInt(profile.getMessPerPage()));
927                 st.setInt(9, Integer.parseInt(profile.getAutoLogin()));
928                 st.setInt(10, Integer.parseInt(profile.getShowEmail()));
929                 st.setString(11, login);
930
931                 st.execute();
932                 st = connection.prepareStatement(dbDriver.getQueries()
933                         .getForumQueries().getSql_UPDATE_SUBSCRIBE());
934                 st.setString(1, profile.getEmail());
935                 st.setString(2, login);
936                 st.execute();
937
938                 return true;
939             }
940         } finally {
941             if (rs != null) {
942                 rs.close();
943             }
944
945             st.close();
946             connection.close();
947         }
948     }
949
950     /**
951      * DOCUMENT ME!
952      *
953      * @param login
954      *
955      * @throws SQLException
956      * DOCUMENT ME!
957      */

958     public void updateIntime(String JavaDoc login) throws SQLException JavaDoc {
959         Connection JavaDoc connection = this.dataSource.getConnection();
960         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
961                 .getQueries().getUserQueries().getSql_SET_LAST_INTIME());
962
963         try {
964             st.setString(1, login);
965             st.execute();
966         } finally {
967             st.close();
968             connection.close();
969         }
970     }
971
972     /**
973      * @param banMap
974      * @throws SQLException
975      */

976     public void fillBanMap(BanMap banMap) throws SQLException JavaDoc {
977         Connection JavaDoc connection = this.dataSource.getConnection();
978         PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
979                 .getQueries().getUserQueries().getSql_GET_BAN_MAP());
980         ResultSet JavaDoc rs = null;
981         try {
982             rs = (ResultSet JavaDoc) st.executeQuery();
983
984             while (rs.next()) {
985                 banMap.add(rs.getString("ban_mask"), rs.getInt("type_id"));
986             }
987         } finally {
988             if (rs != null) {
989                 rs.close();
990             }
991
992             st.close();
993             connection.close();
994         }
995     }
996
997     /**
998      * @param ban
999      */

1000    public void deleteBan(Ban ban) throws SQLException JavaDoc {
1001        Connection JavaDoc connection = this.dataSource.getConnection();
1002        PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
1003                .getQueries().getUserQueries().getSql_DELETE_BAN());
1004
1005        try {
1006            st.setInt(1, ban.getType());
1007            st.setString(2, ban.getMask());
1008            st.execute();
1009        } finally {
1010            st.close();
1011            connection.close();
1012        }
1013
1014    }
1015
1016    /**
1017     * @param ban
1018     */

1019    public void addBan(Ban ban) throws SQLException JavaDoc {
1020        Connection JavaDoc connection = this.dataSource.getConnection();
1021        PreparedStatement JavaDoc st = connection.prepareStatement(dbDriver
1022                .getQueries().getUserQueries().getADD_BAN());
1023
1024        try {
1025            st.setInt(1, ban.getType());
1026            st.setString(2, ban.getMask());
1027            st.execute();
1028        } finally {
1029            st.close();
1030            connection.close();
1031        }
1032    }
1033}
Popular Tags