use of org.hl7.fhir.dstu2.model.Bundle in project beneficiary-fhir-data by CMSgov.
the class TransformerUtils method addResourcesToBundle.
/**
* @param bundle a {@link Bundle} to add the list of {@link ExplanationOfBenefit} resources to.
* @param resources a list of either {@link ExplanationOfBenefit}s, {@link Coverage}s, or {@link
* Patient}s, of which a portion will be added to the bundle based on the paging values
* @return Returns a {@link Bundle} of {@link ExplanationOfBenefit}s, {@link Coverage}s, or {@link
* Patient}s, which may contain multiple matching resources, or may also be empty.
*/
public static Bundle addResourcesToBundle(Bundle bundle, List<IBaseResource> resources) {
Set<String> beneIds = new HashSet<String>();
for (IBaseResource res : resources) {
BundleEntryComponent entry = bundle.addEntry();
entry.setResource((Resource) res);
if (entry.getResource().getResourceType() == ResourceType.ExplanationOfBenefit) {
ExplanationOfBenefit eob = ((ExplanationOfBenefit) entry.getResource());
if (eob != null && eob.getPatient() != null && !Strings.isNullOrEmpty(eob.getPatient().getReference())) {
String reference = eob.getPatient().getReference().replace("Patient/", "");
if (!Strings.isNullOrEmpty(reference)) {
beneIds.add(reference);
}
}
} else if (entry.getResource().getResourceType() == ResourceType.Patient) {
Patient patient = ((Patient) entry.getResource());
if (patient != null && !Strings.isNullOrEmpty(patient.getId())) {
beneIds.add(patient.getId());
}
} else if (entry.getResource().getResourceType() == ResourceType.Coverage) {
Coverage coverage = ((Coverage) entry.getResource());
if (coverage != null && coverage.getBeneficiary() != null && !Strings.isNullOrEmpty(coverage.getBeneficiary().getReference())) {
String reference = coverage.getBeneficiary().getReference().replace("Patient/", "");
if (!Strings.isNullOrEmpty(reference)) {
beneIds.add(reference);
}
}
}
}
logBeneIdToMdc(beneIds);
return bundle;
}
use of org.hl7.fhir.dstu2.model.Bundle in project beneficiary-fhir-data by CMSgov.
the class ExplanationOfBenefitResourceProvider method findByPatient.
/**
* Adds support for the FHIR "search" operation for {@link ExplanationOfBenefit}s, allowing users
* to search by {@link ExplanationOfBenefit#getPatient()}.
*
* <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 patient a {@link ReferenceParam} for the {@link ExplanationOfBenefit#getPatient()} to
* try and find matches for {@link ExplanationOfBenefit}s
* @param type a list of {@link ClaimType} to include in the result. Defaults to all types.
* @param startIndex an {@link OptionalParam} for the startIndex (or offset) used to determine
* pagination
* @param excludeSamhsa an {@link OptionalParam} that, if <code>"true"</code>, will use {@link
* Stu3EobSamhsaMatcher} to filter out all SAMHSA-related claims from the results
* @param lastUpdated an {@link OptionalParam} that specifies a date range for the lastUpdated
* field.
* @param serviceDate an {@link OptionalParam} that specifies a date range for {@link
* ExplanationOfBenefit}s that completed
* @param requestDetails a {@link RequestDetails} containing the details of the request URL, used
* to parse out pagination values
* @return Returns a {@link Bundle} of {@link ExplanationOfBenefit}s, which may contain multiple
* matching resources, or may also be empty.
*/
@Search
@Trace
public Bundle findByPatient(@RequiredParam(name = ExplanationOfBenefit.SP_PATIENT) @Description(shortDefinition = "The patient identifier to search for") ReferenceParam patient, @OptionalParam(name = "type") @Description(shortDefinition = "A list of claim types to include") TokenAndListParam type, @OptionalParam(name = "startIndex") @Description(shortDefinition = "The offset used for result pagination") String startIndex, @OptionalParam(name = "excludeSAMHSA") @Description(shortDefinition = "If true, exclude all SAMHSA-related resources") String excludeSamhsa, @OptionalParam(name = "_lastUpdated") @Description(shortDefinition = "Include resources last updated in the given range") DateRangeParam lastUpdated, @OptionalParam(name = "service-date") @Description(shortDefinition = "Include resources that completed in the given range") DateRangeParam serviceDate, RequestDetails requestDetails) {
/*
* startIndex is an optional parameter here because it must be declared in the
* event it is passed in. However, it is not being used here because it is also
* contained within requestDetails and parsed out along with other parameters
* later.
*/
String beneficiaryId = patient.getIdPart();
Set<ClaimType> claimTypes = parseTypeParam(type);
Boolean includeTaxNumbers = returnIncludeTaxNumbers(requestDetails);
OffsetLinkBuilder paging = new OffsetLinkBuilder(requestDetails, "/ExplanationOfBenefit?");
Operation operation = new Operation(Operation.Endpoint.V1_EOB);
operation.setOption("by", "patient");
operation.setOption("types", (claimTypes.size() == ClaimType.values().length) ? "*" : claimTypes.stream().sorted(Comparator.comparing(ClaimType::name)).collect(Collectors.toList()).toString());
operation.setOption("IncludeTaxNumbers", "" + includeTaxNumbers);
operation.setOption("pageSize", paging.isPagingRequested() ? "" + paging.getPageSize() : "*");
operation.setOption("_lastUpdated", Boolean.toString(lastUpdated != null && !lastUpdated.isEmpty()));
operation.setOption("service-date", Boolean.toString(serviceDate != null && !serviceDate.isEmpty()));
operation.publishOperationName();
List<IBaseResource> eobs = new ArrayList<IBaseResource>();
// Optimize when the lastUpdated parameter is specified and result set is empty
if (loadedFilterManager.isResultSetEmpty(beneficiaryId, lastUpdated)) {
return TransformerUtils.createBundle(paging, eobs, loadedFilterManager.getTransactionTime());
}
/*
* The way our JPA/SQL schema is setup, we have to run a separate search for
* each claim type, then combine the results. It's not super efficient, but it's
* also not so inefficient that it's worth fixing.
*/
if (claimTypes.contains(ClaimType.CARRIER))
eobs.addAll(transformToEobs(ClaimType.CARRIER, findClaimTypeByPatient(ClaimType.CARRIER, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
if (claimTypes.contains(ClaimType.DME))
eobs.addAll(transformToEobs(ClaimType.DME, findClaimTypeByPatient(ClaimType.DME, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
if (claimTypes.contains(ClaimType.HHA))
eobs.addAll(transformToEobs(ClaimType.HHA, findClaimTypeByPatient(ClaimType.HHA, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
if (claimTypes.contains(ClaimType.HOSPICE))
eobs.addAll(transformToEobs(ClaimType.HOSPICE, findClaimTypeByPatient(ClaimType.HOSPICE, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
if (claimTypes.contains(ClaimType.INPATIENT))
eobs.addAll(transformToEobs(ClaimType.INPATIENT, findClaimTypeByPatient(ClaimType.INPATIENT, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
if (claimTypes.contains(ClaimType.OUTPATIENT))
eobs.addAll(transformToEobs(ClaimType.OUTPATIENT, findClaimTypeByPatient(ClaimType.OUTPATIENT, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
if (claimTypes.contains(ClaimType.PDE))
eobs.addAll(transformToEobs(ClaimType.PDE, findClaimTypeByPatient(ClaimType.PDE, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
if (claimTypes.contains(ClaimType.SNF))
eobs.addAll(transformToEobs(ClaimType.SNF, findClaimTypeByPatient(ClaimType.SNF, beneficiaryId, lastUpdated, serviceDate), Optional.of(includeTaxNumbers)));
if (Boolean.parseBoolean(excludeSamhsa))
filterSamhsa(eobs);
eobs.sort(ExplanationOfBenefitResourceProvider::compareByClaimIdThenClaimType);
// Add bene_id to MDC logs
TransformerUtils.logBeneIdToMdc(Arrays.asList(beneficiaryId));
return TransformerUtils.createBundle(paging, eobs, loadedFilterManager.getTransactionTime());
}
use of org.hl7.fhir.dstu2.model.Bundle in project beneficiary-fhir-data by CMSgov.
the class AbstractR4ResourceProvider method createBundleFor.
/**
* Creates a Bundle of resources for the given data using the given {@link ResourceTypeV2}.
*
* @param resourceTypes The {@link ResourceTypeV2} data to retrieve.
* @param mbi The mbi to look up associated data for.
* @param isHashed Denotes if the given mbi is hashed.
* @param lastUpdated Date range of desired lastUpdate values to retrieve data for.
* @param serviceDate Date range of the desired service date to retrieve data for.
* @return A Bundle with data found using the provided parameters.
*/
@VisibleForTesting
Bundle createBundleFor(Set<ResourceTypeV2<T>> resourceTypes, String mbi, boolean isHashed, boolean excludeSamhsa, DateRangeParam lastUpdated, DateRangeParam serviceDate) {
List<T> resources = new ArrayList<>();
for (ResourceTypeV2<T> type : resourceTypes) {
List<?> entities;
entities = claimDao.findAllByMbiAttribute(type.getEntityClass(), type.getEntityMbiRecordAttribute(), mbi, isHashed, lastUpdated, serviceDate, type.getEntityEndDateAttribute());
resources.addAll(entities.stream().filter(e -> !excludeSamhsa || hasNoSamhsaData(metricRegistry, e)).map(e -> type.getTransformer().transform(metricRegistry, e)).collect(Collectors.toList()));
}
Bundle bundle = new Bundle();
resources.forEach(c -> {
Bundle.BundleEntryComponent entry = bundle.addEntry();
entry.setResource((Resource) c);
});
return bundle;
}
use of org.hl7.fhir.dstu2.model.Bundle in project beneficiary-fhir-data by CMSgov.
the class PatientResourceProvider method searchByIdentifier.
/**
* Adds support for the FHIR "search" operation for {@link Patient}s, allowing users to search by
* {@link Patient#getIdentifier()}. Specifically, the following criteria are supported:
*
* <ul>
* <li>Matching a {@link Beneficiary#getHicn()} hash value: when {@link TokenParam#getSystem()}
* matches one of the {@link #SUPPORTED_HASH_IDENTIFIER_SYSTEMS} entries.
* </ul>
*
* <p>Searches that don't match one of the above forms are not supported.
*
* <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 identifier an {@link Identifier} {@link TokenParam} for the {@link
* Patient#getIdentifier()} to try and find a matching {@link Patient} 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 Patient}s, which may contain multiple matching
* resources, or may also be empty.
*/
@Search
@Trace
public Bundle searchByIdentifier(@RequiredParam(name = Patient.SP_IDENTIFIER) @Description(shortDefinition = "The patient identifier to search for") TokenParam identifier, @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) {
if (identifier.getQueryParameterQualifier() != null)
throw new InvalidRequestException("Unsupported query parameter qualifier: " + identifier.getQueryParameterQualifier());
if (!SUPPORTED_HASH_IDENTIFIER_SYSTEMS.contains(identifier.getSystem()))
throw new InvalidRequestException("Unsupported identifier system: " + identifier.getSystem());
RequestHeaders requestHeader = RequestHeaders.getHeaderWrapper(requestDetails);
Operation operation = new Operation(Operation.Endpoint.V1_PATIENT);
operation.setOption("by", "identifier");
requestHeader.getNVPairs().forEach((n, v) -> operation.setOption(n, v.toString()));
operation.setOption("_lastUpdated", Boolean.toString(lastUpdated != null && !lastUpdated.isEmpty()));
operation.publishOperationName();
List<IBaseResource> patients;
try {
Patient patient;
switch(identifier.getSystem()) {
case TransformerConstants.CODING_BBAPI_BENE_HICN_HASH:
case TransformerConstants.CODING_BBAPI_BENE_HICN_HASH_OLD:
patient = queryDatabaseByHicnHash(identifier.getValue(), requestHeader);
break;
case TransformerConstants.CODING_BBAPI_BENE_MBI_HASH:
patient = queryDatabaseByMbiHash(identifier.getValue(), requestHeader);
break;
default:
throw new InvalidRequestException("Unsupported identifier system: " + identifier.getSystem());
}
patients = QueryUtils.isInRange(patient.getMeta().getLastUpdated().toInstant(), lastUpdated) ? Collections.singletonList(patient) : Collections.emptyList();
} catch (NoResultException e) {
patients = new LinkedList<>();
}
OffsetLinkBuilder paging = new OffsetLinkBuilder(requestDetails, "/Patient?");
Bundle bundle = TransformerUtils.createBundle(paging, patients, loadedFilterManager.getTransactionTime());
return bundle;
}
use of org.hl7.fhir.dstu2.model.Bundle in project beneficiary-fhir-data by CMSgov.
the class CoverageResourceProvider 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 = CoverageTransformer.transform(metricRegistry, beneficiaryEntity);
} catch (NoResultException e) {
coverages = new LinkedList<IBaseResource>();
}
OffsetLinkBuilder paging = new OffsetLinkBuilder(requestDetails, "/Coverage?");
Operation operation = new Operation(Operation.Endpoint.V1_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
TransformerUtils.logBeneIdToMdc(Arrays.asList(beneficiary.getIdPart()));
return TransformerUtils.createBundle(paging, coverages, loadedFilterManager.getTransactionTime());
}
Aggregations