Search in sources :

Example 1 with LocationsDao

use of cwms.radar.data.dao.LocationsDao in project cwms-radar-api by USACE.

the class LocationController method getOne.

@OpenApi(queryParams = { @OpenApiParam(name = "office", required = true, description = "Specifies the owning office of the location level(s) whose data is to be included in the response. If this field is not specified, matching location level information from all offices shall be returned."), @OpenApiParam(name = "unit", description = "Specifies the unit or unit system of the response. Valid values for the unit field are:\r\n 1. EN.   Specifies English unit system.  Location values will be in the default English units for their parameters.\r\n2. SI.   Specifies the SI unit system.  Location values will be in the default SI units for their parameters.\r\n3. Other. Any unit returned in the response to the units URI request that is appropriate for the requested parameters.") }, responses = { @OpenApiResponse(status = "200", content = { @OpenApiContent(type = Formats.JSONV2, from = Location.class), @OpenApiContent(type = Formats.XMLV2, from = Location.class) }) }, description = "Returns CWMS Location Data", tags = { "Locations" })
@Override
public void getOne(Context ctx, @NotNull String name) {
    getOneRequest.mark();
    try (final Timer.Context timeContext = getOneRequestTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        String units = ctx.queryParamAsClass("unit", String.class).getOrDefault(UnitSystem.EN.value());
        String office = ctx.queryParam("office");
        String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV2;
        ContentType contentType = Formats.parseHeader(formatHeader);
        ctx.contentType(contentType.toString());
        LocationsDao locationDao = getLocationsDao(dsl);
        Location location = locationDao.getLocation(name, units, office);
        ObjectMapper om = getObjectMapperForFormat(contentType.getType());
        String serializedLocation = om.writeValueAsString(location);
        ctx.result(serializedLocation);
    } catch (IOException ex) {
        String errorMsg = "Error retrieving " + name;
        RadarError re = new RadarError(errorMsg);
        ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
        logger.log(Level.SEVERE, errorMsg, ex);
    }
}
Also used : RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) DSLContext(org.jooq.DSLContext) LocationsDao(cwms.radar.data.dao.LocationsDao) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Location(cwms.radar.data.dto.Location)

Example 2 with LocationsDao

use of cwms.radar.data.dao.LocationsDao in project cwms-radar-api by USACE.

the class LocationController method create.

@OpenApi(queryParams = { @OpenApiParam(name = "office", required = true, description = "Specifies the office in which Location will be created") }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = Location.class, type = Formats.JSON), @OpenApiContent(from = Location.class, type = Formats.XML) }, required = true), description = "Create new CWMS Location", method = HttpMethod.POST, path = "/locations", tags = { "Locations" })
@Override
public void create(Context ctx) {
    createRequest.mark();
    try (final Timer.Context timeContext = createRequestTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        LocationsDao locationsDao = getLocationsDao(dsl);
        String office = ctx.queryParam("office");
        String acceptHeader = ctx.req.getContentType();
        String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSON;
        ContentType contentType = Formats.parseHeader(formatHeader);
        if (contentType == null) {
            throw new FormattingException("Format header could not be parsed");
        }
        Location locationFromBody = deserializeLocation(ctx.body(), contentType.getType(), office);
        locationsDao.storeLocation(locationFromBody);
        ctx.status(HttpServletResponse.SC_ACCEPTED).json("Created Location");
    } catch (IOException ex) {
        RadarError re = new RadarError("failed to process request");
        logger.log(Level.SEVERE, re.toString(), ex);
        ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
    }
}
Also used : RadarError(cwms.radar.api.errors.RadarError) FormattingException(cwms.radar.formatters.FormattingException) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) DSLContext(org.jooq.DSLContext) LocationsDao(cwms.radar.data.dao.LocationsDao) IOException(java.io.IOException) Location(cwms.radar.data.dto.Location)

Example 3 with LocationsDao

use of cwms.radar.data.dao.LocationsDao in project cwms-radar-api by USACE.

