Search in sources :

Example 1 with RequestIncompleteException

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

the class FhirBundleProcessor method createCriteriaList.

private List<CoverageRequirementRuleCriteria> createCriteriaList(CodeableConcept codeableConcept, List<Reference> insurance, List<Organization> payorList) {
    try {
        List<Coding> codings = codeableConcept.getCoding();
        if (codings.size() > 0) {
            logger.info("r4/FhirBundleProcessor::createCriteriaList: code[0]: " + codings.get(0).getCode() + " - " + codings.get(0).getSystem());
        } else {
            logger.info("r4/FhirBundleProcessor::createCriteriaList: empty codes list!");
        }
        List<Organization> payors = new ArrayList<>();
        if (insurance != null) {
            List<Coverage> coverages = insurance.stream().map(reference -> (Coverage) reference.getResource()).collect(Collectors.toList());
            // Remove null coverages that may not have resolved.
            coverages = coverages.stream().filter(coverage -> coverage != null).collect(Collectors.toList());
            payors = Utilities.getPayors(coverages);
        } else if (payorList != null) {
            payors = payorList;
        }
        if (payors.size() > 0) {
            logger.info("r4/FhirBundleProcessor::createCriteriaList: payer[0]: " + payors.get(0).getName());
        } else {
            // default to CMS if no payer was provided
            logger.info("r4/FhirBundleProcessor::createCriteriaList: empty payers list, working around by adding CMS!");
            Organization org = new Organization().setName("Centers for Medicare and Medicaid Services");
            // how to get ID
            org.setId("75f39025-65db-43c8-9127-693cdf75e712");
            payors.add(org);
        // remove the exception to use CMS if no payer is provided
        // JIRA ticket https://jira.mitre.org/browse/DMEERX-894
        // throw new RequestIncompleteException("No Payer found in coverage resource, cannot find documentation.");
        }
        List<CoverageRequirementRuleCriteria> criteriaList = CoverageRequirementRuleCriteria.createQueriesFromR4(codings, payors);
        return criteriaList;
    } catch (RequestIncompleteException e) {
        // rethrow incomplete request exceptions
        throw e;
    } catch (Exception e) {
        // catch all remaining exceptions
        System.out.println(e);
        throw new RequestIncompleteException("Unable to parse list of codes, codesystems, and payors from a device request.");
    }
}
Also used : Logger(org.slf4j.Logger) CoverageRequirementRuleCriteria(org.hl7.davinci.endpoint.rules.CoverageRequirementRuleCriteria) org.hl7.fhir.r4.model(org.hl7.fhir.r4.model) LoggerFactory(org.slf4j.LoggerFactory) RuleMapping(org.hl7.davinci.endpoint.database.RuleMapping) CqlExecutionContextBuilder(org.hl7.davinci.endpoint.cql.r4.CqlExecutionContextBuilder) FileStore(org.hl7.davinci.endpoint.files.FileStore) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) CoverageRequirementRuleResult(org.hl7.davinci.endpoint.rules.CoverageRequirementRuleResult) CqlRule(org.hl7.davinci.endpoint.cql.CqlRule) RequestIncompleteException(org.hl7.davinci.RequestIncompleteException) Utilities(org.hl7.davinci.r4.Utilities) CoverageRequirementRuleCriteria(org.hl7.davinci.endpoint.rules.CoverageRequirementRuleCriteria) RequestIncompleteException(org.hl7.davinci.RequestIncompleteException) ArrayList(java.util.ArrayList) RequestIncompleteException(org.hl7.davinci.RequestIncompleteException)

Example 2 with RequestIncompleteException

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

the class CdsService method handleRequest.

/**
 * Performs generic operations for incoming requests of any type.
 *
 * @param request the generically typed incoming request
 * @return The response from the server
 */
