コード例 #1
0
 @POST
 @Produces(MediaType.APPLICATION_JSON)
 @Timed(name = "create-book")
 public Response createBook(BookLink bookRequest) {
   ArrayList<LinkDto> authorArray = new ArrayList();
   Book newBook =
       new Book(
           bookRequest.getBookTitle(),
           bookRequest.getBookAuthors(),
           bookRequest.getBookLanguage(),
           bookRequest.getNoOfPages(),
           bookRequest.getPublication_date(),
           bookRequest.getBookStatus(),
           bookRequest.getBookReviews()); // newbook created
   Long new_isbn = newBook.getNewIsbn();
   Map<Long, Author> authorMap = new HashMap<Long, Author>();
   book_map.put(new_isbn, newBook);
   for (Author author : bookRequest.getBookAuthors()) {
     Long author_id = author.createAuthorID();
     authorMap.put(author_id, author);
     authorArray.add(
         new LinkDto("view-author", "/books/" + new_isbn + "/authors/" + author_id, "GET"));
   }
   newBook.setBookAuthors(authorArray);
   AuthorHashMap.put(new_isbn, authorMap);
   LinksDto linkResponse = new LinksDto();
   linkResponse.addLink(new LinkDto("view-book", "/books/" + new_isbn, "GET"));
   linkResponse.addLink(new LinkDto("update-book", "/books/" + new_isbn, "PUT"));
   linkResponse.addLink(new LinkDto("delete-book", "/books/" + new_isbn, "PUT"));
   linkResponse.addLink(new LinkDto("create-review", "/books/" + new_isbn + "/reviews", "POST"));
   return Response.created(null).entity(linkResponse).build();
 }
コード例 #2
0
  @GET
  @Path("/{reviewId}")
  @Timed(name = "view-book-by-reviewId")
  public Response viewBookReview(
      @PathParam("isbn") LongParam isbn, @PathParam("reviewId") IntParam reviewId) {

    Book book = bookRepository.getBookByISBN(isbn.get());

    ReviewDto reviewResponse = null;
    List<Review> reviewList = book.getReview();

    List<Review> tempList = new ArrayList<Review>();
    for (Review reviewObj : reviewList) {

      if (reviewObj.getId() == reviewId.get()) tempList.add(reviewObj);
    }
    reviewResponse = new ReviewDto(tempList);
    String location = "/books/" + book.getIsbn() + "/reviews/";

    HashMap<String, Object> map = new HashMap<String, Object>();

    Review review = reviewResponse.getReviewList().get(0);
    map.put("review", review);
    reviewResponse.addLink(new LinkDto("view-review", location + reviewId.get(), "GET"));
    map.put("links", reviewResponse.getLinks());
    return Response.status(200).entity(map).build();
  }
コード例 #3
0
 // ****VIEWING A BOOK****
 @GET
 @Path("/{isbn}")
 @Timed(name = "view-book")
 public BookDto getBookByIsbn(@PathParam("isbn") LongParam isbn) {
   Book book = bookRepository.getBookByISBN(isbn.get());
   BookDto bookResponse = new BookDto(book);
   bookResponse.addLink(new LinkDto("view-book", "/books/" + book.getIsbn(), "GET"));
   bookResponse.addLink(new LinkDto("update-book", "/books/" + book.getIsbn(), "POST"));
   return bookResponse;
 }
コード例 #4
0
  // ****DISAPLAYING ALL REVIEWS****
  @GET
  @Path("/{isbn}/reviews")
  @Timed(name = "view-all-reviews")
  public ReviewsDto displayReviews(@PathParam("isbn") long isbn) {

    Book book = bookRepository.getBookByISBN(isbn);
    ReviewsDto response = new ReviewsDto(book.getReviews());

    return response;
  }
コード例 #5
0
  @GET
  @Timed(name = "view-book-review")
  public Response viewAllBookReview(@PathParam("isbn") LongParam isbn, Review request) {

    Book book = bookRepository.getBookByISBN(isbn.get());

    ReviewDto reviewResponse = new ReviewDto(book.getReview());

    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("reviews", reviewResponse.getReviewList());
    map.put("links", new ArrayList<String>());
    return Response.status(200).entity(map).build();
  }
