Search in sources :

Example 6 with Beneficiary

use of gov.cms.bfd.model.rif.Beneficiary in project beneficiary-fhir-data by CMSgov.

the class CoverageResourceProvider method findBeneficiaryById.

/**
 * @param beneficiaryId the {@link Beneficiary#getBeneficiaryId()} value to find a matching {@link
 *     Beneficiary} for
 * @return the {@link Beneficiary} that matches the specified {@link
 *     Beneficiary#getBeneficiaryId()} value
 * @throws NoResultException A {@link NoResultException} will be thrown if no matching {@link
 *     Beneficiary} can be found in the database.
 */
@Trace
private Beneficiary findBeneficiaryById(String beneficiaryId, DateRangeParam lastUpdatedRange) throws NoResultException {
    // Optimize when the lastUpdated parameter is specified and result set is empty
    if (loadedFilterManager.isResultSetEmpty(beneficiaryId, lastUpdatedRange)) {
        throw new NoResultException();
    }
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Beneficiary> criteria = builder.createQuery(Beneficiary.class);
    Root<Beneficiary> root = criteria.from(Beneficiary.class);
    root.fetch(Beneficiary_.beneficiaryMonthlys, JoinType.LEFT);
    criteria.select(root);
    Predicate wherePredicate = builder.equal(root.get(Beneficiary_.beneficiaryId), beneficiaryId);
    if (lastUpdatedRange != null) {
        Predicate predicate = QueryUtils.createLastUpdatedPredicate(builder, root, lastUpdatedRange);
        wherePredicate = builder.and(wherePredicate, predicate);
    }
    criteria.where(wherePredicate);
    Beneficiary beneficiary = null;
    Long beneByIdQueryNanoSeconds = null;
    Timer.Context timerBeneQuery = metricRegistry.timer(MetricRegistry.name(getClass().getSimpleName(), "query", "bene_by_id")).time();
    try {
        beneficiary = entityManager.createQuery(criteria).getSingleResult();
    } finally {
        beneByIdQueryNanoSeconds = timerBeneQuery.stop();
        TransformerUtils.recordQueryInMdc("bene_by_id.include_", beneByIdQueryNanoSeconds, beneficiary == null ? 0 : 1);
    }
    return beneficiary;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Timer(com.codahale.metrics.Timer) NoResultException(javax.persistence.NoResultException) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) Predicate(javax.persistence.criteria.Predicate) Trace(com.newrelic.api.agent.Trace)

Example 7 with Beneficiary

use of gov.cms.bfd.model.rif.Beneficiary in project beneficiary-fhir-data by CMSgov.

the class BeneficiaryTransformerV2Test method setup.

