PseudoRosterItem.java 9.23 KB
Newer Older
Daniel Henninger's avatar
Daniel Henninger committed
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.wildfire.gateway;

13 14 15 16 17 18
import org.jivesoftware.util.Log;
import org.jivesoftware.util.NotFoundException;
import org.jivesoftware.database.DbConnectionManager;

import java.sql.*;

Daniel Henninger's avatar
Daniel Henninger committed
19
/**
20 21 22 23
 * Representation of a pseudo roster item.
 *
 * This is used for tracking information about a contact that we can't track on the legacy system itself.
 *
Daniel Henninger's avatar
Daniel Henninger committed
24 25 26 27
 * @author Daniel Henninger
 */
public class PseudoRosterItem {

28 29 30 31 32 33 34 35 36 37
    private static final String INSERT_ROSTER_ITEM =
            "INSERT INTO gatewayPseudoRoster(registrationID, username, nickname, groups) VALUES (?,?,?,?)";
    private static final String LOAD_ROSTER_ITEM =
            "SELECT nickname, groups FROM gatewayPseudoRoster WHERE registrationID=? AND username=?";
    private static final String CHANGE_USERNAME =
            "UPDATE gatewayPseudoRoster SET username=? WHERE registrationID=? AND username=?";
    private static final String SET_NICKNAME =
            "UPDATE gatewayPseudoRoster SET nickname=? WHERE registrationID=? AND username=?";
    private static final String SET_GROUPS =
            "UPDATE gatewayPseudoRoster SET groups=? WHERE registrationID=? AND username=?";
38 39
    private static final String REMOVE_ROSTER_ITEM =
            "DELETE FROM gatewayPseudoRoster WHERE registrationID=? AND username=?";
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

    private long registrationID;
    private String username;
    private String nickname;
    private String groups;

    /**
     * Creates a new roster item associated with a registration id.
     *
     * @param registrationID Id of the registration this roster item is associated with.
     * @param username The username of the roster item.
     * @param nickname The nickname associated with the roster item (may be null).
     * @param groups The group list associated wit the roster item (may be null).
     */
    public PseudoRosterItem(Long registrationID, String username, String nickname, String groups) {
        if (registrationID == null || username == null) {
            throw new NullPointerException("Arguments cannot be null.");
        }
        this.registrationID = registrationID;
        this.username = username;
        this.nickname = nickname;
        this.groups = groups;
        try {
63 64 65 66 67 68 69 70 71
            // Clean up potentially already existing item.
            removeFromDb();
            try {
                // Insert new roster item.
                insertIntoDb();
            }
            catch (Exception e) {
                Log.error(e);
            }
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        }
        catch (Exception e) {
            Log.error(e);
        }
    }

    /**
     * Loads an existing roster item.
     *
     * @param registrationID The ID of the registration the roster item is assocaited with.
     * @param username The username of the roster item.
     * @throws org.jivesoftware.util.NotFoundException if the registration could not be loaded.
     */
    public PseudoRosterItem(long registrationID, String username)
            throws NotFoundException
    {
        this.registrationID = registrationID;
        this.username = username;
        loadFromDb();
    }

    /**
     * Returns the unique ID of the registration associated with the roster item.
     *
     * @return the registration ID.
     */
    public long getRegistrationID() {
        return registrationID;
    }

    /**
     * Returns the username associated with the roster item.
     *
     * @return the username.
     */
    public String getUsername() {
        return username;
    }

    /**
     * Returns the nickname associated with the roster item.
     *
     * @return the nickname.
     */
    public String getNickname() {
        return nickname;
    }

    /**
     * Returns the groups associated with the roster item.
     *
     * @return the groups.
     */
    public String getGroups() {
        return groups;
    }

