@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();
  }
 // ****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;
 }
  // ****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;
  }
  @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();
  }
 // ****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();
 }
  // ****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();
  }
  // ****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();
  }
  @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();
  }
  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();
      }
    }
  }