@BeforeEach
public void setup() {
    List<Object> parsedRecords = ServerTestUtils.parseData(Arrays.asList(StaticRifResourceGroup.SAMPLE_A.getResources()));
    // Pull out the base Beneficiary record and fix its HICN and MBI-HASH fields.
    beneficiary = parsedRecords.stream().filter(r -> r instanceof Beneficiary).map(r -> (Beneficiary) r).findFirst().get();
    beneficiary.getSkippedRifRecords().add(new SkippedRifRecord());
    beneficiary.setLastUpdated(Instant.now());
    beneficiary.setMbiHash(Optional.of("someMBIhash"));
    // Add the history records to the Beneficiary, but nill out the HICN fields.
    Set<BeneficiaryHistory> beneficiaryHistories = parsedRecords.stream().filter(r -> r instanceof BeneficiaryHistory).map(r -> (BeneficiaryHistory) r).filter(r -> beneficiary.getBeneficiaryId().equals(r.getBeneficiaryId())).collect(Collectors.toSet());
    beneficiary.getBeneficiaryHistories().addAll(beneficiaryHistories);
    // Add the MBI history records to the Beneficiary.
    Set<MedicareBeneficiaryIdHistory> beneficiaryMbis = parsedRecords.stream().filter(r -> r instanceof MedicareBeneficiaryIdHistory).map(r -> (MedicareBeneficiaryIdHistory) r).filter(r -> beneficiary.getBeneficiaryId().equals(r.getBeneficiaryId().orElse(null))).collect(Collectors.toSet());
    beneficiary.getMedicareBeneficiaryIdHistories().addAll(beneficiaryMbis);
    assertThat(beneficiary, is(notNullValue()));
    createPatient(RequestHeaders.getHeaderWrapper());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StaticRifResource(gov.cms.bfd.model.rif.samples.StaticRifResource) Date(java.util.Date) Identifier(org.hl7.fhir.r4.model.Identifier) IsEmptyCollection(org.hamcrest.collection.IsEmptyCollection) Disabled(org.junit.jupiter.api.Disabled) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) BigDecimal(java.math.BigDecimal) FhirContext(ca.uhn.fhir.context.FhirContext) HumanName(org.hl7.fhir.r4.model.HumanName) StringType(org.hl7.fhir.r4.model.StringType) BeneficiaryHistory(gov.cms.bfd.model.rif.BeneficiaryHistory) TemporalPrecisionEnum(ca.uhn.fhir.model.api.TemporalPrecisionEnum) Patient(org.hl7.fhir.r4.model.Patient) DateType(org.hl7.fhir.r4.model.DateType) Period(org.hl7.fhir.r4.model.Period) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) List(java.util.List) BooleanType(org.hl7.fhir.r4.model.BooleanType) TransformerConstants(gov.cms.bfd.server.war.commons.TransformerConstants) Coding(org.hl7.fhir.r4.model.Coding) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Extension(org.hl7.fhir.r4.model.Extension) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) SkippedRifRecord(gov.cms.bfd.model.rif.SkippedRifRecord) DateTimeType(org.hl7.fhir.r4.model.DateTimeType) CoreMatchers.not(org.hamcrest.CoreMatchers.not) SimpleDateFormat(java.text.SimpleDateFormat) ProfileConstants(gov.cms.bfd.server.war.commons.ProfileConstants) RequestHeaders(gov.cms.bfd.server.war.commons.RequestHeaders) ArrayList(java.util.ArrayList) Address(org.hl7.fhir.r4.model.Address) CcwCodebookVariable(gov.cms.bfd.model.codebook.data.CcwCodebookVariable) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) StaticRifResourceGroup(gov.cms.bfd.model.rif.samples.StaticRifResourceGroup) ServerTestUtils(gov.cms.bfd.server.war.ServerTestUtils) IParser(ca.uhn.fhir.parser.IParser) IBaseDatatype(org.hl7.fhir.instance.model.api.IBaseDatatype) MetricRegistry(com.codahale.metrics.MetricRegistry) MedicareBeneficiaryIdHistory(gov.cms.bfd.model.rif.MedicareBeneficiaryIdHistory) AdministrativeGender(org.hl7.fhir.r4.model.Enumerations.AdministrativeGender) BeneficiaryHistory(gov.cms.bfd.model.rif.BeneficiaryHistory) SkippedRifRecord(gov.cms.bfd.model.rif.SkippedRifRecord) MedicareBeneficiaryIdHistory(gov.cms.bfd.model.rif.MedicareBeneficiaryIdHistory) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with Beneficiary

use of gov.cms.bfd.model.rif.Beneficiary in project beneficiary-fhir-data by CMSgov.

the class CoverageTransformerV2Test method setup.

@BeforeEach
public void setup() {
    List<Object> parsedRecords = ServerTestUtils.parseData(Arrays.asList(StaticRifResourceGroup.SAMPLE_A.getResources()));
    // Pull out the base Beneficiary record and fix its HICN and MBI-HASH fields.
    beneficiary = parsedRecords.stream().filter(r -> r instanceof Beneficiary).map(r -> (Beneficiary) r).findFirst().get();
    Calendar calen = Calendar.getInstance();
    calen.set(2021, 3, 17);
    beneficiary.setLastUpdated(calen.getTime().toInstant());
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Identifier(org.hl7.fhir.r4.model.Identifier) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) ProfileConstants(gov.cms.bfd.server.war.commons.ProfileConstants) Reference(org.hl7.fhir.r4.model.Reference) Disabled(org.junit.jupiter.api.Disabled) ArrayList(java.util.ArrayList) FhirContext(ca.uhn.fhir.context.FhirContext) Calendar(java.util.Calendar) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) StaticRifResourceGroup(gov.cms.bfd.model.rif.samples.StaticRifResourceGroup) ServerTestUtils(gov.cms.bfd.server.war.ServerTestUtils) Coverage(org.hl7.fhir.r4.model.Coverage) MetricRegistry(com.codahale.metrics.MetricRegistry) DateType(org.hl7.fhir.r4.model.DateType) MedicareSegment(gov.cms.bfd.server.war.commons.MedicareSegment) Period(org.hl7.fhir.r4.model.Period) Test(org.junit.jupiter.api.Test) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) List(java.util.List) Coding(org.hl7.fhir.r4.model.Coding) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) LocalDate(java.time.LocalDate) FHIRException(org.hl7.fhir.exceptions.FHIRException) Extension(org.hl7.fhir.r4.model.Extension) Calendar(java.util.Calendar) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with Beneficiary

