1 15 package org.apache.tapestry.vlib.ejb.impl; 16 17 import java.rmi.RemoteException ; 18 import java.sql.Connection ; 19 import java.sql.ResultSet ; 20 import java.sql.SQLException ; 21 import java.sql.Timestamp ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import javax.ejb.CreateException ; 28 import javax.ejb.FinderException ; 29 import javax.ejb.RemoveException ; 30 import javax.ejb.SessionBean ; 31 import javax.ejb.SessionContext ; 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 import javax.rmi.PortableRemoteObject ; 36 import javax.sql.DataSource ; 37 38 import org.apache.tapestry.Tapestry; 39 import org.apache.tapestry.contrib.ejb.XCreateException; 40 import org.apache.tapestry.contrib.ejb.XEJBException; 41 import org.apache.tapestry.contrib.ejb.XRemoveException; 42 import org.apache.tapestry.contrib.jdbc.IStatement; 43 import org.apache.tapestry.contrib.jdbc.StatementAssembly; 44 import org.apache.tapestry.vlib.ejb.Book; 45 import org.apache.tapestry.vlib.ejb.BorrowException; 46 import org.apache.tapestry.vlib.ejb.IBook; 47 import org.apache.tapestry.vlib.ejb.IBookHome; 48 import org.apache.tapestry.vlib.ejb.IPerson; 49 import org.apache.tapestry.vlib.ejb.IPersonHome; 50 import org.apache.tapestry.vlib.ejb.IPublisher; 51 import org.apache.tapestry.vlib.ejb.IPublisherHome; 52 import org.apache.tapestry.vlib.ejb.LoginException; 53 import org.apache.tapestry.vlib.ejb.Person; 54 import org.apache.tapestry.vlib.ejb.Publisher; 55 import org.apache.tapestry.vlib.ejb.RegistrationException; 56 import org.apache.tapestry.vlib.ejb.SortColumn; 57 import org.apache.tapestry.vlib.ejb.SortOrdering; 58 59 69 70 public class OperationsBean implements SessionBean 71 { 72 private SessionContext _context; 73 private transient Context _environment; 74 private transient IBookHome _bookHome; 75 private transient IPersonHome _personHome; 76 private transient IPublisherHome _publisherHome; 77 78 83 84 private transient DataSource _dataSource; 85 86 92 93 public void ejbCreate() 94 { 95 Context initial; 96 97 try 98 { 99 initial = new InitialContext (); 100 _environment = (Context ) initial.lookup("java:comp/env"); 101 } 102 catch (NamingException e) 103 { 104 throw new XEJBException("Could not lookup environment.", e); 105 } 106 107 try 108 { 109 _dataSource = (DataSource ) _environment.lookup("jdbc/dataSource"); 110 } 111 catch (NamingException e) 112 { 113 e.printStackTrace(); 114 throw new XEJBException("Could not lookup data source.", e); 115 } 116 } 117 118 public void ejbRemove() 119 { 120 } 121 122 125 126 public void ejbPassivate() 127 { 128 } 129 130 public void setSessionContext(SessionContext value) 131 { 132 _context = value; 133 } 134 135 139 140 public void ejbActivate() 141 { 142 } 143 144 150 151 public Book borrowBook(Integer bookId, Integer borrowerId) 152 throws FinderException , RemoteException , BorrowException 153 { 154 IBookHome bookHome = getBookHome(); 155 IPersonHome personHome = getPersonHome(); 156 157 IBook book = bookHome.findByPrimaryKey(bookId); 158 159 if (!book.getLendable()) 160 throw new BorrowException("Book may not be borrowed."); 161 162 164 personHome.findByPrimaryKey(borrowerId); 165 166 168 171 personHome.findByPrimaryKey(book.getOwnerId()); 172 173 176 book.setHolderId(borrowerId); 177 178 return getBook(bookId); 179 } 180 181 185 186 public Integer addBook(Map attributes) throws CreateException , RemoteException 187 { 188 IBookHome home = getBookHome(); 189 190 attributes.put("dateAdded", new Timestamp (System.currentTimeMillis())); 191 192 IBook book = home.create(attributes); 193 194 return (Integer ) book.getPrimaryKey(); 195 } 196 197 206 207 public Integer addBook(Map attributes, String publisherName) 208 throws CreateException , RemoteException 209 { 210 IPublisher publisher = null; 211 IPublisherHome publisherHome = getPublisherHome(); 212 213 215 try 216 { 217 publisher = publisherHome.findByName(publisherName); 218 } 219 catch (FinderException e) 220 { 221 } 223 224 if (publisher == null) 225 publisher = publisherHome.create(publisherName); 226 227 attributes.put("publisherId", publisher.getPrimaryKey()); 228 229 return addBook(attributes); 230 } 231 232 240 241 public void updateBook(Integer bookId, Map attributes) throws FinderException , RemoteException 242 { 243 IBookHome bookHome = getBookHome(); 244 245 IBook book = bookHome.findByPrimaryKey(bookId); 246 247 book.updateEntityAttributes(attributes); 248 } 249 250 260 261 public void updateBook(Integer bookId, Map attributes, String publisherName) 262 throws CreateException , FinderException , RemoteException 263 { 264 IPublisher publisher = null; 265 266 IPublisherHome publisherHome = getPublisherHome(); 267 268 try 269 { 270 publisher = publisherHome.findByName(publisherName); 271 } 272 catch (FinderException e) 273 { 274 } 276 277 if (publisher == null) 278 publisher = publisherHome.create(publisherName); 279 280 282 attributes.put("publisherId", publisher.getPrimaryKey()); 283 284 updateBook(bookId, attributes); 285 } 286 287 public void updatePerson(Integer personId, Map attributes) 288 throws FinderException , RemoteException 289 { 290 IPersonHome home = getPersonHome(); 291 292 IPerson person = home.findByPrimaryKey(personId); 293 294 person.updateEntityAttributes(attributes); 295 } 296 297 public Publisher[] getPublishers() 298 { 299 Connection connection = null; 300 IStatement statement = null; 301 ResultSet set = null; 302 303 List list = new ArrayList (); 304 305 try 306 { 307 connection = getConnection(); 308 309 StatementAssembly assembly = new StatementAssembly(); 310 311 assembly.newLine("SELECT PUBLISHER_ID, NAME"); 312 assembly.newLine("FROM PUBLISHER"); 313 assembly.newLine("ORDER BY NAME"); 314 315 statement = assembly.createStatement(connection); 316 317 set = statement.executeQuery(); 318 319 while (set.next()) 320 { 321 Integer primaryKey = (Integer ) set.getObject(1); 322 String name = set.getString(2); 323 324 list.add(new Publisher(primaryKey, name)); 325 } 326 } 327 catch (SQLException ex) 328 { 329 ex.printStackTrace(); 330 throw new XEJBException("Could not fetch all Publishers.", ex); 331 } 332 finally 333 { 334 close(connection, statement, set); 335 } 336 337 339 return (Publisher[]) list.toArray(new Publisher[list.size()]); 340 } 341 342 348 349 public Person[] getPersons() 350 { 351 Connection connection = null; 352 IStatement statement = null; 353 ResultSet set = null; 354 355 List list = new ArrayList (); 356 357 try 358 { 359 connection = getConnection(); 360 361 StatementAssembly assembly = buildBasePersonQuery(); 362 assembly.newLine("ORDER BY LAST_NAME, FIRST_NAME"); 363 364 statement = assembly.createStatement(connection); 365 366 set = statement.executeQuery(); 367 368 Object [] columns = new Object [Person.N_COLUMNS]; 369 370 while (set.next()) 371 { 372 list.add(convertRowToPerson(set, columns)); 373 } 374 } 375 catch (SQLException ex) 376 { 377 throw new XEJBException("Could not fetch all Persons.", ex); 378 } 379 finally 380 { 381 close(connection, statement, set); 382 } 383 384 return (Person[]) list.toArray(new Person[list.size()]); 385 } 386 387 392 393 public Person getPerson(Integer personId) throws FinderException 394 { 395 Connection connection = null; 396 IStatement statement = null; 397 ResultSet set = null; 398 399 Person result = null; 400 401 try 402 { 403 connection = getConnection(); 404 405 StatementAssembly assembly = buildBasePersonQuery(); 406 assembly.newLine("WHERE "); 407 assembly.add("PERSON_ID = "); 408 assembly.addParameter(personId); 409 assembly.newLine("ORDER BY LAST_NAME, FIRST_NAME"); 410 411 statement = assembly.createStatement(connection); 412 413 set = statement.executeQuery(); 414 415 if (!set.next()) 416 throw new FinderException ("Person #" + personId + " does not exist."); 417 418 Object [] columns = new Object [Person.N_COLUMNS]; 419 result = convertRowToPerson(set, columns); 420 421 } 422 catch (SQLException ex) 423 { 424 throw new XEJBException("Unable to perform database query.", ex); 425 } 426 finally 427 { 428 close(connection, statement, set); 429 } 430 431 return result; 432 } 433 434 public Person login(String email, String password) throws RemoteException , LoginException 435 { 436 IPersonHome home = getPersonHome(); 437 IPerson person = null; 438 Person result = null; 439 440 try 441 { 442 person = home.findByEmail(email); 443 } 444 catch (FinderException ex) 445 { 446 throw new LoginException("Unknown e-mail address.", false); 447 } 448 449 if (!person.getPassword().equals(password)) 450 throw new LoginException("Invalid password.", true); 451 452 try 453 { 454 result = getPerson((Integer ) person.getPrimaryKey()); 455 } 456 catch (FinderException ex) 457 { 458 throw new LoginException("Could not read person.", false); 459 } 460 461 if (result.isLockedOut()) 462 throw new LoginException("You have been locked out of the Virtual Library.", false); 463 464 466 person.setLastAccess(new Timestamp (System.currentTimeMillis())); 467 468 return result; 469 } 470 471 public Map getPersonAttributes(Integer personId) throws FinderException , RemoteException 472 { 473 IPersonHome home = getPersonHome(); 474 475 IPerson person = home.findByPrimaryKey(personId); 476 477 return person.getEntityAttributes(); 478 } 479 480 486 487 public Book getBook(Integer bookId) throws FinderException 488 { 489 Connection connection = null; 490 IStatement statement = null; 491 ResultSet set = null; 492 493 Book result = null; 494 495 try 496 { 497 connection = getConnection(); 498 499 StatementAssembly assembly = buildBaseBookQuery(); 500 assembly.addSep(" AND "); 501 assembly.add("book.BOOK_ID = "); 502 assembly.addParameter(bookId); 503 504 statement = assembly.createStatement(connection); 505 506 set = statement.executeQuery(); 507 508 if (!set.next()) 509 throw new FinderException ("Book " + bookId + " does not exist."); 510 511 Object [] columns = new Object [Book.N_COLUMNS]; 512 result = convertRowToBook(set, columns); 513 514 } 515 catch (SQLException ex) 516 { 517 throw new XEJBException("Unable to perform database query.", ex); 518 } 519 finally 520 { 521 close(connection, statement, set); 522 } 523 524 return result; 525 } 526 527 public Map getBookAttributes(Integer bookId) throws FinderException , RemoteException 528 { 529 IBookHome home = getBookHome(); 530 531 IBook book = home.findByPrimaryKey(bookId); 532 533 return book.getEntityAttributes(); 534 } 535 536 542 543 public Person registerNewUser(String firstName, String lastName, String email, String password) 544 throws RegistrationException, CreateException , RemoteException 545 { 546 IPersonHome home; 547 548 if (password == null || password.trim().length() == 0) 549 throw new RegistrationException("Must specify a password."); 550 551 validateUniquePerson(firstName, lastName, email); 552 553 home = getPersonHome(); 554 555 Map attributes = new HashMap (); 556 557 attributes.put("lastName", lastName.trim()); 558 attributes.put("firstName", firstName.trim()); 559 attributes.put("email", email.trim()); 560 attributes.put("password", password.trim()); 561 attributes.put("lastAccess", new Timestamp (System.currentTimeMillis())); 562 563 IPerson person = home.create(attributes); 564 565 Integer personId = (Integer ) person.getPrimaryKey(); 566 567 try 568 { 569 return getPerson(personId); 570 } 571 catch (FinderException ex) 572 { 573 throw new XCreateException("Unable to find newly created Person.", ex); 574 } 575 } 576 577 public Book deleteBook(Integer bookId) throws RemoveException , RemoteException 578 { 579 IBookHome home = getBookHome(); 580 Book result = null; 581 582 try 583 { 584 result = getBook(bookId); 585 } 586 catch (FinderException ex) 587 { 588 throw new XRemoveException(ex); 589 } 590 591 home.remove(bookId); 592 593 return result; 594 595 } 596 597 601 602 public void transferBooks(Integer newOwnerId, Integer [] bookIds) 603 throws FinderException , RemoteException 604 { 605 if (bookIds == null) 606 throw new RemoteException ("Must supply non-null list of books to transfer."); 607 608 if (newOwnerId == null) 609 throw new RemoteException ("Must provide an owner for the books."); 610 611 613 IPersonHome personHome = getPersonHome(); 614 personHome.findByPrimaryKey(newOwnerId); 615 616 618 IBookHome home = getBookHome(); 619 620 for (int i = 0; i < bookIds.length; i++) 621 { 622 IBook book = home.findByPrimaryKey(bookIds[i]); 623 624 book.setOwnerId(newOwnerId); 625 } 626 } 627 628 public void updatePublishers(Publisher[] updated, Integer [] deleted) 629 throws FinderException , RemoveException , RemoteException 630 { 631 IPublisherHome home = getPublisherHome(); 632 633 if (updated != null) 634 { 635 for (int i = 0; i < updated.length; i++) 636 { 637 IPublisher publisher = home.findByPrimaryKey(updated[i].getId()); 638 publisher.setName(updated[i].getName()); 639 } 640 } 641 642 if (deleted != null) 643 { 644 for (int i = 0; i < deleted.length; i++) 645 { 646 home.remove(deleted[i]); 647 } 648 } 649 } 650 651 public void updatePersons( 652 Person[] updated, 653 Integer [] resetPassword, 654 String newPassword, 655 Integer [] deleted, 656 Integer adminId) 657 throws FinderException , RemoveException , RemoteException 658 { 659 IPersonHome home = getPersonHome(); 660 661 int count = Tapestry.size(updated); 662 663 for (int i = 0; i < count; i++) 664 { 665 Person u = updated[i]; 666 IPerson person = home.findByPrimaryKey(u.getId()); 667 668 person.setAdmin(u.isAdmin()); 669 person.setLockedOut(u.isLockedOut()); 670 } 671 672 count = Tapestry.size(resetPassword); 673 674 for (int i = 0; i < count; i++) 675 { 676 IPerson person = home.findByPrimaryKey(resetPassword[i]); 677 678 person.setPassword(newPassword); 679 } 680 681 count = Tapestry.size(deleted); 682 683 if (count > 0) 684 { 685 returnBooksFromDeletedPersons(deleted); 686 moveBooksFromDeletedPersons(deleted, adminId); 687 } 688 689 for (int i = 0; i < count; i++) 690 home.remove(deleted[i]); 691 } 692 693 698 699 private void returnBooksFromDeletedPersons(Integer deletedPersonIds[]) throws RemoveException 700 { 701 StatementAssembly assembly = new StatementAssembly(); 702 703 assembly.add("UPDATE BOOK"); 704 assembly.newLine("SET HOLDER_ID = OWNER_ID"); 705 assembly.newLine("WHERE HOLDER_ID IN ("); 706 assembly.addParameterList(deletedPersonIds, ", "); 707 assembly.add(")"); 708 709 executeUpdate(assembly); 710 } 711 712 716 717 private void moveBooksFromDeletedPersons(Integer deletedPersonIds[], Integer adminId) 718 throws RemoveException 719 { 720 StatementAssembly assembly = new StatementAssembly(); 721 722 assembly.add("UPDATE BOOK"); 723 assembly.newLine("SET OWNER_ID = "); 724 assembly.addParameter(adminId); 725 assembly.newLine("WHERE OWNER_ID IN ("); 726 assembly.addParameterList(deletedPersonIds, ", "); 727 assembly.add(")"); 728 729 executeUpdate(assembly); 730 731 } 732 733 private void executeUpdate(StatementAssembly assembly) throws XRemoveException 734 { 735 Connection connection = null; 736 IStatement statement = null; 737 738 try 739 { 740 connection = getConnection(); 741 742 statement = assembly.createStatement(connection); 743 744 statement.executeUpdate(); 745 746 statement.close(); 747 statement = null; 748 749 connection.close(); 750 connection = null; 751 } 752 catch (SQLException ex) 753 { 754 throw new XRemoveException( 755 "Unable to execute " + assembly + ": " + ex.getMessage(), 756 ex); 757 } 758 finally 759 { 760 close(connection, statement, null); 761 } 762 } 763 764 770 771 protected Book convertRowToBook(ResultSet set, Object [] columns) throws SQLException 772 { 773 int column = 1; 774 775 columns[Book.ID_COLUMN] = set.getObject(column++); 776 columns[Book.TITLE_COLUMN] = set.getString(column++); 777 columns[Book.DESCRIPTION_COLUMN] = set.getString(column++); 778 columns[Book.ISBN_COLUMN] = set.getString(column++); 779 columns[Book.OWNER_ID_COLUMN] = set.getObject(column++); 780 columns[Book.OWNER_NAME_COLUMN] = 781 buildName(set.getString(column++), set.getString(column++)); 782 columns[Book.HOLDER_ID_COLUMN] = set.getObject(column++); 783 columns[Book.HOLDER_NAME_COLUMN] = 784 buildName(set.getString(column++), set.getString(column++)); 785 columns[Book.PUBLISHER_ID_COLUMN] = set.getObject(column++); 786 columns[Book.PUBLISHER_NAME_COLUMN] = set.getString(column++); 787 columns[Book.AUTHOR_COLUMN] = set.getString(column++); 788 columns[Book.HIDDEN_COLUMN] = getBoolean(set, column++); 789 columns[Book.LENDABLE_COLUMN] = getBoolean(set, column++); 790 columns[Book.DATE_ADDED_COLUMN] = set.getTimestamp(column++); 791 792 return new Book(columns); 793 } 794 795 private String buildName(String firstName, String lastName) 796 { 797 if (firstName == null) 798 return lastName; 799 800 return firstName + " " + lastName; 801 } 802 803 809 810 private static final String [] BOOK_SELECT_COLUMNS = 811 { 812 "book.BOOK_ID", 813 "book.TITLE", 814 "book.DESCRIPTION", 815 "book.ISBN", 816 "owner.PERSON_ID", 817 "owner.FIRST_NAME", 818 "owner.LAST_NAME", 819 "holder.PERSON_ID", 820 "holder.FIRST_NAME", 821 "holder.LAST_NAME", 822 "publisher.PUBLISHER_ID", 823 "publisher.NAME", 824 "book.AUTHOR", 825 "book.HIDDEN", 826 "book.LENDABLE", 827 "book.DATE_ADDED" }; 828 829 private static final String [] BOOK_ALIAS_COLUMNS = 830 { "BOOK book", "PERSON owner", "PERSON holder", "PUBLISHER publisher" }; 831 832 private static final String [] BOOK_JOINS = 833 { 834 "book.OWNER_ID = owner.PERSON_ID", 835 "book.HOLDER_ID = holder.PERSON_ID", 836 "book.PUBLISHER_ID = publisher.PUBLISHER_ID" }; 837 838 private static final Map BOOK_SORT_ASCENDING = new HashMap (); 839 private static final Map BOOK_SORT_DESCENDING = new HashMap (); 840 841 static { 842 BOOK_SORT_ASCENDING.put(SortColumn.TITLE, "book.TITLE"); 843 BOOK_SORT_ASCENDING.put(SortColumn.HOLDER, "holder.LAST_NAME, holder.FIRST_NAME"); 844 BOOK_SORT_ASCENDING.put(SortColumn.OWNER, "owner.FIRST_NAME, owner.LAST_NAME"); 845 BOOK_SORT_ASCENDING.put(SortColumn.PUBLISHER, "publisher.NAME"); 846 BOOK_SORT_ASCENDING.put(SortColumn.AUTHOR, "book.AUTHOR"); 847 848 BOOK_SORT_DESCENDING.put(SortColumn.TITLE, "book.TITLE DESC"); 849 BOOK_SORT_DESCENDING.put( 850 SortColumn.HOLDER, 851 "holder.LAST_NAME DESC, holder.FIRST_NAME DESC"); 852 BOOK_SORT_DESCENDING.put(SortColumn.OWNER, "owner.FIRST_NAME DESC, owner.LAST_NAME DESC"); 853 BOOK_SORT_DESCENDING.put(SortColumn.PUBLISHER, "publisher.NAME DESC"); 854 BOOK_SORT_DESCENDING.put(SortColumn.AUTHOR, "book.AUTHOR DESC"); 855 } 856 857 protected StatementAssembly buildBaseBookQuery() 858 { 859 StatementAssembly result = new StatementAssembly(); 860 861 result.newLine("SELECT "); 862 result.addList(BOOK_SELECT_COLUMNS, ", "); 863 864 result.newLine("FROM "); 865 result.addList(BOOK_ALIAS_COLUMNS, ", "); 866 867 result.newLine("WHERE "); 868 result.addList(BOOK_JOINS, " AND "); 869 870 return result; 871 } 872 873 883 884 protected void addSortOrdering(StatementAssembly assembly, SortOrdering ordering) 885 { 886 if (ordering == null) 887 { 888 assembly.newLine("ORDER BY book.TITLE"); 889 return; 890 } 891 892 Map sorts = ordering.isDescending() ? BOOK_SORT_DESCENDING : BOOK_SORT_ASCENDING; 893 894 String term = (String ) sorts.get(ordering.getColumn()); 895 896 assembly.newLine("ORDER BY "); 897 assembly.add(term); 898 } 899 900 protected void addSubstringSearch(StatementAssembly assembly, String column, String value) 901 { 902 if (value == null) 903 return; 904 905 String trimmed = value.trim(); 906 if (trimmed.length() == 0) 907 return; 908 909 912 assembly.addSep(" AND LOWER("); 913 assembly.add(column); 914 assembly.add(") LIKE"); 915 assembly.addParameter("%" + trimmed.toLowerCase() + "%"); 916 } 917 918 923 924 protected void close(Connection connection, IStatement statement, ResultSet resultSet) 925 { 926 if (resultSet != null) 927 { 928 try 929 { 930 resultSet.close(); 931 } 932 catch (SQLException ex) 933 { 934 System.out.println("Exception closing result set."); 935 ex.printStackTrace(); 936 } 937 } 938 939 if (statement != null) 940 { 941 try 942 { 943 statement.close(); 944 } 945 catch (SQLException ex) 946 { 947 System.out.println("Exception closing statement."); 948 ex.printStackTrace(); 949 } 950 } 951 952 if (connection != null) 953 { 954 try 955 { 956 connection.close(); 957 } 958 catch (SQLException ex) 959 { 960 System.out.println("Exception closing connection."); 961 ex.printStackTrace(); 962 } 963 } 964 } 965 966 private IPersonHome getPersonHome() 967 { 968 if (_personHome == null) 969 { 970 try 971 { 972 Object raw = _environment.lookup("ejb/Person"); 973 974 _personHome = (IPersonHome) PortableRemoteObject.narrow(raw, IPersonHome.class); 975 } 976 catch (NamingException ex) 977 { 978 throw new XEJBException("Could not lookup Person home interface.", ex); 979 } 980 981 } 982 983 return _personHome; 984 } 985 986 private IPublisherHome getPublisherHome() 987 { 988 if (_publisherHome == null) 989 { 990 try 991 { 992 Object raw = _environment.lookup("ejb/Publisher"); 993 994 _publisherHome = 995 (IPublisherHome) PortableRemoteObject.narrow(raw, IPublisherHome.class); 996 } 997 catch (NamingException e) 998 { 999 throw new XEJBException("Could not lookup Publisher home interface.", e); 1000 } 1001 1002 } 1003 1004 return _publisherHome; 1005 } 1006 1007 private IBookHome getBookHome() 1008 { 1009 if (_bookHome == null) 1010 { 1011 try 1012 { 1013 Object raw = _environment.lookup("ejb/Book"); 1014 1015 _bookHome = (IBookHome) PortableRemoteObject.narrow(raw, IBookHome.class); 1016 } 1017 catch (NamingException e) 1018 { 1019 throw new XEJBException("Could not lookup Book home interface.", e); 1020 } 1021 1022 } 1023 1024 return _bookHome; 1025 } 1026 1027 1031 1032 protected Connection getConnection() 1033 { 1034 try 1035 { 1036 return _dataSource.getConnection(); 1037 } 1038 catch (SQLException e) 1039 { 1040 throw new XEJBException("Unable to get database connection from pool.", e); 1041 } 1042 } 1043 1044 protected StatementAssembly buildBasePersonQuery() 1045 { 1046 StatementAssembly result; 1047 1048 result = new StatementAssembly(); 1049 1050 result.newLine("SELECT PERSON_ID, FIRST_NAME, LAST_NAME, EMAIL, "); 1051 result.newLine(" LOCKED_OUT, ADMIN, LAST_ACCESS"); 1052 result.newLine("FROM PERSON"); 1053 1054 return result; 1055 } 1056 1057 1063 1064 protected Person convertRowToPerson(ResultSet set, Object [] columns) throws SQLException 1065 { 1066 int column = 1; 1067 1068 columns[Person.ID_COLUMN] = set.getObject(column++); 1069 columns[Person.FIRST_NAME_COLUMN] = set.getString(column++); 1070 columns[Person.LAST_NAME_COLUMN] = set.getString(column++); 1071 columns[Person.EMAIL_COLUMN] = set.getString(column++); 1072 columns[Person.LOCKED_OUT_COLUMN] = getBoolean(set, column++); 1073 columns[Person.ADMIN_COLUMN] = getBoolean(set, column++); 1074 columns[Person.LAST_ACCESS_COLUMN] = set.getTimestamp(column++); 1075 1076 return new Person(columns); 1077 } 1078 1079 private Boolean getBoolean(ResultSet set, int index) throws SQLException 1080 { 1081 return set.getBoolean(index) ? Boolean.TRUE : Boolean.FALSE; 1082 } 1083 1084 private void validateUniquePerson(String firstName, String lastName, String email) 1085 throws RegistrationException 1086 { 1087 Connection connection = null; 1088 IStatement statement = null; 1089 ResultSet set = null; 1090 1091 String trimmedEmail = email.trim().toLowerCase(); 1092 String trimmedLastName = lastName.trim().toLowerCase(); 1093 String trimmedFirstName = firstName.trim().toLowerCase(); 1094 1095 try 1096 { 1097 connection = getConnection(); 1098 1099 StatementAssembly assembly = new StatementAssembly(); 1100 assembly.newLine("SELECT PERSON_ID"); 1101 assembly.newLine("FROM PERSON"); 1102 assembly.newLine("WHERE "); 1103 1104 assembly.add("LOWER(EMAIL) = "); 1105 assembly.addParameter(trimmedEmail); 1106 1107 statement = assembly.createStatement(connection); 1108 set = statement.executeQuery(); 1109 1110 if (set.next()) 1111 throw new RegistrationException("Email address is already in use by another user."); 1112 1113 close(null, statement, set); 1114 1115 assembly = new StatementAssembly(); 1116 assembly.newLine("SELECT PERSON_ID"); 1117 assembly.newLine("FROM PERSON"); 1118 assembly.newLine("WHERE "); 1119 1120 assembly.add("LOWER(FIRST_NAME) = "); 1121 assembly.addParameter(trimmedFirstName); 1122 assembly.addSep(" AND "); 1123 assembly.add("LOWER(LAST_NAME) = "); 1124 assembly.addParameter(trimmedLastName); 1125 1126 statement = assembly.createStatement(connection); 1127 set = statement.executeQuery(); 1128 1129 if (set.next()) 1130 throw new RegistrationException("Name provided is already in use by another user."); 1131 1132 } 1133 catch (SQLException e) 1134 { 1135 throw new RegistrationException("Could not access database: " + e.getMessage(), e); 1136 } 1137 finally 1138 { 1139 close(connection, statement, set); 1140 } 1141 } 1142 1143 public Book returnBook(Integer bookId) throws RemoteException , FinderException 1144 { 1145 IBookHome bookHome = getBookHome(); 1146 IBook book = bookHome.findByPrimaryKey(bookId); 1147 1148 Integer ownerPK = book.getOwnerId(); 1149 1150 book.setHolderId(ownerPK); 1151 1152 return getBook(bookId); 1153 } 1154 1155} | Popular Tags |