use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.
the class DatastoreDao method listBooks.
// [END entitiesToBooks]
// [START listbooks]
@Override
public Result<Book> listBooks(String startCursorString) {
// Only show 10 at a time
FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10);
if (startCursorString != null && !startCursorString.equals("")) {
// Where we left off
fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString));
}
Query query = // We only care about Books
new Query(BOOK_KIND).addSort(Book.TITLE, // Use default Index "title"
SortDirection.ASCENDING);
PreparedQuery preparedQuery = datastore.prepare(query);
QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);
// Retrieve and convert Entities
List<Book> resultBooks = entitiesToBooks(results);
// Where to start next time
Cursor cursor = results.getCursor();
if (cursor != null && resultBooks.size() == 10) {
// Are we paging? Save Cursor
// Cursors are WebSafe
String cursorString = cursor.toWebSafeString();
return new Result<>(resultBooks, cursorString);
} else {
return new Result<>(resultBooks);
}
}
use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.
the class ListBookServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
BookDao dao = (BookDao) this.getServletContext().getAttribute("dao");
String startCursor = req.getParameter("cursor");
List<Book> books = null;
String endCursor = null;
try {
Result<Book> result = dao.listBooks(startCursor);
logger.log(Level.INFO, "Retrieved list of all books");
books = result.getResult();
endCursor = result.getCursor();
} catch (Exception e) {
throw new ServletException("Error listing books", e);
}
req.getSession().getServletContext().setAttribute("books", books);
StringBuilder bookNames = new StringBuilder();
for (Book book : books) {
bookNames.append(book.getTitle()).append(" ");
}
logger.log(Level.INFO, "Loaded books: " + bookNames.toString());
req.setAttribute("cursor", endCursor);
req.setAttribute("page", "list");
req.getRequestDispatcher("/base.jsp").forward(req, resp);
}
use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.
the class FirestoreDao method listBooksByUser.
// [END bookshelf_firestore_list_books]
// [START bookshelf_firestore_list_by_user]
@Override
public Result<Book> listBooksByUser(String userId, String startTitle) {
Query booksQuery = booksCollection.orderBy("title").whereEqualTo(Book.CREATED_BY_ID, userId).limit(10);
if (startTitle != null) {
booksQuery = booksQuery.startAfter(startTitle);
}
try {
QuerySnapshot snapshot = booksQuery.get().get();
List<Book> results = documentsToBooks(snapshot.getDocuments());
String newCursor = null;
if (results.size() > 0) {
newCursor = results.get(results.size() - 1).getTitle();
}
return new Result<>(results, newCursor);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return new Result<>(Lists.newArrayList(), null);
}
use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.
the class ReadBookServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
BookDao dao = (BookDao) this.getServletContext().getAttribute("dao");
Book book = dao.readBook(id);
logger.log(Level.INFO, "Read book with id {0}", id);
req.setAttribute("book", book);
req.setAttribute("page", "view");
req.getRequestDispatcher("/base.jsp").forward(req, resp);
}
use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.
the class DatastoreDao method listBooksByUser.
// [END listbooks]
// [START listbyuser]
@Override
public Result<Book> listBooksByUser(String userId, String startCursorString) {
// Only show 10 at a time
FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10);
if (startCursorString != null && !startCursorString.equals("")) {
// Where we left off
fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString));
}
Query query = // We only care about Books
new Query(BOOK_KIND).setFilter(new Query.FilterPredicate(Book.CREATED_BY_ID, Query.FilterOperator.EQUAL, userId)).addSort(Book.TITLE, SortDirection.ASCENDING);
PreparedQuery preparedQuery = datastore.prepare(query);
QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);
// Retrieve and convert Entities
List<Book> resultBooks = entitiesToBooks(results);
// Where to start next time
Cursor cursor = results.getCursor();
if (cursor != null && resultBooks.size() == 10) {
// Are we paging? Save Cursor
// Cursors are WebSafe
String cursorString = cursor.toWebSafeString();
return new Result<>(resultBooks, cursorString);
} else {
return new Result<>(resultBooks);
}
}
Aggregations