use of gov.cms.bfd.model.rif.Beneficiary in project beneficiary-fhir-data by CMSgov.

the class R4CoverageResourceProvider method read.

/**
 * Adds support for the FHIR "read" operation, for {@link Coverage}s. The {@link Read} annotation
 * indicates that this method supports the read operation.
 *
 * <p>Read operations take a single parameter annotated with {@link IdParam}, and should return a
 * single resource instance.
 *
 * @param coverageId The read operation takes one parameter, which must be of type {@link IdType}
 *     and must be annotated with the {@link IdParam} annotation.
 * @return Returns a resource matching the specified {@link IdDt}, or <code>null</code> if none
 *     exists.
 */
@Read(version = false)
@Trace
public Coverage read(@IdParam IdType coverageId) {
    if (coverageId == null)
        throw new IllegalArgumentException();
    if (coverageId.getVersionIdPartAsLong() != null)
        throw new IllegalArgumentException();
    String coverageIdText = coverageId.getIdPart();
    if (coverageIdText == null || coverageIdText.trim().isEmpty())
        throw new IllegalArgumentException();
    Operation operation = new Operation(Operation.Endpoint.V2_COVERAGE);
    operation.setOption("by", "id");
    operation.publishOperationName();
    Matcher coverageIdMatcher = COVERAGE_ID_PATTERN.matcher(coverageIdText);
    if (!coverageIdMatcher.matches())
        throw new IllegalArgumentException("Unsupported ID pattern: " + coverageIdText);
    String coverageIdSegmentText = coverageIdMatcher.group(1);
    Optional<MedicareSegment> coverageIdSegment = MedicareSegment.selectByUrlPrefix(coverageIdSegmentText);
    if (!coverageIdSegment.isPresent())
        throw new ResourceNotFoundException(coverageId);
    String coverageIdBeneficiaryIdText = coverageIdMatcher.group(2);
    Beneficiary beneficiaryEntity;
    try {
        beneficiaryEntity = findBeneficiaryById(coverageIdBeneficiaryIdText, null);
        if (!beneficiaryEntity.getBeneEnrollmentReferenceYear().isPresent()) {
            throw new ResourceNotFoundException("Cannot find coverage for non present enrollment year");
        }
    } catch (NoResultException e) {
        throw new ResourceNotFoundException(new IdDt(Beneficiary.class.getSimpleName(), coverageIdBeneficiaryIdText));
    }
    Coverage coverage = CoverageTransformerV2.transform(metricRegistry, coverageIdSegment.get(), beneficiaryEntity);
    return coverage;
}
Also used : Matcher(java.util.regex.Matcher) MedicareSegment(gov.cms.bfd.server.war.commons.MedicareSegment) IdDt(ca.uhn.fhir.model.primitive.IdDt) Coverage(org.hl7.fhir.r4.model.Coverage) Operation(gov.cms.bfd.server.war.Operation) NoResultException(javax.persistence.NoResultException) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) Read(ca.uhn.fhir.rest.annotation.Read) Trace(com.newrelic.api.agent.Trace)

Example 10 with Beneficiary

use of gov.cms.bfd.model.rif.Beneficiary in project beneficiary-fhir-data by CMSgov.

the class R4CoverageResourceProvider method searchByBeneficiary.

