Skip to content

Commit

Permalink
Merge pull request #537 from adamkorynta/bugfix/loc_level_store_office
Browse files Browse the repository at this point in the history
fixing bug in timezone with location level delete
  • Loading branch information
adamkorynta authored Feb 10, 2024
2 parents cc6ca20 + 9e03f60 commit 9d81445
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
37 changes: 11 additions & 26 deletions cwms-data-api/src/main/java/cwms/cda/api/LevelsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import cwms.cda.api.enums.UnitSystem;
import cwms.cda.api.errors.CdaError;
import cwms.cda.api.errors.JsonFieldsException;
import cwms.cda.api.errors.NotFoundException;
import cwms.cda.data.dao.LocationLevelsDao;
import cwms.cda.data.dao.LocationLevelsDaoImpl;
import cwms.cda.data.dto.LocationLevel;
Expand All @@ -49,7 +48,6 @@
import cwms.cda.formatters.ContentType;
import cwms.cda.formatters.Formats;
import cwms.cda.formatters.FormattingException;
import cwms.cda.formatters.xml.adapters.ZonedDateTimeAdapter;
import cwms.cda.helpers.DateUtils;
import hec.data.level.JDomLocationLevelRef;
import io.javalin.apibuilder.CrudHandler;
Expand All @@ -74,16 +72,12 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.codahale.metrics.MetricRegistry.name;
import static cwms.cda.api.Controllers.*;
import static cwms.cda.data.dao.JooqDao.getDslContext;

public class LevelsController implements CrudHandler {
private static final Logger logger = Logger.getLogger(LevelsController.class.getName());

private final MetricRegistry metrics;

private final Histogram requestResultSize;
Expand Down Expand Up @@ -160,7 +154,11 @@ public void create(@NotNull Context ctx) {
+ " from all offices."),
@OpenApiParam(name = EFFECTIVE_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.")
+ "delete all data and reference to the location level."),
@OpenApiParam(name = TIMEZONE, description = "Specifies the time zone of "
+ "the value of the effective date field (unless otherwise "
+ "specified).If this field is not specified, the default time zone of UTC "
+ "shall be used."),
},
method = HttpMethod.DELETE,
path = "/levels",
Expand All @@ -174,17 +172,14 @@ public void delete(@NotNull Context ctx, @NotNull String levelId) {
String dateString = queryParamAsClass(ctx,
new String[]{EFFECTIVE_DATE, DATE}, String.class, null, metrics,
name(LevelsController.class.getName(), DELETE));
String timezone = ctx.queryParamAsClass(TIMEZONE, String.class)
.getOrDefault("UTC");
Boolean cascadeDelete = Boolean.parseBoolean(ctx.queryParam(CASCADE_DELETE));
ZonedDateTimeAdapter zonedDateTimeAdapter = new ZonedDateTimeAdapter();
ZonedDateTime unmarshalledDateTime = dateString != null
? zonedDateTimeAdapter.unmarshal(dateString) : null;
ZonedDateTime unmarshalledDateTime = dateString != null ?
DateUtils.parseUserDate(dateString, timezone) : null;
LocationLevelsDao levelsDao = getLevelsDao(dsl);
levelsDao.deleteLocationLevel(levelId, unmarshalledDateTime, office, cascadeDelete);
ctx.status(HttpServletResponse.SC_ACCEPTED).json(levelId + " Deleted");
} catch (Exception ex) {
CdaError re = new CdaError("Failed to delete location level");
logger.log(Level.SEVERE, re.toString(), ex);
ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
}
}

Expand Down Expand Up @@ -382,13 +377,6 @@ String.class, null, metrics, name(LevelsController.class.getName(),
units, unmarshalledDateTime, office);
ctx.json(locationLevel);
ctx.status(HttpServletResponse.SC_OK);
} catch (NotFoundException e) {
throw e;
} catch (Exception ex) {
CdaError re = new CdaError("Failed to retrieve Location Level request: "
+ ex.getLocalizedMessage());
logger.log(Level.SEVERE, re.toString(), ex);
ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
}
}

Expand Down Expand Up @@ -458,10 +446,7 @@ public void update(@NotNull Context ctx, @NotNull String oldLevelId) {
ctx.status(HttpServletResponse.SC_ACCEPTED).json("Updated Location Level");
}
} catch (JsonProcessingException ex) {
CdaError re =
new CdaError("Failed to process request: " + ex.getLocalizedMessage());
logger.log(Level.SEVERE, re.toString(), ex);
ctx.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).json(re);
throw new FormattingException("Failed to format location level update request", ex);
}
}

Expand Down Expand Up @@ -637,7 +622,7 @@ private LocationLevel updatedClearedFields(String body, String format,
+ " response. If this field is not specified, the default time zone "
+ "of UTC shall be used.\r\nIgnored if begin was specified with "
+ "offset and timezone."),
@OpenApiParam(name = UNIT, required = false, description = "Desired unit for "
@OpenApiParam(name = UNIT, required = true, description = "Desired unit for "
+ "the values retrieved."),
},
responses = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,21 @@ public static SeasonalValueBean buildSeasonalValue(usace.cwms.db.dao.ifc.level.S
public void deleteLocationLevel(String locationLevelName, ZonedDateTime zonedDateTime,
String officeId, Boolean cascadeDelete) {
try {
Date date;
Timestamp date;
if (zonedDateTime != null) {
date = Date.from(zonedDateTime.toLocalDateTime().atZone(zonedDateTime.getZone()).toInstant());
date = Timestamp.from(zonedDateTime.toInstant());
} else {
date = null;
}
if (date != null) {
connection(dsl, c -> {
CwmsDbLevel levelJooq = CwmsDbServiceLookup.buildCwmsDb(CwmsDbLevel.class, c);
levelJooq.deleteLocationLevel(c, locationLevelName, date, null,
null, null, cascadeDelete, officeId);
String cascade = "F";
if (cascadeDelete != null && cascadeDelete) {
cascade = "T";
}
CWMS_LEVEL_PACKAGE.call_DELETE_LOCATION_LEVEL(getDslContext(c, officeId).configuration(),
locationLevelName, date, "UTC", null,
null, null, cascade, officeId, "VN");
});
} else {
Record1<Long> levelCode = dsl.selectDistinct(AV_LOCATION_LEVEL.LOCATION_LEVEL_CODE)
Expand Down

0 comments on commit 9d81445

Please sign in to comment.