Search in sources :

Example 1 with CompositeFilter

use of com.google.appengine.api.datastore.Query.CompositeFilter in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method queryInterface_multipleFilters_printsMatchedEntities.

@Test
public void queryInterface_multipleFilters_printsMatchedEntities() throws Exception {
    // Arrange
    Entity a = new Entity("Person", "a");
    a.setProperty("firstName", "Alph");
    a.setProperty("lastName", "Alpha");
    a.setProperty("height", 60);
    Entity b = new Entity("Person", "b");
    b.setProperty("firstName", "Bee");
    b.setProperty("lastName", "Bravo");
    b.setProperty("height", 70);
    Entity c = new Entity("Person", "c");
    c.setProperty("firstName", "Charles");
    c.setProperty("lastName", "Charlie");
    c.setProperty("height", 100);
    datastore.put(ImmutableList.<Entity>of(a, b, c));
    StringWriter buf = new StringWriter();
    PrintWriter out = new PrintWriter(buf);
    long minHeight = 60;
    long maxHeight = 72;
    // Act
    // [START interface_1]
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Filter heightMinFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);
    Filter heightMaxFilter = new FilterPredicate("height", FilterOperator.LESS_THAN_OR_EQUAL, maxHeight);
    // Use CompositeFilter to combine multiple filters
    CompositeFilter heightRangeFilter = CompositeFilterOperator.and(heightMinFilter, heightMaxFilter);
    // Use class Query to assemble a query
    Query q = new Query("Person").setFilter(heightRangeFilter);
    // Use PreparedQuery interface to retrieve results
    PreparedQuery pq = datastore.prepare(q);
    for (Entity result : pq.asIterable()) {
        String firstName = (String) result.getProperty("firstName");
        String lastName = (String) result.getProperty("lastName");
        Long height = (Long) result.getProperty("height");
        out.println(firstName + " " + lastName + ", " + height + " inches tall");
    }
    // [END interface_1]
    // Assert
    assertThat(buf.toString()).contains("Alph Alpha, 60 inches tall");
    assertThat(buf.toString()).contains("Bee Bravo, 70 inches tall");
    assertThat(buf.toString()).doesNotContain("Charlie");
}
Also used : Entity(com.google.appengine.api.datastore.Entity) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) StringWriter(java.io.StringWriter) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) DatastoreService(com.google.appengine.api.datastore.DatastoreService) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

DatastoreService (com.google.appengine.api.datastore.DatastoreService)1 Entity (com.google.appengine.api.datastore.Entity)1 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)1 Query (com.google.appengine.api.datastore.Query)1 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)1 Filter (com.google.appengine.api.datastore.Query.Filter)1 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Test (org.junit.Test)1