Search in sources :

Example 1 with LocationGroupDao

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

the class LocationGroupController method getAll.

@OpenApi(queryParams = { @OpenApiParam(name = "office", description = "Specifies the owning office of the location group(s) whose data is to be included in the response. If this field is not specified, matching location groups information from all offices shall be returned."), @OpenApiParam(name = "includeAssigned", type = Boolean.class, description = "Include the assigned locations in the returned location groups. (default: false)") }, responses = { @OpenApiResponse(status = "200", content = { @OpenApiContent(isArray = true, from = LocationGroup.class, type = Formats.JSON), @OpenApiContent(isArray = true, from = CsvV1LocationGroup.class, type = Formats.CSV) }) }, description = "Returns CWMS Location Groups Data", tags = { TAG })
@Override
public void getAll(Context ctx) {
    getAllRequests.mark();
    try (final Timer.Context timeContext = getAllRequestsTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        LocationGroupDao cdm = new LocationGroupDao(dsl);
        String office = ctx.queryParam("office");
        boolean includeValues = ctx.queryParamAsClass("includeAssigned", Boolean.class).getOrDefault(false);
        List<LocationGroup> grps = cdm.getLocationGroups(office, includeValues);
        if (!grps.isEmpty()) {
            String formatHeader = ctx.header(Header.ACCEPT);
            ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, "");
            String result = Formats.format(contentType, grps, LocationGroup.class);
            ctx.result(result);
            ctx.contentType(contentType.toString());
            requestResultSize.update(result.length());
            ctx.status(HttpServletResponse.SC_OK);
        } else {
            RadarError re = new RadarError("No location groups for office provided");
            logger.info(() -> {
                return new StringBuilder().append(re.toString()).append(System.lineSeparator()).append("for request ").append(ctx.fullUrl()).toString();
            });
            ctx.status(HttpServletResponse.SC_NOT_FOUND).json(re);
        }
    }
}
Also used : RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) LocationGroupDao(cwms.radar.data.dao.LocationGroupDao) DSLContext(org.jooq.DSLContext) CsvV1LocationGroup(cwms.radar.formatters.csv.CsvV1LocationGroup) LocationGroup(cwms.radar.data.dto.LocationGroup) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 2 with LocationGroupDao

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

the class LocationGroupController method getOne.

@OpenApi(pathParams = { @OpenApiParam(name = "group-id", required = true, description = "Specifies the location_group whose data is to be included in the response") }, queryParams = { @OpenApiParam(name = "office", required = true, description = "Specifies the owning office of the location group whose data is to be included in the response."), @OpenApiParam(name = "category-id", required = true, description = "Specifies the category containing the location group whose data is to be included in the response.") }, responses = { @OpenApiResponse(status = "200", content = { @OpenApiContent(from = LocationGroup.class, type = Formats.JSON), @OpenApiContent(from = CsvV1LocationGroup.class, type = Formats.CSV), @OpenApiContent(type = Formats.GEOJSON) }) }, description = "Retrieves requested Location Group", tags = { TAG })
@Override
public void getOne(Context ctx, String groupId) {
    getOneRequest.mark();
    try (final Timer.Context timeContext = getOneRequestTime.time();
        DSLContext dsl = getDslContext(ctx)) {
        LocationGroupDao cdm = new LocationGroupDao(dsl);
        String office = ctx.queryParam("office");
        String categoryId = ctx.queryParam("category-id");
        String formatHeader = ctx.header(Header.ACCEPT);
        ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, "");
        String result;
        if (Formats.GEOJSON.equals(contentType.getType())) {
            FeatureCollection fc = cdm.buildFeatureCollectionForLocationGroup(office, categoryId, groupId, "EN");
            ObjectMapper mapper = ctx.appAttribute("ObjectMapper");
            result = mapper.writeValueAsString(fc);
        } else {
            Optional<LocationGroup> grp = cdm.getLocationGroup(office, categoryId, groupId);
            if (grp.isPresent()) {
                result = Formats.format(contentType, grp.get());
            } else {
                RadarError re = new RadarError("Unable to find location group based on parameters given");
                logger.info(() -> {
                    return new StringBuilder().append(re.toString()).append(System.lineSeparator()).append("for request ").append(ctx.fullUrl()).toString();
                });
                ctx.status(HttpServletResponse.SC_NOT_FOUND).json(re);
                return;
            }
        }
        ctx.result(result);
        ctx.contentType(contentType.toString());
        requestResultSize.update(result.length());
        ctx.status(HttpServletResponse.SC_OK);
    } catch (JsonProcessingException e) {
        RadarError re = new RadarError("Failed to process request");
        logger.log(Level.SEVERE, re.toString(), e);
        ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
    }
}
Also used : ContentType(cwms.radar.formatters.ContentType) LocationGroupDao(cwms.radar.data.dao.LocationGroupDao) DSLContext(org.jooq.DSLContext) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) FeatureCollection(org.geojson.FeatureCollection) CsvV1LocationGroup(cwms.radar.formatters.csv.CsvV1LocationGroup) LocationGroup(cwms.radar.data.dto.LocationGroup) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Aggregations

Timer (com.codahale.metrics.Timer)2 RadarError (cwms.radar.api.errors.RadarError)2 LocationGroupDao (cwms.radar.data.dao.LocationGroupDao)2 LocationGroup (cwms.radar.data.dto.LocationGroup)2 ContentType (cwms.radar.formatters.ContentType)2 CsvV1LocationGroup (cwms.radar.formatters.csv.CsvV1LocationGroup)2 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)2 DSLContext (org.jooq.DSLContext)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FeatureCollection (org.geojson.FeatureCollection)1