use of com.example.getstarted.objects.Result 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.Result 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.Result 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);
}
}
use of com.example.getstarted.objects.Result in project getting-started-java by GoogleCloudPlatform.
the class CloudSqlDao method listBooksByUser.
// [END listbooks]
// [START listbyuser]
@Override
public Result<Book> listBooksByUser(String userId, String startCursor) throws SQLException {
int offset = 0;
if (startCursor != null && !startCursor.equals("")) {
offset = Integer.parseInt(startCursor);
}
final String listBooksString = "SELECT SQL_CALC_FOUND_ROWS author, createdBy, createdById, " + "description, id, publishedDate, title, imageUrl FROM books WHERE createdById = ? " + "ORDER BY title ASC LIMIT 10 OFFSET ?";
try (Connection conn = DriverManager.getConnection(sqlUrl);
PreparedStatement listBooksStmt = conn.prepareStatement(listBooksString)) {
listBooksStmt.setString(1, userId);
listBooksStmt.setInt(2, offset);
List<Book> resultBooks = new ArrayList<>();
try (ResultSet rs = listBooksStmt.executeQuery()) {
while (rs.next()) {
Book book = new Book.Builder().author(rs.getString(Book.AUTHOR)).createdBy(rs.getString(Book.CREATED_BY)).createdById(rs.getString(Book.CREATED_BY_ID)).description(rs.getString(Book.DESCRIPTION)).id(rs.getLong(Book.ID)).publishedDate(rs.getString(Book.PUBLISHED_DATE)).title(rs.getString(Book.TITLE)).imageUrl(rs.getString(Book.IMAGE_URL)).build();
resultBooks.add(book);
}
}
try (ResultSet rs = conn.createStatement().executeQuery("SELECT FOUND_ROWS()")) {
int totalNumRows = 0;
if (rs.next()) {
totalNumRows = rs.getInt(1);
}
if (totalNumRows > offset + 10) {
return new Result<>(resultBooks, Integer.toString(offset + 10));
} else {
return new Result<>(resultBooks);
}
}
}
}
use of com.example.getstarted.objects.Result in project getting-started-java by GoogleCloudPlatform.
the class CloudSqlDao method listBooks.
// [END delete]
// [START listbooks]
@Override
public Result<Book> listBooks(String cursor) throws SQLException {
int offset = 0;
if (cursor != null && !cursor.equals("")) {
offset = Integer.parseInt(cursor);
}
final String listBooksString = "SELECT SQL_CALC_FOUND_ROWS author, createdBy, createdById, " + "description, id, publishedDate, title, imageUrl FROM books5 ORDER BY title ASC " + "LIMIT 10 OFFSET ?";
try (Connection conn = DriverManager.getConnection(sqlUrl);
PreparedStatement listBooksStmt = conn.prepareStatement(listBooksString)) {
listBooksStmt.setInt(1, offset);
List<Book> resultBooks = new ArrayList<>();
try (ResultSet rs = listBooksStmt.executeQuery()) {
while (rs.next()) {
Book book = new Book.Builder().author(rs.getString(Book.AUTHOR)).createdBy(rs.getString(Book.CREATED_BY)).createdById(rs.getString(Book.CREATED_BY_ID)).description(rs.getString(Book.DESCRIPTION)).id(rs.getLong(Book.ID)).publishedDate(rs.getString(Book.PUBLISHED_DATE)).title(rs.getString(Book.TITLE)).imageUrl(rs.getString(Book.IMAGE_URL)).build();
resultBooks.add(book);
}
}
try (ResultSet rs = conn.createStatement().executeQuery("SELECT FOUND_ROWS()")) {
int totalNumRows = 0;
if (rs.next()) {
totalNumRows = rs.getInt(1);
}
if (totalNumRows > offset + 10) {
return new Result<>(resultBooks, Integer.toString(offset + 10));
} else {
return new Result<>(resultBooks);
}
}
}
}
Aggregations