Search in sources :

Example 1 with FormattingException

use of cwms.radar.formatters.FormattingException 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 FormattingException

use of cwms.radar.formatters.FormattingException 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 FormattingException

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

the class LevelsController method create.

@OpenApi(queryParams = { @OpenApiParam(name = "office", required = true, description = "Specifies the office in which Location Level will be created") }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = LocationLevel.class, type = Formats.JSON), @OpenApiContent(from = LocationLevel.class, type = Formats.XML) }, required = true), description = "Create new CWMS Location Level", method = HttpMethod.POST, path = "/levels", tags = { "Levels" })
@Override
public void create(@NotNull Context ctx) {
    try (final Timer.Context timeContext = markAndTime("create");
        DSLContext dsl = getDslContext(ctx)) {
        LocationLevelsDao levelsDao = getLevelsDao(dsl);
        String office = ctx.queryParam("office");
        String reqContentType = ctx.req.getContentType();
        String formatHeader = reqContentType != null ? reqContentType : Formats.JSON;
        ContentType contentType = Formats.parseHeader(formatHeader);
        if (contentType == null) {
            throw new FormattingException("Format header could not be parsed");
        }
        LocationLevel level = deserializeLocationLevel(ctx.body(), formatHeader, office);
        // getUnmarshalledDateTime(ctx.body(), contentType.getType());
        ZonedDateTime unmarshalledDateTime = level.getLevelDate();
        ZoneId timezoneId = unmarshalledDateTime.getZone();
        if (timezoneId == null) {
            timezoneId = ZoneId.systemDefault();
        }
        level = new LocationLevel.Builder(level).withLevelDate(unmarshalledDateTime).build();
        level.validate();
        levelsDao.storeLocationLevel(level, timezoneId);
        ctx.status(HttpServletResponse.SC_ACCEPTED).json("Created Location Level");
    }
}
Also used : LocationLevelsDao(cwms.radar.data.dao.LocationLevelsDao) FormattingException(cwms.radar.formatters.FormattingException) Timer(com.codahale.metrics.Timer) ContentType(cwms.radar.formatters.ContentType) ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) DSLContext(org.jooq.DSLContext) LocationLevel(cwms.radar.data.dto.LocationLevel) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 4 with FormattingException

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

the class LevelsController method update.

@OpenApi(queryParams = { @OpenApiParam(name = "office", required = true, description = "Specifies the office in which Location Level will be updated"), @OpenApiParam(name = "date", required = true, description = "Specifies the effective date of Location Level that will be updated") }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = LocationLevel.class, type = Formats.JSON), @OpenApiContent(from = LocationLevel.class, type = Formats.XML) }, required = true), description = "Update CWMS Location Level", method = HttpMethod.PATCH, path = "/levels", tags = { "Levels" })
@Override
public void update(@NotNull Context ctx, String id) {
    try (final Timer.Context timeContext = markAndTime("update");
        DSLContext dsl = getDslContext(ctx)) {
        LocationLevelsDao levelsDao = getLevelsDao(dsl);
        String office = ctx.queryParam("office");
        String dateString = ctx.queryParam("date");
        ZonedDateTimeAdapter zonedDateTimeAdapter = new ZonedDateTimeAdapter();
        ZonedDateTime unmarshalledDateTime = zonedDateTimeAdapter.unmarshal(dateString);
        ZoneId timezoneId = unmarshalledDateTime.getZone();
        if (timezoneId == null) {
            timezoneId = ZoneId.systemDefault();
        }
        String reqContentType = ctx.req.getContentType();
        String formatHeader = reqContentType != null ? reqContentType : Formats.JSON;
        ContentType contentType = Formats.parseHeader(formatHeader);
        if (contentType == null) {
            throw new FormattingException("Format header could not be parsed");
        }
        LocationLevel levelFromBody = deserializeLocationLevel(ctx.body(), contentType.getType(), office);
        // retrieveLocationLevel will throw an error if level does not exist
        LocationLevel existingLevelLevel = levelsDao.retrieveLocationLevel(id, UnitSystem.EN.getValue(), unmarshalledDateTime, office);
        existingLevelLevel = updatedClearedFields(ctx.body(), contentType.getType(), existingLevelLevel);
        // only store (update) if level does exist
        LocationLevel updatedLocationLevel = getUpdatedLocationLevel(existingLevelLevel, levelFromBody);
        updatedLocationLevel = new LocationLevel.Builder(updatedLocationLevel).withLevelDate(unmarshalledDateTime).build();
        if (// if name changed then delete location with old name
        !updatedLocationLevel.getLocationLevelId().equalsIgnoreCase(existingLevelLevel.getLocationLevelId())) {
            levelsDao.renameLocationLevel(id, updatedLocationLevel);
            ctx.status(HttpServletResponse.SC_ACCEPTED).json("Updated and renamed Location Level");
        } else {
            levelsDao.storeLocationLevel(updatedLocationLevel, timezoneId);
            ctx.status(HttpServletResponse.SC_ACCEPTED).json("Updated Location Level");
        }
    } catch (Exception 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 : LocationLevelsDao(cwms.radar.data.dao.LocationLevelsDao) FormattingException(cwms.radar.formatters.FormattingException) ZoneId(java.time.ZoneId) ContentType(cwms.radar.formatters.ContentType) DSLContext(org.jooq.DSLContext) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FormattingException(cwms.radar.formatters.FormattingException) JsonFieldsException(cwms.radar.api.errors.JsonFieldsException) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ZonedDateTime(java.time.ZonedDateTime) ZonedDateTimeAdapter(cwms.radar.formatters.xml.adapters.ZonedDateTimeAdapter) LocationLevel(cwms.radar.data.dto.LocationLevel) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 5 with FormattingException

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

the class LevelsController method getObjectMapperForFormat.

private static ObjectMapper getObjectMapperForFormat(String format) {
    ObjectMapper om;
    if ((Formats.XML).equals(format)) {
        JacksonXmlModule module = new JacksonXmlModule();
        module.setDefaultUseWrapper(false);
        om = new XmlMapper(module);
    } else if (Formats.JSON.equals(format)) {
        om = new ObjectMapper();
    } else {
        throw new FormattingException("Format is not currently supported for Levels");
    }
    om.registerModule(new JavaTimeModule());
    return om;
}
Also used : JacksonXmlModule(com.fasterxml.jackson.dataformat.xml.JacksonXmlModule) FormattingException(cwms.radar.formatters.FormattingException) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Aggregations

FormattingException (cwms.radar.formatters.FormattingException)10 ContentType (cwms.radar.formatters.ContentType)6 Timer (com.codahale.metrics.Timer)4 DSLContext (org.jooq.DSLContext)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 RadarError (cwms.radar.api.errors.RadarError)3 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)2 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)2 Graph (cwms.radar.api.graph.Graph)2 BasinConnectivityGraph (cwms.radar.api.graph.basinconnectivity.BasinConnectivityGraph)2 LocationLevelsDao (cwms.radar.data.dao.LocationLevelsDao)2 LocationsDao (cwms.radar.data.dao.LocationsDao)2 Location (cwms.radar.data.dto.Location)2 LocationLevel (cwms.radar.data.dto.LocationLevel)2 Basin (cwms.radar.data.dto.basinconnectivity.Basin)2 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)2 ZoneId (java.time.ZoneId)2 ZonedDateTime (java.time.ZonedDateTime)2