use of com.google.cloud.firestore.QueryDocumentSnapshot in project getting-started-java by GoogleCloudPlatform.
the class UserJourneyTestIT method tearDownClass.
@AfterClass
public static void tearDownClass() throws ExecutionException, InterruptedException {
// Clear the firestore list if we're not using the local emulator
Firestore firestore = FirestoreOptions.getDefaultInstance().getService();
for (QueryDocumentSnapshot docSnapshot : firestore.collection("translations").get().get().getDocuments()) {
try {
docSnapshot.getReference().delete().get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
service.stop();
}
use of com.google.cloud.firestore.QueryDocumentSnapshot in project getting-started-java by GoogleCloudPlatform.
the class TranslateServlet method doGet.
// [START getting_started_background_app_list]
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Firestore firestore = (Firestore) this.getServletContext().getAttribute("firestore");
CollectionReference translations = firestore.collection("translations");
QuerySnapshot snapshot;
try {
snapshot = translations.limit(10).get().get();
} catch (InterruptedException | ExecutionException e) {
throw new ServletException("Exception retrieving documents from Firestore.", e);
}
List<TranslateMessage> translateMessages = Lists.newArrayList();
List<QueryDocumentSnapshot> documents = Lists.newArrayList(snapshot.getDocuments());
documents.sort(Comparator.comparing(DocumentSnapshot::getCreateTime));
for (DocumentSnapshot document : Lists.reverse(documents)) {
String encoded = gson.toJson(document.getData());
TranslateMessage message = gson.fromJson(encoded, TranslateMessage.class);
message.setData(decode(message.getData()));
translateMessages.add(message);
}
req.setAttribute("messages", translateMessages);
req.setAttribute("page", "list");
req.getRequestDispatcher("/base.jsp").forward(req, resp);
}
use of com.google.cloud.firestore.QueryDocumentSnapshot in project java-docs-samples by GoogleCloudPlatform.
the class RetrieveDataSnippetsIT method testRetrieveAllDocuments.
@Test
public void testRetrieveAllDocuments() throws Exception {
List<QueryDocumentSnapshot> docs = retrieveDataSnippets.getAllDocuments();
assertEquals(docs.size(), 5);
Set<String> docIds = new HashSet<>();
for (DocumentSnapshot doc : docs) {
docIds.add(doc.getId());
}
assertTrue(docIds.contains("SF") && docIds.contains("LA") && docIds.contains("DC") && docIds.contains("TOK") && docIds.contains("BJ"));
}
use of com.google.cloud.firestore.QueryDocumentSnapshot in project java-docs-samples by GoogleCloudPlatform.
the class Quickstart method retrieveAllDocuments.
void retrieveAllDocuments() throws Exception {
// [START fs_get_all]
// asynchronously retrieve all users
ApiFuture<QuerySnapshot> query = db.collection("users").get();
// ...
// query.get() blocks on response
QuerySnapshot querySnapshot = query.get();
List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
for (QueryDocumentSnapshot document : documents) {
System.out.println("User: " + document.getId());
System.out.println("First: " + document.getString("first"));
if (document.contains("middle")) {
System.out.println("Middle: " + document.getString("middle"));
}
System.out.println("Last: " + document.getString("last"));
System.out.println("Born: " + document.getLong("born"));
}
// [END fs_get_all]
}
use of com.google.cloud.firestore.QueryDocumentSnapshot in project java-docs-samples by GoogleCloudPlatform.
the class RetrieveDataSnippets method getQueryResults.
/**
* Return multiple documents from a collection based on a query.
*
* @return list of documents of capital cities.
*/
public List<QueryDocumentSnapshot> getQueryResults() throws Exception {
// [START fs_get_multiple_docs]
// asynchronously retrieve multiple documents
ApiFuture<QuerySnapshot> future = db.collection("cities").whereEqualTo("capital", true).get();
// future.get() blocks on response
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
for (DocumentSnapshot document : documents) {
System.out.println(document.getId() + " => " + document.toObject(City.class));
}
// [END fs_get_multiple_docs]
return documents;
}
Aggregations