use of org.hl7.fhir.r4.model.Bundle.SearchEntryMode.MATCH in project beneficiary-fhir-data by CMSgov.
the class TransformerUtils method addCareTeamPractitioner.
/**
* Ensures that the specified {@link ExplanationOfBenefit} has the specified {@link
* CareTeamComponent}, and links the specified {@link ItemComponent} to that {@link
* CareTeamComponent} (via {@link ItemComponent#addCareTeamLinkId(int)}).
*
* @param eob the {@link ExplanationOfBenefit} that the {@link CareTeamComponent} should be part
* of
* @param eobItem the {@link ItemComponent} that should be linked to the {@link CareTeamComponent}
* @param practitionerIdSystem the {@link Identifier#getSystem()} of the practitioner to reference
* in {@link CareTeamComponent#getProvider()}
* @param practitionerIdValue the {@link Identifier#getValue()} of the practitioner to reference
* in {@link CareTeamComponent#getProvider()}
* @param careTeamRole the {@link ClaimCareteamrole} to use for the {@link
* CareTeamComponent#getRole()}
* @return the {@link CareTeamComponent} that was created/linked
*/
static CareTeamComponent addCareTeamPractitioner(ExplanationOfBenefit eob, ItemComponent eobItem, String practitionerIdSystem, String practitionerIdValue, ClaimCareteamrole careTeamRole) {
// Try to find a matching pre-existing entry.
CareTeamComponent careTeamEntry = eob.getCareTeam().stream().filter(ctc -> ctc.getProvider().hasIdentifier()).filter(ctc -> practitionerIdSystem.equals(ctc.getProvider().getIdentifier().getSystem()) && practitionerIdValue.equals(ctc.getProvider().getIdentifier().getValue())).filter(ctc -> ctc.hasRole()).filter(ctc -> careTeamRole.toCode().equals(ctc.getRole().getCodingFirstRep().getCode()) && careTeamRole.getSystem().equals(ctc.getRole().getCodingFirstRep().getSystem())).findAny().orElse(null);
// If no match was found, add one to the EOB.
if (careTeamEntry == null) {
careTeamEntry = eob.addCareTeam();
careTeamEntry.setSequence(eob.getCareTeam().size() + 1);
careTeamEntry.setProvider(createIdentifierReference(practitionerIdSystem, practitionerIdValue));
CodeableConcept careTeamRoleConcept = createCodeableConcept(ClaimCareteamrole.OTHER.getSystem(), careTeamRole.toCode());
careTeamRoleConcept.getCodingFirstRep().setDisplay(careTeamRole.getDisplay());
careTeamEntry.setRole(careTeamRoleConcept);
}
// care team entry is at eob level so no need to create item link id
if (eobItem == null) {
return careTeamEntry;
}
// Link the EOB.item to the care team entry (if it isn't already).
final int careTeamEntrySequence = careTeamEntry.getSequence();
if (eobItem.getCareTeamLinkId().stream().noneMatch(id -> id.getValue() == careTeamEntrySequence)) {
eobItem.addCareTeamLinkId(careTeamEntrySequence);
}
return careTeamEntry;
}
use of org.hl7.fhir.r4.model.Bundle.SearchEntryMode.MATCH in project beneficiary-fhir-data by CMSgov.
the class TransformerUtilsV2 method mapEobType.
/**
* maps a blue button claim type to a FHIR claim type
*
* @param eob the {@link CodeableConcept} that will get remapped
* @param blueButtonClaimType the blue button {@link ClaimTypeV2} we are mapping from
* @param ccwNearLineRecordIdCode if present, the blue button near line id code {@link
* Optional}<{@link Character}> gets remapped to a ccw record id code
* @param ccwClaimTypeCode if present, the blue button claim type code {@link Optional}<{@link
* String}> gets remapped to a nch claim type code
*/
static void mapEobType(ExplanationOfBenefit eob, ClaimTypeV2 blueButtonClaimType, Optional<Character> ccwNearLineRecordIdCode, Optional<String> ccwClaimTypeCode) {
// NCH_CLM_TYPE_CD => ExplanationOfBenefit.type.coding
if (ccwClaimTypeCode.isPresent()) {
eob.getType().addCoding(createCoding(eob, CcwCodebookVariable.NCH_CLM_TYPE_CD, ccwClaimTypeCode));
}
// This Coding MUST always be present as it's the only one we can definitely map
// for all 8 of our claim types.
// EOB Type => ExplanationOfBenefit.type.coding
eob.getType().addCoding().setSystem(TransformerConstants.CODING_SYSTEM_BBAPI_EOB_TYPE).setCode(blueButtonClaimType.name());
// Map a Coding for FHIR's ClaimType coding system, if we can.
org.hl7.fhir.r4.model.codesystems.ClaimType fhirClaimType;
switch(blueButtonClaimType) {
case PDE:
fhirClaimType = org.hl7.fhir.r4.model.codesystems.ClaimType.PHARMACY;
break;
case INPATIENT:
case OUTPATIENT:
case HOSPICE:
case SNF:
case DME:
fhirClaimType = org.hl7.fhir.r4.model.codesystems.ClaimType.INSTITUTIONAL;
break;
case CARRIER:
case HHA:
fhirClaimType = org.hl7.fhir.r4.model.codesystems.ClaimType.PROFESSIONAL;
break;
default:
// All options on ClaimTypeV2 are covered above, but this is there to appease linter
throw new BadCodeMonkeyException("No match found for ClaimTypeV2");
}
// Claim Type => ExplanationOfBenefit.type.coding
if (fhirClaimType != null) {
eob.getType().addCoding(new Coding(fhirClaimType.getSystem(), fhirClaimType.toCode(), fhirClaimType.getDisplay()));
}
// NCH_NEAR_LINE_REC_IDENT_CD => ExplanationOfBenefit.extension
if (ccwNearLineRecordIdCode.isPresent()) {
eob.addExtension(createExtensionCoding(eob, CcwCodebookVariable.NCH_NEAR_LINE_REC_IDENT_CD, ccwNearLineRecordIdCode));
}
}
use of org.hl7.fhir.r4.model.Bundle.SearchEntryMode.MATCH in project beneficiary-fhir-data by CMSgov.
the class PatientResourceProvider method queryDatabaseByHash.
/**
* @param hash the {@link Beneficiary} hash value to match
* @param hashType a string to represent the hash type (used for logging purposes)
* @param requestHeader the {@link #RequestHeaders} where resource request headers are
* encapsulated
* @param beneficiaryHashField the JPA location of the beneficiary hash field
* @param beneficiaryHistoryHashField the JPA location of the beneficiary history hash field
* @return a FHIR {@link Patient} for the CCW {@link Beneficiary} that matches the specified
* {@link Beneficiary} hash value
* @throws NoResultException A {@link NoResultException} will be thrown if no matching {@link
* Beneficiary} can be found
*/
@Trace
private Patient queryDatabaseByHash(String hash, String hashType, SingularAttribute<Beneficiary, String> beneficiaryHashField, SingularAttribute<BeneficiaryHistory, String> beneficiaryHistoryHashField, RequestHeaders requestHeader) {
if (hash == null || hash.trim().isEmpty())
throw new IllegalArgumentException();
/*
* Beneficiaries' HICN/MBIs can change over time and those past HICN/MBIs may land in
* BeneficiaryHistory records. Accordingly, we need to search for matching HICN/MBIs in both the
* Beneficiary and the BeneficiaryHistory records.
*
* There's no sane way to do this in a single query with JPA 2.1, it appears: JPA doesn't
* support UNIONs and it doesn't support subqueries in FROM clauses. That said, the ideal query
* would look like this:
*
* SELECT * FROM ( SELECT DISTINCT "beneficiaryId" FROM "Beneficiaries" WHERE "hicn" =
* :'hicn_hash' UNION SELECT DISTINCT "beneficiaryId" FROM "BeneficiariesHistory" WHERE "hicn" =
* :'hicn_hash') AS matching_benes INNER JOIN "Beneficiaries" ON matching_benes."beneficiaryId"
* = "Beneficiaries"."beneficiaryId" LEFT JOIN "BeneficiariesHistory" ON
* "Beneficiaries"."beneficiaryId" = "BeneficiariesHistory"."beneficiaryId" LEFT JOIN
* "MedicareBeneficiaryIdHistory" ON "Beneficiaries"."beneficiaryId" =
* "MedicareBeneficiaryIdHistory"."beneficiaryId";
*
* ... with the returned columns and JOINs being dynamic, depending on IncludeIdentifiers.
*
* In lieu of that, we run two queries: one to find HICN/MBI matches in BeneficiariesHistory,
* and a second to find BENE_ID or HICN/MBI matches in Beneficiaries (with all of their data, so
* we're ready to return the result). This is bad and dumb but I can't find a better working
* alternative.
*
* (I'll just note that I did also try JPA/Hibernate native SQL queries but couldn't get the
* joins or fetch groups to work with them.)
*
* If we want to fix this, we need to move identifiers out entirely to separate tables:
* BeneficiaryHicns and BeneficiaryMbis. We could then safely query these tables and join them
* back to Beneficiaries (and hopefully the optimizer will play nice, too).
*/
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
// First, find all matching hashes from BeneficiariesHistory.
CriteriaQuery<String> beneHistoryMatches = builder.createQuery(String.class);
Root<BeneficiaryHistory> beneHistoryMatchesRoot = beneHistoryMatches.from(BeneficiaryHistory.class);
beneHistoryMatches.select(beneHistoryMatchesRoot.get(BeneficiaryHistory_.beneficiaryId));
beneHistoryMatches.where(builder.equal(beneHistoryMatchesRoot.get(beneficiaryHistoryHashField), hash));
List<String> matchingIdsFromBeneHistory = null;
Long hicnsFromHistoryQueryNanoSeconds = null;
Timer.Context beneHistoryMatchesTimer = metricRegistry.timer(MetricRegistry.name(getClass().getSimpleName(), "query", "bene_by_" + hashType, hashType + "s_from_beneficiarieshistory")).time();
try {
matchingIdsFromBeneHistory = entityManager.createQuery(beneHistoryMatches).getResultList();
} finally {
hicnsFromHistoryQueryNanoSeconds = beneHistoryMatchesTimer.stop();
TransformerUtils.recordQueryInMdc("bene_by_" + hashType + "." + hashType + "s_from_beneficiarieshistory", hicnsFromHistoryQueryNanoSeconds, matchingIdsFromBeneHistory == null ? 0 : matchingIdsFromBeneHistory.size());
}
// Then, find all Beneficiary records that match the hash or those BENE_IDs.
CriteriaQuery<Beneficiary> beneMatches = builder.createQuery(Beneficiary.class);
Root<Beneficiary> beneMatchesRoot = beneMatches.from(Beneficiary.class);
beneMatchesRoot.fetch(Beneficiary_.skippedRifRecords, JoinType.LEFT);
if (requestHeader.isHICNinIncludeIdentifiers())
beneMatchesRoot.fetch(Beneficiary_.beneficiaryHistories, JoinType.LEFT);
if (requestHeader.isMBIinIncludeIdentifiers())
beneMatchesRoot.fetch(Beneficiary_.medicareBeneficiaryIdHistories, JoinType.LEFT);
beneMatches.select(beneMatchesRoot);
Predicate beneHashMatches = builder.equal(beneMatchesRoot.get(beneficiaryHashField), hash);
if (matchingIdsFromBeneHistory != null && !matchingIdsFromBeneHistory.isEmpty()) {
Predicate beneHistoryHashMatches = beneMatchesRoot.get(Beneficiary_.beneficiaryId).in(matchingIdsFromBeneHistory);
beneMatches.where(builder.or(beneHashMatches, beneHistoryHashMatches));
} else {
beneMatches.where(beneHashMatches);
}
List<Beneficiary> matchingBenes = Collections.emptyList();
Long benesByHashOrIdQueryNanoSeconds = null;
Timer.Context timerHicnQuery = metricRegistry.timer(MetricRegistry.name(getClass().getSimpleName(), "query", "bene_by_" + hashType, "bene_by_" + hashType + "_or_id")).time();
try {
matchingBenes = entityManager.createQuery(beneMatches).getResultList();
} finally {
benesByHashOrIdQueryNanoSeconds = timerHicnQuery.stop();
TransformerUtils.recordQueryInMdc(String.format("bene_by_" + hashType + ".bene_by_" + hashType + "_or_id.include_%s", String.join("_", (List<String>) requestHeader.getValue(HEADER_NAME_INCLUDE_IDENTIFIERS))), benesByHashOrIdQueryNanoSeconds, matchingBenes.size());
}
// Then, if we found more than one distinct BENE_ID, or none, throw an error.
long distinctBeneIds = matchingBenes.stream().map(Beneficiary::getBeneficiaryId).filter(Objects::nonNull).distinct().count();
Beneficiary beneficiary = null;
if (distinctBeneIds <= 0) {
throw new NoResultException();
} else if (distinctBeneIds > 1) {
MDC.put("database_query.by_hash.collision.distinct_bene_ids", Long.toString(distinctBeneIds));
throw new ResourceNotFoundException("By hash query found more than one distinct BENE_ID: " + Long.toString(distinctBeneIds));
} else if (distinctBeneIds == 1) {
beneficiary = matchingBenes.get(0);
}
// Null out the unhashed HICNs if we're not supposed to be returning them
if (!requestHeader.isHICNinIncludeIdentifiers()) {
beneficiary.setHicnUnhashed(Optional.empty());
}
// Null out the unhashed MBIs if we're not supposed to be returning
if (!requestHeader.isMBIinIncludeIdentifiers()) {
beneficiary.setMedicareBeneficiaryId(Optional.empty());
}
Patient patient = BeneficiaryTransformer.transform(metricRegistry, beneficiary, requestHeader);
return patient;
}
use of org.hl7.fhir.r4.model.Bundle.SearchEntryMode.MATCH 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.r4.model.Bundle.SearchEntryMode.MATCH in project beneficiary-fhir-data by CMSgov.
the class R4PatientResourceProvider 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:
*
* <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.V2_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_MBI_HASH:
patient = queryDatabaseByMbiHash(identifier.getValue(), requestHeader);
break;
case TransformerConstants.CODING_BBAPI_BENE_HICN_HASH:
case TransformerConstants.CODING_BBAPI_BENE_HICN_HASH_OLD:
patient = queryDatabaseByHicnHash(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?");
return TransformerUtilsV2.createBundle(paging, patients, loadedFilterManager.getTransactionTime());
}
Aggregations