Search in sources :

Example 1 with LocationLevelsDao

use of cwms.radar.data.dao.LocationLevelsDao 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 2 with LocationLevelsDao

use of cwms.radar.data.dao.LocationLevelsDao 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 3 with LocationLevelsDao

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

the class LevelsController method getAll.

@OpenApi(queryParams = { @OpenApiParam(name = "name", description = "Specifies the name(s) of the location level(s) whose data is to be included in the response. Uses * for all."), @OpenApiParam(name = "office", description = "Specifies the owning office of the location level(s) whose data is to be included in the response. If this field is not specified, matching location level information from all offices shall be returned."), @OpenApiParam(name = "unit", 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.  Location level values will be in the default English units for their parameters.\r\n2. SI.   Specifies the SI unit system.  Location level values will be in the default SI units for their parameters.\r\n3. Other. Any unit returned in the response to the units URI request that is appropriate for the requested parameters."), @OpenApiParam(name = "datum", description = "Specifies the elevation datum of the response. This field affects only elevation location levels. Valid values for this field are:\r\n1. NAVD88.  The elevation values will in the specified or default units above the NAVD-88 datum.\r\n2. NGVD29.  The elevation values will be in the specified or default units above the NGVD-29 datum."), @OpenApiParam(name = "begin", description = "Specifies the start of the time window for data to be included in the response. If this field is not specified, any required time window begins 24 hours prior to the specified or default end time."), @OpenApiParam(name = "end", description = "Specifies the end of the time window for data to be included in the response. If this field is not specified, any required time window ends at the current time"), @OpenApiParam(name = "timezone", description = "Specifies the time zone of the values of the begin and end fields (unless otherwise specified), as well as the time zone of any times in the response. If this field is not specified, the default time zone of UTC shall be used."), @OpenApiParam(name = "format", description = "Specifies the encoding format of the response. Valid values for the format field for this URI are:\r\n1.    tab\r\n2.    csv\r\n3.    xml\r\n4.  wml2 (only if name field is specified)\r\n5.    json (default)") }, responses = { @OpenApiResponse(status = "200") }, tags = { "Levels" })
@Override
public void getAll(Context ctx) {
    try (final Timer.Context timeContext = markAndTime("getAll");
        DSLContext dsl = getDslContext(ctx)) {
        LocationLevelsDao levelsDao = getLevelsDao(dsl);
        String format = ctx.queryParamAsClass("format", String.class).getOrDefault("json");
        String names = ctx.queryParam("name");
        String office = ctx.queryParam("office");
        String unit = ctx.queryParam("unit");
        String datum = ctx.queryParam("datum");
        String begin = ctx.queryParam("begin");
        String end = ctx.queryParam("end");
        String timezone = ctx.queryParam("timezone");
        switch(format) {
            case "json":
                {
                    ctx.contentType(Formats.JSON);
                    break;
                }
            case "tab":
                {
                    ctx.contentType(Formats.TAB);
                    break;
                }
            case "csv":
                {
                    ctx.contentType(Formats.CSV);
                    break;
                }
            case "xml":
                {
                    ctx.contentType(Formats.XML);
                    break;
                }
            case "wml2":
                {
                    ctx.contentType(Formats.WML2);
                    break;
                }
            // fall next
            case "png":
            // fall next
            case "jpg":
            default:
                {
                    ctx.status(HttpServletResponse.SC_NOT_IMPLEMENTED).json(RadarError.notImplemented());
                }
        }
        String results = levelsDao.getLocationLevels(format, names, office, unit, datum, begin, end, timezone);
        ctx.status(HttpServletResponse.SC_OK);
        ctx.result(results);
        requestResultSize.update(results.length());
    }
}
Also used : LocationLevelsDao(cwms.radar.data.dao.LocationLevelsDao) Timer(com.codahale.metrics.Timer) DSLContext(org.jooq.DSLContext) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 4 with LocationLevelsDao

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

the class LevelsController method delete.

@OpenApi(queryParams = { @OpenApiParam(name = "cascade-delete", type = Boolean.class), @OpenApiParam(name = "office", description = "Specifies the owning office of the location level whose data is to be deleted. If this field is not specified, matching location level information will be deleted from all offices."), @OpenApiParam(name = "date", description = "Specifies the effective date of the level to be deleted. If not provided will delete all data and reference to the location level.") }, description = "Delete CWMS Location Level", method = HttpMethod.DELETE, path = "/levels", tags = { "Levels" })
@Override
public void delete(@NotNull Context ctx, String id) {
    try (final Timer.Context timeContext = markAndTime("delete");
        DSLContext dsl = getDslContext(ctx)) {
        String office = ctx.queryParam("office");
        String dateString = ctx.queryParam("date");
        Boolean cascadeDelete = Boolean.parseBoolean(ctx.queryParam("cascade-delete"));
        ZonedDateTimeAdapter zonedDateTimeAdapter = new ZonedDateTimeAdapter();
        ZonedDateTime unmarshalledDateTime = dateString != null ? zonedDateTimeAdapter.unmarshal(dateString) : null;
        LocationLevelsDao levelsDao = getLevelsDao(dsl);
        levelsDao.deleteLocationLevel(id, unmarshalledDateTime, office, cascadeDelete);
        ctx.status(HttpServletResponse.SC_ACCEPTED).json(id + " Deleted");
    } catch (Exception ex) {
        RadarError re = new RadarError("Failed to delete location level");
        logger.log(Level.SEVERE, re.toString(), ex);
        ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
    }
}
Also used : LocationLevelsDao(cwms.radar.data.dao.LocationLevelsDao) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) ZonedDateTime(java.time.ZonedDateTime) ZonedDateTimeAdapter(cwms.radar.formatters.xml.adapters.ZonedDateTimeAdapter) 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) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Aggregations

Timer (com.codahale.metrics.Timer)4 LocationLevelsDao (cwms.radar.data.dao.LocationLevelsDao)4 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)4 DSLContext (org.jooq.DSLContext)4 FormattingException (cwms.radar.formatters.FormattingException)3 ZonedDateTime (java.time.ZonedDateTime)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 JsonFieldsException (cwms.radar.api.errors.JsonFieldsException)2 RadarError (cwms.radar.api.errors.RadarError)2 LocationLevel (cwms.radar.data.dto.LocationLevel)2 ContentType (cwms.radar.formatters.ContentType)2 ZonedDateTimeAdapter (cwms.radar.formatters.xml.adapters.ZonedDateTimeAdapter)2 IOException (java.io.IOException)2 ZoneId (java.time.ZoneId)2