/**
 * Adds support for the FHIR "search" operation for {@link Coverage}s, allowing users to search by
 * {@link Coverage#getBeneficiary()}.
 *
 * <p>The {@link Search} annotation indicates that this method supports the search operation.
 * There may be many different methods annotated with this {@link Search} annotation, to support
 * many different search criteria.
 *
 * @param beneficiary a {@link ReferenceParam} for the {@link Coverage#getBeneficiary()} to try
 *     and find matches for
 * @param startIndex an {@link OptionalParam} for the startIndex (or offset) used to determine
 *     pagination
 * @param lastUpdated an {@link OptionalParam} to filter the results based on the passed date
 *     range
 * @param requestDetails a {@link RequestDetails} containing the details of the request URL, used
 *     to parse out pagination values
 * @return Returns a {@link List} of {@link Coverage}s, which may contain multiple matching
 *     resources, or may also be empty.
 */
@Search
@Trace
public Bundle searchByBeneficiary(@RequiredParam(name = Coverage.SP_BENEFICIARY) @Description(shortDefinition = "The patient identifier to search for") ReferenceParam beneficiary, @OptionalParam(name = "startIndex") @Description(shortDefinition = "The offset used for result pagination") String startIndex, @OptionalParam(name = "_lastUpdated") @Description(shortDefinition = "Include resources last updated in the given range") DateRangeParam lastUpdated, RequestDetails requestDetails) {
    List<IBaseResource> coverages;
    try {
        Beneficiary beneficiaryEntity = findBeneficiaryById(beneficiary.getIdPart(), lastUpdated);
        if (!beneficiaryEntity.getBeneEnrollmentReferenceYear().isPresent()) {
            throw new ResourceNotFoundException("Cannot find coverage for non present enrollment year");
        }
        coverages = CoverageTransformerV2.transform(metricRegistry, beneficiaryEntity);
    } catch (NoResultException e) {
        coverages = new LinkedList<IBaseResource>();
    }
    OffsetLinkBuilder paging = new OffsetLinkBuilder(requestDetails, "/Coverage?");
    Operation operation = new Operation(Operation.Endpoint.V2_COVERAGE);
    operation.setOption("by", "beneficiary");
    operation.setOption("pageSize", paging.isPagingRequested() ? "" + paging.getPageSize() : "*");
    operation.setOption("_lastUpdated", Boolean.toString(lastUpdated != null && !lastUpdated.isEmpty()));
    operation.publishOperationName();
    // Add bene_id to MDC logs
    TransformerUtilsV2.logBeneIdToMdc(Arrays.asList(beneficiary.getIdPart()));
    return TransformerUtilsV2.createBundle(paging, coverages, loadedFilterManager.getTransactionTime());
}
Also used : OffsetLinkBuilder(gov.cms.bfd.server.war.commons.OffsetLinkBuilder) NoResultException(javax.persistence.NoResultException) Operation(gov.cms.bfd.server.war.Operation) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) LinkedList(java.util.LinkedList) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) Trace(com.newrelic.api.agent.Trace) Search(ca.uhn.fhir.rest.annotation.Search)

Aggregations

Beneficiary (gov.cms.bfd.model.rif.Beneficiary)152 Test (org.junit.jupiter.api.Test)105 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)102 List (java.util.List)97 Arrays (java.util.Arrays)96 RequestHeaders (gov.cms.bfd.server.war.commons.RequestHeaders)94 TransformerConstants (gov.cms.bfd.server.war.commons.TransformerConstants)93 StaticRifResourceGroup (gov.cms.bfd.model.rif.samples.StaticRifResourceGroup)90 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)90 ServerTestUtils (gov.cms.bfd.server.war.ServerTestUtils)89 Collectors (java.util.stream.Collectors)87 PipelineTestUtils (gov.cms.bfd.pipeline.sharedutils.PipelineTestUtils)85 AfterEach (org.junit.jupiter.api.AfterEach)84 BeforeAll (org.junit.jupiter.api.BeforeAll)83 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)77 Instant (java.time.Instant)73 InvalidRequestException (ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)68 BeneficiaryHistory (gov.cms.bfd.model.rif.BeneficiaryHistory)68 Date (java.util.Date)67 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)67