Search in sources :

Example 46 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-docs-samples by GoogleCloudPlatform.

the class QuickstartIT method validate.

private void validate(DocumentReference docRef, Map<String, Object> data) throws Exception {
    DocumentSnapshot documentSnapshot = docRef.get().get();
    assertTrue(Objects.equals(documentSnapshot.getData(), data));
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot)

Example 47 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-docs-samples by GoogleCloudPlatform.

the class QueryDataSnippetsIT method getResults.

private List<String> getResults(Query query) throws Exception {
    // asynchronously retrieve query results
    ApiFuture<QuerySnapshot> future = query.get();
    // block on response
    QuerySnapshot querySnapshot = future.get();
    List<String> docIds = new ArrayList<>();
    for (DocumentSnapshot document : querySnapshot.getDocuments()) {
        docIds.add(document.getId());
    }
    return docIds;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) ArrayList(java.util.ArrayList) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 48 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-docs-samples by GoogleCloudPlatform.

the class RetrieveDataSnippetsIT method testRetrieveQueryResults.

@Test
public void testRetrieveQueryResults() throws Exception {
    List<QueryDocumentSnapshot> docs = retrieveDataSnippets.getQueryResults();
    assertEquals(docs.size(), 3);
    Set<String> docIds = new HashSet<>();
    for (DocumentSnapshot doc : docs) {
        docIds.add(doc.getId());
    }
    assertTrue(docIds.contains("BJ") && docIds.contains("TOK") && docIds.contains("DC"));
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) HashSet(java.util.HashSet) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 49 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-docs-samples by GoogleCloudPlatform.

the class RetrieveDataSnippetsIT method deleteAllDocuments.

private static void deleteAllDocuments() throws Exception {
    ApiFuture<QuerySnapshot> future = db.collection("cities").get();
    QuerySnapshot querySnapshot = future.get();
    for (DocumentSnapshot doc : querySnapshot.getDocuments()) {
        // block on delete operation
        db.collection("cities").document(doc.getId()).delete().get();
    }
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 50 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot 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);
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) ServletException(javax.servlet.ServletException) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Firestore(com.google.cloud.firestore.Firestore) TranslateMessage(com.getstarted.background.objects.TranslateMessage) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)103 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)73 Test (org.junit.Test)52 DocumentReference (com.google.cloud.firestore.DocumentReference)49 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)23 ExecutionException (java.util.concurrent.ExecutionException)13 CollectionReference (com.google.cloud.firestore.CollectionReference)12 Query (com.google.cloud.firestore.Query)12 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)10 SingleField (com.google.cloud.firestore.LocalFirestoreHelper.SingleField)9 WriteResult (com.google.cloud.firestore.WriteResult)9 ArrayList (java.util.ArrayList)8 FileSystemExecutionException (bio.terra.service.filedata.exception.FileSystemExecutionException)6 HashMap (java.util.HashMap)6 BulkWriter (com.google.cloud.firestore.BulkWriter)5 City (com.example.firestore.snippets.model.City)4 Timestamp (com.google.cloud.Timestamp)4 FirestoreBundleTest.verifyNamedQuery (com.google.cloud.firestore.FirestoreBundleTest.verifyNamedQuery)4 AllSupportedTypes (com.google.cloud.firestore.LocalFirestoreHelper.AllSupportedTypes)4 WriteBatch (com.google.cloud.firestore.WriteBatch)4