use of codeu.model.store.persistence.PersistentDataStoreException in project CodeU-Spring-2018 by jwang281.
the class PersistentDataStore method loadUsers.
/**
* Loads all User 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<User> loadUsers() throws PersistentDataStoreException {
List<User> users = new ArrayList<>();
// Retrieve all users from the datastore.
Query query = new Query("chat-users");
PreparedQuery results = datastore.prepare(query);
for (Entity entity : results.asIterable()) {
try {
UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
String userName = (String) entity.getProperty("username");
String password = (String) entity.getProperty("password");
Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
User user = new User(uuid, userName, password, creationTime);
users.add(user);
} catch (Exception e) {
// database entity definition mismatches, or service mismatches.
throw new PersistentDataStoreException(e);
}
}
return users;
}
Aggregations