1 53 54 106 107 package com.Yasna.forum.database; 108 109 import java.sql.*; 110 import java.util.*; 111 import java.text.SimpleDateFormat ; 112 113 import com.Yasna.forum.*; 114 import com.Yasna.forum.Exceptions.RapidPostingException; 115 import com.Yasna.forum.Exceptions.UserBlackListedException; 116 import com.Yasna.forum.util.ClientIP; 117 import com.Yasna.util.Cacheable; 118 import com.Yasna.util.CacheSizes; 119 import com.Yasna.util.StringUtils; 120 121 126 public final class DbForumMessage implements ForumMessage, Cacheable { 127 128 129 private static final String CHECK_LASTPOST= 130 "select max(a.creationDate) as lastpost from yazdMessage a,yazdMessageProp b where a.messageID=b.messageID and b.name=? and b.propValue=?"; 131 private static final String CHECK_LASTPOSTWITHUSER = 132 "select max(creationDate) as lastpost from yazdMessage where userID=?"; 133 private static final String LOAD_PROPERTIES = 134 "SELECT name, propValue FROM yazdMessageProp WHERE messageID=?"; 135 private static final String DELETE_PROPERTIES = 136 "DELETE FROM yazdMessageProp WHERE messageID=?"; 137 private static final String INSERT_PROPERTY = 138 "INSERT INTO yazdMessageProp(messageID,name,propValue) VALUES(?,?,?)"; 139 private static final String LOAD_MESSAGE = 140 "SELECT userID, creationDate, modifiedDate, subject, body, threadID, " + 141 "replyPrivateUserId, approved,ranking FROM yazdMessage WHERE messageID=?"; 142 private static final String INSERT_MESSAGE = 143 "INSERT INTO yazdMessage(messageID, threadID,creationDate,modifiedDate,userID," + 144 "subject,body,approved, replyPrivateUserId,ranking) VALUES(?,?,?,?,?,?,?,?,?,0)"; 145 private static final String SAVE_MESSAGE = 146 "UPDATE yazdMessage SET userID=?, subject=?, body=?, creationDate=?, " + 147 "modifiedDate=?, replyPrivateUserId = ?, approved = ?,ranking=? WHERE messageID=?"; 148 private static final String GET_FORUM_BY_THREAD = 149 "SELECT forumID FROM yazdThread where threadID=?"; 150 private static final String CHECK_BLACK_LIST_IP="select ip from yazdBlackList where ip=?"; 151 private static final String UPDATE_BLACK_LIST_IP="update yazdBlackList set blockcount=blockcount+1 where ip=?"; 152 private static final String CHECK_BLACK_LIST_USER="select userID from yazdUserProp where userID=? and name=? and propValue=?"; 153 private static final String CHECK_SESSION="select initime from yazdSessions where IP=?"; 154 155 private int id = -1; 156 private java.util.Date creationDate; 157 private java.util.Date modifiedDate; 158 private String subject = ""; 159 private int replyPrivateUserId = 0; 160 private boolean approved; 161 private String body = ""; 162 private int userID; 163 private int threadID; 164 private Map properties; 165 private Object propertyLock = new Object (); 166 private DbForumFactory factory; 167 private ForumThread thread = null; 168 private int ranking = MessageRanking.NOTRANKED; 170 175 private boolean isReadyToSave = false; 176 177 181 protected DbForumMessage(User user, DbForumFactory factory) { 182 this.id = -1; 183 long now = System.currentTimeMillis(); 184 creationDate = new java.util.Date (now); 185 modifiedDate = new java.util.Date (now); 186 this.userID = user.getID(); 187 this.factory = factory; 188 this.approved = true; 189 properties = Collections.synchronizedMap(new HashMap()); 190 isReadyToSave = false; 191 } 192 193 196 protected DbForumMessage(User user, DbForumFactory factory, boolean approved,ClientIP clientIP) throws RapidPostingException,UserBlackListedException { 197 checkBlackList(user,clientIP); 198 checkForRapidPosts(user,clientIP); 199 checkForSession(user,clientIP); 200 this.id = DbSequenceManager.nextID("ForumMessage"); 201 long now = System.currentTimeMillis(); 202 creationDate = new java.util.Date (now); 203 modifiedDate = new java.util.Date (now); 204 this.userID = user.getID(); 205 this.factory = factory; 206 this.approved = approved; 207 properties = Collections.synchronizedMap(new HashMap()); 208 } 209 210 213 protected DbForumMessage(int id, DbForumFactory factory) 214 throws ForumMessageNotFoundException 215 { 216 this.id = id; 217 this.factory = factory; 218 loadFromDb(); 219 loadProperties(); 220 isReadyToSave = true; 221 } 222 223 225 public int getID() { 226 return id; 227 } 228 229 public java.util.Date getCreationDate() { 230 return creationDate; 231 } 232 233 public void setCreationDate(java.util.Date creationDate) 234 throws UnauthorizedException 235 { 236 this.creationDate = creationDate; 237 if (!isReadyToSave) { 239 return; 240 } 241 saveToDb(); 242 } 243 244 public java.util.Date getModifiedDate() { 245 return modifiedDate; 246 } 247 248 public void setModifiedDate(java.util.Date modifiedDate) 249 throws UnauthorizedException 250 { 251 this.modifiedDate = modifiedDate; 252 if (!isReadyToSave) { 254 return; 255 } 256 saveToDb(); 257 } 258 259 public String getSubject() { 260 return subject; 261 } 262 263 public int getReplyPrivateUserId() { 264 return replyPrivateUserId; 265 } 266 267 public boolean isPrivate() { 268 return replyPrivateUserId > 0; 269 } 270 271 public String getUnfilteredSubject() { 272 return subject; 273 } 274 275 public void setSubject(String subject) throws UnauthorizedException { 276 this.subject = subject; 277 if (!isReadyToSave) { 279 return; 280 } 281 modifiedDate.setTime(System.currentTimeMillis()); 283 saveToDb(); 284 } 285 286 public void setReplyPrivateUserId(int replyPrivateUserId) throws UnauthorizedException { 287 this.replyPrivateUserId = replyPrivateUserId; 288 if (!isReadyToSave) { 290 return; 291 } 292 modifiedDate.setTime(System.currentTimeMillis()); 294 saveToDb(); 295 } 296 297 public void setApprovment(boolean approved) throws UnauthorizedException { 298 this.approved = approved; 299 if (!isReadyToSave) { 301 return; 302 } 303 modifiedDate.setTime(System.currentTimeMillis()); 305 saveToDb(); 306 } 307 308 public boolean isApproved() { 309 return approved; 310 } 311 312 public String getBody() { 313 return body; 314 } 315 316 public String getUnfilteredBody() { 317 return body; 318 } 319 320 public void setBody(String body) throws UnauthorizedException { 321 this.body = body; 322 if (!isReadyToSave) { 324 return; 325 } 326 modifiedDate.setTime(System.currentTimeMillis()); 328 saveToDb(); 329 } 330 331 public User getUser() { 332 User user = null; 333 try { 334 if (userID == -1) { 335 user = factory.getProfileManager().getAnonymousUser(); 336 } 337 else { 338 user = factory.getProfileManager().getUser(userID); 339 } 340 } 341 catch (UserNotFoundException unfe) { 342 unfe.printStackTrace(); 343 } 344 return user; 345 } 346 347 public String getProperty(String name) { 348 return StringUtils.escapeHTMLTags((String )properties.get(name)); 350 } 351 352 public String getUnfilteredProperty(String name) { 353 return (String )properties.get(name); 354 } 355 356 public void setProperty(String name, String value) { 357 properties.put(name, value); 358 if (!isReadyToSave) { 360 return; 361 } 362 saveProperties(); 363 } 364 365 public Iterator propertyNames() { 366 return Collections.unmodifiableSet(properties.keySet()).iterator(); 367 } 368 369 public boolean isAnonymous() { 370 return (userID == -1); 371 } 372 373 public ForumThread getForumThread() { 374 if (thread != null) { 375 return thread; 376 } 377 else { 380 383 int forumID = -1; 384 Connection con = null; 385 PreparedStatement pstmt = null; 386 try { 387 con = DbConnectionManager.getConnection(); 388 pstmt = con.prepareStatement(GET_FORUM_BY_THREAD); 389 pstmt.setInt(1, threadID); 390 ResultSet rs = pstmt.executeQuery(); 391 if (rs.next()) { 392 forumID = rs.getInt("forumID"); 393 } 394 } 395 catch( SQLException sqle ) { 396 sqle.printStackTrace(); 397 } 398 finally { 399 try { pstmt.close(); } 400 catch (Exception e) { e.printStackTrace(); } 401 try { con.close(); } 402 catch (Exception e) { e.printStackTrace(); } 403 } 404 if (forumID < 1) { 407 System.err.println("WARNING: forumID of " + forumID + 408 " found for message " + id + " in DbForumMessage.getForumThread()." + 409 " You may wish to delete the message from your database." 410 ); 411 return null; 412 } 413 414 Forum forum = null; 415 ForumThread thread = null; 416 try { 417 forum = factory.getForum(forumID); 418 thread = forum.getThread(threadID); 420 } 421 catch (Exception e) { 422 e.printStackTrace(); 423 return null; 424 } 425 this.thread = thread; 426 return thread; 427 } 428 } 429 430 public boolean hasPermission(int type) { 431 return true; 432 } 433 434 436 public int getSize() { 437 int size = 0; 440 size += CacheSizes.sizeOfObject(); size += CacheSizes.sizeOfInt(); size += CacheSizes.sizeOfString(subject); size += CacheSizes.sizeOfString(body); size += CacheSizes.sizeOfDate(); size += CacheSizes.sizeOfDate(); size += CacheSizes.sizeOfInt(); size += CacheSizes.sizeOfInt(); size += CacheSizes.sizeOfInt(); size += CacheSizes.sizeOfBoolean(); size += CacheSizes.sizeOfMap(properties); size += CacheSizes.sizeOfObject(); size += CacheSizes.sizeOfObject(); 454 return size; 455 } 456 457 459 464 public String toString() { 465 return subject; 466 } 467 468 public int hashCode() { 469 return id; 470 } 471 472 public boolean equals(Object object) { 473 if (this == object) { 474 return true; 475 } 476 if (object != null && object instanceof DbForumMessage) { 477 return id == ((DbForumMessage)object).getID(); 478 } 479 else { 480 return false; 481 } 482 } 483 484 487 private void loadProperties() { 488 synchronized(propertyLock) { 489 Properties newProps = new Properties(); 490 Connection con = null; 491 PreparedStatement pstmt = null; 492 try { 493 con = DbConnectionManager.getConnection(); 494 pstmt = con.prepareStatement(LOAD_PROPERTIES); 495 pstmt.setInt(1, id); 496 ResultSet rs = pstmt.executeQuery(); 497 while(rs.next()) { 498 String name = rs.getString("name"); 499 String value = rs.getString("propValue"); 500 newProps.put(name, value); 501 } 502 } 503 catch( SQLException sqle ) { 504 System.err.println("Error in DbForumMessage:loadProperties():" + sqle); 505 sqle.printStackTrace(); 506 } 507 finally { 508 try { pstmt.close(); } 509 catch (Exception e) { e.printStackTrace(); } 510 try { con.close(); } 511 catch (Exception e) { e.printStackTrace(); } 512 } 513 this.properties = newProps; 514 } 515 } 516 517 520 private void saveProperties() { 521 synchronized(propertyLock) { 522 Connection con = null; 523 PreparedStatement pstmt = null; 524 try { 525 con = DbConnectionManager.getConnection(); 526 pstmt = con.prepareStatement(DELETE_PROPERTIES); 528 pstmt.setInt(1, id); 529 pstmt.execute(); 530 pstmt.close(); 531 pstmt = con.prepareStatement(INSERT_PROPERTY); 533 Iterator iter = properties.keySet().iterator(); 534 while (iter.hasNext()) { 535 String name = (String )iter.next(); 536 String value = (String )properties.get(name); 537 pstmt.setInt(1, id); 538 pstmt.setString(2, name); 539 pstmt.setString(3, value); 540 pstmt.executeUpdate(); 541 } 542 } 543 catch( SQLException sqle ) { 544 System.err.println(sqle); 545 } 546 finally { 547 try { pstmt.close(); } 548 catch (Exception e) { e.printStackTrace(); } 549 try { con.close(); } 550 catch (Exception e) { e.printStackTrace(); } 551 } 552 } 553 } 554 555 558 private void loadFromDb() throws ForumMessageNotFoundException { 559 Connection con = null; 561 PreparedStatement pstmt = null; 562 try { 563 con = DbConnectionManager.getConnection(); 564 pstmt = con.prepareStatement(LOAD_MESSAGE); 565 pstmt.setInt(1, id); 566 ResultSet rs = pstmt.executeQuery(); 567 if( !rs.next() ) { 568 throw new ForumMessageNotFoundException("Message " + id + 569 " could not be loaded from the database."); 570 } 571 this.userID = rs.getInt(1); 575 this.creationDate = 578 new java.util.Date (Long.parseLong(rs.getString(2).trim())); 579 this.modifiedDate = 580 new java.util.Date (Long.parseLong(rs.getString(3).trim())); 581 this.subject = rs.getString(4); 582 this.body = rs.getString(5); 583 this.threadID = rs.getInt(6); 584 this.replyPrivateUserId = rs.getInt(7); 585 this.approved = rs.getInt(8) == 1 ? true : false; 586 this.ranking = rs.getInt("ranking"); 587 } 588 catch( SQLException sqle ) { 589 throw new ForumMessageNotFoundException( "Message of id " 590 + id + " was not found in the database." 591 ); 592 } 593 catch (NumberFormatException nfe) { 594 System.err.println("WARNING: In DbForumMessage.loadFromDb() -- there " + 595 "was an error parsing the dates returned from the database. Ensure " + 596 "that they're being stored correctly."); 597 } 598 finally { 599 try { pstmt.close(); } 600 catch (Exception e) { e.printStackTrace(); } 601 try { con.close(); } 602 catch (Exception e) { e.printStackTrace(); } 603 } 604 } 605 606 615 public void insertIntoDb(Connection con, ForumThread thread) 616 throws SQLException 617 { 618 this.threadID = thread.getID(); 621 PreparedStatement pstmt = con.prepareStatement(INSERT_MESSAGE); 622 pstmt.setInt(1, id); 623 pstmt.setInt(2, threadID); 624 pstmt.setString(3, Long.toString(creationDate.getTime())); 625 pstmt.setString(4, Long.toString(modifiedDate.getTime())); 626 pstmt.setInt(5, userID); 627 pstmt.setString(6, subject); 628 pstmt.setString(7, body); 629 pstmt.setInt(8, approved ? 1 : 0); 630 pstmt.setInt(9, replyPrivateUserId); 631 pstmt.executeUpdate(); 632 pstmt.close(); 633 634 saveProperties(); 637 638 factory.getWatchManager().addMessage(this); 640 isReadyToSave = true; 643 } 644 645 648 private synchronized void saveToDb() { 649 Connection con = null; 650 PreparedStatement pstmt = null; 651 try { 652 con = DbConnectionManager.getConnection(); 653 pstmt = con.prepareStatement(SAVE_MESSAGE); 654 pstmt.setInt(1, userID); 655 pstmt.setString(2, subject); 656 pstmt.setString(3, body); 657 pstmt.setString(4, Long.toString(creationDate.getTime())); 658 pstmt.setString(5, Long.toString(modifiedDate.getTime())); 659 pstmt.setInt(6, replyPrivateUserId); 660 pstmt.setInt(7, approved ? 1 : 0); 661 pstmt.setInt(8,this.ranking); 662 pstmt.setInt(9, id); 663 pstmt.executeUpdate(); 664 } 665 catch( SQLException sqle ) { 666 System.err.println( "SQLException in DbForumMessage:saveToDb()- " + sqle ); 667 sqle.printStackTrace(); 668 } 669 finally { 670 try { pstmt.close(); } 671 catch (Exception e) { e.printStackTrace(); } 672 try { con.close(); } 673 catch (Exception e) { e.printStackTrace(); } 674 } 675 } 676 682 private void checkForRapidPosts(User user,ClientIP clientIP) throws RapidPostingException{ 683 Connection con = null; 687 PreparedStatement pstmt = null; 688 try { 689 con = DbConnectionManager.getConnection(); 690 int ranking=10; if(user.isAnonymous()){ 692 pstmt = con.prepareStatement(CHECK_LASTPOST); 693 pstmt.setString(1, "IP"); 694 pstmt.setString(2,clientIP.getRemoteIP()); 695 } else { 696 pstmt = con.prepareStatement(CHECK_LASTPOSTWITHUSER); 697 pstmt.setInt(1, user.getID()); 698 ranking=Integer.parseInt(user.getProperty("ranking")); 699 } 700 ResultSet rs = pstmt.executeQuery(); 701 if( rs.next() ) { 702 if (rs.getString("lastpost")!=null && !rs.getString("lastpost").toUpperCase().equals("NULL") && !rs.getString("lastpost").equals("")){ 703 if(Calendar.getInstance().getTimeInMillis()-Long.parseLong(rs.getString("lastpost")) < Long.parseLong(SystemProperty.getProperty("PeriodBetweenPosts"))*ranking){ 704 long timediff = (Calendar.getInstance().getTimeInMillis() - Long.parseLong(rs.getString("lastpost")))/1000; 705 throw new RapidPostingException("Rapid Posting Error. There was a post already posted at " + Long.toString(timediff)+" seconds ago!"); 706 } 707 } 708 } 709 } 710 catch( SQLException sqle ) { 711 System.err.println("an error occured in DbForumMessage (0934):"+sqle.getMessage()); 712 sqle.printStackTrace(); 713 } 714 finally { 715 try { pstmt.close(); } 716 catch (Exception e) { e.printStackTrace(); } 717 try { con.close(); } 718 catch (Exception e) { e.printStackTrace(); } 719 } 720 } 721 private void checkForSession(User user,ClientIP clientIP) throws RapidPostingException{ 722 Connection con = null; 723 PreparedStatement pstmt = null; 724 try { 725 con = DbConnectionManager.getConnection(); 726 pstmt = con.prepareStatement(CHECK_SESSION); 727 pstmt.setString(1,clientIP.getRemoteIP()); 728 ResultSet rs = pstmt.executeQuery(); 729 if( rs.next() ) { 730 if(Calendar.getInstance().getTimeInMillis()-Long.parseLong(rs.getString("initime"))*1000.0 < Long.parseLong(SystemProperty.getProperty("WaitPeriod"))){ 731 throw new RapidPostingException("Rapid Posting Error. Wait period is " + SystemProperty.getProperty("WaitPeriod")+"!"); 732 } 733 } 734 } 735 catch( SQLException sqle ) { 736 System.err.println("an error occured in DbForumMessage (0936):"+sqle.getMessage()); 737 sqle.printStackTrace(); 738 } 739 finally { 740 try { pstmt.close(); } 741 catch (Exception e) { e.printStackTrace(); } 742 try { con.close(); } 743 catch (Exception e) { e.printStackTrace(); } 744 } 745 } 746 747 private void checkBlackList(User user,ClientIP clientIP) throws UserBlackListedException { 748 System.err.println("checking blacklist"); 749 Connection con = null; 750 PreparedStatement pstmt = null; 751 try { 752 con = DbConnectionManager.getConnection(); 753 pstmt = con.prepareStatement(CHECK_BLACK_LIST_IP); 754 pstmt.setString(1,clientIP.getRemoteIP()); 755 ResultSet rs = pstmt.executeQuery(); 756 if(rs.next()){ 757 pstmt = con.prepareStatement(UPDATE_BLACK_LIST_IP); 758 pstmt.setString(1,clientIP.getRemoteIP()); 759 pstmt.executeUpdate(); 760 throw new UserBlackListedException("User Black Listed Error. The IP address "+clientIP.getRemoteIP()+" has been black listed"); 761 } 762 pstmt = con.prepareStatement(CHECK_BLACK_LIST_USER); 763 pstmt.setInt(1,user.getID()); 764 pstmt.setString(2,"BLOCKED"); 765 pstmt.setString(3,"true"); 766 rs = pstmt.executeQuery(); 767 if(rs.next()){ 768 throw new UserBlackListedException("User Black Listed Error. This user has been black listed from the sytem"); 769 } 770 } 771 catch( SQLException sqle ) { 772 System.err.println("an error occured in DbForumMessage (0935):"+sqle.getMessage()); 773 sqle.printStackTrace(); 774 } 775 finally { 776 try { pstmt.close(); } 777 catch (Exception e) { e.printStackTrace(); } 778 try { con.close(); } 779 catch (Exception e) { e.printStackTrace(); } 780 } 781 } 782 public MessageRanking getRanking(){ 783 return new MessageRanking(this.ranking); 784 } 785 public void setRanking(int para) throws UnauthorizedException{ 786 this.ranking=para; 787 saveToDb(); 788 789 } 790 791 } | Popular Tags |