public void addCopies(Set<BookCopy> bookCopiesSet) throws BookStoreException {
    int ISBN, numCopies;

    if (bookCopiesSet == null) {
      throw new BookStoreException(BookStoreConstants.NULL_INPUT);
    }
    try {
      bsLock.updateLock().lock();
      for (BookCopy bookCopy : bookCopiesSet) {
        ISBN = bookCopy.getISBN();
        numCopies = bookCopy.getNumCopies();
        if (BookStoreUtility.isInvalidISBN(ISBN))
          throw new BookStoreException(BookStoreConstants.ISBN + ISBN + BookStoreConstants.INVALID);
        if (!bookMap.containsKey(ISBN))
          throw new BookStoreException(BookStoreConstants.ISBN + ISBN + BookStoreConstants.INVALID);
        if (BookStoreUtility.isInvalidNoCopies(numCopies))
          throw new BookStoreException(
              BookStoreConstants.NUM_COPIES + numCopies + BookStoreConstants.INVALID);
      }
      bsLock.writeLock().lock();
      BookStoreBook book;
      // Update the number of copies
      for (BookCopy bookCopy : bookCopiesSet) {
        ISBN = bookCopy.getISBN();
        numCopies = bookCopy.getNumCopies();
        book = bookMap.get(ISBN);
        book.addCopies(numCopies);
      }

    } finally {
      bsLock.writeLock().unlock();
      bsLock.updateLock().unlock();
    }
  }
Esempio n. 2
0
  public void addBooks(Set<StockBook> bookSet) throws BookStoreException {
    ContentExchange exchange = new ContentExchange();
    String urlString;
    urlString = serverAddress + "/" + BookStoreMessageTag.ADDBOOKS;

    String listBooksxmlString = BookStoreUtility.serializeObjectToXMLString(bookSet);
    exchange.setMethod("POST");
    exchange.setURL(urlString);
    Buffer requestContent = new ByteArrayBuffer(listBooksxmlString);
    exchange.setRequestContent(requestContent);

    BookStoreUtility.SendAndRecv(this.client, exchange);
  }
Esempio n. 3
0
  @SuppressWarnings("unchecked")
  public List<StockBook> getBooksByISBN(Set<Integer> isbns) throws BookStoreException {
    ContentExchange exchange = new ContentExchange();
    String urlString;
    urlString = serverAddress + "/" + BookStoreMessageTag.GETSTOCKBOOKSBYISBN;

    String listBooksxmlString = BookStoreUtility.serializeObjectToXMLString(isbns);
    exchange.setMethod("POST");
    exchange.setURL(urlString);
    Buffer requestContent = new ByteArrayBuffer(listBooksxmlString);
    exchange.setRequestContent(requestContent);

    return (List<StockBook>) BookStoreUtility.SendAndRecv(this.client, exchange);
  }
  public void addCopies(Set<BookCopy> bookCopiesSet) throws BookStoreException {

    String listBookCopiesxmlString = BookStoreUtility.serializeObjectToXMLString(bookCopiesSet);
    Buffer requestContent = new ByteArrayBuffer(listBookCopiesxmlString);
    BookStoreResult result = null;

    ContentExchange exchange = new ContentExchange();
    String urlString = getMasterServerAddress() + "/" + BookStoreMessageTag.ADDCOPIES;
    exchange.setMethod("POST");
    exchange.setURL(urlString);
    exchange.setRequestContent(requestContent);
    result = BookStoreUtility.SendAndRecv(this.client, exchange);
    this.setSnapshotId(result.getSnapshotId());
  }