コード例 #6
0
 // ****DISPLAYING A REVIEWS****
 @GET
 @Path("/{isbn}/reviews/{id}")
 @Timed(name = "view-review")
 public Response displayReview(@PathParam("isbn") long isbn, @PathParam("id") long id) {
   int i = 0;
   Book book = bookRepository.getBookByISBN(isbn);
   ReviewDto response = new ReviewDto(book.getoneReview(i));
   LinksDto links = new LinksDto();
   links.addLink(
       new LinkDto(
           "view-review",
           "/books/" + book.getIsbn() + "/reviews/" + book.getoneReview(i).getId(),
           "GET"));
   return Response.ok(links).build();
 }
コード例 #7
0
  // ****CREATING A BOOK****
  @POST
  @Timed(name = "create-book")
  public Response createBook(Book request) {
    // Store the new book in the BookRepository so that we can retrieve it.
    Book savedBook = bookRepository.saveBook(request);

    String location = "/books/" + savedBook.getIsbn();
    BookDto bookResponse = new BookDto(savedBook);

    ArrayList<LinkDto> links = new ArrayList<LinkDto>();
    links.add(new LinkDto("view-book", location, "GET"));
    links.add(new LinkDto("update-book", location, "PUT"));
    links.add(new LinkDto("add-book", location, "POST"));
    links.add(new LinkDto("delete-book", location, "DELETE"));
    return Response.status(201).entity(links).build();
  }
コード例 #8
0
  @PUT
  @Path("/{isbn}")
  @Timed(name = "update-book")
  public Response updateBookStatus(
      @PathParam("isbn") LongParam isbn, @QueryParam("status") String status) {

    Book book = (Book) book_map.get(isbn.get());
    book.setNewStatus(status);
    LinksDto response = new LinksDto();
    response.addLink(new LinkDto("view-book", "/books/" + isbn, "GET"));
    response.addLink(new LinkDto("update-book", "/books/" + isbn, "PUT"));
    response.addLink(new LinkDto("delete-book", "/books/" + isbn, "DELETE"));
    response.addLink(new LinkDto("create-review", "/books/" + isbn + "/reviews", "POST"));
    if (reviewsArray.size() > 0)
      response.addLink(new LinkDto("view-all-reviews", "/books/" + isbn + "/reviews", "GET"));
    return Response.ok(response).build();
  }
コード例 #9
0
  // ****UPDATING THE BOOK****
  @PUT
  @Path("/{isbn}")
  @Timed(name = "update-book")
  public Response updateBook(
      @PathParam("isbn") long isbn, @QueryParam("status") String status, Book request) {
    Book book = bookRepository.getBookByISBN(isbn);
    Book book1 = new Book();
    if (book.getIsbn() == isbn) book1 = bookRepository.updateBook(isbn, status);

    String location = "/books/" + book1.getIsbn();
    BookDto bookResponse = new BookDto(book1);

    ArrayList<LinkDto> links = new ArrayList<LinkDto>();
    links.add(new LinkDto("view-book", location, "GET"));
    links.add(new LinkDto("update-book", location, "PUT"));
    links.add(new LinkDto("add-book", location, "POST"));
    links.add(new LinkDto("delete-book", location, "DELETE"));

    return Response.status(201).entity(links).build();
  }
コード例 #10
0
  // ****CREATING REVIEWS****
  @POST
  @Path("/{isbn}/reviews")
  @Timed(name = "create-review")
  public Response createReview(@Valid Review reviews, @PathParam("isbn") long isbn) {

    Book retrieveBook = bookRepository.getBookByISBN(isbn);

    reviews.setId((int) reviewID);
    retrieveBook.getReviews().add(reviews);
    reviewID++;

    ReviewDto reviewResponse = new ReviewDto();
    reviewResponse.addLink(
        new LinkDto(
            "view-review",
            "/books/" + retrieveBook.getIsbn() + "/reviews/" + reviews.getId(),
            "GET"));

    return Response.status(201).entity(reviewResponse.getLinks()).build();
  }
コード例 #11
0
  @POST
  @Timed(name = "create-review")
  @Produces(MediaType.APPLICATION_JSON)
  @Consumes(MediaType.APPLICATION_JSON)
  public Response createReview(@PathParam("isbn") LongParam isbn, Review request) {

    // int rating = request.getRating();

    if (request.getComment() == null) {
      return Response.status(400).type("text/plain").entity(" Comment is a required field").build();
    }

    if (request.getRating() == 0) {
      return Response.status(400)
          .type("text/plain")
          .entity(" Rating is a required field !!!!")
          .build();
    }
    final List<Integer> ratingList = Arrays.asList(1, 2, 3, 4, 5);

    if (!(ratingList.contains(request.getRating()))) {
      return Response.status(400)
          .type("text/plain")
          .entity("Invalid value for Rating. Ratings can be between 1 - 5 stars")
          .build();
    }

    Review reviewObject = bookRepository.createReview(isbn.get(), request);

    Book book = bookRepository.getBookByISBN(isbn.get());

    String location = "/books/" + book.getIsbn() + "/reviews/" + reviewObject.getId();
    BookDto bookResponse = new BookDto(book);
    bookResponse.addLink(new LinkDto("view-review", location, "GET"));

    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("links", bookResponse.getLinks());
    return Response.status(201).entity(map).build();
  }
