Search in sources :

Example 1 with CdsRequest

use of org.cdshooks.CdsRequest in project CRD by HL7-DaVinci.

the class QueryBatchRequest method performQueryBatchRequest.

/**
 * Backfills the missing required values of the response that prefetch may have missed.
 * This implementation pulls the IDs of the required references from the request object's draft
 * orders, checks which of those values are missing from the current CRD response, builds the
 * Query Batch JSON request using
 * http://build.fhir.org/ig/HL7/davinci-crd/hooks.html#fhir-resource-access,
 * then populates the CRD response with the response from the Query Batch.
 */
public void performQueryBatchRequest(CdsRequest<?, ?> cdsRequest, CrdPrefetch crdPrefetch) {
    logger.info("***** ***** Performing Query Batch Request.");
    CrdPrefetch crdResponse = crdPrefetch;
    // The list of references that should be queried in the batch request.
    List<String> requiredReferences = new ArrayList<String>();
    // Get the IDs of references in the request's draft orders.
    Bundle draftOrdersBundle = cdsRequest.getContext().getDraftOrders();
    // This assumes that only the first draft order is relevant.
    Resource initialRequestResource = draftOrdersBundle.getEntry().get(0).getResource();
    ResourceType requestType = initialRequestResource.getResourceType();
    // Extract the references by iterating through the JSON.
    Gson gson = new Gson();
    final JsonObject jsonObject = gson.toJsonTree(initialRequestResource).getAsJsonObject();
    for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
        FhirRequestProcessor.extractReferenceIds(requiredReferences, entry.getValue());
    }
    // Filter out references that already exist in the CRD Response.
    requiredReferences = requiredReferences.stream().filter(referenceId -> !crdResponse.containsRequestResourceId(referenceId)).collect(Collectors.toList());
    logger.info("References to query: " + requiredReferences);
    if (requiredReferences.isEmpty()) {
        logger.info("A Query Batch Request is not needed: all references have already already fetched.");
        return;
    }
    // Build the Query Batch Request JSON.
    Bundle queryBatchRequestBundle = buildQueryBatchRequestBundle(requiredReferences);
    String queryBatchRequestBody = FhirContext.forR4().newJsonParser().encodeResourceToString(queryBatchRequestBundle);
    // Make the query batch request to the EHR server.
    Bundle queryResponseBundle = null;
    try {
        logger.info("Executing Query Batch Request: " + queryBatchRequestBody);
        queryResponseBundle = (Bundle) FhirRequestProcessor.executeFhirQueryBody(queryBatchRequestBody, cdsRequest, this.fhirComponents, HttpMethod.POST);
        queryResponseBundle = extractNestedBundledResources(queryResponseBundle);
        logger.info("Extracted Query Batch Resources: " + (queryResponseBundle).getEntry().stream().map(entry -> entry.getResource()).collect(Collectors.toList()));
    } catch (Exception e) {
        logger.error("Failed to backfill prefetch with Query Batch Request " + queryBatchRequestBody, e);
    }
    if (queryResponseBundle == null) {
        logger.error("No response recieved from the Query Batch Request.");
        return;
    }
    // Add the request resource to the query batch response as it may be missing.
    // Coverage and Subject are not automatically being
    // linked to the request object. It seems to somehow automatically link during
    // standard prefetch, but not here so we're doing it manually.
    List<Coverage> coverages = FhirRequestProcessor.extractCoverageFromBundle(queryResponseBundle);
    List<Patient> patients = FhirRequestProcessor.extractPatientsFromBundle(queryResponseBundle);
    FhirRequestProcessor.addInsuranceAndSubject(initialRequestResource, patients, coverages);
    BundleEntryComponent newEntry = new BundleEntryComponent();
    newEntry.setResource(initialRequestResource);
    queryResponseBundle.addEntry(newEntry);
    // Add the query batch response resources to the CRD Prefetch request.
    logger.info("Query Batch Response Entries: " + queryResponseBundle.getEntry());
    FhirRequestProcessor.addToCrdPrefetchRequest(crdResponse, requestType, queryResponseBundle.getEntry());
    logger.info("Post-Query Batch CRDResponse: " + crdResponse);
}
Also used : CrdPrefetch(org.hl7.davinci.r4.crdhook.CrdPrefetch) JsonObject(com.google.gson.JsonObject) Logger(org.slf4j.Logger) FhirRequestProcessor(org.hl7.davinci.endpoint.cdshooks.services.crd.r4.FhirRequestProcessor) LoggerFactory(org.slf4j.LoggerFactory) HttpMethod(org.springframework.http.HttpMethod) Resource(org.hl7.fhir.r4.model.Resource) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HTTPVerb(org.hl7.fhir.r4.model.Bundle.HTTPVerb) JsonElement(com.google.gson.JsonElement) FhirComponentsT(org.hl7.davinci.FhirComponentsT) BundleEntryRequestComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryRequestComponent) ResourceType(org.hl7.fhir.r4.model.ResourceType) FhirContext(ca.uhn.fhir.context.FhirContext) List(java.util.List) Gson(com.google.gson.Gson) Map(java.util.Map) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.r4.model.Bundle) CdsRequest(org.cdshooks.CdsRequest) BundleType(org.hl7.fhir.r4.model.Bundle.BundleType) Coverage(org.hl7.fhir.r4.model.Coverage) Patient(org.hl7.fhir.r4.model.Patient) Bundle(org.hl7.fhir.r4.model.Bundle) ArrayList(java.util.ArrayList) Resource(org.hl7.fhir.r4.model.Resource) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) Patient(org.hl7.fhir.r4.model.Patient) ResourceType(org.hl7.fhir.r4.model.ResourceType) Coverage(org.hl7.fhir.r4.model.Coverage) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) JsonElement(com.google.gson.JsonElement) CrdPrefetch(org.hl7.davinci.r4.crdhook.CrdPrefetch) Map(java.util.Map)

Aggregations

FhirContext (ca.uhn.fhir.context.FhirContext)1 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 CdsRequest (org.cdshooks.CdsRequest)1 FhirComponentsT (org.hl7.davinci.FhirComponentsT)1 FhirRequestProcessor (org.hl7.davinci.endpoint.cdshooks.services.crd.r4.FhirRequestProcessor)1 CrdPrefetch (org.hl7.davinci.r4.crdhook.CrdPrefetch)1 Bundle (org.hl7.fhir.r4.model.Bundle)1 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)1 BundleEntryRequestComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryRequestComponent)1 BundleType (org.hl7.fhir.r4.model.Bundle.BundleType)1 HTTPVerb (org.hl7.fhir.r4.model.Bundle.HTTPVerb)1 Coverage (org.hl7.fhir.r4.model.Coverage)1 Patient (org.hl7.fhir.r4.model.Patient)1 Resource (org.hl7.fhir.r4.model.Resource)1