the class LocationController method getAll.

@OpenApi(queryParams = { @OpenApiParam(name = "names", description = "Specifies the name(s) of the location(s) whose data is to be included in the response"), @OpenApiParam(name = "office", description = "Specifies the owning office of the location level(s) whose data is to be included in the response. If this field is not specified, matching location level information from all offices shall be returned."), @OpenApiParam(name = "unit", description = "Specifies the unit or unit system of the response. Valid values for the unit field are:\r\n 1. EN.   Specifies English unit system.  Location level values will be in the default English units for their parameters.\r\n2. SI.   Specifies the SI unit system.  Location level values will be in the default SI units for their parameters.\r\n3. Other. Any unit returned in the response to the units URI request that is appropriate for the requested parameters."), @OpenApiParam(name = "datum", description = "Specifies the elevation datum of the response. This field affects only elevation location levels. Valid values for this field are:\r\n1. NAVD88.  The elevation values will in the specified or default units above the NAVD-88 datum.\r\n2. NGVD29.  The elevation values will be in the specified or default units above the NGVD-29 datum."), @OpenApiParam(name = "format", description = "Specifies the encoding format of the response. Valid values for the format field for this URI are:\r\n1.    tab\r\n2.    csv\r\n3.    xml\r\n4.  wml2 (only if name field is specified)\r\n5.    json (default)\n" + "6.    geojson") }, responses = { @OpenApiResponse(status = "200", content = { @OpenApiContent(type = Formats.JSON), @OpenApiContent(type = Formats.TAB), @OpenApiContent(type = Formats.CSV), @OpenApiContent(type = Formats.XML), @OpenApiContent(type = Formats.WML2), @OpenApiContent(type = Formats.GEOJSON) }) }, description = "Returns CWMS Location Data", tags = { "Locations" })
@Override
public void getAll(Context ctx) {
    getAllRequests.mark();
    try (final Timer.Context timeContext = getAllRequestsTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        LocationsDao locationsDao = getLocationsDao(dsl);
        String names = ctx.queryParam("names");
        String units = ctx.queryParam("unit");
        String datum = ctx.queryParam("datum");
        String office = ctx.queryParam("office");
        String formatParm = ctx.queryParamAsClass("format", String.class).getOrDefault("");
        String formatHeader = ctx.header(Header.ACCEPT);
        ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, formatParm);
        ctx.contentType(contentType.toString());
        final String results;
        if (contentType.getType().equals(Formats.GEOJSON)) {
            FeatureCollection collection = locationsDao.buildFeatureCollection(names, units, office);
            ctx.json(collection);
            requestResultSize.update(ctx.res.getBufferSize());
        } else {
            String format = getFormatFromContent(contentType);
            results = locationsDao.getLocations(names, format, units, datum, office);
            ctx.result(results);
            requestResultSize.update(results.length());
        }
        ctx.status(HttpServletResponse.SC_OK);
    } catch (Exception ex) {
        RadarError re = new RadarError("failed to process request");
        logger.log(Level.SEVERE, re.toString(), ex);
        ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
    }
}
Also used : RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) FeatureCollection(org.geojson.FeatureCollection) DSLContext(org.jooq.DSLContext) LocationsDao(cwms.radar.data.dao.LocationsDao) IOException(java.io.IOException) FormattingException(cwms.radar.formatters.FormattingException)

Example 4 with LocationsDao

use of cwms.radar.data.dao.LocationsDao in project cwms-radar-api by USACE.

the class LocationController method update.

