1 package edu.rice.rubis.servlets; 2 3 import java.io.IOException ; 4 import java.sql.Connection ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 9 import javax.servlet.ServletException ; 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 13 19 public class AboutMe extends RubisHttpServlet 20 { 21 22 23 public int getPoolSize() 24 { 25 return Config.AboutMePoolSize; 26 } 27 28 31 private void closeConnection(PreparedStatement stmt, Connection conn) 32 { 33 try 34 { 35 if (stmt != null) 36 stmt.close(); if (conn != null) 38 releaseConnection(conn); 39 } 40 catch (Exception ignore) 41 { 42 } 43 } 44 45 49 private void printError(String errorMsg, ServletPrinter sp) 50 { 51 sp.printHTML( 53 "<h3>Your request has not been processed due to the following error :</h3><br>"); 54 sp.printHTML(errorMsg); 55 sp.printHTMLfooter(); 56 57 } 58 59 60 private boolean listItem(Integer userId, Connection conn, ServletPrinter sp) 61 { 62 ResultSet currentSellings = null; 63 ResultSet pastSellings = null; 64 PreparedStatement currentStmt = null; 65 PreparedStatement pastStmt = null; 66 67 String itemName, endDate, startDate; 68 float currentPrice, initialPrice, buyNow, reservePrice; 69 int quantity, itemId; 70 71 try 73 { 74 currentStmt = 75 conn.prepareStatement( 76 "SELECT * FROM items WHERE items.seller=? AND items.end_date>=NOW()"); 77 currentStmt.setInt(1, userId.intValue()); 78 currentSellings = currentStmt.executeQuery(); 79 } 80 catch (Exception e) 81 { 82 printError("Exception getting current sellings list: " + e + "<br>", sp); 83 closeConnection(currentStmt, conn); 84 return false; 85 } 86 87 try 88 { 89 if (!currentSellings.first()) 90 { 91 sp.printHTML("<br>"); 92 sp.printHTMLHighlighted("<h3>You are currently selling no item.</h3>"); 93 currentStmt.close(); 94 } 95 else 96 { 97 sp.printHTML("<br>"); 99 sp.printSellHeader("Items you are currently selling."); 100 do 101 { 102 try 104 { 105 itemId = currentSellings.getInt("id"); 106 itemName = currentSellings.getString("name"); 107 endDate = currentSellings.getString("end_date"); 108 startDate = currentSellings.getString("start_date"); 109 initialPrice = currentSellings.getFloat("initial_price"); 110 reservePrice = currentSellings.getFloat("reserve_price"); 111 buyNow = currentSellings.getFloat("buy_now"); 112 quantity = currentSellings.getInt("quantity"); 113 114 currentPrice = currentSellings.getFloat("max_bid"); 115 if (currentPrice < initialPrice) 116 currentPrice = initialPrice; 117 118 } 119 catch (Exception e) 120 { 121 printError("Exception getting item: " + e + "<br>", sp); 122 closeConnection(currentStmt, conn); 123 return false; 124 } 125 sp.printSell( 127 itemId, 128 itemName, 129 initialPrice, 130 reservePrice, 131 quantity, 132 buyNow, 133 startDate, 134 endDate, 135 currentPrice); 136 } 137 while (currentSellings.next()); 138 currentStmt.close(); 139 sp.printItemFooter(); 140 } 141 } 142 catch (Exception e) 143 { 144 printError("Exception getting current items in sell: " + e + "<br>", sp); 145 closeConnection(currentStmt, conn); 146 return false; 147 } 148 149 try 151 { 152 pastStmt = 153 conn.prepareStatement( 154 "SELECT * FROM items WHERE items.seller=? AND TO_DAYS(NOW()) - TO_DAYS(items.end_date) < 30"); 155 pastStmt.setInt(1, userId.intValue()); 156 pastSellings = pastStmt.executeQuery(); 157 } 158 catch (Exception e) 159 { 160 printError("Exception getting past sellings list: " + e + "<br>", sp); 161 closeConnection(pastStmt, conn); 162 return false; 163 } 164 165 try 166 { 167 if (!pastSellings.first()) 168 { 169 sp.printHTML("<br>"); 170 sp.printHTMLHighlighted("<h3>You didn't sell any item.</h3>"); 171 pastStmt.close(); 172 return true; 173 } 174 sp.printHTML("<br>"); 176 sp.printSellHeader("Items you sold in the last 30 days."); 177 do 178 { 179 try 181 { 182 itemId = pastSellings.getInt("id"); 183 itemName = pastSellings.getString("name"); 184 endDate = pastSellings.getString("end_date"); 185 startDate = pastSellings.getString("start_date"); 186 initialPrice = pastSellings.getFloat("initial_price"); 187 reservePrice = pastSellings.getFloat("reserve_price"); 188 buyNow = pastSellings.getFloat("buy_now"); 189 quantity = pastSellings.getInt("quantity"); 190 191 currentPrice = pastSellings.getFloat("max_bid"); 192 if (currentPrice < initialPrice) 193 currentPrice = initialPrice; 194 195 } 196 catch (Exception e) 197 { 198 printError("Exception getting sold item: " + e + "<br>", sp); 199 closeConnection(pastStmt, conn); 200 return false; 201 } 202 sp.printSell( 204 itemId, 205 itemName, 206 initialPrice, 207 reservePrice, 208 quantity, 209 buyNow, 210 startDate, 211 endDate, 212 currentPrice); 213 } 214 while (pastSellings.next()); 215 pastStmt.close(); 216 } 217 catch (Exception e) 218 { 219 printError("Exception getting sold items: " + e + "<br>", sp); 220 closeConnection(pastStmt, conn); 221 return false; 222 } 223 sp.printItemFooter(); 224 return true; 225 } 226 227 228 private boolean listBoughtItems(Integer userId, PreparedStatement stmt, Connection conn, ServletPrinter sp) 229 { 230 ResultSet buy = null; 231 String itemName, sellerName; 232 int quantity, sellerId, itemId; 233 float buyNow; 234 235 try 237 { 238 stmt = 239 conn.prepareStatement( 240 "SELECT * FROM buy_now WHERE buy_now.buyer_id=? AND TO_DAYS(NOW()) - TO_DAYS(buy_now.date)<=30"); 241 stmt.setInt(1, userId.intValue()); 242 buy = stmt.executeQuery(); 243 if (!buy.first()) 244 { 245 sp.printHTML("<br>"); 246 sp.printHTMLHighlighted( 247 "<h3>You didn't buy any item in the last 30 days.</h3>"); 248 sp.printHTML("<br>"); 249 stmt.close(); 250 return true; 251 } 252 } 253 catch (Exception e) 254 { 255 printError("Exception getting bought items list: " + e + "<br>", sp); 256 closeConnection(stmt, conn); 257 return false; 258 } 259 260 sp.printUserBoughtItemHeader(); 261 262 try 263 { 264 do 265 { 266 itemId = buy.getInt("item_id"); 267 quantity = buy.getInt("qty"); 268 try 270 { 271 PreparedStatement itemStmt = null; 272 try 273 { 274 ResultSet itemRS = null; 275 itemStmt = 276 conn.prepareStatement("SELECT * FROM items WHERE id=?"); 277 itemStmt.setInt(1, itemId); 278 itemRS = itemStmt.executeQuery(); 279 if (!itemRS.first()) 280 { 281 sp.printHTML("Couldn't find bought item.<br>"); 282 itemStmt.close(); 283 return true; 284 } 285 itemName = itemRS.getString("name"); 286 sellerId = itemRS.getInt("seller"); 287 buyNow = itemRS.getFloat("buy_now"); 288 itemStmt.close(); 289 } 290 catch (SQLException e) 291 { 292 sp.printHTML("Failed to execute Query for item (buy now): " + e); 293 if (itemStmt != null) 294 itemStmt.close(); 295 closeConnection(stmt, conn); 296 return false; 297 } 298 PreparedStatement sellerStmt = null; 299 try 300 { 301 sellerStmt = 302 conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 303 sellerStmt.setInt(1, sellerId); 304 ResultSet sellerResult = sellerStmt.executeQuery(); 305 if (sellerResult.first()) 307 sellerName = sellerResult.getString("nickname"); 308 else 309 { 310 sp.printHTML("Unknown seller"); 311 sellerStmt.close(); 312 closeConnection(stmt, conn); 313 return false; 314 } 315 sellerStmt.close(); 316 317 } 318 catch (SQLException e) 319 { 320 sp.printHTML("Failed to execute Query for seller (buy now): " + e); 321 if (sellerStmt != null) 322 sellerStmt.close(); 323 closeConnection(stmt, conn); 324 return false; 325 } 326 } 327 catch (Exception e) 328 { 329 printError("Exception getting buyNow: " + e + "<br>", sp); 330 closeConnection(stmt, conn); 331 return false; 332 } 333 sp.printUserBoughtItem( 335 itemId, 336 itemName, 337 buyNow, 338 quantity, 339 sellerId, 340 sellerName); 341 } 342 while (buy.next()); 343 stmt.close(); 344 } 345 catch (Exception e) 346 { 347 printError("Exception getting bought items: " + e + "<br>", sp); 348 closeConnection(stmt, conn); 349 return false; 350 } 351 sp.printItemFooter(); 352 return true; 353 354 } 355 356 357 private boolean listWonItems(Integer userId, PreparedStatement stmt, Connection conn, ServletPrinter sp) 358 { 359 int sellerId, itemId; 360 float currentPrice, initialPrice; 361 String itemName, sellerName; 362 ResultSet won = null; 363 364 try 366 { 367 stmt = 368 conn.prepareStatement( 369 "SELECT item_id FROM bids, items WHERE bids.user_id=? AND bids.item_id=items.id AND TO_DAYS(NOW()) - TO_DAYS(items.end_date) < 30 GROUP BY item_id"); 370 stmt.setInt(1, userId.intValue()); 371 won = stmt.executeQuery(); 372 if (!won.first()) 373 { 374 sp.printHTML("<br>"); 375 sp.printHTMLHighlighted( 376 "<h3>You didn't win any item in the last 30 days.</h3>"); 377 sp.printHTML("<br>"); 378 stmt.close(); 379 return true; 380 } 381 } 382 catch (Exception e) 383 { 384 sp.printHTML("Exception getting won items list: " + e + "<br>"); 385 closeConnection(stmt, conn); 386 return false; 387 } 388 389 sp.printUserWonItemHeader(); 390 try 391 { 392 do 393 { 394 itemId = won.getInt("item_id"); 395 try 397 { 398 PreparedStatement itemStmt = null; 399 try 400 { 401 ResultSet itemRS = null; 402 itemStmt = 403 conn.prepareStatement("SELECT * FROM items WHERE id=?"); 404 itemStmt.setInt(1, itemId); 405 itemRS = itemStmt.executeQuery(); 406 if (!itemRS.first()) 407 { 408 sp.printHTML("Couldn't find won item.<br>"); 409 itemStmt.close(); 410 return true; 411 } 412 itemName = itemRS.getString("name"); 413 sellerId = itemRS.getInt("seller"); 414 initialPrice = itemRS.getFloat("initial_price"); 415 416 currentPrice = itemRS.getFloat("max_bid"); 417 if (currentPrice < initialPrice) 418 currentPrice = initialPrice; 419 420 itemStmt.close(); 421 } 422 catch (SQLException e) 423 { 424 sp.printHTML("Failed to execute Query for item (won items): " + e); 425 if (itemStmt != null) 426 itemStmt.close(); 427 closeConnection(stmt, conn); 428 return false; 429 } 430 PreparedStatement sellerStmt = null; 431 try 432 { 433 sellerStmt = 434 conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 435 sellerStmt.setInt(1, sellerId); 436 ResultSet sellerResult = sellerStmt.executeQuery(); 437 if (sellerResult.first()) 439 sellerName = sellerResult.getString("nickname"); 440 else 441 { 442 sp.printHTML("Unknown seller"); 443 sellerStmt.close(); 444 closeConnection(stmt, conn); 445 return false; 446 } 447 sellerStmt.close(); 448 449 } 450 catch (SQLException e) 451 { 452 sp.printHTML( 453 "Failed to execute Query for seller (won items): " + e); 454 if (sellerStmt != null) 455 sellerStmt.close(); 456 closeConnection(stmt, conn); 457 sellerStmt = null; 458 return false; 459 } 460 } 480 catch (Exception e) 481 { 482 printError("Exception getting item: " + e + "<br>", sp); 483 closeConnection(stmt, conn); 484 return false; 485 } 486 sp.printUserWonItem( 488 itemId, 489 itemName, 490 currentPrice, 491 sellerId, 492 sellerName); 493 } 494 while (won.next()); 495 stmt.close(); 496 } 497 catch (Exception e) 498 { 499 sp.printHTML("Exception getting won items: " + e + "<br>"); 500 closeConnection(stmt, conn); 501 return false; 502 } 503 sp.printItemFooter(); 504 return true; 505 } 506 507 508 private boolean listComment(Integer userId, PreparedStatement stmt, Connection conn, ServletPrinter sp) 509 { 510 ResultSet rs = null; 511 String date, comment; 512 int authorId; 513 514 try 515 { 516 conn.setAutoCommit(false); 518 try 520 { 521 stmt = 522 conn.prepareStatement("SELECT * FROM comments WHERE to_user_id=?"); 523 stmt.setInt(1, userId.intValue()); 524 rs = stmt.executeQuery(); 525 } 526 catch (Exception e) 527 { 528 sp.printHTML("Failed to execute Query for list of comments: " + e); 529 conn.rollback(); 530 closeConnection(stmt, conn); 531 return false; 532 } 533 if (!rs.first()) 534 { 535 sp.printHTML("<br>"); 536 sp.printHTMLHighlighted( 537 "<h3>There is no comment yet for this user.</h3>"); 538 sp.printHTML("<br>"); 539 conn.commit(); 540 stmt.close(); 541 return true; 542 } 543 else 544 sp.printHTML("<br><hr><br><h3>Comments for this user</h3><br>"); 545 546 sp.printCommentHeader(); 547 do 549 { 550 comment = rs.getString("comment"); 551 date = rs.getString("date"); 552 authorId = rs.getInt("from_user_id"); 553 554 String authorName = "none"; 555 ResultSet authorRS = null; 556 PreparedStatement authorStmt = null; 557 try 558 { 559 authorStmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 560 authorStmt.setInt(1, authorId); 561 authorRS = authorStmt.executeQuery(); 562 if (authorRS.first()) 563 authorName = authorRS.getString("nickname"); 564 authorStmt.close(); 565 } 566 catch (Exception e) 567 { 568 sp.printHTML("Failed to execute Query for the comment author: " + e); 569 conn.rollback(); 570 if (authorStmt != null) 571 authorStmt.close(); 572 closeConnection(stmt, conn); 573 return false; 574 } 575 sp.printComment(authorName, authorId, date, comment); 576 } 577 while (rs.next()); 578 sp.printCommentFooter(); 579 conn.commit(); 580 stmt.close(); 581 } 582 catch (Exception e) 583 { 584 sp.printHTML("Exception getting comment list: " + e + "<br>"); 585 try 586 { 587 conn.rollback(); 588 closeConnection(stmt, conn); 589 return false; 590 } 591 catch (Exception se) 592 { 593 sp.printHTML("Transaction rollback failed: " + e + "<br>"); 594 closeConnection(stmt, conn); 595 return false; 596 } 597 } 598 return true; 599 } 600 601 602 private boolean listBids(Integer userId, String username, String password, PreparedStatement stmt, Connection conn, ServletPrinter sp) 603 { 604 605 float currentPrice, initialPrice, maxBid; 606 String itemName, sellerName, startDate, endDate; 607 int sellerId, quantity, itemId; 608 ResultSet bid = null; 609 610 try 612 { 613 stmt = 614 conn.prepareStatement( 615 "SELECT item_id, bids.max_bid FROM bids, items WHERE bids.user_id=? AND bids.item_id=items.id AND items.end_date>=NOW() GROUP BY item_id"); 616 stmt.setInt(1, userId.intValue()); 617 bid = stmt.executeQuery(); 618 if (!bid.first()) 619 { 620 sp.printHTMLHighlighted("<h3>You didn't put any bid.</h3>"); 621 sp.printHTML("<br>"); 622 stmt.close(); 623 return true; 624 } 625 } 626 catch (Exception e) 627 { 628 sp.printHTML("Exception getting bids list: " + e + "<br>"); 629 closeConnection(stmt, conn); 630 return false; 631 } 632 633 sp.printUserBidsHeader(); 634 ResultSet rs = null; 635 PreparedStatement itemStmt = null; 636 try 637 { 638 do 639 { 640 itemId = bid.getInt("item_id"); 641 maxBid = bid.getFloat("max_bid"); 642 try 643 { 644 itemStmt = conn.prepareStatement("SELECT * FROM items WHERE id=?"); 645 itemStmt.setInt(1, itemId); 646 rs = itemStmt.executeQuery(); 647 } 648 catch (Exception e) 649 { 650 sp.printHTML( 651 "Failed to execute Query for item the user has bid on: " + e); 652 if (itemStmt != null) 653 itemStmt.close(); 654 closeConnection(stmt, conn); 655 return false; 656 } 657 658 try 660 { 661 if (!rs.first()) 662 { 663 sp.printHTML("<h3>Failed to get items.</h3><br>"); 664 itemStmt.close(); 665 closeConnection(stmt, conn); 666 return false; 667 } 668 itemName = rs.getString("name"); 669 initialPrice = rs.getFloat("initial_price"); 670 quantity = rs.getInt("quantity"); 671 startDate = rs.getString("start_date"); 672 endDate = rs.getString("end_date"); 673 sellerId = rs.getInt("seller"); 674 675 currentPrice = rs.getFloat("max_bid"); 676 if (currentPrice < initialPrice) 677 currentPrice = initialPrice; 678 679 itemStmt.close(); 680 681 PreparedStatement sellerStmt = null; 682 try 683 { 684 sellerStmt = 685 conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 686 sellerStmt.setInt(1, sellerId); 687 ResultSet sellerResult = sellerStmt.executeQuery(); 688 if (sellerResult.first()) 690 sellerName = sellerResult.getString("nickname"); 691 else 692 { 693 sp.printHTML("Unknown seller"); 694 closeConnection(stmt, conn); 695 if (sellerStmt != null) 696 sellerStmt.close(); 697 return false; 698 } 699 sellerStmt.close(); 700 701 } 702 catch (Exception e) 703 { 704 sp.printHTML("Failed to execute Query for seller (bids): " + e); 705 if (itemStmt != null) 706 itemStmt.close(); 707 if (sellerStmt != null) 708 sellerStmt.close(); 709 closeConnection(stmt, conn); 710 return false; 711 } 712 } 713 catch (Exception e) 714 { 715 printError("Exception getting item: " + e + "<br>", sp); 716 closeConnection(stmt, conn); 717 return false; 718 } 719 sp.printItemUserHasBidOn( 721 itemId, 722 itemName, 723 initialPrice, 724 quantity, 725 startDate, 726 endDate, 727 sellerId, 728 sellerName, 729 currentPrice, 730 maxBid, 731 username, 732 password); 733 } 734 while (bid.next()); 735 stmt.close(); 736 } 737 catch (Exception e) 738 { 739 printError("Exception getting items the user has bid on: " + e + "<br>", sp); 740 closeConnection(stmt, conn); 741 return false; 742 } 743 sp.printItemFooter(); 744 return true; 745 } 746 747 755 public void doGet(HttpServletRequest request, HttpServletResponse response) 756 throws IOException , ServletException 757 { 758 doPost(request, response); 759 } 760 761 770 public void doPost(HttpServletRequest request, HttpServletResponse response) 771 throws IOException , ServletException 772 { 773 String password = null, username = null; 774 Integer userId = null; 775 ResultSet rs = null; 776 PreparedStatement stmt = null; 777 Connection conn = null; 778 ServletPrinter sp = null; 779 780 sp = new ServletPrinter(response, "About me"); 781 782 username = request.getParameter("nickname"); 783 password = request.getParameter("password"); 784 conn = getConnection(); 785 if ((username != null && username != "") 787 || (password != null && password != "")) 788 { 789 Auth auth = new Auth(conn, sp); 790 int id = auth.authenticate(username, password); 791 if (id == -1) 792 { 793 printError("You don't have an account on RUBiS!<br>You have to register first.<br>", sp); 794 closeConnection(conn); 795 return; 796 } 797 userId = new Integer (id); 798 } 799 else 800 { 801 printError(" You must provide valid username and password.", sp); 802 return; 803 } 804 try 806 { 807 stmt = conn.prepareStatement("SELECT * FROM users WHERE id=?"); 808 stmt.setInt(1, userId.intValue()); 809 rs = stmt.executeQuery(); 810 } 811 catch (Exception e) 812 { 813 sp.printHTML("Failed to execute Query for user: " + e); 814 closeConnection(stmt, conn); 815 sp.printHTMLfooter(); 816 return; 817 } 818 try 819 { 820 if (!rs.first()) 821 { 822 sp.printHTML("<h2>This user does not exist!</h2>"); 823 closeConnection(stmt, conn); 824 sp.printHTMLfooter(); 825 return; 826 } 827 String firstname = rs.getString("firstname"); 828 String lastname = rs.getString("lastname"); 829 String nickname = rs.getString("nickname"); 830 String email = rs.getString("email"); 831 String date = rs.getString("creation_date"); 832 int rating = rs.getInt("rating"); 833 stmt.close(); 834 835 String result = new String (); 836 837 result = result + "<h2>Information about " + nickname + "<br></h2>"; 838 result = 839 result + "Real life name : " + firstname + " " + lastname + "<br>"; 840 result = result + "Email address : " + email + "<br>"; 841 result = result + "User since : " + date + "<br>"; 842 result = result + "Current rating : <b>" + rating + "</b><br>"; 843 sp.printHTMLheader("RUBiS: About " + nickname); 844 sp.printHTML(result); 845 846 } 847 catch (SQLException s) 848 { 849 sp.printHTML("Failed to get general information about the user: " + s); 850 closeConnection(stmt, conn); 851 sp.printHTMLfooter(); 852 return; 853 } 854 855 boolean connAlive; 856 857 connAlive = listBids(userId, username, password, stmt, conn, sp); 858 if (connAlive) { 859 connAlive = listItem(userId, conn, sp); 860 } 861 if (connAlive) { 862 connAlive = listWonItems(userId, stmt, conn, sp); 863 } 864 if (connAlive) { 865 connAlive = listBoughtItems(userId, stmt, conn, sp); 866 } 867 if (connAlive) { 868 connAlive = listComment(userId, stmt, conn, sp); 869 } 870 871 sp.printHTMLfooter(); 872 if (connAlive) { 873 closeConnection(stmt, conn); 874 } 875 } 876 877 880 public void destroy() 881 { 882 super.destroy(); 883 } 884 885 } 886 | Popular Tags |