Esempio n. 5
0
  public void updateEditorPicks(Set<BookEditorPick> editorPicksValues) throws BookStoreException {
    ContentExchange exchange = new ContentExchange();
    String urlString = serverAddress + "/" + BookStoreMessageTag.UPDATEEDITORPICKS + "?";

    String xmlStringEditorPicksValues =
        BookStoreUtility.serializeObjectToXMLString(editorPicksValues);

    exchange.setMethod("POST");
    exchange.setURL(urlString);
    Buffer requestContent = new ByteArrayBuffer(xmlStringEditorPicksValues);

    exchange.setRequestContent(requestContent);

    BookStoreUtility.SendAndRecv(this.client, exchange);
  }
  public List<Book> getBooks(Set<Integer> isbnSet) throws BookStoreException {
    if (isbnSet == null) {
      throw new BookStoreException(BookStoreConstants.NULL_INPUT);
    }
    List<Book> listBooks = new ArrayList<Book>();
    try {
      bsLock.readLock().lock();
      // Check that all ISBNs that we rate are there first.
      for (Integer ISBN : isbnSet) {
        if (BookStoreUtility.isInvalidISBN(ISBN))
          throw new BookStoreException(BookStoreConstants.ISBN + ISBN + BookStoreConstants.INVALID);
        if (!bookMap.containsKey(ISBN))
          throw new BookStoreException(
              BookStoreConstants.ISBN + ISBN + BookStoreConstants.NOT_AVAILABLE);
      }

      // Get the books
      for (Integer ISBN : isbnSet) {
        listBooks.add(bookMap.get(ISBN).immutableBook());
      }
    } finally {
      bsLock.readLock().unlock();
    }
    return listBooks;
  }
 public void updateEditorPicks(Set<BookEditorPick> editorPicks) throws BookStoreException {
   // Check that all ISBNs that we add/remove are there first.
   if (editorPicks == null) {
     throw new BookStoreException(BookStoreConstants.NULL_INPUT);
   }
   int ISBNVal;
   try {
     bsLock.updateLock().lock();
     for (BookEditorPick editorPickArg : editorPicks) {
       ISBNVal = editorPickArg.getISBN();
       if (BookStoreUtility.isInvalidISBN(ISBNVal))
         throw new BookStoreException(
             BookStoreConstants.ISBN + ISBNVal + BookStoreConstants.INVALID);
       if (!bookMap.containsKey(ISBNVal))
         throw new BookStoreException(
             BookStoreConstants.ISBN + ISBNVal + BookStoreConstants.NOT_AVAILABLE);
     }
     bsLock.writeLock().lock();
     for (BookEditorPick editorPickArg : editorPicks) {
       bookMap.get(editorPickArg.getISBN()).setEditorPick(editorPickArg.isEditorPick());
     }
   } finally {
     bsLock.writeLock().unlock();
     bsLock.updateLock().unlock();
   }
   return;
 }
Esempio n. 8
0
  @SuppressWarnings("unchecked")
  public List<StockBook> getBooks() throws BookStoreException {
    ContentExchange exchange = new ContentExchange();
    String urlString = serverAddress + "/" + BookStoreMessageTag.LISTBOOKS;

    exchange.setURL(urlString);

    return (List<StockBook>) BookStoreUtility.SendAndRecv(this.client, exchange);
  }
  @Override
  public void rateBooks(Set<BookRating> bookRating) throws BookStoreException {
    // TODO Auto-generated method stub
    // lock update lock, only allow read
    bsLock.updateLock().lock();
    try {
      /* Check if the input is valid */
      if (bookRating == null) throw new BookStoreException(BookStoreConstants.NULL_INPUT);

      int ISBN, rating;
      BookStoreBook book;
      for (BookRating br : bookRating) {
        ISBN = br.getISBN();
        rating = br.getRating();
        if (BookStoreUtility.isInvalidISBN(ISBN))
          throw new BookStoreException(BookStoreConstants.ISBN + ISBN + BookStoreConstants.INVALID);
        if (!bookMap.containsKey(ISBN))
          throw new BookStoreException(
              BookStoreConstants.ISBN + ISBN + BookStoreConstants.NOT_AVAILABLE);
        if (BookStoreUtility.isInvalidRating(rating))
          throw new BookStoreException(
              BookStoreConstants.RATING + rating + BookStoreConstants.INVALID);
      }

      try {
        bsLock.writeLock().lock(); // upgrade to write lock
        /* Inputs are valid so, ready to rate the books change */
        for (BookRating br : bookRating) {
          book = bookMap.get(br.getISBN());
          book.addRating(br.getRating());
        }
      } finally {
        bsLock.writeLock().unlock(); // downgrade to update lock
      }

    } finally {
      bsLock.updateLock().unlock();
    }

    return;
  }
