DefaultGroupProvider.java 20.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/**
 * $RCSfile$
 * $Revision: 3117 $
 * $Date: 2005-11-25 22:57:29 -0300 (Fri, 25 Nov 2005) $
 *
 * Copyright (C) 2004 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.
 */

12
package org.jivesoftware.openfire.group;
13 14 15

import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.util.Log;
16
import org.jivesoftware.util.StringUtils;
17
import org.jivesoftware.openfire.XMPPServer;
18 19
import org.xmpp.packet.JID;

20 21
import java.sql.*;
import java.util.*;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

/**
 * Database implementation of the GroupManager interface.
 *
 * @author Matt Tucker
 */
public class DefaultGroupProvider implements GroupProvider {

    private static final String INSERT_GROUP =
        "INSERT INTO jiveGroup (groupName, description) VALUES (?, ?)";
    private static final String SAVE_GROUP =
        "UPDATE jiveGroup SET description=? WHERE groupName=?";
    private static final String SET_GROUP_NAME_1 =
        "UPDATE jiveGroup SET groupName=? WHERE groupName=?";
    private static final String SET_GROUP_NAME_2 =
        "UPDATE jiveGroupProp SET groupName=? WHERE groupName=?";
    private static final String SET_GROUP_NAME_3 =
        "UPDATE jiveGroupUser SET groupName=? WHERE groupName=?";
    private static final String DELETE_GROUP_USERS =
        "DELETE FROM jiveGroupUser WHERE groupName=?";
    private static final String DELETE_PROPERTIES =
        "DELETE FROM jiveGroupProp WHERE groupName=?";
    private static final String DELETE_GROUP =
        "DELETE FROM jiveGroup WHERE groupName=?";
    private static final String GROUP_COUNT = "SELECT count(*) FROM jiveGroup";
     private static final String LOAD_ADMINS =
48
        "SELECT username FROM jiveGroupUser WHERE administrator=1 AND groupName=? ORDER BY username";
49
    private static final String LOAD_MEMBERS =
50
        "SELECT username FROM jiveGroupUser WHERE administrator=0 AND groupName=? ORDER BY username";
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    private static final String LOAD_GROUP =
        "SELECT description FROM jiveGroup WHERE groupName=?";
    private static final String REMOVE_USER =
        "DELETE FROM jiveGroupUser WHERE groupName=? AND username=?";
    private static final String ADD_USER =
        "INSERT INTO jiveGroupUser (groupName, username, administrator) VALUES (?, ?, ?)";
    private static final String UPDATE_USER =
        "UPDATE jiveGroupUser SET administrator=? WHERE groupName=? AND username=?";
    private static final String USER_GROUPS =
        "SELECT groupName FROM jiveGroupUser WHERE username=?";
    private static final String ALL_GROUPS = "SELECT groupName FROM jiveGroup ORDER BY groupName";

    private XMPPServer server = XMPPServer.getInstance();

    public Group createGroup(String name) throws GroupAlreadyExistsException {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(INSERT_GROUP);
            pstmt.setString(1, name);
            pstmt.setString(2, "");
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
79 80 81 82
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
83
            catch (Exception e) { Log.error(e); }
84 85 86 87
            try {
                if (con != null) {
                    con.close();
                } }
88 89 90 91
            catch (Exception e) { Log.error(e); }
        }
        Collection<JID> members = getMembers(name, false);
        Collection<JID> administrators = getMembers(name, true);
Matt Tucker's avatar
Matt Tucker committed
92
        return new Group(name, "", members, administrators);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    }

    public Group getGroup(String name) throws GroupNotFoundException {
        String description = null;

        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_GROUP);
            pstmt.setString(1, name);
            ResultSet rs = pstmt.executeQuery();
            if (!rs.next()) {
                throw new GroupNotFoundException("Group with name "
                    + name + " not found.");
            }
            description = rs.getString(1);
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
115 116 117 118
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
119
            catch (Exception e) { Log.error(e); }
120 121 122 123
            try {
                if (con != null) {
                    con.close();
                } }
124 125 126 127
            catch (Exception e) { Log.error(e); }
        }
        Collection<JID> members = getMembers(name, false);
        Collection<JID> administrators = getMembers(name, true);