    /**
     * Changes the username of the roster item.
     * @param username New username.
     */
    public void changeUsername(String username) {
        if (username == null) {
            throw new NullPointerException("Arguments cannot be null.");
        }
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(CHANGE_USERNAME);
            pstmt.setString(1, username);
            pstmt.setLong(2, registrationID);
            pstmt.setString(3, this.username);
            pstmt.executeUpdate();
            this.username = username;
        }
        catch (SQLException sqle) {
            Log.error(sqle);
        }
        finally {
            DbConnectionManager.closeConnection(pstmt, con);
        }
    }

    /**
     * Sets the nickname for the roster item.
     * @param nickname New nickname to be associated.
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(SET_NICKNAME);
            if (nickname != null) {
                pstmt.setString(1, nickname);
            }
            else {
                pstmt.setNull(1, Types.VARCHAR);
            }
            pstmt.setLong(2, registrationID);
            pstmt.setString(3, this.username);
            pstmt.executeUpdate();
        }
        catch (SQLException sqle) {
            Log.error(sqle);
        }
        finally {
            DbConnectionManager.closeConnection(pstmt, con);
        }
    }

    /**
     * Sets the groups for the roster item.
     * @param groups New group list to be associated.
     */
    public void setGroups(String groups) {
        this.groups = groups;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(SET_GROUPS);
            if (groups != null) {
                pstmt.setString(1, groups);
            }
            else {
                pstmt.setNull(1, Types.VARCHAR);
            }
            pstmt.setLong(2, registrationID);
            pstmt.setString(3, this.username);
            pstmt.executeUpdate();
        }
        catch (SQLException sqle) {
            Log.error(sqle);
        }
        finally {
            DbConnectionManager.closeConnection(pstmt, con);
        }
    }


    public String toString() {
        return username + ", " + nickname + ", " + groups;
    }

    /**
220
     * Inserts a new roster item into the database.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
     */
    private void insertIntoDb() throws SQLException {
        Connection con = null;
        PreparedStatement pstmt = null;
        boolean abortTransaction = false;
        try {
            con = DbConnectionManager.getTransactionConnection();
            pstmt = con.prepareStatement(INSERT_ROSTER_ITEM);
            pstmt.setLong(1, registrationID);
            pstmt.setString(2, username);
            if (nickname != null) {
                pstmt.setString(3, nickname);
            }
            else {
                pstmt.setNull(3, Types.VARCHAR);
            }
            if (groups != null) {
                pstmt.setString(4, groups);
            }
            else {
                pstmt.setNull(4, Types.VARCHAR);
            }
            pstmt.executeUpdate();
        }
        catch (SQLException sqle) {
            abortTransaction = true;
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
            throw sqle;
        }
        finally {
            DbConnectionManager.closeTransactionConnection(pstmt, con, abortTransaction);
        }
    }

    /**
     * Removeds a roster item from the database.
     */
    private void removeFromDb() throws SQLException {
        Connection con = null;
        PreparedStatement pstmt = null;
        boolean abortTransaction = false;
        try {
            con = DbConnectionManager.getTransactionConnection();
            pstmt = con.prepareStatement(REMOVE_ROSTER_ITEM);
            pstmt.setLong(1, registrationID);
            pstmt.setString(2, username);
            pstmt.executeUpdate();
        }
        catch (SQLException sqle) {
            abortTransaction = true;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
            throw sqle;
        }
        finally {
            DbConnectionManager.closeTransactionConnection(pstmt, con, abortTransaction);
        }
    }

    private void loadFromDb() throws NotFoundException {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_ROSTER_ITEM);
            pstmt.setLong(1, registrationID);
            pstmt.setString(2, username);
            rs = pstmt.executeQuery();
            if (!rs.next()) {
                throw new NotFoundException("Pseudo roster item not found: " + registrationID + "/" + username);
            }
            this.nickname = rs.getString(1);
            this.groups = rs.getString(2);
        }
        catch (SQLException sqle) {
            Log.error(sqle);
        }
        finally {
            DbConnectionManager.closeConnection(rs, pstmt, con);
        }
    }
    
Daniel Henninger's avatar
Daniel Henninger committed
301
}