1 25 package org.nemesis.forum.impl; 26 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.nemesis.forum.Authorization; 37 import org.nemesis.forum.ForumPermissions; 38 import org.nemesis.forum.Group; 39 import org.nemesis.forum.ProfileManager; 40 import org.nemesis.forum.User; 41 import org.nemesis.forum.exception.GroupNotFoundException; 42 import org.nemesis.forum.exception.UnauthorizedException; 43 import org.nemesis.forum.exception.UserNotFoundException; 44 import org.nemesis.forum.util.cache.CacheSizes; 45 import org.nemesis.forum.util.cache.Cacheable; 46 import org.nemesis.forum.util.jdbc.DbConnectionManager; 47 52 public class DbGroup implements Group, Cacheable { 53 static protected Log log = LogFactory.getLog(DbGroup.class); 54 55 private static final String ADD_ADMIN = "INSERT INTO yazdGroupUser(groupID,userID,administrator) VALUES(?,?,1)"; 56 private static final String REMOVE_ADMIN = "DELETE FROM yazdGroupUser WHERE groupID=? AND userID=? AND administrator=1"; 57 private static final String ADD_USER = "INSERT INTO yazdGroupUser(groupID,userID,administrator) VALUES(?,?,0)"; 58 private static final String REMOVE_USER = "DELETE FROM yazdGroupUser WHERE groupID=? AND userID=? AND administrator=0"; 59 private static final String ADMIN_TEST = "SELECT userID FROM yazdGroupUser WHERE groupID=? AND userID=? AND " + "administrator=1"; 60 private static final String MEMBER_TEST = "SELECT userID FROM yazdGroupUser WHERE groupID=? AND userID=?"; 61 private static final String ADMIN_COUNT = "SELECT count(*) FROM yazdGroupUser WHERE groupID=? " + "AND administrator=1"; 62 private static final String MEMBER_COUNT = "SELECT DISTINCT count(userID) FROM yazdGroupUser " + "WHERE groupID=?"; 63 private static final String LOAD_ADMINS = "SELECT userID FROM yazdGroupUser WHERE administrator=1 AND groupID=?"; 64 private static final String LOAD_USERS = "SELECT userID FROM yazdGroupUser WHERE groupID=?"; 65 private static final String LOAD_GROUP_BY_ID = "SELECT * FROM yazdGroup WHERE groupID=?"; 66 private static final String LOAD_GROUP_BY_NAME = "SELECT * FROM yazdGroup WHERE name=?"; 67 private static final String INSERT_GROUP = "INSERT INTO yazdGroup(name,description,groupID) VALUES(?,?,?)"; 68 private static final String SAVE_GROUP = "UPDATE yazdGroup SET name=?, description=? WHERE groupID=?"; 69 70 private int id; 71 private String name = null; 72 private String description = ""; 73 74 private ProfileManager profileManager; 75 private DbForumFactory factory; 76 77 84 protected DbGroup(String name, DbForumFactory factory) { 85 this.name = name; 86 this.factory = factory; 87 this.profileManager = factory.getProfileManager(); 88 this.id = DbSequenceManager.nextID("Group"); 89 insertIntoDb(); 90 } 91 92 99 protected DbGroup(int id, DbForumFactory factory) throws GroupNotFoundException { 100 this.id = id; 101 this.factory = factory; 102 this.profileManager = factory.getProfileManager(); 103 loadFromDb(); 104 } 105 106 118 protected DbGroup(String name, Object fake, DbForumFactory factory) throws GroupNotFoundException { 119 this.name = name; 120 this.factory = factory; 121 this.profileManager = factory.getProfileManager(); 122 loadFromDb(); 123 } 124 125 127 public int getID() { 128 return id; 129 } 130 131 public String getName() { 132 return name; 133 } 134 135 public void setName(String name) throws UnauthorizedException { 136 this.name = name; 137 saveToDb(); 138 } 139 140 public String getDescription() { 141 return description; 142 } 143 144 public void setDescription(String description) throws UnauthorizedException { 145 this.description = description; 146 saveToDb(); 147 } 148 149 public void addAdministrator(User user) throws UnauthorizedException { 150 Connection con = null; 151 PreparedStatement pstmt = null; 152 try { 153 con = DbConnectionManager.getConnection(); 154 pstmt = con.prepareStatement(ADD_ADMIN); 155 pstmt.setInt(1, id); 156 pstmt.setInt(2, user.getID()); 157 pstmt.execute(); 158 } catch (SQLException sqle) { 159 log.error("",sqle); 160 } finally { 161 try { 162 pstmt.close(); 163 } catch (Exception e) { 164 log.error("",e); 165 } 166 try { 167 con.close(); 168 } catch (Exception e) { 169 log.error("",e); 170 } 171 } 172 173 DbCacheManager cacheManager = factory.getCacheManager(); 176 cacheManager.removeUserPerm(new Integer (user.getID())); 177 } 178 179 public void removeAdministrator(User user) throws UnauthorizedException { 180 Connection con = null; 181 PreparedStatement pstmt = null; 182 try { 183 con = DbConnectionManager.getConnection(); 184 pstmt = con.prepareStatement(REMOVE_ADMIN); 185 pstmt.setInt(1, id); 186 pstmt.setInt(2, user.getID()); 187 pstmt.execute(); 188 } catch (SQLException sqle) { 189 log.error("",sqle); 190 } finally { 191 try { 192 pstmt.close(); 193 } catch (Exception e) { 194 log.error("",e); 195 } 196 try { 197 con.close(); 198 } catch (Exception e) { 199 log.error("",e); 200 } 201 } 202 203 DbCacheManager cacheManager = factory.getCacheManager(); 206 cacheManager.removeUserPerm(new Integer (user.getID())); 207 } 208 209 public void addMember(User user) throws UnauthorizedException { 210 Connection con = null; 211 PreparedStatement pstmt = null; 212 try { 213 con = DbConnectionManager.getConnection(); 214 pstmt = con.prepareStatement(ADD_USER); 215 pstmt.setInt(1, id); 216 pstmt.setInt(2, user.getID()); 217 pstmt.execute(); 218 } catch (SQLException sqle) { 219 log.error("",sqle); 220 } finally { 221 try { 222 pstmt.close(); 223 } catch (Exception e) { 224 log.error("",e); 225 } 226 try { 227 con.close(); 228 } catch (Exception e) { 229 log.error("",e); 230 } 231 } 232 233 DbCacheManager cacheManager = factory.getCacheManager(); 236 cacheManager.removeUserPerm(new Integer (user.getID())); 237 } 238 239 public void removeMember(User user) throws UnauthorizedException { 240 Connection con = null; 241 PreparedStatement pstmt = null; 242 try { 243 con = DbConnectionManager.getConnection(); 244 pstmt = con.prepareStatement(REMOVE_USER); 245 pstmt.setInt(1, id); 246 pstmt.setInt(2, user.getID()); 247 pstmt.execute(); 248 } catch (SQLException sqle) { 249 log.error("",sqle); 250 } finally { 251 try { 252 pstmt.close(); 253 } catch (Exception e) { 254 log.error("",e); 255 } 256 try { 257 con.close(); 258 } catch (Exception e) { 259 log.error("",e); 260 } 261 } 262 263 DbCacheManager cacheManager = factory.getCacheManager(); 266 cacheManager.removeUserPerm(new Integer (user.getID())); 267 } 268 269 public boolean isAdministrator(User user) { 270 boolean answer = false; 271 Connection con = null; 272 PreparedStatement pstmt = null; 273 try { 274 con = DbConnectionManager.getConnection(); 275 pstmt = con.prepareStatement(ADMIN_TEST); 276 pstmt.setInt(1, id); 277 pstmt.setInt(2, user.getID()); 278 ResultSet rs = pstmt.executeQuery(); 279 if (rs.next()) { 280 answer = true; 281 } 282 } catch (SQLException sqle) { 283 log.error("",sqle); 284 } finally { 285 try { 286 pstmt.close(); 287 } catch (Exception e) { 288 log.error("",e); 289 } 290 try { 291 con.close(); 292 } catch (Exception e) { 293 log.error("",e); 294 } 295 } 296 return answer; 297 } 298 299 public boolean isMember(User user) { 300 boolean answer = false; 301 Connection con = null; 302 PreparedStatement pstmt = null; 303 try { 304 con = DbConnectionManager.getConnection(); 305 pstmt = con.prepareStatement(MEMBER_TEST); 306 pstmt.setInt(1, id); 307 pstmt.setInt(2, user.getID()); 308 ResultSet rs = pstmt.executeQuery(); 309 if (rs.next()) { 310 answer = true; 311 } 312 } catch (SQLException sqle) { 313 log.error("",sqle); 314 } finally { 315 try { 316 pstmt.close(); 317 } catch (Exception e) { 318 log.error("",e); 319 } 320 try { 321 con.close(); 322 } catch (Exception e) { 323 log.error("",e); 324 } 325 } 326 return answer; 327 } 328 329 public int getAdministratorCount() { 330 int count = 0; 331 boolean answer = false; 332 Connection con = null; 333 PreparedStatement pstmt = null; 334 try { 335 con = DbConnectionManager.getConnection(); 336 pstmt = con.prepareStatement(ADMIN_COUNT); 337 pstmt.setInt(1, id); 338 ResultSet rs = pstmt.executeQuery(); 339 if (rs.next()) { 340 count = rs.getInt(1); 341 } 342 } catch (SQLException sqle) { 343 log.error("",sqle); 344 } finally { 345 try { 346 pstmt.close(); 347 } catch (Exception e) { 348 log.error("",e); 349 } 350 try { 351 con.close(); 352 } catch (Exception e) { 353 log.error("",e); 354 } 355 } 356 return count; 357 } 358 359 public int getMemberCount() { 360 int count = 0; 361 boolean answer = false; 362 Connection con = null; 363 PreparedStatement pstmt = null; 364 try { 365 con = DbConnectionManager.getConnection(); 366 pstmt = con.prepareStatement(MEMBER_COUNT); 367 pstmt.setInt(1, id); 368 ResultSet rs = pstmt.executeQuery(); 369 if (rs.next()) { 370 count = rs.getInt(1); 371 } 372 } catch (SQLException sqle) { 373 log.error("",sqle); 374 } finally { 375 try { 376 pstmt.close(); 377 } catch (Exception e) { 378 log.error("",e); 379 } 380 try { 381 con.close(); 382 } catch (Exception e) { 383 log.error("",e); 384 } 385 } 386 return count; 387 } 388 389 public Iterator members() { 390 ArrayList admins = new ArrayList (); 391 Connection con = null; 393 PreparedStatement pstmt = null; 394 395 try { 396 con = DbConnectionManager.getConnection(); 397 pstmt = con.prepareStatement(LOAD_USERS); 398 pstmt.setInt(1, id); 399 ResultSet rs = pstmt.executeQuery(); 400 User user = null; 401 while (rs.next()) { 402 try { 403 user = profileManager.getUser(rs.getInt("userID")); 404 } catch (UserNotFoundException unfe) { 405 log.error("",unfe); 406 } 407 admins.add(user); 408 } 409 } catch (SQLException sqle) { 410 log.error("SQLException in DbGroup.java:" + "users():reading group data " , sqle); 411 412 } finally { 413 try { 414 pstmt.close(); 415 } catch (Exception e) { 416 log.error("",e); 417 } 418 try { 419 con.close(); 420 } catch (Exception e) { 421 log.error("",e); 422 } 423 } 424 return admins.iterator(); 425 } 426 427 public Iterator administrators() { 428 ArrayList admins = new ArrayList (); 429 Connection con = null; 431 PreparedStatement pstmt = null; 432 433 try { 434 con = DbConnectionManager.getConnection(); 435 pstmt = con.prepareStatement(LOAD_ADMINS); 436 pstmt.setInt(1, id); 437 ResultSet rs = pstmt.executeQuery(); 438 User user = null; 439 while (rs.next()) { 440 try { 441 user = profileManager.getUser(rs.getInt("userID")); 442 } catch (UserNotFoundException unfe) { 443 log.error("" , unfe); 444 } 445 admins.add(user); 446 } 447 } catch (SQLException sqle) { 448 log.error("SQLException in DbGroup.java:" + "administrators():reading group data " , sqle); 449 450 } finally { 451 try { 452 pstmt.close(); 453 } catch (Exception e) { 454 log.error("",e); 455 } 456 try { 457 con.close(); 458 } catch (Exception e) { 459 log.error("",e); 460 } 461 } 462 return admins.iterator(); 463 } 464 465 public ForumPermissions getPermissions(Authorization authorization) { 466 int userID = authorization.getUserID(); 467 try { 468 User user = profileManager.getUser(userID); 469 if (isAdministrator(user)) { 470 return new ForumPermissions(false, false, false, false, true, false, false, false); 471 } 472 } catch (Exception e) { 473 } 474 475 return ForumPermissions.none(); 476 } 477 478 public boolean hasPermission(int type) { 479 return true; 480 } 481 482 484 public int getSize() { 485 int size = 0; 488 size += CacheSizes.sizeOfObject(); size += CacheSizes.sizeOfInt(); size += CacheSizes.sizeOfString(name); size += CacheSizes.sizeOfString(description); size += CacheSizes.sizeOfObject(); size += CacheSizes.sizeOfObject(); 495 return size; 496 } 497 498 500 505 public String toString() { 506 return name; 507 } 508 509 public int hashCode() { 510 return id; 511 } 512 513 public boolean equals(Object object) { 514 if (this == object) { 515 return true; 516 } 517 if (object != null && object instanceof DbGroup) { 518 return id == ((DbGroup) object).getID(); 519 } else { 520 return false; 521 } 522 } 523 524 527 private synchronized void loadFromDb() throws GroupNotFoundException { 528 String query; 529 if (name == null) { 530 query = LOAD_GROUP_BY_ID; 531 } else { 532 query = LOAD_GROUP_BY_NAME; 533 } 534 Connection con = null; 535 PreparedStatement pstmt = null; 536 try { 537 con = DbConnectionManager.getConnection(); 538 pstmt = con.prepareStatement(query); 539 if (name == null) { 540 pstmt.setInt(1, id); 541 } else { 542 pstmt.setString(1, name); 543 } 544 ResultSet rs = pstmt.executeQuery(); 545 if (!rs.next()) { 546 throw new GroupNotFoundException(); 547 } 548 this.id = rs.getInt("groupID"); 549 this.name = rs.getString("name"); 550 this.description = rs.getString("description"); 551 } catch (SQLException sqle) { 552 log.error("SQLException in DbGroup.java:" + "loadFromDb():reading group data " , sqle); 553 throw new GroupNotFoundException(); 554 } finally { 555 try { 556 pstmt.close(); 557 } catch (Exception e) { 558 log.error("",e); 559 } 560 try { 561 con.close(); 562 } catch (Exception e) { 563 log.error("",e); 564 } 565 } 566 } 567 568 571 private void insertIntoDb() { 572 StringBuffer insert = new StringBuffer (); 573 Connection con = null; 574 PreparedStatement pstmt = null; 575 try { 576 con = DbConnectionManager.getConnection(); 577 pstmt = con.prepareStatement(INSERT_GROUP); 578 pstmt.setString(1, name); 579 pstmt.setString(2, description); 580 pstmt.setInt(3, id); 581 pstmt.executeUpdate(); 582 } catch (SQLException sqle) { 583 log.error("Error in DbGroup:insertIntoDb()-" , sqle); 584 585 } finally { 586 try { 587 pstmt.close(); 588 } catch (Exception e) { 589 log.error("",e); 590 } 591 try { 592 con.close(); 593 } catch (Exception e) { 594 log.error("",e); 595 } 596 } 597 } 598 599 602 private synchronized void saveToDb() { 603 Connection con = null; 604 PreparedStatement pstmt = null; 605 try { 606 con = DbConnectionManager.getConnection(); 607 pstmt = con.prepareStatement(SAVE_GROUP); 608 pstmt.setString(1, name); 609 pstmt.setString(2, description); 610 pstmt.setInt(3, id); 611 pstmt.executeUpdate(); 612 } catch (SQLException sqle) { 613 log.error("SQLException in DbGroup.java:saveToDb(): " , sqle); 614 615 } finally { 616 try { 617 pstmt.close(); 618 } catch (Exception e) { 619 log.error("" , e); 620 } 621 try { 622 con.close(); 623 } catch (Exception e) { 624 log.error("" , e); 625 } 626 } 627 } 628 } 629 | Popular Tags |