Search in sources :

Example 11 with DateParam

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)));
}
Also used : DateRangeParam(ca.uhn.fhir.rest.param.DateRangeParam) IBundleProvider(ca.uhn.fhir.rest.api.server.IBundleProvider) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) DateParam(ca.uhn.fhir.rest.param.DateParam) SearchParameterMap(org.openmrs.module.fhir2.api.search.param.SearchParameterMap) BaseModuleContextSensitiveTest(org.openmrs.test.BaseModuleContextSensitiveTest) Test(org.junit.Test)

Example 12 with DateParam

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;
}
Also used : DateOrListParam(ca.uhn.fhir.rest.param.DateOrListParam) Pattern(java.util.regex.Pattern) DateParam(ca.uhn.fhir.rest.param.DateParam) AppointmentSearch(uk.gov.hscic.appointment.appointment.AppointmentSearch) ScheduleSearch(uk.gov.hscic.appointment.schedule.ScheduleSearch) SlotSearch(uk.gov.hscic.appointment.slot.SlotSearch)

Example 13 with DateParam

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;
}
Also used : DateParam(ca.uhn.fhir.rest.param.DateParam)

Example 14 with DateParam

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");
}
Also used : DateRangeParam(ca.uhn.fhir.rest.param.DateRangeParam) LoadedFileFilter(gov.cms.bfd.server.war.commons.LoadedFileFilter) BloomFilter(org.apache.spark.util.sketch.BloomFilter) Date(java.util.Date) DateParam(ca.uhn.fhir.rest.param.DateParam) Test(org.junit.jupiter.api.Test)

Example 15 with DateParam

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);
}
Also used : RDATestUtils(gov.cms.bfd.server.war.utils.RDATestUtils) Set(java.util.Set) AssertUtils(gov.cms.bfd.server.war.utils.AssertUtils) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) FhirContext(ca.uhn.fhir.context.FhirContext) List(java.util.List) BeforeAll(org.junit.jupiter.api.BeforeAll) ReferenceParam(ca.uhn.fhir.rest.param.ReferenceParam) Map(java.util.Map) Bundle(org.hl7.fhir.r4.model.Bundle) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) ServerTestUtils(gov.cms.bfd.server.war.ServerTestUtils) Comparator(java.util.Comparator) Claim(org.hl7.fhir.r4.model.Claim) DateParam(ca.uhn.fhir.rest.param.DateParam) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) Bundle(org.hl7.fhir.r4.model.Bundle) ReferenceParam(ca.uhn.fhir.rest.param.ReferenceParam) Claim(org.hl7.fhir.r4.model.Claim) DateParam(ca.uhn.fhir.rest.param.DateParam) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

DateParam (ca.uhn.fhir.rest.param.DateParam)30 DateRangeParam (ca.uhn.fhir.rest.param.DateRangeParam)23 IBundleProvider (ca.uhn.fhir.rest.api.server.IBundleProvider)21 Test (org.junit.Test)21 SearchParameterMap (org.openmrs.module.fhir2.api.search.param.SearchParameterMap)21 BaseModuleContextSensitiveTest (org.openmrs.test.BaseModuleContextSensitiveTest)20 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)19 ReferenceParam (ca.uhn.fhir.rest.param.ReferenceParam)3 StringParam (ca.uhn.fhir.rest.param.StringParam)3 TokenParam (ca.uhn.fhir.rest.param.TokenParam)3 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 FhirContext (ca.uhn.fhir.context.FhirContext)2 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)2 DateOrListParam (ca.uhn.fhir.rest.param.DateOrListParam)2 StringAndListParam (ca.uhn.fhir.rest.param.StringAndListParam)2 TokenOrListParam (ca.uhn.fhir.rest.param.TokenOrListParam)2 InvalidRequestException (ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)2 Date (java.util.Date)2 List (java.util.List)2