Search in sources :

Example 1 with GeoAreaType

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();
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) VersionedFormStorage(org.activityinfo.store.spi.VersionedFormStorage) FormStorage(org.activityinfo.store.spi.FormStorage) GeoAreaType(org.activityinfo.model.type.geo.GeoAreaType) ParseException(com.vividsolutions.jts.io.ParseException) WKBReader(com.vividsolutions.jts.io.WKBReader) NotFoundException(com.sun.jersey.api.NotFoundException) SQLException(java.sql.SQLException) InvalidUpdateException(org.activityinfo.store.query.server.InvalidUpdateException) ParseException(com.vividsolutions.jts.io.ParseException)

Aggregations

NotFoundException (com.sun.jersey.api.NotFoundException)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 ParseException (com.vividsolutions.jts.io.ParseException)1 WKBReader (com.vividsolutions.jts.io.WKBReader)1 SQLException (java.sql.SQLException)1 GeoAreaType (org.activityinfo.model.type.geo.GeoAreaType)1 InvalidUpdateException (org.activityinfo.store.query.server.InvalidUpdateException)1 FormStorage (org.activityinfo.store.spi.FormStorage)1 VersionedFormStorage (org.activityinfo.store.spi.VersionedFormStorage)1