/**
  * Updates book information.
  *
  * @param oldBook book, which parameters should be updated.
  * @param newBook book, which parameters will be set to old book.
  * @throws LibraryProviderException in case of LibraryProviderException
  * @throws RemoteException in case of RemoteException
  */
 public synchronized void updateBook(Book oldBook, Book newBook)
     throws LibraryProviderException, RemoteException {
   Book copyOld = oldBook.copyBook();
   int newBookCount = newBook.getCount();
   int reservedBookCount = recordProvider.getReservedBookCount(oldBook.getId());
   if (newBookCount < reservedBookCount) {
     throw new LibraryProviderException(
         "Can't decrease count of copies of book with ISBN "
             + oldBook.getIsbn()
             + " to "
             + newBookCount
             + ", minimum value is "
             + reservedBookCount);
   }
   if (newBook.getCount() <= 5) {
     try {
       bookDAO.updateBook(oldBook.getId(), newBook);
       newBook.setId(oldBook.getId());
     } catch (LibraryDAOException e) {
       throw new LibraryProviderException(e.getMessage(), e);
     }
     updateISBNCash(copyOld, newBook);
     updateSearchCash(copyOld, newBook);
   } else {
     throw new LibraryProviderException(
         "Count of books with ISBN "
             + newBook.getIsbn()
             + " can't be updated up "
             + "to "
             + newBook.getCount()
             + ", maximum count of "
             + "book copies should be less than 5, you can add no more than 5 books.");
   }
 }
Exemple #2
0
 public Book getBook(int id) {
   for (Book book : books) {
     if (book.getId() == id) {
       return book;
     }
   }
   return null;
 }
 /**
  * Creates new book in book storage. If book with the isbn as in new book is already exists,
  * updates count of books with appropriate isbn. Doesn't allow to create more than 5 copies of
  * books with the same isbn.
  *
  * @param book for creation
  * @throws LibraryProviderException in case of LibraryProviderException
  * @throws RemoteException in case of RemoteException
  */
 public synchronized void createBook(Book book) throws LibraryProviderException, RemoteException {
   checkISBNCount(book);
   Book sameBook = isbnCash.get(book.getIsbn());
   if (sameBook == null) {
     try {
       bookDAO.createBook(book);
     } catch (LibraryDAOException e) {
       throw new LibraryProviderException(e.getMessage(), e);
     }
     addBookToSearchCash(book);
     addToISBNCash(book);
   } else {
     book.setCount(sameBook.getCount() + book.getCount());
     book.setId(sameBook.getId());
     try {
       bookDAO.updateBook(sameBook.getId(), book);
     } catch (LibraryDAOException e) {
       throw new LibraryProviderException(e.getMessage(), e);
     }
     updateISBNCash(sameBook, book);
     updateSearchCash(sameBook, book);
   }
 }
 public synchronized void deleteBooks(Book book, int deleteCount)
     throws LibraryProviderException, RemoteException {
   int bookCount = getBookCount(book);
   checkDeletionChance(book, deleteCount);
   if (bookCount <= deleteCount) {
     try {
       bookDAO.delete(book.getId());
     } catch (LibraryDAOException e) {
       throw new LibraryProviderException(e.getMessage(), e);
     }
     removeFromISBNCash(book);
     removeBookFromSearchCash(book);
   } else {
     Book copy = book.copyBook();
     book.setCount(bookCount - deleteCount);
     try {
       bookDAO.updateBook(book.getId(), book);
     } catch (LibraryDAOException e) {
       throw new LibraryProviderException(e.getMessage(), e);
     }
     updateISBNCash(copy, book);
     updateSearchCash(copy, book);
   }
 }
 private void checkDeletionChance(Book book, int deleteCount)
     throws LibraryProviderException, RemoteException {
   int bookCount = getBookCount(book);
   int reservedCount = recordProvider.getReservedBookCount(book.getId());
   int availableCount = bookCount - reservedCount;
   if (availableCount < deleteCount) {
     throw new LibraryProviderException(
         "Can't delete"
             + deleteCount
             + " book(s) with ISBN '"
             + book.getIsbn()
             + "'. There are "
             + reservedCount
             + " book(s) reserved and "
             + availableCount
             + " book(s) available.");
   }
 }