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