Search in sources :

Example 11 with Book

use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.

the class FirestoreDao method listBooks.

// [END bookshelf_firestore_documents_to_books]
// [START bookshelf_firestore_list_books]
@Override
public Result<Book> listBooks(String startTitle) {
    Query booksQuery = booksCollection.orderBy("title").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);
}
Also used : Query(com.google.cloud.firestore.Query) Book(com.example.getstarted.objects.Book) ExecutionException(java.util.concurrent.ExecutionException) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) Result(com.example.getstarted.objects.Result)

Example 12 with Book

use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.

the class ListByUserServlet 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.listBooksByUser((String) req.getSession().getAttribute("userId"), startCursor);
        books = result.result;
        endCursor = result.cursor;
    } 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() + " ");
    }
    logger.log(Level.INFO, "Loaded books: " + bookNames.toString() + " for user " + (String) req.getSession().getAttribute("userId"));
    req.getSession().setAttribute("cursor", endCursor);
    req.getSession().setAttribute("page", "list");
    req.getRequestDispatcher("/base.jsp").forward(req, resp);
}
Also used : ServletException(javax.servlet.ServletException) Book(com.example.getstarted.objects.Book) BookDao(com.example.getstarted.daos.BookDao) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

Book (com.example.getstarted.objects.Book)12 BookDao (com.example.getstarted.daos.BookDao)6 Result (com.example.getstarted.objects.Result)6 IOException (java.io.IOException)5 ServletException (javax.servlet.ServletException)4 FileUploadException (org.apache.commons.fileupload.FileUploadException)3 CloudStorageHelper (com.example.getstarted.util.CloudStorageHelper)2 Cursor (com.google.appengine.api.datastore.Cursor)2 Entity (com.google.appengine.api.datastore.Entity)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)2 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)2 Query (com.google.appengine.api.datastore.Query)2 Query (com.google.cloud.firestore.Query)2 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2