Search in sources :

Example 1 with PrefetchTemplateElement

use of org.hl7.davinci.PrefetchTemplateElement in project CRD by HL7-DaVinci.

the class PrefetchHydrator method hydrate.

/**
 * Attempt to hydrate missing prefetch elements, note that this modifies the request object.
 */
public void hydrate() {
    Object crdResponse = cdsRequest.getPrefetch();
    for (PrefetchTemplateElement prefetchElement : cdsService.getPrefetchElements()) {
        String prefetchKey = prefetchElement.getKey();
        // check if the prefetch has already been populated with that key
        Boolean alreadyIncluded = false;
        try {
            alreadyIncluded = (PropertyUtils.getProperty(crdResponse, prefetchKey) != null);
        } catch (Exception e) {
            throw new RuntimeException("System error: Mismatch in prefetch keys between the " + "CrdPrefetch and the key templates set in the service.", e);
        }
        if (!alreadyIncluded) {
            // check if the bundle actually has element
            String prefetchQuery = cdsService.prefetch.get(prefetchKey);
            String hydratedPrefetchQuery = hydratePrefetchQuery(prefetchQuery);
            // e.g. this could be a query template for a medication order but we have a device request
            if (hydratedPrefetchQuery != null) {
                if (cdsRequest.getFhirServer() == null) {
                    throw new FatalRequestIncompleteException("Attempting to fill the prefetch, but no fhir " + "server provided. Either provide a full prefetch or provide a fhir server.");
                }
                try {
                    PropertyUtils.setProperty(crdResponse, prefetchKey, prefetchElement.getReturnType().cast(FhirRequestProcessor.executeFhirQueryUrl(hydratedPrefetchQuery, cdsRequest, fhirComponents, HttpMethod.GET)));
                } catch (Exception e) {
                    logger.warn("Failed to fill prefetch for key: " + prefetchKey, e);
                }
            }
        }
    }
}
Also used : FatalRequestIncompleteException(org.hl7.davinci.FatalRequestIncompleteException) PrefetchTemplateElement(org.hl7.davinci.PrefetchTemplateElement) FatalRequestIncompleteException(org.hl7.davinci.FatalRequestIncompleteException)

Aggregations

FatalRequestIncompleteException (org.hl7.davinci.FatalRequestIncompleteException)1 PrefetchTemplateElement (org.hl7.davinci.PrefetchTemplateElement)1