@OpenApi(queryParams = { @OpenApiParam(name = "office", required = true, description = "Specifies the office in which Location will be updated") }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = Location.class, type = Formats.JSON), @OpenApiContent(from = Location.class, type = Formats.XML) }, required = true), description = "Update CWMS Location", method = HttpMethod.PATCH, path = "/locations", tags = { "Locations" })
@Override
public void update(Context ctx, @NotNull String locationId) {
    updateRequest.mark();
    try (final Timer.Context timeContext = updateRequestTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        LocationsDao locationsDao = getLocationsDao(dsl);
        String office = ctx.queryParam("office");
        String acceptHeader = ctx.req.getContentType();
        String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSON;
        ContentType contentType = Formats.parseHeader(formatHeader);
        if (contentType == null) {
            throw new FormattingException("Format header could not be parsed");
        }
        Location locationFromBody = deserializeLocation(ctx.body(), contentType.getType(), office);
        // getLocation will throw an error if location does not exist
        Location existingLocation = locationsDao.getLocation(locationId, UnitSystem.EN.getValue(), office);
        existingLocation = updatedClearedFields(ctx.body(), contentType.getType(), existingLocation);
        // only store (update) if location does exist
        Location updatedLocation = getUpdatedLocation(existingLocation, locationFromBody);
        if (// if name changed then delete location with old name
        !updatedLocation.getName().equalsIgnoreCase(existingLocation.getName())) {
            locationsDao.renameLocation(locationId, updatedLocation);
            ctx.status(HttpServletResponse.SC_ACCEPTED).json("Updated and renamed Location");
        } else {
            locationsDao.storeLocation(updatedLocation);
            ctx.status(HttpServletResponse.SC_ACCEPTED).json("Updated Location");
        }
    } catch (IOException ex) {
        RadarError re = new RadarError("Failed to process request: " + ex.getLocalizedMessage());
        logger.log(Level.SEVERE, re.toString(), ex);
        ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
    }
}
Also used : RadarError(cwms.radar.api.errors.RadarError) FormattingException(cwms.radar.formatters.FormattingException) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) DSLContext(org.jooq.DSLContext) LocationsDao(cwms.radar.data.dao.LocationsDao) IOException(java.io.IOException) Location(cwms.radar.data.dto.Location)

Example 5 with LocationsDao

use of cwms.radar.data.dao.LocationsDao in project cwms-radar-api by USACE.

the class LocationController method delete.

@OpenApi(queryParams = { @OpenApiParam(name = "office", description = "Specifies the owning office of the location whose data is to be deleted. If this field is not specified, matching location information will be deleted from all offices.") }, description = "Delete CWMS Location", method = HttpMethod.DELETE, path = "/locations", tags = { "Locations" })
@Override
public void delete(Context ctx, @NotNull String locationId) {
    deleteRequest.mark();
    try (final Timer.Context timeContext = deleteRequestTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        String office = ctx.queryParam("office");
        LocationsDao locationsDao = getLocationsDao(dsl);
        locationsDao.deleteLocation(locationId, office);
        ctx.status(HttpServletResponse.SC_ACCEPTED).json(locationId + " Deleted");
    } catch (IOException ex) {
        RadarError re = new RadarError("Failed to delete location");
        logger.log(Level.SEVERE, re.toString(), ex);
        ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
    }
}
Also used : RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) DSLContext(org.jooq.DSLContext) LocationsDao(cwms.radar.data.dao.LocationsDao) IOException(java.io.IOException)

Aggregations

Timer (com.codahale.metrics.Timer)6 RadarError (cwms.radar.api.errors.RadarError)6 LocationsDao (cwms.radar.data.dao.LocationsDao)6 DSLContext (org.jooq.DSLContext)6 ContentType (cwms.radar.formatters.ContentType)5 IOException (java.io.IOException)5 Location (cwms.radar.data.dto.Location)3 FormattingException (cwms.radar.formatters.FormattingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 LocationsDaoImpl (cwms.radar.data.dao.LocationsDaoImpl)1 TimeSeriesDao (cwms.radar.data.dao.TimeSeriesDao)1 TimeSeriesDaoImpl (cwms.radar.data.dao.TimeSeriesDaoImpl)1 Catalog (cwms.radar.data.dto.Catalog)1 Office (cwms.radar.data.dto.Office)1 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)1 FeatureCollection (org.geojson.FeatureCollection)1 PolicyFactory (org.owasp.html.PolicyFactory)1