Search in sources :

Example 1 with NamedPgJsonFormatter

use of cwms.radar.formatters.json.NamedPgJsonFormatter in project cwms-radar-api by USACE.

the class BasinController method getAll.

@OpenApi(queryParams = { @OpenApiParam(name = "office", required = false, description = "Specifies the owning office of the basin whose data is to be included in the response. If this field is not specified, matching basin information from all offices shall be returned."), @OpenApiParam(name = "unit", required = false, 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. Basin values will be in the default English units for their parameters. (This is default if no value is entered)\r\n2. SI.   Specifies the SI unit system. Basin values will be in the default SI units for their parameters.") }, responses = { @OpenApiResponse(status = "200", content = { @OpenApiContent(from = Basin.class, type = Formats.NAMED_PGJSON) }), @OpenApiResponse(status = "404", description = "The provided combination of parameters did not find a basin."), @OpenApiResponse(status = "501", description = "Requested format is not implemented") }, description = "Returns CWMS Basin Data", tags = { TAG })
@Override
public void getAll(@NotNull Context ctx) {
    getAllRequests.mark();
    try (final Timer.Context timeContext = getAllRequestsTime.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.NAMED_PGJSON;
        ContentType contentType = Formats.parseHeader(formatHeader);
        ctx.contentType(contentType.toString());
        BasinDao basinDao = new BasinDao(dsl);
        List<Basin> basins = basinDao.getAllBasins(units, office);
        if (contentType.getType().equals(Formats.NAMED_PGJSON)) {
            NamedPgJsonFormatter basinPgJsonFormatter = new NamedPgJsonFormatter();
            String result = basinPgJsonFormatter.format(basins);
            ctx.result(result);
        }
    } catch (SQLException ex) {
        LOGGER.log(Level.SEVERE, "Error retrieving all basins", ex);
    }
}
Also used : Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) SQLException(java.sql.SQLException) Basin(cwms.radar.data.dto.basinconnectivity.Basin) NamedPgJsonFormatter(cwms.radar.formatters.json.NamedPgJsonFormatter) DSLContext(org.jooq.DSLContext) BasinDao(cwms.radar.data.dao.BasinDao) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 2 with NamedPgJsonFormatter

use of cwms.radar.formatters.json.NamedPgJsonFormatter in project cwms-radar-api by USACE.

the class BasinController method getOne.

@OpenApi(queryParams = { @OpenApiParam(name = "office", required = false, description = "Specifies the owning office of the basin whose data is to be included in the response. If this field is not specified, matching basin information from all offices shall be returned."), @OpenApiParam(name = "unit", required = false, 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. Basin values will be in the default English units for their parameters. (This is default if no value is entered)\r\n2. SI.   Specifies the SI unit system. Basin values will be in the default SI units for their parameters.") }, responses = { @OpenApiResponse(status = "200", content = { @OpenApiContent(from = Basin.class, type = Formats.NAMED_PGJSON) }), @OpenApiResponse(status = "404", description = "The provided combination of parameters did not find a basin."), @OpenApiResponse(status = "501", description = "Requested format is not implemented") }, description = "Returns CWMS Basin Data", tags = { TAG })
@Override
public void getOne(@NotNull Context ctx, @NotNull String basinId) {
    getOneRequest.mark();
    try (final Timer.Context timeContext = getAllRequestsTime.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.NAMED_PGJSON;
        ContentType contentType = Formats.parseHeader(formatHeader);
        ctx.contentType(contentType.toString());
        BasinDao basinDao = new BasinDao(dsl);
        Basin basin = basinDao.getBasin(basinId, units, office);
        if (contentType.getType().equals(Formats.NAMED_PGJSON)) {
            NamedPgJsonFormatter basinPgJsonFormatter = new NamedPgJsonFormatter();
            String result = basinPgJsonFormatter.format(basin);
            ctx.result(result);
        } else {
            ctx.status(HttpServletResponse.SC_NOT_FOUND).json(new RadarError("Unsupported format for basins"));
        }
    } catch (SQLException ex) {
        String errorMsg = "Error retrieving " + basinId;
        LOGGER.log(Level.SEVERE, errorMsg, ex);
    }
}
Also used : RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) SQLException(java.sql.SQLException) Basin(cwms.radar.data.dto.basinconnectivity.Basin) NamedPgJsonFormatter(cwms.radar.formatters.json.NamedPgJsonFormatter) DSLContext(org.jooq.DSLContext) BasinDao(cwms.radar.data.dao.BasinDao) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Aggregations

Timer (com.codahale.metrics.Timer)2 BasinDao (cwms.radar.data.dao.BasinDao)2 Basin (cwms.radar.data.dto.basinconnectivity.Basin)2 ContentType (cwms.radar.formatters.ContentType)2 NamedPgJsonFormatter (cwms.radar.formatters.json.NamedPgJsonFormatter)2 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)2 SQLException (java.sql.SQLException)2 DSLContext (org.jooq.DSLContext)2 RadarError (cwms.radar.api.errors.RadarError)1