Esempio n. 10
0
  public void removeAllBooks() throws BookStoreException {
    ContentExchange exchange = new ContentExchange();
    String urlString;
    urlString = serverAddress + "/" + BookStoreMessageTag.REMOVEALLBOOKS;

    String test = "test";
    exchange.setMethod("POST");
    exchange.setURL(urlString);
    Buffer requestContent = new ByteArrayBuffer(test);
    exchange.setRequestContent(requestContent);

    BookStoreUtility.SendAndRecv(this.client, exchange);
  }
  @SuppressWarnings("unchecked")
  public List<StockBook> getBooks() throws BookStoreException {

    BookStoreResult result = null;
    do {
      ContentExchange exchange = new ContentExchange();
      String urlString = getReplicaAddress() + "/" + BookStoreMessageTag.LISTBOOKS;

      exchange.setURL(urlString);
      result = BookStoreUtility.SendAndRecv(this.client, exchange);
    } while (result.getSnapshotId() < this.getSnapshotId());
    this.setSnapshotId(result.getSnapshotId());
    return (List<StockBook>) result.getResultList();
  }
  public void addBooks(Set<StockBook> bookSet) throws BookStoreException {

    if (bookSet == null) {
      throw new BookStoreException(BookStoreConstants.NULL_INPUT);
    }
    try {
      // Check if all are there
      bsLock.updateLock().lock();
      for (StockBook book : bookSet) {
        int ISBN = book.getISBN();
        String bookTitle = book.getTitle();
        String bookAuthor = book.getAuthor();
        int noCopies = book.getNumCopies();
        float bookPrice = book.getPrice();
        if (BookStoreUtility.isInvalidISBN(ISBN)
            || BookStoreUtility.isEmpty(bookTitle)
            || BookStoreUtility.isEmpty(bookAuthor)
            || BookStoreUtility.isInvalidNoCopies(noCopies)
            || bookPrice < 0.0) {
          throw new BookStoreException(
              BookStoreConstants.BOOK + book.toString() + BookStoreConstants.INVALID);
        } else if (bookMap.containsKey(ISBN)) {
          throw new BookStoreException(
              BookStoreConstants.ISBN + ISBN + BookStoreConstants.DUPLICATED);
        }
      }
      bsLock.writeLock().lock();
      for (StockBook book : bookSet) {
        int ISBN = book.getISBN();
        bookMap.put(ISBN, new BookStoreBook(book));
      }
    } finally {
      bsLock.writeLock().unlock();
      bsLock.updateLock().unlock();
    }
    return;
  }
  public void buyBooks(Set<BookCopy> bookCopiesToBuy) throws BookStoreException {
    if (bookCopiesToBuy == null) {
      throw new BookStoreException(BookStoreConstants.NULL_INPUT);
    }
    // Check that all ISBNs that we buy are there first.
    int ISBN;
    BookStoreBook book;
    Boolean saleMiss = false;
    try {
      bsLock.updateLock().lock();
      for (BookCopy bookCopyToBuy : bookCopiesToBuy) {
        ISBN = bookCopyToBuy.getISBN();
        if (BookStoreUtility.isInvalidISBN(ISBN))
          throw new BookStoreException(BookStoreConstants.ISBN + ISBN + BookStoreConstants.INVALID);
        if (!bookMap.containsKey(ISBN))
          throw new BookStoreException(
              BookStoreConstants.ISBN + ISBN + BookStoreConstants.NOT_AVAILABLE);
        book = bookMap.get(ISBN);
        if (!book.areCopiesInStore(bookCopyToBuy.getNumCopies())) {
          book.addSaleMiss(); // If we cannot sell the copies of the
          // book
          // its a miss
          saleMiss = true;
        }
      }

      // We throw exception now since we want to see how many books in the
      // order incurred misses which is used by books in demand
      if (saleMiss)
        throw new BookStoreException(BookStoreConstants.BOOK + BookStoreConstants.NOT_AVAILABLE);
      bsLock.writeLock().lock();
      // Then make purchase
      for (BookCopy bookCopyToBuy : bookCopiesToBuy) {
        book = bookMap.get(bookCopyToBuy.getISBN());
        book.buyCopies(bookCopyToBuy.getNumCopies());
      }
    } finally {
      bsLock.writeLock().unlock();
      bsLock.updateLock().unlock();
    }
    return;
  }