Matt Tucker's avatar
Matt Tucker committed
128
        return new Group(name, description, members, administrators);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    }

    public void setDescription(String name, String description)
            throws GroupNotFoundException
    {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(SAVE_GROUP);
            pstmt.setString(1, description);
            pstmt.setString(2, name);
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
            Log.error(e);
            throw new GroupNotFoundException();
        }
        finally {
148 149 150 151
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
152
            catch (Exception e) { Log.error(e); }
153 154 155 156
            try {
                if (con != null) {
                    con.close();
                } }
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
            catch (Exception e) { Log.error(e); }
        }
    }

    public void setName(String oldName, String newName) throws UnsupportedOperationException,
            GroupAlreadyExistsException
    {
        Connection con = null;
        PreparedStatement pstmt = null;
        boolean abortTransaction = false;
        try {
            con = DbConnectionManager.getTransactionConnection();
            pstmt = con.prepareStatement(SET_GROUP_NAME_1);
            pstmt.setString(1, newName);
            pstmt.setString(2, oldName);
            pstmt.executeUpdate();
            pstmt.close();
            pstmt = con.prepareStatement(SET_GROUP_NAME_2);
            pstmt.setString(1, newName);
            pstmt.setString(2, oldName);
            pstmt.executeUpdate();
            pstmt.close();
            pstmt = con.prepareStatement(SET_GROUP_NAME_3);
            pstmt.setString(1, newName);
            pstmt.setString(2, oldName);
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
            Log.error(e);
            abortTransaction = true;
        }
        finally {
189 190 191 192
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
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 220 221 222 223
            catch (Exception e) { Log.error(e); }
            DbConnectionManager.closeTransactionConnection(con, abortTransaction);
        }
    }

    public void deleteGroup(String groupName) {
        Connection con = null;
        PreparedStatement pstmt = null;
        boolean abortTransaction = false;
        try {
            con = DbConnectionManager.getTransactionConnection();
            // Remove all users in the group.
            pstmt = con.prepareStatement(DELETE_GROUP_USERS);
            pstmt.setString(1, groupName);
            pstmt.executeUpdate();
            pstmt.close();
            // Remove all properties of the group.
            pstmt = con.prepareStatement(DELETE_PROPERTIES);
            pstmt.setString(1, groupName);
            pstmt.executeUpdate();
            pstmt.close();
            // Remove the group entry.
            pstmt = con.prepareStatement(DELETE_GROUP);
            pstmt.setString(1, groupName);
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
            Log.error(e);
            abortTransaction = true;
        }
        finally {
224 225 226 227
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
            catch (Exception e) { Log.error(e); }
            DbConnectionManager.closeTransactionConnection(con, abortTransaction);
        }
    }

    public int getGroupCount() {
        int count = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(GROUP_COUNT);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
            rs.close();
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
250 251 252 253
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
254
            catch (Exception e) { Log.error(e); }
255 256 257 258
            try {
                if (con != null) {
                    con.close();
                } }
259 260 261 262 263
            catch (Exception e) { Log.error(e); }
        }
        return count;
    }

264
    public Collection<String> getGroupNames() {
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        List<String> groupNames = new ArrayList<String>();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(ALL_GROUPS);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                groupNames.add(rs.getString(1));
            }
            rs.close();
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
281 282 283 284
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
285
            catch (Exception e) { Log.error(e); }
286 287 288 289
            try {
                if (con != null) {
                    con.close();
                } }
290 291
            catch (Exception e) { Log.error(e); }
        }
292
        return groupNames;
293 294
    }

295
    public Collection<String> getGroupNames(int startIndex, int numResults) {
296 297 298 299 300
        List<String> groupNames = new ArrayList<String>();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
301
            pstmt = DbConnectionManager.createScrollablePreparedStatement(con, ALL_GROUPS);
302 303 304 305 306 307 308 309 310 311 312 313 314
            ResultSet rs = pstmt.executeQuery();
            DbConnectionManager.scrollResultSet(rs, startIndex);
            int count = 0;
            while (rs.next() && count < numResults) {
                groupNames.add(rs.getString(1));
                count++;
            }
            rs.close();
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
315 316 317 318
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
319
            catch (Exception e) { Log.error(e); }
320 321 322 323
            try {
                if (con != null) {
                    con.close();
                } }
324 325
            catch (Exception e) { Log.error(e); }
        }
326
        return groupNames;
327 328
    }

329
    public Collection<String> getGroupNames(JID user) {
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
        List<String> groupNames = new ArrayList<String>();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(USER_GROUPS);
            pstmt.setString(1, server.isLocal(user) ? user.getNode() : user.toString());
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                groupNames.add(rs.getString(1));
            }
            rs.close();
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
347 348 349
            try {
                if (pstmt != null) {
                    pstmt.close();
350 351
                }
            }
352
            catch (Exception e) { Log.error(e); }
353 354 355
            try {
                if (con != null) {
                    con.close();
356 357
                }
            }
358 359
            catch (Exception e) { Log.error(e); }
        }
