1 11 12 package org.jivesoftware.messenger.group; 13 14 import org.jivesoftware.database.DbConnectionManager; 15 import org.jivesoftware.util.*; 16 import org.jivesoftware.messenger.user.User; 17 18 import java.sql.Connection ; 19 import java.sql.PreparedStatement ; 20 import java.sql.ResultSet ; 21 import java.sql.SQLException ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.Collection ; 25 26 31 public class DefaultGroupProvider implements GroupProvider { 32 33 private static final String INSERT_GROUP = 34 "INSERT INTO jiveGroup (groupName, description) VALUES (?, ?)"; 35 private static final String SAVE_GROUP = 36 "UPDATE jiveGroup SET description=? WHERE groupName=?"; 37 private static final String SET_GROUP_NAME_1 = 38 "UPDATE jiveGroup SET groupName=? WHERE groupName=?"; 39 private static final String SET_GROUP_NAME_2 = 40 "UPDATE jiveGroupProp SET groupName=? WHERE groupName=?"; 41 private static final String SET_GROUP_NAME_3 = 42 "UPDATE jiveGroupUser SET groupName=? WHERE groupName=?"; 43 private static final String DELETE_GROUP_USERS = 44 "DELETE FROM jiveGroupUser WHERE groupName=?"; 45 private static final String DELETE_PROPERTIES = 46 "DELETE FROM jiveGroupProp WHERE groupName=?"; 47 private static final String DELETE_GROUP = 48 "DELETE FROM jiveGroup WHERE groupName=?"; 49 private static final String GROUP_COUNT = "SELECT count(*) FROM jiveGroup"; 50 private static final String LOAD_ADMINS = 51 "SELECT username FROM jiveGroupUser WHERE administrator=1 AND groupName=?"; 52 private static final String LOAD_MEMBERS = 53 "SELECT username FROM jiveGroupUser WHERE administrator=0 AND groupName=?"; 54 private static final String LOAD_GROUP = 55 "SELECT description FROM jiveGroup WHERE groupName=?"; 56 private static final String REMOVE_USER = 57 "DELETE FROM jiveGroupUser WHERE groupName=? AND username=?"; 58 private static final String ADD_USER = 59 "INSERT INTO jiveGroupUser (groupName, username, administrator) VALUES (?, ?, ?)"; 60 private static final String UPDATE_USER = 61 "UPDATE jiveGroupUser SET administrator=? WHERE groupName=? AND username=?"; 62 private static final String USER_GROUPS = 63 "SELECT groupName FROM jiveGroupUser WHERE username=?"; 64 private static final String ALL_GROUPS = "SELECT groupName FROM jiveGroup ORDER BY groupName"; 65 66 public Group createGroup(String name) throws GroupAlreadyExistsException { 67 Connection con = null; 68 PreparedStatement pstmt = null; 69 try { 70 con = DbConnectionManager.getConnection(); 71 pstmt = con.prepareStatement(INSERT_GROUP); 72 pstmt.setString(1, name); 73 pstmt.setString(2, ""); 74 pstmt.executeUpdate(); 75 } 76 catch (SQLException e) { 77 Log.error(e); 78 } 79 finally { 80 try { if (pstmt != null) pstmt.close(); } 81 catch (Exception e) { Log.error(e); } 82 try { if (con != null) con.close(); } 83 catch (Exception e) { Log.error(e); } 84 } 85 Collection <String > members = getMembers(name, false); 86 Collection <String > administrators = getMembers(name, true); 87 return new Group(this, name, "", members, administrators); 88 } 89 90 public Group getGroup(String name) throws GroupNotFoundException { 91 String description = null; 92 93 Connection con = null; 94 PreparedStatement pstmt = null; 95 try { 96 con = DbConnectionManager.getConnection(); 97 pstmt = con.prepareStatement(LOAD_GROUP); 98 pstmt.setString(1, name); 99 ResultSet rs = pstmt.executeQuery(); 100 if (!rs.next()) { 101 throw new GroupNotFoundException("Group with name " 102 + name + " not found."); 103 } 104 description = rs.getString(1); 105 } 106 catch (SQLException e) { 107 Log.error(e); 108 } 109 finally { 110 try { if (pstmt != null) pstmt.close(); } 111 catch (Exception e) { Log.error(e); } 112 try { if (con != null) con.close(); } 113 catch (Exception e) { Log.error(e); } 114 } 115 Collection <String > members = getMembers(name, false); 116 Collection <String > administrators = getMembers(name, true); 117 return new Group(this, name, description, members, administrators); 118 } 119 120 public void setDescription(String name, String description) 121 throws GroupNotFoundException 122 { 123 Connection con = null; 124 PreparedStatement pstmt = null; 125 try { 126 con = DbConnectionManager.getConnection(); 127 pstmt = con.prepareStatement(SAVE_GROUP); 128 pstmt.setString(1, description); 129 pstmt.setString(2, name); 130 pstmt.executeUpdate(); 131 } 132 catch (SQLException e) { 133 Log.error(e); 134 throw new GroupNotFoundException(); 135 } 136 finally { 137 try { if (pstmt != null) pstmt.close(); } 138 catch (Exception e) { Log.error(e); } 139 try { if (con != null) con.close(); } 140 catch (Exception e) { Log.error(e); } 141 } 142 } 143 144 public void setName(String oldName, String newName) throws UnsupportedOperationException , 145 GroupAlreadyExistsException 146 { 147 Connection con = null; 148 PreparedStatement pstmt = null; 149 boolean abortTransaction = false; 150 try { 151 con = DbConnectionManager.getTransactionConnection(); 152 pstmt = con.prepareStatement(SET_GROUP_NAME_1); 153 pstmt.setString(1, newName); 154 pstmt.setString(2, oldName); 155 pstmt.executeUpdate(); 156 pstmt.close(); 157 pstmt = con.prepareStatement(SET_GROUP_NAME_2); 158 pstmt.setString(1, newName); 159 pstmt.setString(2, oldName); 160 pstmt.executeUpdate(); 161 pstmt.close(); 162 pstmt = con.prepareStatement(SET_GROUP_NAME_3); 163 pstmt.setString(1, newName); 164 pstmt.setString(2, oldName); 165 pstmt.executeUpdate(); 166 } 167 catch (SQLException e) { 168 Log.error(e); 169 abortTransaction = true; 170 } 171 finally { 172 try { if (pstmt != null) pstmt.close(); } 173 catch (Exception e) { Log.error(e); } 174 DbConnectionManager.closeTransactionConnection(con, abortTransaction); 175 } 176 } 177 178 public void deleteGroup(String groupName) { 179 Connection con = null; 180 PreparedStatement pstmt = null; 181 boolean abortTransaction = false; 182 try { 183 con = DbConnectionManager.getTransactionConnection(); 184 pstmt = con.prepareStatement(DELETE_GROUP_USERS); 186 pstmt.setString(1, groupName); 187 pstmt.executeUpdate(); 188 pstmt.close(); 189 pstmt = con.prepareStatement(DELETE_PROPERTIES); 191 pstmt.setString(1, groupName); 192 pstmt.executeUpdate(); 193 pstmt.close(); 194 pstmt = con.prepareStatement(DELETE_GROUP); 196 pstmt.setString(1, groupName); 197 pstmt.executeUpdate(); 198 pstmt.close(); 199 } 200 catch (SQLException e) { 201 Log.error(e); 202 abortTransaction = true; 203 } 204 finally { 205 try { if (pstmt != null) pstmt.close(); } 206 catch (Exception e) { Log.error(e); } 207 DbConnectionManager.closeTransactionConnection(con, abortTransaction); 208 } 209 } 210 211 public int getGroupCount() { 212 int count = 0; 213 Connection con = null; 214 PreparedStatement pstmt = null; 215 try { 216 con = DbConnectionManager.getConnection(); 217 pstmt = con.prepareStatement(GROUP_COUNT); 218 ResultSet rs = pstmt.executeQuery(); 219 if (rs.next()) { 220 count = rs.getInt(1); 221 } 222 rs.close(); 223 } 224 catch (SQLException e) { 225 Log.error(e); 226 } 227 finally { 228 try { if (pstmt != null) pstmt.close(); } 229 catch (Exception e) { Log.error(e); } 230 try { if (con != null) con.close(); } 231 catch (Exception e) { Log.error(e); } 232 } 233 return count; 234 } 235 236 public Collection <Group> getGroups() { 237 List <String > groupNames = new ArrayList <String >(); 238 Connection con = null; 239 PreparedStatement pstmt = null; 240 try { 241 con = DbConnectionManager.getConnection(); 242 pstmt = con.prepareStatement(ALL_GROUPS); 243 ResultSet rs = pstmt.executeQuery(); 244 while (rs.next()) { 245 groupNames.add(rs.getString(1)); 246 } 247 rs.close(); 248 } 249 catch (SQLException e) { 250 Log.error(e); 251 } 252 finally { 253 try { if (pstmt != null) pstmt.close(); } 254 catch (Exception e) { Log.error(e); } 255 try { if (con != null) con.close(); } 256 catch (Exception e) { Log.error(e); } 257 } 258 List <Group> groups = new ArrayList <Group>(groupNames.size()); 259 GroupManager manager = GroupManager.getInstance(); 260 for (String groupName : groupNames) { 261 try { 262 groups.add(manager.getGroup(groupName)); 263 } 264 catch (GroupNotFoundException e) { 265 Log.error(e); 266 } 267 } 268 return groups; 269 } 270 271 public Collection <Group> getGroups(int startIndex, int numResults) { 272 List <String > groupNames = new ArrayList <String >(); 273 Connection con = null; 274 PreparedStatement pstmt = null; 275 try { 276 con = DbConnectionManager.getConnection(); 277 pstmt = con.prepareStatement(ALL_GROUPS); 278 ResultSet rs = pstmt.executeQuery(); 279 DbConnectionManager.scrollResultSet(rs, startIndex); 280 int count = 0; 281 while (rs.next() && count < numResults) { 282 groupNames.add(rs.getString(1)); 283 count++; 284 } 285 rs.close(); 286 } 287 catch (SQLException e) { 288 Log.error(e); 289 } 290 finally { 291 try { if (pstmt != null) pstmt.close(); } 292 catch (Exception e) { Log.error(e); } 293 try { if (con != null) con.close(); } 294 catch (Exception e) { Log.error(e); } 295 } 296 List <Group> groups = new ArrayList <Group>(groupNames.size()); 297 GroupManager manager = GroupManager.getInstance(); 298 for (String groupName : groupNames) { 299 try { 300 groups.add(manager.getGroup(groupName)); 301 } 302 catch (GroupNotFoundException e) { 303 Log.error(e); 304 } 305 } 306 return groups; 307 } 308 309 public Collection <Group> getGroups(User user) { 310 List <String > groupNames = new ArrayList <String >(); 311 Connection con = null; 312 PreparedStatement pstmt = null; 313 try { 314 con = DbConnectionManager.getConnection(); 315 pstmt = con.prepareStatement(USER_GROUPS); 316 pstmt.setString(1, user.getUsername()); 317 ResultSet rs = pstmt.executeQuery(); 318 while (rs.next()) { 319 groupNames.add(rs.getString(1)); 320 } 321 rs.close(); 322 } 323 catch (SQLException e) { 324 Log.error(e); 325 } 326 finally { 327 try { if (pstmt != null) pstmt.close(); } 328 catch (Exception e) { Log.error(e); } 329 try { if (con != null) con.close(); } 330 catch (Exception e) { Log.error(e); } 331 } 332 List <Group> groups = new ArrayList <Group>(groupNames.size()); 333 GroupManager manager = GroupManager.getInstance(); 334 for (String groupName : groupNames) { 335 try { 336 groups.add(manager.getGroup(groupName)); 337 } 338 catch (GroupNotFoundException e) { 339 Log.error(e); 340 } 341 } 342 return groups; 343 } 344 345 public void addMember(String groupName, String username, boolean administrator) { 346 Connection con = null; 347 PreparedStatement pstmt = null; 348 try { 349 con = DbConnectionManager.getConnection(); 350 pstmt = con.prepareStatement(ADD_USER); 351 pstmt.setString(1, groupName); 352 pstmt.setString(2, username); 353 pstmt.setInt(3, administrator ? 1 : 0); 354 pstmt.executeUpdate(); 355 } 356 catch (SQLException e) { 357 Log.error(e); 358 } 359 finally { 360 try { if (pstmt != null) pstmt.close(); } 361 catch (Exception e) { Log.error(e); } 362 try { if (con != null) con.close(); } 363 catch (Exception e) { Log.error(e); } 364 } 365 } 366 367 public void updateMember(String groupName, String username, boolean administrator) { 368 Connection con = null; 369 PreparedStatement pstmt = null; 370 try { 371 con = DbConnectionManager.getConnection(); 372 pstmt = con.prepareStatement(UPDATE_USER); 373 pstmt.setInt(1, administrator ? 1 : 0); 374 pstmt.setString(2, groupName); 375 pstmt.setString(3, username); 376 pstmt.executeUpdate(); 377 } 378 catch (SQLException e) { 379 Log.error(e); 380 } 381 finally { 382 try { if (pstmt != null) pstmt.close(); } 383 catch (Exception e) { Log.error(e); } 384 try { if (con != null) con.close(); } 385 catch (Exception e) { Log.error(e); } 386 } 387 } 388 389 public void deleteMember(String groupName, String username) { 390 Connection con = null; 391 PreparedStatement pstmt = null; 392 try { 393 con = DbConnectionManager.getConnection(); 394 pstmt = con.prepareStatement(REMOVE_USER); 395 pstmt.setString(1, groupName); 396 pstmt.setString(2, username); 397 pstmt.executeUpdate(); 398 } 399 catch (SQLException e) { 400 Log.error(e); 401 } 402 finally { 403 try { if (pstmt != null) pstmt.close(); } 404 catch (Exception e) { Log.error(e); } 405 try { if (con != null) con.close(); } 406 catch (Exception e) { Log.error(e); } 407 } 408 } 409 410 public boolean isReadOnly() { 411 return false; 412 } 413 414 private Collection <String > getMembers(String groupName, boolean adminsOnly) { 415 List <String > members = new ArrayList <String >(); 416 Connection con = null; 417 PreparedStatement pstmt = null; 418 try { 419 con = DbConnectionManager.getConnection(); 420 if (adminsOnly) { 421 pstmt = con.prepareStatement(LOAD_ADMINS); 422 } 423 else { 424 pstmt = con.prepareStatement(LOAD_MEMBERS); 425 } 426 pstmt.setString(1, groupName); 427 ResultSet rs = pstmt.executeQuery(); 428 while (rs.next()) { 429 members.add(rs.getString(1)); 430 } 431 rs.close(); 432 } 433 catch (SQLException e) { 434 Log.error(e); 435 } 436 finally { 437 try { if (pstmt != null) pstmt.close(); } 438 catch (Exception e) { Log.error(e); } 439 try { if (con != null) con.close(); } 440 catch (Exception e) { Log.error(e); } 441 } 442 return members; 443 } 444 } | Popular Tags |