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

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
package org.jivesoftware.messenger.user.spi;

import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.database.SequenceManager;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.XMPPAddress;
import org.jivesoftware.messenger.user.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
28 29
import java.util.ArrayList;
import java.util.List;
Matt Tucker's avatar
Matt Tucker committed
30 31 32 33 34 35 36 37 38 39

/**
 * <p>Implements the roster item provider against the jiveRoster table
 * using standard Jive default JDBC connections.</p>
 *
 * @author Iain Shigeoka
 */
public class DbRosterItemProvider implements RosterItemProvider {

    private static final String CREATE_ROSTER_ITEM =
40
            "INSERT INTO jiveRoster (username, rosterID, jid, sub, ask, recv, nick) " +
Matt Tucker's avatar
Matt Tucker committed
41 42
            "VALUES (?, ?, ?, ?, ?, ?, ?)";

43
    public CachedRosterItem createItem(String username, RosterItem item)
Matt Tucker's avatar
Matt Tucker committed
44 45 46 47 48 49 50 51 52
            throws UserAlreadyExistsException, UnsupportedOperationException {
        Connection con = null;
        PreparedStatement pstmt = null;
        CachedRosterItem cachedItem = null;
        try {
            con = DbConnectionManager.getConnection();

            long rosterID = SequenceManager.nextID(JiveConstants.ROSTER);
            pstmt = con.prepareStatement(CREATE_ROSTER_ITEM);
53
            pstmt.setString(1, username);
Matt Tucker's avatar
Matt Tucker committed
54 55 56 57 58 59
            pstmt.setLong(2, rosterID);
            pstmt.setString(3, item.getJid().toBareString());
            pstmt.setInt(4, item.getSubStatus().getValue());
            pstmt.setInt(5, item.getAskStatus().getValue());
            pstmt.setInt(6, item.getRecvStatus().getValue());
            pstmt.setString(7, item.getNickname());
Derek DeMoro's avatar
Derek DeMoro committed
60
            pstmt.executeUpdate();
Matt Tucker's avatar
Matt Tucker committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

            if (item instanceof CachedRosterItemImpl) {
                // If a RosterItemImpl we can reuse it by setting the new roster ID
                cachedItem = (CachedRosterItem)item;
                ((CachedRosterItemImpl)cachedItem).setID(rosterID);
            }
            else {
                // Otherwise, just create a coyy of the item with the new roster ID
                cachedItem = new CachedRosterItemImpl(rosterID, item);
            }
            insertGroups(rosterID, item.getGroups().iterator(), pstmt, con);
        }
        catch (SQLException e) {
            throw new UserAlreadyExistsException(item.getJid().toStringPrep());
        }
        finally {
77
            try { if (pstmt != null) { pstmt.close(); } }
Matt Tucker's avatar
Matt Tucker committed
78 79 80 81 82 83 84 85 86 87 88 89
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
        }
        return cachedItem;
    }

    private static final String UPDATE_ROSTER_ITEM =
            "UPDATE jiveRoster SET sub=?, ask=?, recv=?, nick=? WHERE rosterID=?";
    private static final String DELETE_ROSTER_ITEM_GROUPS =
            "DELETE FROM jiveRosterGroups WHERE rosterID=?";

90
    public void updateItem(String username, CachedRosterItem item)
Matt Tucker's avatar
Matt Tucker committed
91 92 93 94 95 96 97 98 99 100 101 102 103
            throws UserNotFoundException, UnsupportedOperationException {
        Connection con = null;
        PreparedStatement pstmt = null;
        long rosterID = item.getID();
        try {
            con = DbConnectionManager.getConnection();
            // Update existing roster item
            pstmt = con.prepareStatement(UPDATE_ROSTER_ITEM);
            pstmt.setInt(1, item.getSubStatus().getValue());
            pstmt.setInt(2, item.getAskStatus().getValue());
            pstmt.setInt(3, item.getRecvStatus().getValue());
            pstmt.setString(4, item.getNickname());
            pstmt.setLong(5, rosterID);
Derek DeMoro's avatar
Derek DeMoro committed
104
            pstmt.executeUpdate();
Matt Tucker's avatar
Matt Tucker committed
105 106 107 108

            // Delete old group list
            pstmt = con.prepareStatement(DELETE_ROSTER_ITEM_GROUPS);
            pstmt.setLong(1, rosterID);
Derek DeMoro's avatar
Derek DeMoro committed
109
            pstmt.executeUpdate();
Matt Tucker's avatar
Matt Tucker committed
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

            insertGroups(rosterID, item.getGroups().iterator(), pstmt, con);

        }
        catch (SQLException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
        finally {
            try { if (pstmt != null) { pstmt.close(); } }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
        }
    }

    private static final String CREATE_ROSTER_ITEM_GROUPS =
            "INSERT INTO jiveRosterGroups (rosterID, rank, groupName) VALUES (?, ?, ?)";

    /**
     * <p>Insert the groups into the given roster item.</p>
     *
     * @param rosterID The roster ID of the item the groups belong to
     * @param iter     An iterator over the group names to insert
     */
    private void insertGroups(long rosterID,
                              Iterator iter,
                              PreparedStatement pstmt,
                              Connection con)
            throws SQLException {
        try {
            pstmt = con.prepareStatement(CREATE_ROSTER_ITEM_GROUPS);
            pstmt.setLong(1, rosterID);
            for (int i = 0; iter.hasNext(); i++) {
                pstmt.setInt(2, i);
                pstmt.setString(3, (String)iter.next());
Derek DeMoro's avatar
Derek DeMoro committed
145
                pstmt.executeUpdate();
Matt Tucker's avatar
Matt Tucker committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            }
        }
        finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
            }
            catch (Exception e) {
                Log.error(e);
            }
        }
    }

    private static final String DELETE_ROSTER_ITEM =
            "DELETE FROM jiveRoster WHERE rosterID=?";

