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;
}
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;
}
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");
}
}
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);
}
}
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;
}
Aggregations