コード例 #12
0
  /** This will auto-generate unique ISBN for new books. */
  @Override
  public Book saveBook(Book newBook) {
    checkNotNull(newBook, "newBook instance must not be null");
    // Generate new ISBN
    Long isbn = generateISBNKey();
    newBook.setIsbn(isbn);
    // TODO: create and associate other fields such as author

    // Finally, save the new book into the map
    bookInMemoryMap.putIfAbsent(isbn, newBook);

    return newBook;
  }
コード例 #13
0
  private ConcurrentHashMap<Long, Book> seedData() {
    ConcurrentHashMap<Long, Book> bookMap = new ConcurrentHashMap<Long, Book>();
    Book book = new Book();
    book.setIsbn(1);
    book.setCategory("computer");
    book.setTitle("Java Concurrency in Practice");
    try {
      book.setCoverimage(new URL("http://goo.gl/N96GJN"));
    } catch (MalformedURLException e) {
      // eat the exception
    }
    bookMap.put(book.getIsbn(), book);

    book = new Book();
    book.setIsbn(2);
    book.setCategory("computer");
    book.setTitle("Restful Web Services");
    try {
      book.setCoverimage(new URL("http://goo.gl/ZGmzoJ"));
    } catch (MalformedURLException e) {
      // eat the exception
    }
    bookMap.put(book.getIsbn(), book);

    return bookMap;
  }
コード例 #14
0
 @Override
 public void addNewBook(Book newBook) {
   // TODO Auto-generated method stub
   bookInMemoryMap.putIfAbsent(newBook.getIsbn(), newBook);
 }
コード例 #15
0
  public Runnable listener() throws JMSException {
    long isbn;
    String bookTitle;
    String bookCategory;
    String webURL;
    Book tempBook = new Book();
    ArrayList<String> arrivals = new ArrayList<String>();
    StompJmsConnectionFactory factory = new StompJmsConnectionFactory();
    factory.setBrokerURI("tcp://" + apolloHost + ":" + apolloPort);
    System.currentTimeMillis();
    // System.out.println("Waiting for messages...");
    while (true) {
      Connection connection = factory.createConnection(apolloUser, apolloPassword);
      connection.start();
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Destination dest = new StompJmsDestination(stompTopic);
      MessageConsumer consumer = session.createConsumer(dest);
      while (true) {
        Message msg = consumer.receive(500);
        if (msg == null) break;
        if (msg instanceof TextMessage) {
          String body = ((TextMessage) msg).getText();
          arrivals.add(body);

        } else {
          System.out.println("Unexpected message type: " + msg.getClass());
        }
      }
      connection.close();
      if (!arrivals.isEmpty()) {
        for (String arrival : arrivals) {
          isbn = Long.parseLong(arrival.split(":")[0]);
          bookTitle = arrival.split(":")[1].replaceAll("^\"|\"$", "");
          bookCategory = arrival.split(":")[2].replaceAll("^\"|\"$", "");
          webURL = arrival.split(":\"")[3];
          webURL = webURL.substring(0, webURL.length() - 1);
          tempBook = bookRepository.getBookByISBN(isbn);
          // System.out.println("tempBook is "+tempBook);
          // System.out.println("dummyBook is "+dummyBook);

          if (tempBook.getIsbn() == 0) {
            // System.out.println("reachable");
            tempBook.setIsbn(isbn);
            tempBook.setCategory(bookCategory);
            tempBook.setTitle(bookTitle);
            try {
              tempBook.setCoverimage(new URL(webURL));
            } catch (MalformedURLException e) {
              e.printStackTrace();
            }
            bookRepository.addBook(tempBook);

          } else {
            // System.out.println("reachable, changing to available");
            tempBook.setStatus(Status.available);
          }
        }
        arrivals.clear();
      }
    }
  }