163
    public void deleteItem(String username, long rosterItemID)
Matt Tucker's avatar
Matt Tucker committed
164 165 166 167 168 169 170 171 172 173
            throws UnsupportedOperationException {
        // Only try to remove the user if they exist in the roster already:
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            // Remove roster groups
            pstmt = con.prepareStatement(DELETE_ROSTER_ITEM_GROUPS);

            pstmt.setLong(1, rosterItemID);
Derek DeMoro's avatar
Derek DeMoro committed
174
            pstmt.executeUpdate();
Matt Tucker's avatar
Matt Tucker committed
175 176 177 178 179

            // Remove roster
            pstmt = con.prepareStatement(DELETE_ROSTER_ITEM);

            pstmt.setLong(1, rosterItemID);
Derek DeMoro's avatar
Derek DeMoro committed
180
            pstmt.executeUpdate();
Matt Tucker's avatar
Matt Tucker committed
181 182 183 184 185 186 187 188 189 190 191 192
        }
        catch (SQLException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
        finally {
            try { if (pstmt != null) { pstmt.close(); } }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
        }
    }

193 194
    private static final String LOAD_USERNAMES =
        "SELECT DISTINCT username from jiveRoster WHERE jid=?";
Matt Tucker's avatar
Matt Tucker committed
195

196 197
    public Iterator<String> getUsernames(String jid) {
        List<String> answer = new ArrayList<String>();
Matt Tucker's avatar
Matt Tucker committed
198 199 200 201
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
202 203
            pstmt = con.prepareStatement(LOAD_USERNAMES);
            pstmt.setString(1, jid);
Matt Tucker's avatar
Matt Tucker committed
204 205
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
206
                answer.add(rs.getString(1));
Matt Tucker's avatar
Matt Tucker committed
207 208 209 210 211 212 213 214 215 216 217
            }
        }
        catch (SQLException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
        finally {
            try { if (pstmt != null) { pstmt.close(); } }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
        }
218
        return answer.iterator();
Matt Tucker's avatar
Matt Tucker committed
219 220 221
    }

    private static final String COUNT_ROSTER_ITEMS =
222
        "SELECT COUNT(rosterID) FROM jiveRoster WHERE username=?";
Matt Tucker's avatar
Matt Tucker committed
223

224
    public int getItemCount(String username) {
Matt Tucker's avatar
Matt Tucker committed
225 226 227 228 229 230
        int count = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(COUNT_ROSTER_ITEMS);
231
            pstmt.setString(1, username);
Matt Tucker's avatar
Matt Tucker committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        }
        catch (SQLException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
        finally {
            try { if (pstmt != null) { pstmt.close(); } }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
        }
        return count;
    }

    private static final String LOAD_ROSTER =
250
        "SELECT jid, rosterID, sub, ask, recv, nick FROM jiveRoster WHERE username=?";
Matt Tucker's avatar
Matt Tucker committed
251
    private static final String LOAD_ROSTER_ITEM_GROUPS =
252
        "SELECT groupName FROM jiveRosterGroups WHERE rosterID=? ORDER BY rank";
Matt Tucker's avatar
Matt Tucker committed
253

254 255
    public Iterator getItems(String username) {
        LinkedList itemList = new LinkedList();
Matt Tucker's avatar
Matt Tucker committed
256 257 258 259 260
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_ROSTER);
261
            pstmt.setString(1, username);
Matt Tucker's avatar
Matt Tucker committed
262 263 264 265 266 267 268 269 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
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                CachedRosterItem item = new CachedRosterItemImpl(rs.getLong(2),
                        XMPPAddress.parseJID(rs.getString(1)),
                        RosterItem.SubType.getTypeFromInt(rs.getInt(3)),
                        RosterItem.AskType.getTypeFromInt(rs.getInt(4)),
                        RosterItem.RecvType.getTypeFromInt(rs.getInt(5)),
                        rs.getString(6),
                        null);
                PreparedStatement gstmt = null;
                ResultSet gs = null;
                try {
                    gstmt = con.prepareStatement(LOAD_ROSTER_ITEM_GROUPS);
                    gstmt.setLong(1, item.getID());
                    gs = gstmt.executeQuery();
                    while (gs.next()) {
                        item.getGroups().add(gs.getString(1));
                    }
                    itemList.add(item);
                }
                finally {
                    try {if (gs != null) { gs.close(); } }
                    catch (Exception e) { Log.error(e); }
                    try {if (gstmt != null) { gstmt.close(); } }
                    catch (Exception e) { Log.error(e); }
                }
            }
        }
        catch (SQLException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
        finally {
            try { if (pstmt != null) { pstmt.close(); } }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
        }
        return itemList.iterator();
    }
301
}