0001: /*
0002: * Copyright (c) JForum Team
0003: * All rights reserved.
0004: *
0005: * Redistribution and use in source and binary forms,
0006: * with or without modification, are permitted provided
0007: * that the following conditions are met:
0008: *
0009: * 1) Redistributions of source code must retain the above
0010: * copyright notice, this list of conditions and the
0011: * following disclaimer.
0012: * 2) Redistributions in binary form must reproduce the
0013: * above copyright notice, this list of conditions and
0014: * the following disclaimer in the documentation and/or
0015: * other materials provided with the distribution.
0016: * 3) Neither the name of "Rafael Steil" nor
0017: * the names of its contributors may be used to endorse
0018: * or promote products derived from this software without
0019: * specific prior written permission.
0020: *
0021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
0022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
0023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
0024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
0027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
0028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
0032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
0033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
0034: * IN CONTRACT, STRICT LIABILITY, OR TORT
0035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
0036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
0037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
0038: *
0039: * This file creation date: Apr 5, 2003 / 11:43:46 PM
0040: * The JForum Project
0041: * http://www.jforum.net
0042: */
0043: package net.jforum.dao.generic;
0044:
0045: import java.sql.PreparedStatement;
0046: import java.sql.ResultSet;
0047: import java.sql.SQLException;
0048: import java.sql.Timestamp;
0049: import java.util.ArrayList;
0050: import java.util.Date;
0051: import java.util.Iterator;
0052: import java.util.List;
0053:
0054: import net.jforum.JForumExecutionContext;
0055: import net.jforum.dao.DataAccessDriver;
0056: import net.jforum.dao.UserDAO;
0057: import net.jforum.entities.Group;
0058: import net.jforum.entities.KarmaStatus;
0059: import net.jforum.entities.User;
0060: import net.jforum.exceptions.DatabaseException;
0061: import net.jforum.exceptions.ForumException;
0062: import net.jforum.sso.LoginAuthenticator;
0063: import net.jforum.util.DbUtils;
0064: import net.jforum.util.preferences.ConfigKeys;
0065: import net.jforum.util.preferences.SystemGlobals;
0066:
0067: /**
0068: * @author Rafael Steil
0069: * @version $Id: GenericUserDAO.java,v 1.29 2007/09/21 17:26:09 rafaelsteil Exp $
0070: */
0071: public class GenericUserDAO extends AutoKeys implements UserDAO {
0072: private static LoginAuthenticator loginAuthenticator;
0073:
0074: public GenericUserDAO() {
0075: loginAuthenticator = (LoginAuthenticator) SystemGlobals
0076: .getObjectValue(ConfigKeys.LOGIN_AUTHENTICATOR_INSTANCE);
0077:
0078: if (loginAuthenticator == null) {
0079: throw new ForumException(
0080: "There is no login.authenticator configured. Check your login.authenticator configuration key.");
0081: }
0082:
0083: loginAuthenticator.setUserModel(this );
0084: }
0085:
0086: /**
0087: * @see net.jforum.dao.UserDAO#pendingActivations()
0088: */
0089: public List pendingActivations() {
0090: PreparedStatement p = null;
0091: ResultSet rs = null;
0092:
0093: List l = new ArrayList();
0094:
0095: try {
0096: p = JForumExecutionContext
0097: .getConnection()
0098: .prepareStatement(
0099: SystemGlobals
0100: .getSql("UserModel.pendingActivations"));
0101:
0102: rs = p.executeQuery();
0103:
0104: while (rs.next()) {
0105: User user = new User();
0106:
0107: user.setId(rs.getInt("user_id"));
0108: user.setUsername(rs.getString("username"));
0109: user.setRegistrationDate(new Date(rs.getTimestamp(
0110: "user_regdate").getTime()));
0111:
0112: l.add(user);
0113: }
0114: } catch (SQLException e) {
0115: throw new DatabaseException(e);
0116: } finally {
0117: DbUtils.close(rs, p);
0118: }
0119:
0120: return l;
0121: }
0122:
0123: /**
0124: * @see net.jforum.dao.UserDAO#selectById(int)
0125: */
0126: public User selectById(int userId) {
0127: String q = SystemGlobals.getSql("UserModel.selectById");
0128: PreparedStatement p = null;
0129: ResultSet rs = null;
0130:
0131: try {
0132: p = JForumExecutionContext.getConnection()
0133: .prepareStatement(q);
0134: p.setInt(1, userId);
0135:
0136: rs = p.executeQuery();
0137: User u = new User();
0138:
0139: if (rs.next()) {
0140: this .fillUserFromResultSet(u, rs);
0141: u
0142: .setPrivateMessagesCount(rs
0143: .getInt("private_messages"));
0144:
0145: rs.close();
0146: p.close();
0147:
0148: // User groups
0149: p = JForumExecutionContext
0150: .getConnection()
0151: .prepareStatement(
0152: SystemGlobals
0153: .getSql("UserModel.selectGroups"));
0154: p.setInt(1, userId);
0155:
0156: rs = p.executeQuery();
0157: while (rs.next()) {
0158: Group g = new Group();
0159: g.setName(rs.getString("group_name"));
0160: g.setId(rs.getInt("group_id"));
0161:
0162: u.getGroupsList().add(g);
0163: }
0164: }
0165:
0166: return u;
0167: } catch (SQLException e) {
0168: throw new DatabaseException(e);
0169: } finally {
0170: DbUtils.close(rs, p);
0171: }
0172: }
0173:
0174: public User selectByName(String username) {
0175: PreparedStatement p = null;
0176: ResultSet rs = null;
0177: try {
0178: p = JForumExecutionContext.getConnection()
0179: .prepareStatement(
0180: SystemGlobals
0181: .getSql("UserModel.selectByName"));
0182: p.setString(1, username);
0183:
0184: rs = p.executeQuery();
0185: User u = null;
0186:
0187: if (rs.next()) {
0188: u = new User();
0189: this .fillUserFromResultSet(u, rs);
0190: }
0191:
0192: return u;
0193: } catch (SQLException e) {
0194: throw new DatabaseException(e);
0195: } finally {
0196: DbUtils.close(rs, p);
0197: }
0198: }
0199:
0200: protected void fillUserFromResultSet(User u, ResultSet rs)
0201: throws SQLException {
0202: u.setAim(rs.getString("user_aim"));
0203: u.setAvatar(rs.getString("user_avatar"));
0204: u.setGender(rs.getString("gender"));
0205: u.setRankId(rs.getInt("rank_id"));
0206: u.setThemeId(rs.getInt("themes_id"));
0207: u.setPrivateMessagesEnabled(rs.getInt("user_allow_pm") == 1);
0208: u.setNotifyOnMessagesEnabled(rs.getInt("user_notify") == 1);
0209: u.setViewOnlineEnabled(rs.getInt("user_viewonline") == 1);
0210: u.setPassword(rs.getString("user_password"));
0211: u.setViewEmailEnabled(rs.getInt("user_viewemail") == 1);
0212: u.setViewOnlineEnabled(rs.getInt("user_allow_viewonline") == 1);
0213: u.setAvatarEnabled(rs.getInt("user_allowavatar") == 1);
0214: u.setBbCodeEnabled(rs.getInt("user_allowbbcode") == 1);
0215: u.setHtmlEnabled(rs.getInt("user_allowhtml") == 1);
0216: u.setSmiliesEnabled(rs.getInt("user_allowsmilies") == 1);
0217: u.setEmail(rs.getString("user_email"));
0218: u.setFrom(rs.getString("user_from"));
0219: u.setIcq(rs.getString("user_icq"));
0220: u.setId(rs.getInt("user_id"));
0221: u.setInterests(rs.getString("user_interests"));
0222: u.setBiography(rs.getString("user_biography"));
0223: u.setLastVisit(rs.getTimestamp("user_lastvisit"));
0224: u.setOccupation(rs.getString("user_occ"));
0225: u.setTotalPosts(rs.getInt("user_posts"));
0226: u.setRegistrationDate(new Date(rs.getTimestamp("user_regdate")
0227: .getTime()));
0228: u.setSignature(rs.getString("user_sig"));
0229: u.setWebSite(rs.getString("user_website"));
0230: u.setYim(rs.getString("user_yim"));
0231: u.setUsername(rs.getString("username"));
0232: u.setAttachSignatureEnabled(rs.getInt("user_attachsig") == 1);
0233: u.setMsnm(rs.getString("user_msnm"));
0234: u.setLang(rs.getString("user_lang"));
0235: u.setActive(rs.getInt("user_active"));
0236: u.setKarma(new KarmaStatus(u.getId(), rs
0237: .getDouble("user_karma")));
0238: u
0239: .setNotifyPrivateMessagesEnabled(rs
0240: .getInt("user_notify_pm") == 1);
0241: u.setDeleted(rs.getInt("deleted"));
0242: u.setNotifyAlways(rs.getInt("user_notify_always") == 1);
0243: u.setNotifyText(rs.getInt("user_notify_text") == 1);
0244:
0245: String actkey = rs.getString("user_actkey");
0246: u.setActivationKey(actkey == null || "".equals(actkey) ? null
0247: : actkey);
0248: }
0249:
0250: /**
0251: * @see net.jforum.dao.UserDAO#delete(int)
0252: */
0253: public void delete(int userId) {
0254: PreparedStatement p = null;
0255: try {
0256: p = JForumExecutionContext.getConnection()
0257: .prepareStatement(
0258: SystemGlobals
0259: .getSql("UserModel.deletedStatus"));
0260: p.setInt(1, 1);
0261: p.setInt(2, userId);
0262:
0263: p.executeUpdate();
0264: } catch (SQLException e) {
0265: throw new DatabaseException(e);
0266: } finally {
0267: DbUtils.close(p);
0268: }
0269: }
0270:
0271: /**
0272: * @see net.jforum.dao.UserDAO#update(net.jforum.entities.User)
0273: */
0274: public void update(User user) {
0275: PreparedStatement p = null;
0276: try {
0277: p = JForumExecutionContext.getConnection()
0278: .prepareStatement(
0279: SystemGlobals.getSql("UserModel.update"));
0280:
0281: p.setString(1, user.getAim());
0282: p.setString(2, user.getAvatar());
0283: p.setString(3, user.getGender());
0284: p.setInt(4, user.getThemeId());
0285: p.setInt(5, user.isPrivateMessagesEnabled() ? 1 : 0);
0286: p.setInt(6, user.isAvatarEnabled() ? 1 : 0);
0287: p.setInt(7, user.isBbCodeEnabled() ? 1 : 0);
0288: p.setInt(8, user.isHtmlEnabled() ? 1 : 0);
0289: p.setInt(9, user.isSmiliesEnabled() ? 1 : 0);
0290: p.setString(10, user.getEmail());
0291: p.setString(11, user.getFrom());
0292: p.setString(12, user.getIcq());
0293: p.setString(13, user.getInterests());
0294: p.setString(14, user.getOccupation());
0295: p.setString(15, user.getSignature());
0296: p.setString(16, user.getWebSite());
0297: p.setString(17, user.getYim());
0298: p.setString(18, user.getMsnm());
0299: p.setString(19, user.getPassword());
0300: p.setInt(20, user.isViewEmailEnabled() ? 1 : 0);
0301: p.setInt(21, user.isViewOnlineEnabled() ? 1 : 0);
0302: p.setInt(22, user.isNotifyOnMessagesEnabled() ? 1 : 0);
0303: p.setInt(23, user.getAttachSignatureEnabled() ? 1 : 0);
0304: p.setString(24, user.getUsername());
0305: p.setString(25, user.getLang());
0306: p.setInt(26, user.isNotifyPrivateMessagesEnabled() ? 1 : 0);
0307: p.setString(27, user.getBiography());
0308:
0309: if (user.getLastVisit() == null) {
0310: user.setLastVisit(new Date());
0311: }
0312:
0313: p.setTimestamp(28, new Timestamp(user.getLastVisit()
0314: .getTime()));
0315: p.setInt(29, user.notifyAlways() ? 1 : 0);
0316: p.setInt(30, user.notifyText() ? 1 : 0);
0317: p.setInt(31, user.getRankId());
0318: p.setInt(32, user.getId());
0319:
0320: p.executeUpdate();
0321: } catch (SQLException e) {
0322: throw new DatabaseException(e);
0323: } finally {
0324: DbUtils.close(p);
0325: }
0326: }
0327:
0328: /**
0329: * @see net.jforum.dao.UserDAO#addNew(net.jforum.entities.User)
0330: */
0331: public int addNew(User user) {
0332: PreparedStatement p = null;
0333: try {
0334: p = this .getStatementForAutoKeys("UserModel.addNew");
0335:
0336: this .initNewUser(user, p);
0337:
0338: this .setAutoGeneratedKeysQuery(SystemGlobals
0339: .getSql("UserModel.lastGeneratedUserId"));
0340: int id = this .executeAutoKeysQuery(p);
0341:
0342: this .addToGroup(id, new int[] { SystemGlobals
0343: .getIntValue(ConfigKeys.DEFAULT_USER_GROUP) });
0344:
0345: user.setId(id);
0346: return id;
0347: } catch (SQLException e) {
0348: throw new DatabaseException(e);
0349: } finally {
0350: DbUtils.close(p);
0351: }
0352: }
0353:
0354: protected void initNewUser(User user, PreparedStatement p)
0355: throws SQLException {
0356: p.setString(1, user.getUsername());
0357: p.setString(2, user.getPassword());
0358: p.setString(3, user.getEmail());
0359: p.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
0360: p.setString(5, user.getActivationKey());
0361: }
0362:
0363: /**
0364: * @see net.jforum.dao.UserDAO#addNewWithId(net.jforum.entities.User)
0365: */
0366: public void addNewWithId(User user) {
0367: PreparedStatement p = null;
0368: try {
0369: p = this .getStatementForAutoKeys("UserModel.addNewWithId");
0370:
0371: this .initNewUser(user, p);
0372: p.setInt(6, user.getId());
0373:
0374: p.executeUpdate();
0375:
0376: this .addToGroup(user.getId(), new int[] { SystemGlobals
0377: .getIntValue(ConfigKeys.DEFAULT_USER_GROUP) });
0378: } catch (SQLException e) {
0379: throw new DatabaseException(e);
0380: } finally {
0381: DbUtils.close(p);
0382: }
0383: }
0384:
0385: /**
0386: * @see net.jforum.dao.UserDAO#decrementPosts(int)
0387: */
0388: public void decrementPosts(int userId) {
0389: PreparedStatement p = null;
0390: try {
0391: p = JForumExecutionContext
0392: .getConnection()
0393: .prepareStatement(
0394: SystemGlobals
0395: .getSql("UserModel.decrementPosts"));
0396: p.setInt(1, userId);
0397:
0398: p.executeUpdate();
0399: } catch (SQLException e) {
0400: throw new DatabaseException(e);
0401: } finally {
0402: DbUtils.close(p);
0403: }
0404: }
0405:
0406: /**
0407: * @see net.jforum.dao.UserDAO#incrementPosts(int)
0408: */
0409: public void incrementPosts(int userId) {
0410: PreparedStatement p = null;
0411: try {
0412: p = JForumExecutionContext
0413: .getConnection()
0414: .prepareStatement(
0415: SystemGlobals
0416: .getSql("UserModel.incrementPosts"));
0417: p.setInt(1, userId);
0418:
0419: p.executeUpdate();
0420: } catch (SQLException e) {
0421: throw new DatabaseException(e);
0422: } finally {
0423: DbUtils.close(p);
0424: }
0425: }
0426:
0427: /**
0428: * @see net.jforum.dao.UserDAO#incrementRanking(int)
0429: */
0430: public void setRanking(int userId, int rankingId) {
0431: PreparedStatement p = null;
0432: try {
0433: p = JForumExecutionContext
0434: .getConnection()
0435: .prepareStatement(
0436: SystemGlobals.getSql("UserModel.rankingId"));
0437: p.setInt(1, rankingId);
0438: p.setInt(2, userId);
0439:
0440: p.executeUpdate();
0441: } catch (SQLException e) {
0442: throw new DatabaseException(e);
0443: } finally {
0444: DbUtils.close(p);
0445: }
0446: }
0447:
0448: /**
0449: * @see net.jforum.dao.UserDAO#setActive(int, boolean)
0450: */
0451: public void setActive(int userId, boolean active) {
0452: PreparedStatement p = null;
0453: try {
0454: p = JForumExecutionContext.getConnection()
0455: .prepareStatement(
0456: SystemGlobals
0457: .getSql("UserModel.activeStatus"));
0458: p.setInt(1, active ? 1 : 0);
0459: p.setInt(2, userId);
0460:
0461: p.executeUpdate();
0462: } catch (SQLException e) {
0463: throw new DatabaseException(e);
0464: } finally {
0465: DbUtils.close(p);
0466: }
0467: }
0468:
0469: /**
0470: * @see net.jforum.dao.UserDAO#undelete(int)
0471: */
0472: public void undelete(int userId) {
0473: PreparedStatement p = null;
0474: try {
0475: p = JForumExecutionContext.getConnection()
0476: .prepareStatement(
0477: SystemGlobals
0478: .getSql("UserModel.deletedStatus"));
0479: p.setInt(1, 0);
0480: p.setInt(2, userId);
0481:
0482: p.executeUpdate();
0483: } catch (SQLException e) {
0484: throw new DatabaseException(e);
0485: } finally {
0486: DbUtils.close(p);
0487: }
0488: }
0489:
0490: /**
0491: * @see net.jforum.dao.UserDAO#selectAll()
0492: */
0493: public List selectAll() {
0494: return selectAll(0, 0);
0495: }
0496:
0497: /**
0498: * @see net.jforum.dao.UserDAO#selectAll(int, int)
0499: */
0500: public List selectAll(int startFrom, int count) {
0501: PreparedStatement p = null;
0502: ResultSet rs = null;
0503:
0504: try {
0505: if (count > 0) {
0506: p = JForumExecutionContext
0507: .getConnection()
0508: .prepareStatement(
0509: SystemGlobals
0510: .getSql("UserModel.selectAllByLimit"));
0511: p.setInt(1, startFrom);
0512: p.setInt(2, count);
0513: } else {
0514: p = JForumExecutionContext.getConnection()
0515: .prepareStatement(
0516: SystemGlobals
0517: .getSql("UserModel.selectAll"));
0518: }
0519:
0520: rs = p.executeQuery();
0521:
0522: return this .processSelectAll(rs);
0523: } catch (SQLException e) {
0524: throw new DatabaseException(e);
0525: } finally {
0526: DbUtils.close(rs, p);
0527: }
0528: }
0529:
0530: /**
0531: * @see net.jforum.dao.UserDAO#selectAllWithKarma()
0532: */
0533: public List selectAllWithKarma() {
0534: return this .selectAllWithKarma(0, 0);
0535: }
0536:
0537: /**
0538: * @see net.jforum.dao.UserDAO#selectAllWithKarma(int, int)
0539: */
0540: public List selectAllWithKarma(int startFrom, int count) {
0541: return this .loadKarma(this .selectAll(startFrom, count));
0542: }
0543:
0544: protected List processSelectAll(ResultSet rs) throws SQLException {
0545: List list = new ArrayList();
0546:
0547: while (rs.next()) {
0548: User u = new User();
0549:
0550: u.setEmail(rs.getString("user_email"));
0551: u.setId(rs.getInt("user_id"));
0552: u.setTotalPosts(rs.getInt("user_posts"));
0553: u.setRegistrationDate(new Date(rs.getTimestamp(
0554: "user_regdate").getTime()));
0555: u.setUsername(rs.getString("username"));
0556: u.setDeleted(rs.getInt("deleted"));
0557: KarmaStatus karma = new KarmaStatus();
0558: karma.setKarmaPoints(rs.getInt("user_karma"));
0559: u.setKarma(karma);
0560: u.setFrom(rs.getString("user_from"));
0561: u.setWebSite(rs.getString("user_website"));
0562: u.setViewEmailEnabled(rs.getInt("user_viewemail") == 1);
0563:
0564: list.add(u);
0565: }
0566:
0567: return list;
0568: }
0569:
0570: /**
0571: * @see net.jforum.dao.UserDAO#selectAllByGroup(int, int, int)
0572: */
0573: public List selectAllByGroup(int groupId, int start, int count) {
0574: PreparedStatement p = null;
0575: ResultSet rs = null;
0576: try {
0577: p = JForumExecutionContext
0578: .getConnection()
0579: .prepareStatement(
0580: SystemGlobals
0581: .getSql("UserModel.selectAllByGroup"));
0582: p.setInt(1, groupId);
0583: p.setInt(2, start);
0584: p.setInt(3, count);
0585:
0586: rs = p.executeQuery();
0587:
0588: return this .processSelectAll(rs);
0589: } catch (SQLException e) {
0590: throw new DatabaseException(e);
0591: } finally {
0592: DbUtils.close(rs, p);
0593: }
0594: }
0595:
0596: /**
0597: * @see net.jforum.dao.UserDAO#getLastUserInfo()
0598: */
0599: public User getLastUserInfo() {
0600: PreparedStatement p = null;
0601: ResultSet rs = null;
0602: try {
0603: User u = new User();
0604:
0605: p = JForumExecutionContext
0606: .getConnection()
0607: .prepareStatement(
0608: SystemGlobals
0609: .getSql("UserModel.lastUserRegistered"));
0610: rs = p.executeQuery();
0611: rs.next();
0612:
0613: u.setUsername(rs.getString("username"));
0614: u.setId(rs.getInt("user_id"));
0615:
0616: return u;
0617: } catch (SQLException e) {
0618: throw new DatabaseException(e);
0619: } finally {
0620: DbUtils.close(rs, p);
0621: }
0622: }
0623:
0624: /**
0625: * @see net.jforum.dao.UserDAO#getTotalUsers()
0626: */
0627: public int getTotalUsers() {
0628: PreparedStatement preparedStatement = null;
0629: try {
0630: preparedStatement = JForumExecutionContext.getConnection()
0631: .prepareStatement(
0632: SystemGlobals
0633: .getSql("UserModel.totalUsers"));
0634: return this .getTotalUsersCommon(preparedStatement);
0635: } catch (SQLException e) {
0636: throw new DatabaseException(e);
0637: } finally {
0638: DbUtils.close(preparedStatement);
0639: }
0640: }
0641:
0642: /**
0643: * @see net.jforum.dao.UserDAO#getTotalUsersByGroup(int)
0644: */
0645: public int getTotalUsersByGroup(int groupId) {
0646: PreparedStatement p = null;
0647: try {
0648: p = JForumExecutionContext
0649: .getConnection()
0650: .prepareStatement(
0651: SystemGlobals
0652: .getSql("UserModel.totalUsersByGroup"));
0653: p.setInt(1, groupId);
0654:
0655: return this .getTotalUsersCommon(p);
0656: } catch (SQLException e) {
0657: throw new DatabaseException(e);
0658: } finally {
0659: DbUtils.close(p);
0660: }
0661: }
0662:
0663: protected int getTotalUsersCommon(PreparedStatement p)
0664: throws SQLException {
0665: ResultSet rs = p.executeQuery();
0666: rs.next();
0667:
0668: int total = rs.getInt(1);
0669:
0670: rs.close();
0671: p.close();
0672:
0673: return total;
0674: }
0675:
0676: /**
0677: * @see net.jforum.dao.UserDAO#isDeleted(int user_id)
0678: */
0679: public boolean isDeleted(int userId) {
0680: PreparedStatement p = null;
0681: ResultSet rs = null;
0682: try {
0683: p = JForumExecutionContext
0684: .getConnection()
0685: .prepareStatement(
0686: SystemGlobals.getSql("UserModel.isDeleted"));
0687: p.setInt(1, userId);
0688:
0689: int deleted = 0;
0690:
0691: rs = p.executeQuery();
0692: if (rs.next()) {
0693: deleted = rs.getInt("deleted");
0694: }
0695:
0696: return deleted == 1;
0697: } catch (SQLException e) {
0698: throw new DatabaseException(e);
0699: } finally {
0700: DbUtils.close(rs, p);
0701: }
0702: }
0703:
0704: /**
0705: * @see net.jforum.dao.UserDAO#isUsernameRegistered(java.lang.String)
0706: */
0707: public boolean isUsernameRegistered(String username) {
0708: boolean status = false;
0709:
0710: PreparedStatement p = null;
0711: ResultSet rs = null;
0712: try {
0713: p = JForumExecutionContext
0714: .getConnection()
0715: .prepareStatement(
0716: SystemGlobals
0717: .getSql("UserModel.isUsernameRegistered"));
0718: p.setString(1, username);
0719:
0720: rs = p.executeQuery();
0721: if (rs.next() && rs.getInt("registered") > 0) {
0722: status = true;
0723: }
0724:
0725: return status;
0726: } catch (SQLException e) {
0727: throw new DatabaseException(e);
0728: } finally {
0729: DbUtils.close(rs, p);
0730: }
0731: }
0732:
0733: /**
0734: * @see net.jforum.dao.UserDAO#validateLogin(java.lang.String, java.lang.String)
0735: */
0736: public User validateLogin(String username, String password) {
0737: return loginAuthenticator.validateLogin(username, password,
0738: null);
0739: }
0740:
0741: /**
0742: * @see net.jforum.dao.UserDAO#addToGroup(int, int[])
0743: */
0744: public void addToGroup(int userId, int[] groupId) {
0745: PreparedStatement p = null;
0746: try {
0747: p = JForumExecutionContext.getConnection()
0748: .prepareStatement(
0749: SystemGlobals
0750: .getSql("UserModel.addToGroup"));
0751: p.setInt(1, userId);
0752:
0753: for (int i = 0; i < groupId.length; i++) {
0754: p.setInt(2, groupId[i]);
0755: p.executeUpdate();
0756: }
0757: } catch (SQLException e) {
0758: throw new DatabaseException(e);
0759: } finally {
0760: DbUtils.close(p);
0761: }
0762:
0763: }
0764:
0765: /**
0766: * @see net.jforum.dao.UserDAO#removeFromGroup(int, int[])
0767: */
0768: public void removeFromGroup(int userId, int[] groupId) {
0769: PreparedStatement p = null;
0770: try {
0771: p = JForumExecutionContext
0772: .getConnection()
0773: .prepareStatement(
0774: SystemGlobals
0775: .getSql("UserModel.removeFromGroup"));
0776: p.setInt(1, userId);
0777:
0778: for (int i = 0; i < groupId.length; i++) {
0779: p.setInt(2, groupId[i]);
0780: p.executeUpdate();
0781: }
0782: } catch (SQLException e) {
0783: throw new DatabaseException(e);
0784: } finally {
0785: DbUtils.close(p);
0786: }
0787: }
0788:
0789: /**
0790: * @see net.jforum.dao.UserDAO#saveNewPassword(java.lang.String, java.lang.String)
0791: */
0792: public void saveNewPassword(String password, String email) {
0793: PreparedStatement p = null;
0794: try {
0795: p = JForumExecutionContext
0796: .getConnection()
0797: .prepareStatement(
0798: SystemGlobals
0799: .getSql("UserModel.saveNewPassword"));
0800: p.setString(1, password);
0801: p.setString(2, email);
0802: p.executeUpdate();
0803: } catch (SQLException e) {
0804: throw new DatabaseException(e);
0805: } finally {
0806: DbUtils.close(p);
0807: }
0808: }
0809:
0810: /**
0811: * @see net.jforum.dao.UserDAO#validateLostPasswordHash(java.lang.String, java.lang.String)
0812: */
0813: public boolean validateLostPasswordHash(String email, String hash) {
0814: PreparedStatement p = null;
0815: ResultSet rs = null;
0816: try {
0817: p = JForumExecutionContext
0818: .getConnection()
0819: .prepareStatement(
0820: SystemGlobals
0821: .getSql("UserModel.validateLostPasswordHash"));
0822: p.setString(1, hash);
0823: p.setString(2, email);
0824:
0825: boolean status = false;
0826:
0827: rs = p.executeQuery();
0828: if (rs.next() && rs.getInt("valid") > 0) {
0829: status = true;
0830:
0831: this .writeLostPasswordHash(email, "");
0832: }
0833:
0834: return status;
0835: } catch (SQLException e) {
0836: throw new DatabaseException(e);
0837: } finally {
0838: DbUtils.close(rs, p);
0839: }
0840: }
0841:
0842: /**
0843: * @see net.jforum.dao.UserDAO#writeLostPasswordHash(java.lang.String, java.lang.String)
0844: */
0845: public void writeLostPasswordHash(String email, String hash) {
0846: PreparedStatement p = null;
0847: try {
0848: p = JForumExecutionContext
0849: .getConnection()
0850: .prepareStatement(
0851: SystemGlobals
0852: .getSql("UserModel.writeLostPasswordHash"));
0853: p.setString(1, hash);
0854: p.setString(2, email);
0855: p.executeUpdate();
0856: } catch (SQLException e) {
0857: throw new DatabaseException(e);
0858: } finally {
0859: DbUtils.close(p);
0860: }
0861: }
0862:
0863: /**
0864: * @see net.jforum.dao.UserDAO#getUsernameByEmail(java.lang.String)
0865: */
0866: public String getUsernameByEmail(String email) {
0867: PreparedStatement p = null;
0868: ResultSet rs = null;
0869: try {
0870: p = JForumExecutionContext
0871: .getConnection()
0872: .prepareStatement(
0873: SystemGlobals
0874: .getSql("UserModel.getUsernameByEmail"));
0875: p.setString(1, email);
0876:
0877: String username = "";
0878:
0879: rs = p.executeQuery();
0880: if (rs.next()) {
0881: username = rs.getString("username");
0882: }
0883:
0884: return username;
0885: } catch (SQLException e) {
0886: throw new DatabaseException(e);
0887: } finally {
0888: DbUtils.close(rs, p);
0889: }
0890: }
0891:
0892: /**
0893: * @see net.jforum.dao.UserDAO#findByName(java.lang.String, boolean)
0894: */
0895: public List findByName(String input, boolean exactMatch) {
0896: List namesList = new ArrayList();
0897:
0898: PreparedStatement p = null;
0899: ResultSet rs = null;
0900: try {
0901: p = JForumExecutionContext.getConnection()
0902: .prepareStatement(
0903: SystemGlobals
0904: .getSql("UserModel.findByName"));
0905: p.setString(1, exactMatch ? input : "%" + input + "%");
0906:
0907: rs = p.executeQuery();
0908: while (rs.next()) {
0909: User u = new User();
0910:
0911: u.setId(rs.getInt("user_id"));
0912: u.setUsername(rs.getString("username"));
0913: u.setEmail(rs.getString("user_email"));
0914: u.setDeleted(rs.getInt("deleted"));
0915:
0916: namesList.add(u);
0917: }
0918: return namesList;
0919: } catch (SQLException e) {
0920: throw new DatabaseException(e);
0921: } finally {
0922: DbUtils.close(rs, p);
0923: }
0924: }
0925:
0926: /**
0927: * @see net.jforum.dao.UserDAO#validateActivationKeyHash(int, java.lang.String)
0928: */
0929: public boolean validateActivationKeyHash(int userId, String hash) {
0930: PreparedStatement p = null;
0931: ResultSet rs = null;
0932: try {
0933: p = JForumExecutionContext
0934: .getConnection()
0935: .prepareStatement(
0936: SystemGlobals
0937: .getSql("UserModel.validateActivationKeyHash"));
0938: p.setString(1, hash);
0939: p.setInt(2, userId);
0940:
0941: boolean status = false;
0942:
0943: rs = p.executeQuery();
0944: if (rs.next() && rs.getInt("valid") == 1) {
0945: status = true;
0946: }
0947:
0948: return status;
0949: } catch (SQLException e) {
0950: throw new DatabaseException(e);
0951: } finally {
0952: DbUtils.close(rs, p);
0953: }
0954: }
0955:
0956: /**
0957: * @see net.jforum.dao.UserDAO#writeUserActive(int)
0958: */
0959: public void writeUserActive(int userId) {
0960: PreparedStatement p = null;
0961: try {
0962: p = JForumExecutionContext
0963: .getConnection()
0964: .prepareStatement(
0965: SystemGlobals
0966: .getSql("UserModel.writeUserActive"));
0967: p.setInt(1, userId);
0968: p.executeUpdate();
0969: } catch (SQLException e) {
0970: throw new DatabaseException(e);
0971: } finally {
0972: DbUtils.close(p);
0973: }
0974: }
0975:
0976: /**
0977: * @see net.jforum.dao.UserDAO#updateUsername(int, String)
0978: */
0979: public void updateUsername(int userId, String username) {
0980: PreparedStatement p = null;
0981: try {
0982: p = JForumExecutionContext
0983: .getConnection()
0984: .prepareStatement(
0985: SystemGlobals
0986: .getSql("UserModel.updateUsername"));
0987: p.setString(1, username);
0988: p.setInt(2, userId);
0989: p.executeUpdate();
0990: } catch (SQLException e) {
0991: throw new DatabaseException(e);
0992: } finally {
0993: DbUtils.close(p);
0994: }
0995: }
0996:
0997: /**
0998: * @see net.jforum.dao.UserDAO#hasUsernameChanged(int, java.lang.String)
0999: */
1000: public boolean hasUsernameChanged(int userId, String usernameToCheck) {
1001: PreparedStatement p = null;
1002: ResultSet rs = null;
1003: try {
1004: p = JForumExecutionContext.getConnection()
1005: .prepareStatement(
1006: SystemGlobals
1007: .getSql("UserModel.getUsername"));
1008: p.setString(1, usernameToCheck);
1009: p.setInt(2, userId);
1010:
1011: String dbUsername = null;
1012:
1013: rs = p.executeQuery();
1014: if (rs.next()) {
1015: dbUsername = rs.getString("username");
1016: }
1017:
1018: boolean status = false;
1019:
1020: if (!usernameToCheck.equals(dbUsername)) {
1021: status = true;
1022: }
1023:
1024: return status;
1025: } catch (SQLException e) {
1026: throw new DatabaseException(e);
1027: } finally {
1028: DbUtils.close(rs, p);
1029: }
1030: }
1031:
1032: /**
1033: * Load KarmaStatus from a list of users.
1034: *
1035: * @param users
1036: * List
1037: * @return List
1038: * @throws SQLException
1039: */
1040: protected List loadKarma(List users) {
1041: List result = new ArrayList(users.size());
1042:
1043: for (Iterator iter = users.iterator(); iter.hasNext();) {
1044: User user = (User) iter.next();
1045:
1046: // Load Karma
1047: DataAccessDriver.getInstance().newKarmaDAO()
1048: .getUserTotalKarma(user);
1049: result.add(user);
1050: }
1051:
1052: return result;
1053: }
1054:
1055: /**
1056: * @see net.jforum.dao.UserDAO#saveUserAuthHash(int, java.lang.String)
1057: */
1058: public void saveUserAuthHash(int userId, String hash) {
1059: PreparedStatement p = null;
1060: try {
1061: p = JForumExecutionContext
1062: .getConnection()
1063: .prepareStatement(
1064: SystemGlobals
1065: .getSql("UserModel.saveUserAuthHash"));
1066: p.setString(1, hash);
1067: p.setInt(2, userId);
1068: p.executeUpdate();
1069: } catch (SQLException e) {
1070: throw new DatabaseException(e);
1071: } finally {
1072: DbUtils.close(p);
1073: }
1074: }
1075:
1076: /**
1077: * @see net.jforum.dao.UserDAO#getUserAuthHash(int)
1078: */
1079: public String getUserAuthHash(int userId) {
1080: PreparedStatement p = null;
1081: ResultSet rs = null;
1082: try {
1083: p = JForumExecutionContext
1084: .getConnection()
1085: .prepareStatement(
1086: SystemGlobals
1087: .getSql("UserModel.getUserAuthHash"));
1088: p.setInt(1, userId);
1089:
1090: rs = p.executeQuery();
1091:
1092: String hash = null;
1093: if (rs.next()) {
1094: hash = rs.getString(1);
1095: }
1096:
1097: return hash;
1098: } catch (SQLException e) {
1099: throw new DatabaseException(e);
1100: } finally {
1101: DbUtils.close(rs, p);
1102: }
1103: }
1104:
1105: /**
1106: * @see net.jforum.dao.UserDAO#findByEmail(java.lang.String)
1107: */
1108: public User findByEmail(String email) {
1109: PreparedStatement p = null;
1110: ResultSet rs = null;
1111:
1112: User u = null;
1113:
1114: try {
1115: p = JForumExecutionContext.getConnection()
1116: .prepareStatement(
1117: SystemGlobals
1118: .getSql("UserModel.findByEmail"));
1119: p.setString(1, email);
1120: rs = p.executeQuery();
1121:
1122: if (rs.next()) {
1123: u = new User();
1124: fillUserFromResultSet(u, rs);
1125: }
1126: } catch (SQLException e) {
1127: throw new DatabaseException(e);
1128: } finally {
1129: DbUtils.close(rs, p);
1130: }
1131:
1132: return u;
1133: }
1134: }
|