Search in sources :

Example 1 with Basin

use of cwms.radar.data.dto.basinconnectivity.Basin in project cwms-radar-api by USACE.

the class NamedPgJsonFormatter method format.

@Override
public String format(CwmsDTO dto) {
    String retVal;
    try {
        if (dto instanceof Basin) {
            Basin basin = (Basin) dto;
            Graph graph = new BasinConnectivityGraph.Builder(basin).build();
            String name = basin.getBasinName();
            retVal = formatNamedGraph(name, graph);
        } else {
            throw new FormattingException(dto.getClass().getSimpleName() + " is not currently supported for Named-PG-JSON format.");
        }
    } catch (JsonProcessingException e) {
        throw new FormattingException(e.getMessage());
    }
    return retVal;
}
Also used : BasinConnectivityGraph(cwms.radar.api.graph.basinconnectivity.BasinConnectivityGraph) Graph(cwms.radar.api.graph.Graph) FormattingException(cwms.radar.formatters.FormattingException) Basin(cwms.radar.data.dto.basinconnectivity.Basin) BasinConnectivityGraph(cwms.radar.api.graph.basinconnectivity.BasinConnectivityGraph) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with Basin

use of cwms.radar.data.dto.basinconnectivity.Basin in project cwms-radar-api by USACE.

the class PgJsonFormatter method format.

@Override
public String format(CwmsDTO dto) {
    String retVal;
    Graph graph;
    if (dto instanceof Basin) {
        Basin basin = (Basin) dto;
        graph = new BasinConnectivityGraph.Builder(basin).build();
    } else {
        throw new FormattingException(dto.getClass().getSimpleName() + " is not currently supported for PG-JSON format.");
    }
    try {
        retVal = formatGraph(graph);
    } catch (JsonProcessingException e) {
        throw new FormattingException(e.getMessage());
    }
    return retVal;
}
Also used : BasinConnectivityGraph(cwms.radar.api.graph.basinconnectivity.BasinConnectivityGraph) Graph(cwms.radar.api.graph.Graph) FormattingException(cwms.radar.formatters.FormattingException) Basin(cwms.radar.data.dto.basinconnectivity.Basin) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with Basin

use of cwms.radar.data.dto.basinconnectivity.Basin 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 4 with Basin

use of cwms.radar.data.dto.basinconnectivity.Basin 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)

Example 5 with Basin

use of cwms.radar.data.dto.basinconnectivity.Basin in project cwms-radar-api by USACE.

the class BasinDao method buildBasinsFromResultSet.

private List<Basin> buildBasinsFromResultSet(ResultSet rs, String unitSystem) throws SQLException {
    List<Basin> retval = new ArrayList<>();
    while (rs.next()) {
        String officeId = rs.getString("OFFICE_ID");
        String basinId = rs.getString("BASIN_ID");
        String parentBasinId = rs.getString("PARENT_BASIN_ID");
        Double sortOrder = rs.getDouble("SORT_ORDER");
        String primaryStreamId = rs.getString("PRIMARY_STREAM_ID");
        Double basinArea = rs.getDouble("TOTAL_DRAINAGE_AREA");
        Double contributingArea = rs.getDouble("CONTRIBUTING_DRAINAGE_AREA");
        Basin basin = new Basin.Builder(basinId, officeId).withBasinArea(basinArea).withContributingArea(contributingArea).withParentBasinId(parentBasinId).withSortOrder(sortOrder).build();
        if (primaryStreamId != null) {
            StreamDao streamDao = new StreamDao(dsl);
            Stream primaryStream = streamDao.getStream(primaryStreamId, unitSystem, officeId);
            basin = new Basin.Builder(basin).withPrimaryStream(primaryStream).build();
        }
        retval.add(basin);
    }
    return retval;
}
Also used : Basin(cwms.radar.data.dto.basinconnectivity.Basin) ArrayList(java.util.ArrayList) Stream(cwms.radar.data.dto.basinconnectivity.Stream)

Aggregations

Basin (cwms.radar.data.dto.basinconnectivity.Basin)7 SQLException (java.sql.SQLException)3 Timer (com.codahale.metrics.Timer)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Graph (cwms.radar.api.graph.Graph)2 BasinConnectivityGraph (cwms.radar.api.graph.basinconnectivity.BasinConnectivityGraph)2 BasinDao (cwms.radar.data.dao.BasinDao)2 Stream (cwms.radar.data.dto.basinconnectivity.Stream)2 ContentType (cwms.radar.formatters.ContentType)2 FormattingException (cwms.radar.formatters.FormattingException)2 NamedPgJsonFormatter (cwms.radar.formatters.json.NamedPgJsonFormatter)2 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)2 ArrayList (java.util.ArrayList)2 DSLContext (org.jooq.DSLContext)2 CwmsDbBasinJooq (usace.cwms.db.jooq.dao.CwmsDbBasinJooq)2 RadarError (cwms.radar.api.errors.RadarError)1 ResultSet (java.sql.ResultSet)1