Search in sources :

Example 1 with RatingException

use of hec.data.RatingException in project cwms-radar-api by USACE.

the class RatingSetDao method create.

@Override
public void create(RatingSet ratingSet) throws IOException, RatingException {
    try {
        dsl.connection(c -> {
            // can't exist if we are creating, if it exists use store
            boolean overwriteExisting = false;
            ratingSet.storeToDatabase(c, overwriteExisting);
        });
    } catch (DataAccessException ex) {
        Throwable cause = ex.getCause();
        if (cause instanceof RatingException) {
            throw (RatingException) cause;
        }
        throw new IOException("Failed to create Rating", ex);
    }
}
Also used : RatingException(hec.data.RatingException) IOException(java.io.IOException) DataAccessException(org.jooq.exception.DataAccessException)

Example 2 with RatingException

use of hec.data.RatingException in project cwms-radar-api by USACE.

the class RatingController method delete.

@OpenApi(queryParams = { @OpenApiParam(name = "office", required = true, description = "Specifies the owning office of the rating to be deleted.") }, method = HttpMethod.DELETE, tags = { "Ratings" })
@Override
public void delete(Context ctx, String ratingSpecId) {
    String office = ctx.queryParam("office");
    try (final Timer.Context timeContext = markAndTime("delete");
        DSLContext dsl = getDslContext(ctx)) {
        RatingDao ratingDao = getRatingDao(dsl);
        ratingDao.delete(office, ratingSpecId);
        ctx.status(HttpServletResponse.SC_ACCEPTED);
        ctx.json("Deleted RatingSet");
    } catch (IOException | RatingException ex) {
        RadarError re = new RadarError("Failed to process delete request");
        logger.log(Level.SEVERE, re.toString(), ex);
        ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
    }
}
Also used : RatingException(hec.data.RatingException) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) DSLContext(org.jooq.DSLContext) IOException(java.io.IOException) RatingDao(cwms.radar.data.dao.RatingDao) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 3 with RatingException

use of hec.data.RatingException in project cwms-radar-api by USACE.

the class RatingController method update.

@OpenApi(description = "Update a RatingSet", requestBody = @OpenApiRequestBody(content = { @OpenApiContent(type = Formats.XMLV2), @OpenApiContent(type = Formats.JSONV2) }, required = true), method = HttpMethod.PUT, path = "/ratings", tags = { "Ratings" })
public void update(Context ctx, String ratingId) {
    try (final Timer.Context timeContext = markAndTime("update");
        DSLContext dsl = getDslContext(ctx)) {
        RatingDao ratingDao = getRatingDao(dsl);
        // Retrieve the rating specified by ratingId and then somehow apply the update?
        // RatingSet ratingSet = ratingDao.retrieve(officeId, ratingId);
        // Or just store what they sent us?
        RatingSet ratingSet = deserializeRatingSet(ctx);
        ratingDao.store(ratingSet);
        ctx.status(HttpServletResponse.SC_ACCEPTED).json("Updated RatingSet");
    } catch (IOException | RatingException ex) {
        RadarError re = new RadarError("Failed to process request to update RatingSet");
        logger.log(Level.SEVERE, re.toString(), ex);
        ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
    }
}
Also used : RatingException(hec.data.RatingException) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) DSLContext(org.jooq.DSLContext) IOException(java.io.IOException) RatingDao(cwms.radar.data.dao.RatingDao) RatingSet(hec.data.cwmsRating.RatingSet) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 4 with RatingException

use of hec.data.RatingException in project cwms-radar-api by USACE.

the class RatingController method getRatingSetString.

@Nullable
private String getRatingSetString(Context ctx, String officeId, String rating) {
    String retval = null;
    try (final Timer.Context timeContext = markAndTime("getRatingSetString")) {
        String acceptHeader = ctx.header(Header.ACCEPT);
        if (Formats.JSONV2.equals(acceptHeader) || Formats.XMLV2.equals(acceptHeader)) {
            try {
                RatingSet ratingSet = getRatingSet(ctx, officeId, rating);
                if (ratingSet != null) {
                    if (Formats.JSONV2.equals(acceptHeader)) {
                        retval = JsonRatingUtils.toJson(ratingSet);
                    } else if (Formats.XMLV2.equals(acceptHeader)) {
                        retval = ratingSet.toXmlString(" ");
                    }
                } else {
                    ctx.status(HttpCode.NOT_FOUND);
                }
            } catch (RatingException e) {
                RadarError re = new RadarError("Failed to process request to retrieve RatingSet");
                logger.log(Level.SEVERE, re.toString(), e);
                ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                ctx.json(re);
            } catch (IOException e) {
                RadarError re = new RadarError("Failed to process request to retrieve RatingSet");
                logger.log(Level.SEVERE, re.toString(), e);
                ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
            }
        } else {
            RadarError re = new RadarError("Currently supporting only: " + Formats.JSONV2 + " and " + Formats.XMLV2);
            logger.log(Level.WARNING, "Provided accept header not recognized:" + acceptHeader, re);
            ctx.status(HttpServletResponse.SC_NOT_IMPLEMENTED);
            ctx.json(RadarError.notImplemented());
        }
    }
    return retval;
}
Also used : RatingException(hec.data.RatingException) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) IOException(java.io.IOException) RatingSet(hec.data.cwmsRating.RatingSet) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with RatingException

use of hec.data.RatingException in project cwms-radar-api by USACE.

the class RatingController method create.

@OpenApi(description = "Create new RatingSet", requestBody = @OpenApiRequestBody(content = { @OpenApiContent(type = Formats.XMLV2), @OpenApiContent(type = Formats.JSONV2) }, required = true), method = HttpMethod.POST, path = "/ratings", tags = { "Ratings" })
public void create(Context ctx) {
    try (final Timer.Context timeContext = markAndTime("create");
        DSLContext dsl = getDslContext(ctx)) {
        RatingDao ratingDao = getRatingDao(dsl);
        RatingSet ratingSet = deserializeRatingSet(ctx);
        ratingDao.create(ratingSet);
        ctx.status(HttpServletResponse.SC_ACCEPTED).json("Created RatingSet");
    } catch (IOException | RatingException ex) {
        RadarError re = new RadarError("Failed to process create request");
        logger.log(Level.SEVERE, re.toString(), ex);
        ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
    }
}
Also used : RatingException(hec.data.RatingException) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) DSLContext(org.jooq.DSLContext) IOException(java.io.IOException) RatingDao(cwms.radar.data.dao.RatingDao) RatingSet(hec.data.cwmsRating.RatingSet) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Aggregations

RatingException (hec.data.RatingException)7 IOException (java.io.IOException)7 Timer (com.codahale.metrics.Timer)4 RadarError (cwms.radar.api.errors.RadarError)4 RatingSet (hec.data.cwmsRating.RatingSet)4 RatingDao (cwms.radar.data.dao.RatingDao)3 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)3 DSLContext (org.jooq.DSLContext)3 DataAccessException (org.jooq.exception.DataAccessException)2 Nullable (org.jetbrains.annotations.Nullable)1