use of com.google.appengine.api.datastore.Query in project iosched by google.
the class UpdateRunLogger method getLastRun.
public Entity getLastRun() {
Query query = new Query(UPDATERUN_ENTITY_KIND).addSort("date", Query.SortDirection.DESCENDING);
List<Entity> result = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(1));
if (result == null || result.isEmpty()) {
return null;
}
return result.get(0);
}
use of com.google.appengine.api.datastore.Query in project CodeU-Spring-2018 by jwang281.
the class PersistentDataStore method loadMessages.
/**
* Loads all Message objects from the Datastore service and returns them in a List.
*
* @throws PersistentDataStoreException if an error was detected during the load from the
* Datastore service
*/
public List<Message> loadMessages() throws PersistentDataStoreException {
List<Message> messages = new ArrayList<>();
// Retrieve all messages from the datastore.
Query query = new Query("chat-messages");
PreparedQuery results = datastore.prepare(query);
for (Entity entity : results.asIterable()) {
try {
UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
UUID conversationUuid = UUID.fromString((String) entity.getProperty("conv_uuid"));
UUID authorUuid = UUID.fromString((String) entity.getProperty("author_uuid"));
Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
String content = (String) entity.getProperty("content");
Message message = new Message(uuid, conversationUuid, authorUuid, content, creationTime);
messages.add(message);
} catch (Exception e) {
// database entity definition mismatches, or service mismatches.
throw new PersistentDataStoreException(e);
}
}
return messages;
}
use of com.google.appengine.api.datastore.Query in project CodeU-Spring-2018 by jwang281.
the class PersistentDataStore method loadConversations.
/**
* Loads all Conversation objects from the Datastore service and returns them in a List.
*
* @throws PersistentDataStoreException if an error was detected during the load from the
* Datastore service
*/
public List<Conversation> loadConversations() throws PersistentDataStoreException {
List<Conversation> conversations = new ArrayList<>();
// Retrieve all conversations from the datastore.
Query query = new Query("chat-conversations");
PreparedQuery results = datastore.prepare(query);
for (Entity entity : results.asIterable()) {
try {
UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
UUID ownerUuid = UUID.fromString((String) entity.getProperty("owner_uuid"));
String title = (String) entity.getProperty("title");
Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
Conversation conversation = new Conversation(uuid, ownerUuid, title, creationTime);
conversations.add(conversation);
} catch (Exception e) {
// database entity definition mismatches, or service mismatches.
throw new PersistentDataStoreException(e);
}
}
return conversations;
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class SignGuestbookServletTest method doPost_userNotLoggedIn.
@Test
public void doPost_userNotLoggedIn() throws Exception {
servletUnderTest.doPost(mockRequest, mockResponse);
Query query = new Query("Greeting").setAncestor(new KeyFactory.Builder("Guestbook", "default").getKey());
PreparedQuery pq = ds.prepare(query);
// Should only be one at this point.
Entity greeting = pq.asSingleEntity();
assertEquals(greeting.getProperty("content"), testPhrase);
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class GuestbookTestUtilities method cleanDatastore.
public static void cleanDatastore(DatastoreService ds, String book) {
Query query = new Query("Greeting").setAncestor(new KeyFactory.Builder("Guestbook", book).getKey()).setKeysOnly();
PreparedQuery pq = ds.prepare(query);
List<Entity> entities = pq.asList(FetchOptions.Builder.withDefaults());
ArrayList<Key> keys = new ArrayList<>(entities.size());
for (Entity e : entities) {
keys.add(e.getKey());
}
ds.delete(keys);
}
Aggregations