public CdsResponse handleRequest(@Valid @RequestBody requestTypeT request, URL applicationBaseUrl) {
    // create the RequestLog
    RequestLog requestLog = new RequestLog(request, new Date().getTime(), this.fhirComponents.getFhirVersion().toString(), this.id, requestService, 5);
    // Parsed request
    requestLog.advanceTimeline(requestService);
    PrefetchHydrator prefetchHydrator = new PrefetchHydrator(this, request, this.fhirComponents);
    prefetchHydrator.hydrate();
    // hydrated
    requestLog.advanceTimeline(requestService);
    // Attempt a Query Batch Request to backfill missing attributes.
    if (myConfig.isQueryBatchRequest()) {
        QueryBatchRequest qbr = new QueryBatchRequest(this.fhirComponents);
        this.attempQueryBatchRequest(request, qbr);
    }
    logger.info("***** ***** request from requestLog: " + requestLog.toString());
    CdsResponse response = new CdsResponse();
    Gson gson = new Gson();
    final String jsonObject = gson.toJson(request.getPrefetch());
    logger.info("Final populated CRDPrefetch: " + jsonObject);
    // CQL Fetched
    List<CoverageRequirementRuleResult> lookupResults;
    try {
        lookupResults = this.createCqlExecutionContexts(request, fileStore, applicationBaseUrl.toString() + "/");
        requestLog.advanceTimeline(requestService);
    } catch (RequestIncompleteException e) {
        logger.warn("RequestIncompleteException " + request);
        logger.warn(e.getMessage() + "; summary card sent to client");
        response.addCard(CardBuilder.summaryCard(CardTypes.COVERAGE, e.getMessage()));
        requestLog.setCardListFromCards(response.getCards());
        requestLog.setResults(e.getMessage());
        requestService.edit(requestLog);
        return response;
    }
    // process the extension for the configuration
    // load hook configuration with default values
    Configuration hookConfiguration = new Configuration();
    Extension extension = request.getExtension();
    if (extension != null) {
        if (extension.getConfiguration() != null) {
            hookConfiguration = extension.getConfiguration();
        }
    }
    boolean errorCardOnEmpty = !(request instanceof OrderSelectRequest);
    // no error cards on empty when order-select request
    boolean foundApplicableRule = false;
    for (CoverageRequirementRuleResult lookupResult : lookupResults) {
        requestLog.addTopic(requestService, lookupResult.getTopic());
        CqlResultsForCard results = executeCqlAndGetRelevantResults(lookupResult.getContext(), lookupResult.getTopic());
        CoverageRequirements coverageRequirements = results.getCoverageRequirements();
        if (results.ruleApplies()) {
            foundApplicableRule = true;
            if (results.getCoverageRequirements().getApplies()) {
                // if prior auth already approved
                if (coverageRequirements.isPriorAuthApproved()) {
                    response.addCard(CardBuilder.priorAuthCard(results, results.getRequest(), fhirComponents, coverageRequirements.getPriorAuthId(), request.getContext().getPatientId(), lookupResult.getCriteria().getPayorId(), request.getContext().getUserId(), applicationBaseUrl.toString() + "/fhir/" + fhirComponents.getFhirVersion().toString(), fhirResourceRepository));
                } else if (coverageRequirements.isDocumentationRequired() || coverageRequirements.isPriorAuthRequired()) {
                    if (StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireOrderUri()) || StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireFaceToFaceUri()) || StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireLabUri()) || StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireProgressNoteUri()) || StringUtils.isNotEmpty(coverageRequirements.getQuestionnairePARequestUri()) || StringUtils.isNotEmpty(coverageRequirements.getQuestionnairePlanOfCareUri()) || StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireDispenseUri()) || StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireAdditionalUri())) {
                        List<Link> smartAppLinks = createQuestionnaireLinks(request, applicationBaseUrl, lookupResult, results);
                        if (coverageRequirements.isPriorAuthRequired()) {
                            Card card = CardBuilder.transform(CardTypes.PRIOR_AUTH, results, smartAppLinks);
                            card.addSuggestionsItem(CardBuilder.createSuggestionWithNote(card, results.getRequest(), fhirComponents, "Save Update To EHR", "Update original " + results.getRequest().fhirType() + " to add note", true, CoverageGuidance.ADMIN));
                            response.addCard(card);
                        } else if (coverageRequirements.isDocumentationRequired()) {
                            Card card = CardBuilder.transform(CardTypes.DTR_CLIN, results, smartAppLinks);
                            card.addSuggestionsItem(CardBuilder.createSuggestionWithNote(card, results.getRequest(), fhirComponents, "Save Update To EHR", "Update original " + results.getRequest().fhirType() + " to add note", true, CoverageGuidance.CLINICAL));
                            response.addCard(card);
                        }
                        // add a card for an alternative therapy if there is one
                        if (results.getAlternativeTherapy().getApplies() && hookConfiguration.getAlternativeTherapy()) {
                            try {
                                response.addCard(CardBuilder.alternativeTherapyCard(results.getAlternativeTherapy(), results.getRequest(), fhirComponents));
                            } catch (RuntimeException e) {
                                logger.warn("Failed to process alternative therapy: " + e.getMessage());
                            }
                        }
                    } else {
                        logger.warn("Unspecified Questionnaire URI; summary card sent to client");
                        response.addCard(CardBuilder.transform(CardTypes.COVERAGE, results));
                    }
                } else {
                    // no prior auth or documentation required
                    logger.info("Add the no doc or prior auth required card");
                    Card card = CardBuilder.transform(CardTypes.COVERAGE, results);
                    card.addSuggestionsItem(CardBuilder.createSuggestionWithNote(card, results.getRequest(), fhirComponents, "Save Update To EHR", "Update original " + results.getRequest().fhirType() + " to add note", true, CoverageGuidance.COVERED));
                    card.setSelectionBehavior(Card.SelectionBehaviorEnum.ANY);
                    response.addCard(card);
                }
            }
            // apply the DrugInteractions
            if (results.getDrugInteraction().getApplies()) {
                response.addCard(CardBuilder.drugInteractionCard(results.getDrugInteraction(), results.getRequest()));
            }
        }
    }
    // CQL Executed
    requestLog.advanceTimeline(requestService);
    if (errorCardOnEmpty) {
        if (!foundApplicableRule) {
            String msg = "No documentation rules found";
            logger.warn(msg + "; summary card sent to client");
            response.addCard(CardBuilder.summaryCard(CardTypes.COVERAGE, msg));
        }
        CardBuilder.errorCardIfNonePresent(CardTypes.COVERAGE, response);
    }
    // Ading card to requestLog
    requestLog.setCardListFromCards(response.getCards());
    requestService.edit(requestLog);
    return response;
}
Also used : CqlResultsForCard(org.hl7.davinci.endpoint.components.CardBuilder.CqlResultsForCard) OrderSelectRequest(org.hl7.davinci.r4.crdhook.orderselect.OrderSelectRequest) PrefetchHydrator(org.hl7.davinci.endpoint.components.PrefetchHydrator) Gson(com.google.gson.Gson) Date(java.util.Date) CqlResultsForCard(org.hl7.davinci.endpoint.components.CardBuilder.CqlResultsForCard) DiscoveryExtension(org.hl7.davinci.r4.crdhook.DiscoveryExtension) RequestLog(org.hl7.davinci.endpoint.database.RequestLog) RequestIncompleteException(org.hl7.davinci.RequestIncompleteException) CoverageRequirementRuleResult(org.hl7.davinci.endpoint.rules.CoverageRequirementRuleResult) ArrayList(java.util.ArrayList) List(java.util.List) QueryBatchRequest(org.hl7.davinci.endpoint.components.QueryBatchRequest)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 RequestIncompleteException (org.hl7.davinci.RequestIncompleteException)2 CoverageRequirementRuleResult (org.hl7.davinci.endpoint.rules.CoverageRequirementRuleResult)2 Gson (com.google.gson.Gson)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Collectors (java.util.stream.Collectors)1 CqlResultsForCard (org.hl7.davinci.endpoint.components.CardBuilder.CqlResultsForCard)1 PrefetchHydrator (org.hl7.davinci.endpoint.components.PrefetchHydrator)1 QueryBatchRequest (org.hl7.davinci.endpoint.components.QueryBatchRequest)1 CqlRule (org.hl7.davinci.endpoint.cql.CqlRule)1 CqlExecutionContextBuilder (org.hl7.davinci.endpoint.cql.r4.CqlExecutionContextBuilder)1 RequestLog (org.hl7.davinci.endpoint.database.RequestLog)1 RuleMapping (org.hl7.davinci.endpoint.database.RuleMapping)1 FileStore (org.hl7.davinci.endpoint.files.FileStore)1 CoverageRequirementRuleCriteria (org.hl7.davinci.endpoint.rules.CoverageRequirementRuleCriteria)1 Utilities (org.hl7.davinci.r4.Utilities)1 DiscoveryExtension (org.hl7.davinci.r4.crdhook.DiscoveryExtension)1 OrderSelectRequest (org.hl7.davinci.r4.crdhook.orderselect.OrderSelectRequest)1