use of ca.uhn.fhir.rest.param.DateParam in project openmrs-module-fhir2 by openmrs.
the class ConditionSearchQueryImpl_2_2Test method searchForConditions_shouldReturnConditionByUnboundedRecordedDate.
@Test
public void searchForConditions_shouldReturnConditionByUnboundedRecordedDate() {
DateRangeParam onsetDate = new DateRangeParam(new DateParam("gt" + RECORDED_START_DATE));
SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.DATE_RANGE_SEARCH_HANDLER, "dateCreated", onsetDate);
IBundleProvider results = search(theParams);
List<IBaseResource> resultList = get(results);
assertThat(results, notNullValue());
assertThat(resultList, not(empty()));
assertThat(resultList, hasSize(greaterThanOrEqualTo(1)));
}
use of ca.uhn.fhir.rest.param.DateParam in project gpconnect-demonstrator by nhsconnect.
the class AppointmentResourceProvider method getAppointmentsForPatientIdAndDates.
@Search
public List<Appointment> getAppointmentsForPatientIdAndDates(@RequiredParam(name = "patient") IdType patientLocalId, @Sort SortSpec sort, @Count Integer count, @RequiredParam(name = "start") DateAndListParam startDates) {
Date startLowerDate = null;
Date startUpperDate = null;
List<DateOrListParam> startDateList = startDates.getValuesAsQueryTokens();
if (startDateList.size() != 2) {
throwUnprocessableEntityInvalid422_ParameterException("Appointment search must have both 'le' and 'ge' start date parameters.");
}
Pattern dateOnlyPattern = Pattern.compile("[0-9]{4}(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1])))");
boolean lePrefix = false;
boolean gePrefix = false;
for (DateOrListParam filter : startDateList) {
DateParam token = filter.getValuesAsQueryTokens().get(0);
if (!dateOnlyPattern.matcher(token.getValueAsString()).matches()) {
throwUnprocessableEntityInvalid422_ParameterException("Search dates must be of the format: yyyy-MM-dd and should not include time or timezone.");
}
if (null != token.getPrefix()) {
switch(token.getPrefix()) {
case GREATERTHAN_OR_EQUALS:
startLowerDate = token.getValue();
gePrefix = true;
break;
case LESSTHAN_OR_EQUALS:
Calendar upper = Calendar.getInstance();
upper.setTime(token.getValue());
upper.add(Calendar.HOUR, 23);
upper.add(Calendar.MINUTE, 59);
upper.add(Calendar.SECOND, 59);
startUpperDate = upper.getTime();
lePrefix = true;
break;
default:
throwUnprocessableEntityInvalid422_ParameterException("Unknown prefix on start date parameter: only le and ge prefixes are allowed.");
}
}
}
if (!gePrefix || !lePrefix) {
throwUnprocessableEntityInvalid422_ParameterException("Parameters must contain two start parameters, one with prefix 'ge' and one with prefix 'le'.");
} else if (startLowerDate.before(getYesterday())) {
throwUnprocessableEntityInvalid422_ParameterException("Search dates from the past: " + startLowerDate.toString() + " are not allowed in appointment search.");
} else if (startUpperDate.before(getYesterday())) {
throwUnprocessableEntityInvalid422_ParameterException("Search dates from the past: " + startUpperDate.toString() + " are not allowed in appointment search.");
} else if (startUpperDate.before(startLowerDate)) {
throwUnprocessableEntityInvalid422_ParameterException("Upper search date must be after the lower search date.");
}
List<Appointment> appointment = appointmentSearch.searchAppointments(patientLocalId.getIdPartAsLong(), startLowerDate, startUpperDate).stream().map(this::appointmentDetailToAppointmentResourceConverter).collect(Collectors.toList());
List<Appointment> futureAppointments = appointmentValidation.filterToReturnFutureAppointments(appointment);
if (futureAppointments.isEmpty()) {
return null;
}
// Update startIndex if we do paging
return count != null ? futureAppointments.subList(0, count) : futureAppointments;
}
use of ca.uhn.fhir.rest.param.DateParam in project beneficiary-fhir-data by CMSgov.
the class LoadedFileFilter method matchesDateRange.
/**
* Tests the filter's time span overlaps the passed in date range.
*
* @param dateRangeParam to compare
* @return true if there is some overlap
*/
public boolean matchesDateRange(DateRangeParam dateRangeParam) {
if (dateRangeParam == null)
return true;
final DateParam upperBound = dateRangeParam.getUpperBound();
if (upperBound != null) {
switch(upperBound.getPrefix()) {
case LESSTHAN:
if (upperBound.getValue().toInstant().isBefore(getFirstUpdated())) {
return false;
}
break;
case LESSTHAN_OR_EQUALS:
if (!upperBound.getValue().toInstant().isAfter(getFirstUpdated())) {
return false;
}
break;
default:
throw new IllegalArgumentException("Invalid upper bound in _lastUpdated");
}
}
final DateParam lowerBound = dateRangeParam.getLowerBound();
if (lowerBound != null) {
switch(lowerBound.getPrefix()) {
case GREATERTHAN:
if (!lowerBound.getValue().toInstant().isBefore(getLastUpdated())) {
return false;
}
break;
case GREATERTHAN_OR_EQUALS:
if (lowerBound.getValue().toInstant().isAfter(getLastUpdated())) {
return false;
}
break;
default:
throw new IllegalArgumentException("Invalid lower bound in _lastUpdated");
}
}
return true;
}
use of ca.uhn.fhir.rest.param.DateParam in project beneficiary-fhir-data by CMSgov.
the class LoadedFilterTest method testMatchesDateRange.
@Test
public void testMatchesDateRange() {
final BloomFilter emptyFilter = LoadedFileFilter.createFilter(10);
final LoadedFileFilter filter1 = new LoadedFileFilter(1, 0, Instant.now().minusSeconds(10), Instant.now().minusSeconds(5), emptyFilter);
assertTrue(filter1.matchesDateRange(null), "Expected null range to be treated as an infinite range");
assertTrue(filter1.matchesDateRange(new DateRangeParam()), "Expected empty range to be treated as an infinite range");
final DateRangeParam sinceYesterday = new DateRangeParam(new DateParam().setPrefix(ParamPrefixEnum.GREATERTHAN).setValue(Date.from(Instant.now().minus(1, ChronoUnit.DAYS))));
assertTrue(filter1.matchesDateRange(sinceYesterday), "Expected since yesterday period to cover");
final DateRangeParam beforeNow = new DateRangeParam(new DateParam().setPrefix(ParamPrefixEnum.LESSTHAN_OR_EQUALS).setValue(new Date()));
assertTrue(filter1.matchesDateRange(beforeNow), "Expected since yesterday period to cover");
final DateRangeParam beforeYesterday = new DateRangeParam(new DateParam().setPrefix(ParamPrefixEnum.LESSTHAN).setValue(Date.from(Instant.now().minus(1, ChronoUnit.DAYS))));
assertFalse(filter1.matchesDateRange(beforeYesterday), "Expected before yesterday period to not match");
final DateRangeParam afterNow = new DateRangeParam(new DateParam().setPrefix(ParamPrefixEnum.GREATERTHAN_OR_EQUALS).setValue(new Date()));
assertFalse(filter1.matchesDateRange(afterNow), "Expected after now period to not match");
final DateRangeParam beforeSevenSeconds = new DateRangeParam(new DateParam().setPrefix(ParamPrefixEnum.LESSTHAN).setValue(Date.from(Instant.now().minus(7, ChronoUnit.SECONDS))));
assertTrue(filter1.matchesDateRange(beforeSevenSeconds), "Expected partial match to match");
final DateRangeParam afterSevenSeconds = new DateRangeParam(new DateParam().setPrefix(ParamPrefixEnum.GREATERTHAN).setValue(Date.from(Instant.now().minus(7, ChronoUnit.SECONDS))));
assertTrue(filter1.matchesDateRange(afterSevenSeconds), "Expected partial match to match");
final DateRangeParam sevenSeconds = new DateRangeParam(Date.from(Instant.now().minusSeconds(8)), Date.from(Instant.now().minusSeconds(7)));
assertTrue(filter1.matchesDateRange(sevenSeconds), "Expected partial match to match");
}
use of ca.uhn.fhir.rest.param.DateParam in project beneficiary-fhir-data by CMSgov.
the class R4ClaimResourceProviderIT method shouldGetCorrectClaimResourcesByMbiHash.
@Test
public void shouldGetCorrectClaimResourcesByMbiHash() {
IGenericClient fhirClient = ServerTestUtils.get().createFhirClientV2();
Bundle claimResult = fhirClient.search().forResource(Claim.class).where(Map.of("mbi", List.of(new ReferenceParam(RDATestUtils.MBI_HASH)), "service-date", List.of(new DateParam("gt1970-07-18"), new DateParam("lt1970-07-30")))).returnBundle(Bundle.class).execute();
// Sort entries for consistent testing results
claimResult.getEntry().sort(Comparator.comparing(a -> a.getResource().getId()));
String expected = testUtils.expectedResponseFor("claimSearch");
String actual = FhirContext.forR4().newJsonParser().encodeResourceToString(claimResult);
Set<String> ignorePatterns = new HashSet<>(IGNORE_PATTERNS);
ignorePatterns.add("\"/id\"");
ignorePatterns.add("\"/entry/[0-9]+/resource/created\"");
AssertUtils.assertJsonEquals(expected, actual, ignorePatterns);
}
Aggregations