Skip to content

Commit

Permalink
Add dedicated methods to parse start and end date/times. Spotless.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukedegruchy committed Oct 1, 2024
1 parent d22ae89 commit 7402a5b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import ca.uhn.fhir.rest.api.server.RequestDetails;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.util.DateUtils;
import jakarta.annotation.Nonnull;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -95,6 +94,46 @@ public ZonedDateTime deSerialize(String theInputDateString) {
return ZonedDateTime.parse(theInputDateString, DATE_TIME_FORMATTER_JSON_SERIALIZE);
}

/**
* Get the start period as a parsed ZoneDateTime (ex 2024 to 2024-01-01T00:00:00-07:00).
*
* @param theInputDateTimeString A String representation of the period start date in yyyy, yyyy-MM, YYYY-MM-dd, or yyyy-MM-ddTHH:mm:ss
* @param theTimezone A 6 with which to convert the timestamp
* @return the parsed start date/time with zone info
*/
public ZonedDateTime getStartZonedDateTime(String theInputDateTimeString, ZoneId theTimezone) {
final DateTimeFormatter dateTimeFormat = validateAndGetDateTimeFormat(theInputDateTimeString);

final LocalDateTime localDateTime = validateAndGetLocalDateTime(
theInputDateTimeString, dateTimeFormat, DateUtils::extractLocalDateTimeForRangeStartOrEmpty, true);

return ZonedDateTime.of(localDateTime, theTimezone);
}

/**
* Get the end period as a parsed ZoneDateTime (ex 2024 to 2024-12-31T23:59:59-07:00).
*
* @param theInputDateTimeString A String representation of the period start date in yyyy, yyyy-MM, YYYY-MM-dd, or yyyy-MM-ddTHH:mm:ss
* @param theTimezone A ZoneId with which to convert the timestamp
* @return the parsed end date/time with zone info
*/
public ZonedDateTime getEndZonedDateTime(String theInputDateTimeString, ZoneId theTimezone) {
final DateTimeFormatter dateTimeFormat = validateAndGetDateTimeFormat(theInputDateTimeString);

final LocalDateTime localDateTime = validateAndGetLocalDateTime(
theInputDateTimeString, dateTimeFormat, DateUtils::extractLocalDateTimeForRangeEndOrEmpty, true);

return ZonedDateTime.of(localDateTime, theTimezone);
}

/**
* Convert the String representations of both period start and end dates to their ZonedDateTime equivalents
*
* @param theRequestDetails RequestDetails which may or may not contain a Timezone header
* @param thePeriodStart A String representation of the period start date in yyyy, yyyy-MM, YYYY-MM-dd, or yyyy-MM-ddTHH:mm:ss
* @param thePeriodEnd A String representation of the period start date in yyyy, yyyy-MM, YYYY-MM-dd, or yyyy-MM-ddTHH:mm:ss
* @return A MeasurePeriodForEvaluation containing both the period start and end as ZonedDateTimes
*/
public MeasurePeriodForEvaluation validateAndProcessTimezone(
RequestDetails theRequestDetails, String thePeriodStart, String thePeriodEnd) {
final ZoneId clientTimezone = getClientTimezoneOrInvalidRequest(theRequestDetails);
Expand Down Expand Up @@ -122,7 +161,8 @@ private MeasurePeriodForEvaluation validateInputDates(
Msg.code(2555), thePeriodStart, thePeriodEnd));
}

final DateTimeFormatter dateTimeFormatterStart = validateAndGetDateTimeFormat(thePeriodStart, thePeriodEnd);
final DateTimeFormatter dateTimeFormatterStart = validateAndGetDateTimeFormat(thePeriodStart);
validateAndGetDateTimeFormat(thePeriodEnd);

final LocalDateTime localDateTimeStart = validateAndGetLocalDateTime(
thePeriodStart, dateTimeFormatterStart, DateUtils::extractLocalDateTimeForRangeStartOrEmpty, true);
Expand Down Expand Up @@ -165,17 +205,16 @@ private LocalDateTime validateAndGetLocalDateTime(
Msg.code(2558), isStart ? "start" : "end", thePeriod)));
}

@Nonnull
private static DateTimeFormatter validateAndGetDateTimeFormat(String theThePeriodStart, String theThePeriodEnd) {
final DateTimeFormatter dateTimeFormatterStart =
VALID_DATE_TIME_FORMATTERS_BY_FORMAT_LENGTH.get(theThePeriodStart.length());
private DateTimeFormatter validateAndGetDateTimeFormat(String theInputDateTimeString) {
final DateTimeFormatter dateTimeFormatter =
VALID_DATE_TIME_FORMATTERS_BY_FORMAT_LENGTH.get(theInputDateTimeString.length());

if (dateTimeFormatterStart == null) {
if (dateTimeFormatter == null) {
throw new InvalidRequestException(String.format(
"%sUnsupported Date/Time format for period start: %s or end: %s",
Msg.code(2559), theThePeriodStart, theThePeriodEnd));
"%sUnsupported Date/Time format for input: %s", Msg.code(2559), theInputDateTimeString));
}
return dateTimeFormatterStart;

return dateTimeFormatter;
}

private ZoneId getClientTimezoneOrInvalidRequest(RequestDetails theRequestDetails) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.cr.common.IRepositoryFactory;
import ca.uhn.fhir.cr.common.RepositoryFactoryForRepositoryInterface;
import ca.uhn.fhir.cr.common.StringTimePeriodHandler;
import ca.uhn.fhir.cr.config.CrBaseConfig;
import ca.uhn.fhir.cr.config.ProviderLoader;
import ca.uhn.fhir.cr.config.ProviderSelector;
Expand All @@ -40,7 +41,6 @@
import ca.uhn.fhir.cr.r4.measure.CollectDataOperationProvider;
import ca.uhn.fhir.cr.r4.measure.DataRequirementsOperationProvider;
import ca.uhn.fhir.cr.r4.measure.MeasureOperationsProvider;
import ca.uhn.fhir.cr.common.StringTimePeriodHandler;
import ca.uhn.fhir.cr.r4.measure.SubmitDataProvider;
import ca.uhn.fhir.rest.server.RestfulServer;
import org.opencds.cqf.fhir.cql.EvaluationSettings;
Expand Down Expand Up @@ -148,8 +148,7 @@ SubmitDataProvider r4SubmitDataProvider() {

@Bean
MeasureOperationsProvider r4MeasureOperationsProvider(
IMeasureServiceFactory theR4MeasureServiceFactory,
StringTimePeriodHandler theStringTimePeriodHandler) {
IMeasureServiceFactory theR4MeasureServiceFactory, StringTimePeriodHandler theStringTimePeriodHandler) {
return new MeasureOperationsProvider(theR4MeasureServiceFactory, theStringTimePeriodHandler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public class MeasureOperationsProvider {
private final StringTimePeriodHandler myMeasureReportPeriodRequestProcessingService;

public MeasureOperationsProvider(
IMeasureServiceFactory theR4MeasureServiceFactory,
StringTimePeriodHandler theStringTimePeriodHandler) {
IMeasureServiceFactory theR4MeasureServiceFactory, StringTimePeriodHandler theStringTimePeriodHandler) {
myR4MeasureServiceFactory = theR4MeasureServiceFactory;
myMeasureReportPeriodRequestProcessingService = theStringTimePeriodHandler;
}
Expand Down

0 comments on commit 7402a5b

Please sign in to comment.