360
        return groupNames;
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    }

    public void addMember(String groupName, JID user, boolean administrator) {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(ADD_USER);
            pstmt.setString(1, groupName);
            pstmt.setString(2, server.isLocal(user) ? user.getNode() : user.toString());
            pstmt.setInt(3, administrator ? 1 : 0);
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
378 379 380 381
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
382
            catch (Exception e) { Log.error(e); }
383 384 385 386
            try {
                if (con != null) {
                    con.close();
                } }
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
            catch (Exception e) { Log.error(e); }
        }
    }

    public void updateMember(String groupName, JID user, boolean administrator) {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(UPDATE_USER);
            pstmt.setInt(1, administrator ? 1 : 0);
            pstmt.setString(2, groupName);
            pstmt.setString(3, server.isLocal(user) ? user.getNode() : user.toString());
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
406 407 408 409
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
410
            catch (Exception e) { Log.error(e); }
411 412 413 414
            try {
                if (con != null) {
                    con.close();
                } }
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
            catch (Exception e) { Log.error(e); }
        }
    }

    public void deleteMember(String groupName, JID user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(REMOVE_USER);
            pstmt.setString(1, groupName);
            pstmt.setString(2, server.isLocal(user) ? user.getNode() : user.toString());
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
433 434 435 436
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
437
            catch (Exception e) { Log.error(e); }
438 439 440 441
            try {
                if (con != null) {
                    con.close();
                } }
442 443 444 445 446 447 448 449
            catch (Exception e) { Log.error(e); }
        }
    }

    public boolean isReadOnly() {
        return false;
    }

450
    public Collection<String> search(String query) {
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
        if (query == null || "".equals(query)) {
            return Collections.emptyList();
        }
        // SQL LIKE queries don't map directly into a keyword/wildcard search like we want.
        // Therefore, we do a best approximiation by replacing '*' with '%' and then
        // surrounding the whole query with two '%'. This will return more data than desired,
        // but is better than returning less data than desired.
        query = "%" + query.replace('*', '%') + "%";
        if (query.endsWith("%%")) {
            query = query.substring(0, query.length()-1);
        }

        List<String> groupNames = new ArrayList<String>();
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            con = DbConnectionManager.getConnection();
            stmt = con.createStatement();
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT groupName FROM jiveGroup WHERE groupName LIKE '").append(
                    StringUtils.escapeForSQL(query)).append("' ORDER BY groupName");
            rs = stmt.executeQuery(sql.toString());
            while (rs.next()) {
                groupNames.add(rs.getString(1));
            }
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
            DbConnectionManager.closeConnection(rs, stmt, con);
        }
484
        return groupNames;
485 486
    }

487
    public Collection<String> search(String query, int startIndex, int numResults) {
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
        if (query == null || "".equals(query)) {
            return Collections.emptyList();
        }
        // SQL LIKE queries don't map directly into a keyword/wildcard search like we want.
        // Therefore, we do a best approximiation by replacing '*' with '%' and then
        // surrounding the whole query with two '%'. This will return more data than desired,
        // but is better than returning less data than desired.
        query = "%" + query.replace('*', '%') + "%";
        if (query.endsWith("%%")) {
            query = query.substring(0, query.length()-1);
        }

        List<String> groupNames = new ArrayList<String>();
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            con = DbConnectionManager.getConnection();
            stmt = DbConnectionManager.createScrollableStatement(con);
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT groupName FROM jiveGroup WHERE groupName LIKE '").append(
                    StringUtils.escapeForSQL(query)).append("' ORDER BY groupName");
            rs = stmt.executeQuery(sql.toString());
            DbConnectionManager.setFetchSize(rs, startIndex + numResults);
            DbConnectionManager.scrollResultSet(rs, startIndex);
            int count = 0;
            while (rs.next() && count < numResults) {
                groupNames.add(rs.getString(1));
                count++;
            }
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
            DbConnectionManager.closeConnection(rs, stmt, con);
        }
525
        return groupNames;
526 527 528 529 530 531
    }

    public boolean isSearchSupported() {
        return true;
    }

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    private Collection<JID> getMembers(String groupName, boolean adminsOnly) {
        List<JID> members = new ArrayList<JID>();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            if (adminsOnly) {
                pstmt = con.prepareStatement(LOAD_ADMINS);
            }
            else {
                pstmt = con.prepareStatement(LOAD_MEMBERS);
            }
            pstmt.setString(1, groupName);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                String user = rs.getString(1);
548
                JID userJID = null;
549 550 551 552 553 554
                if (user.indexOf('@') == -1) {
                    // Create JID of local user if JID does not match a component's JID
                    if (!server.matchesComponent(userJID)) {
                        userJID = server.createJID(user, null);
                    }
                }
555 556 557
                else {
                    userJID = new JID(user);
                }
558 559 560 561 562 563 564 565
                members.add(userJID);
            }
            rs.close();
        }
        catch (SQLException e) {
            Log.error(e);
        }
        finally {
566 567 568 569
            try {
                if (pstmt != null) {
                    pstmt.close();
                } }
570
            catch (Exception e) { Log.error(e); }
571 572 573 574
            try {
                if (con != null) {
                    con.close();
                } }
575 576 577 578 579
            catch (Exception e) { Log.error(e); }
        }
        return members;
    }
}