Search in sources :

Example 1 with Book

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);
    }
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Entity(com.google.appengine.api.datastore.Entity) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(com.google.appengine.api.datastore.Query) Book(com.example.getstarted.objects.Book) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Cursor(com.google.appengine.api.datastore.Cursor) Result(com.example.getstarted.objects.Result)

Example 2 with Book

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);
}
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)

Example 3 with Book

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);
}
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 4 with Book

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);
}
Also used : Book(com.example.getstarted.objects.Book) BookDao(com.example.getstarted.daos.BookDao)

Example 5 with Book

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);
    }
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Entity(com.google.appengine.api.datastore.Entity) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(com.google.appengine.api.datastore.Query) Book(com.example.getstarted.objects.Book) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Cursor(com.google.appengine.api.datastore.Cursor) Result(com.example.getstarted.objects.Result)

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