Skip to content

Instantly share code, notes, and snippets.

@thorwebdev
Created December 7, 2014 16:03
Show Gist options
  • Save thorwebdev/1b7b0ec5a987bdc697fe to your computer and use it in GitHub Desktop.
Save thorwebdev/1b7b0ec5a987bdc697fe to your computer and use it in GitHub Desktop.
When we query our fences we only return the ids of the fences that were returned, therefore we need an endpoint to retrieve the metadata that corresponds to a fence id. This endpoint simply accepts an id, queries the Datastore and returns the metadata of the fence with the corresponding id.
@ApiMethod(name = "getById", httpMethod = "get", path = "getById")
public ArrayList < MyFence > getFenceById(@Named("group") String group, @Named("id") long id) {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key fenceKey = KeyFactory.createKey("Geofence", group);
Filter propertyFilter = new FilterPredicate("id", FilterOperator.EQUAL, id);
Query query = new Query("Fence", fenceKey).setFilter(propertyFilter);
Entity fenceFromStore = datastore.prepare(query).asSingleEntity();
ArrayList < MyFence > fences = new ArrayList < MyFence > ();
if (fenceFromStore != null) {
String name = (String) fenceFromStore.getProperty("name");
String description = (String) fenceFromStore.getProperty("description");
Gson gson = new Gson();
Text vText = (Text) fenceFromStore.getProperty("vertices");
String vString = vText.getValue();
double[][] vertices = gson.fromJson(vString, double[][].class);
MyFence tempFence = new MyFence(id, name, group, description, vertices);
fences.add(tempFence);
}
return fences;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment