use of org.activityinfo.model.type.geo.GeoAreaType in project activityinfo by bedatadriven.
the class FormResource method updateGeometry.
@POST
@Path("record/{recordId}/field/{fieldId}/geometry")
public Response updateGeometry(@PathParam("recordId") String recordId, @PathParam("fieldId") String fieldId, byte[] binaryBody) {
// Parse the Geometry
WKBReader reader = new WKBReader(new GeometryFactory());
Geometry geometry;
try {
geometry = reader.read(binaryBody);
} catch (ParseException e) {
return Response.status(Response.Status.BAD_REQUEST).entity("Could not parse WKB geometry: " + e.getMessage()).build();
}
geometry.normalize();
if (!geometry.isValid()) {
return Response.status(Response.Status.BAD_REQUEST).entity(geometry.getGeometryType() + " is not valid").build();
}
// Check first to see if this form exists
Optional<FormStorage> storage = backend.getStorage().getForm(formId);
if (!storage.isPresent()) {
return Response.status(Response.Status.NOT_FOUND).entity("Form " + formId + " does not exist.").build();
}
// Find the field and verify that it's a GeoArea type
FormField field;
try {
field = storage.get().getFormClass().getField(ResourceId.valueOf(fieldId));
} catch (IllegalArgumentException e) {
return Response.status(Response.Status.NOT_FOUND).entity("Record " + recordId + " does not exist.").build();
}
if (!(field.getType() instanceof GeoAreaType)) {
return Response.status(Response.Status.BAD_REQUEST).entity("Field " + fieldId + " is not a GeoArea type").build();
}
try {
storage.get().updateGeometry(ResourceId.valueOf(recordId), ResourceId.valueOf(fieldId), geometry);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to update geometry for record " + recordId, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
return Response.ok().build();
}
Aggregations