Search in sources :

Example 51 with Questionnaire

use of org.hl7.fhir.r4.model.Questionnaire in project CRD by HL7-DaVinci.

the class QuestionnaireValueSetProcessor method findAndReplaceValueSetReferences.

/**
 * Recursively visits every questionnaire item and replaces every `answerValueSet` that isn't a local
 * hash (#) reference into a local reference. This fills the valueSetMap with the loaded valuesets.
 *
 * @param itemComponents The item components to visit.
 * @param valueSetMap A mapping of ValueSet urls to loaded valuesets that should be filled as references are found.
 * @param fileStore The file store that is used to load valuesets from.
 * @param baseUrl The base url of the server from the request. Used to identify local valuesets.
 */
private void findAndReplaceValueSetReferences(List<QuestionnaireItemComponent> itemComponents, Map<String, ValueSet> valueSetMap, FileStore fileStore, String baseUrl) {
    for (QuestionnaireItemComponent itemComponent : itemComponents) {
        // If there is an answerValueSet field we need to do some work on this item
        if (itemComponent.hasAnswerValueSet()) {
            // Only look for a valueset to embed if it does not appear to be a hash reference
            if (!itemComponent.getAnswerValueSet().startsWith("#")) {
                logger.info("answerValueSet found with url - " + itemComponent.getAnswerValueSet());
                String valueSetId = findAndLoadValueSet(itemComponent.getAnswerValueSet(), valueSetMap, fileStore, baseUrl);
                if (valueSetId != null) {
                    itemComponent.getAnswerValueSet();
                    itemComponent.setAnswerValueSet("#" + valueSetId);
                    logger.info("answerValueSet replaced with  - " + itemComponent.getAnswerValueSet());
                } else {
                    logger.warn("Referenced ValueSet: " + itemComponent.getAnswerValueSet() + " was not found.");
                }
            }
        }
        // Recurse down into child items.
        if (itemComponent.hasItem()) {
            findAndReplaceValueSetReferences(itemComponent.getItem(), valueSetMap, fileStore, baseUrl);
        }
    }
}
Also used : QuestionnaireItemComponent(org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent)

Example 52 with Questionnaire

use of org.hl7.fhir.r4.model.Questionnaire in project CRD by HL7-DaVinci.

the class SubQuestionnaireProcessor method processItem.

/**
 * Determines if this item is a sub-questionnaire reference and returns the items to replace it with. Returns the same list
 * otherwise. Also recursively continues scanning if this is just a grouping item.
 *
 * @param item The item to check for sub-questionnaire referece.
 * @param fileStore The FileStore to be used for fetching sub-questionnaires.
 * @param baseUrl The base url from the server.
 * @param containedList List of contained resources to put in the assembled Questionnaire. This will be filled while iterating.
 * @param extensionList List of extensions to put in the assembled Questionnaire. This will be filled while iterating.
 * @return New list of items to replace this item with.
 */
private List<QuestionnaireItemComponent> processItem(QuestionnaireItemComponent item, FileStore fileStore, String baseUrl, Hashtable<String, org.hl7.fhir.r4.model.Resource> containedList, List<Extension> extensionList) {
    // find if item has an extension is sub-questionnaire
    Extension e = item.getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/sub-questionnaire");
    if (e != null) {
        // read sub questionnaire from file store
        CanonicalType value = e.castToCanonical(e.getValue());
        logger.info("SubQuestionnaireProcessor::parseItem(): Looking for SubQuestionnaire " + value);
        // strip the type off of the id if it is there
        String id = value.asStringValue();
        String[] parts = id.split("/");
        if (parts.length > 1) {
            id = parts[1];
        }
        boolean expandRootItem = false;
        Extension expand = item.getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/sub-questionnaire-expand");
        if (expand != null) {
            expandRootItem = expand.castToBoolean(expand.getValue()).booleanValue();
        }
        FileResource subFileResource = fileStore.getFhirResourceById("R4", "questionnaire", id, baseUrl, false);
        if (subFileResource != null) {
            Questionnaire subQuestionnaire = (Questionnaire) this.parseFhirFileResource(subFileResource);
            if (subQuestionnaire != null) {
                // merge extensions
                for (Extension subExtension : subQuestionnaire.getExtension()) {
                    if (extensionList.stream().noneMatch(ext -> ext.equalsDeep(subExtension))) {
                        extensionList.add(subExtension);
                    }
                }
                // merge contained resources
                for (org.hl7.fhir.r4.model.Resource r : subQuestionnaire.getContained()) {
                    containedList.put(r.getId(), r);
                }
                List<QuestionnaireItemComponent> rootItems = subQuestionnaire.getItem();
                // there are more than one root items in sub questionnaire, don't expand
                if (!expandRootItem || rootItems.size() > 1) {
                    return rootItems;
                } else {
                    return rootItems.get(0).getItem();
                }
            } else {
                // SubQuestionnaire could not be loaded
                logger.warn("SubQuestionnaireProcessor::parseItem(): Could not load SubQuestionnaire " + value.asStringValue());
                return Arrays.asList(item);
            }
        } else {
            // SubQuestionnaire could not be found
            logger.warn("SubQuestionnaireProcessor::parseItem(): Could not find SubQuestionnaire " + value.asStringValue());
            return Arrays.asList(item);
        }
    }
    // parse sub-items
    this.processItemList(item.getItem(), fileStore, baseUrl, containedList, extensionList);
    return Arrays.asList(item);
}
Also used : QuestionnaireItemComponent(org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent) Questionnaire(org.hl7.fhir.r4.model.Questionnaire) CanonicalType(org.hl7.fhir.r4.model.CanonicalType) Extension(org.hl7.fhir.r4.model.Extension)

Example 53 with Questionnaire

use of org.hl7.fhir.r4.model.Questionnaire in project CRD by HL7-DaVinci.

the class QuestionnaireController method addQuestionSetToQuestionnaireResponse.

/**
 * Adds the given set of questions to the contained questionniare in the questionnaire response.
 * @param inputQuestionnaireResponse
 * @param questionSet
 */
private static void addQuestionSetToQuestionnaireResponse(QuestionnaireResponse inputQuestionnaireResponse, List<QuestionnaireItemComponent> questionSet) {
    // Add the next question set to the QuestionnaireResponse.contained[0].item[].
    Questionnaire containedQuestionnaire = (Questionnaire) inputQuestionnaireResponse.getContained().get(0);
    questionSet.forEach(questionItem -> containedQuestionnaire.addItem(questionItem));
}
Also used : Questionnaire(org.hl7.fhir.r4.model.Questionnaire)

Example 54 with Questionnaire

use of org.hl7.fhir.r4.model.Questionnaire in project cqf-ruler by DBCG.

the class ActivityDefinitionApplyProvider method resolveTask.

private Task resolveTask(ActivityDefinition activityDefinition, String patientId) {
    Task task = new Task();
    task.setStatus(Task.TaskStatus.DRAFT);
    task.setIntent(Task.TaskIntent.PROPOSAL);
    task.setFor(new Reference(patientId));
    Task.ParameterComponent input = new Task.ParameterComponent();
    if (activityDefinition.hasCode()) {
        task.setCode(activityDefinition.getCode());
        input.setType(activityDefinition.getCode());
    }
    // Extension defined by CPG-on-FHIR for Questionnaire canonical URI
    Extension collectsWith = activityDefinition.getExtensionByUrl("http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-collectWith");
    if (collectsWith != null && collectsWith.getValueAsPrimitive().toString() != null) {
        CanonicalType uri = new CanonicalType(collectsWith.getValueAsPrimitive().toString());
        input.setValue(uri);
    }
    if (activityDefinition.hasRelatedArtifact()) {
        for (RelatedArtifact artifact : activityDefinition.getRelatedArtifact()) {
            if (artifact.hasUrl()) {
                Attachment attachment = new Attachment().setUrl(artifact.getUrl());
                if (artifact.hasDisplay()) {
                    attachment.setTitle(artifact.getDisplay());
                }
                input.setValue(artifact.hasDisplay() ? attachment.setTitle(artifact.getDisplay()) : attachment);
            }
        // TODO - other relatedArtifact types
        }
    }
    // If input has been populated, then add it to the Task.
    if (input.getType() != null || input.getValue() != null) {
        task.addInput(input);
    }
    return task;
}
Also used : Extension(org.hl7.fhir.r4.model.Extension) Task(org.hl7.fhir.r4.model.Task) Reference(org.hl7.fhir.r4.model.Reference) Attachment(org.hl7.fhir.r4.model.Attachment) RelatedArtifact(org.hl7.fhir.r4.model.RelatedArtifact) CanonicalType(org.hl7.fhir.r4.model.CanonicalType)

Example 55 with Questionnaire

use of org.hl7.fhir.r4.model.Questionnaire in project cqf-ruler by DBCG.

the class ExtractProvider method getQuestionnaireCodeMap.

private Map<String, Coding> getQuestionnaireCodeMap(String questionnaireUrl) {
    String url = mySdcProperties.getExtract().getEndpoint();
    if (null == url || url.length() < 1) {
        throw new IllegalArgumentException("Unable to transmit observation bundle.  No observation.endpoint defined in sdc properties.");
    }
    String user = mySdcProperties.getExtract().getUsername();
    String password = mySdcProperties.getExtract().getPassword();
    IGenericClient client = Clients.forUrl(myFhirContext, url);
    Clients.registerBasicAuth(client, user, password);
    Questionnaire questionnaire = client.read().resource(Questionnaire.class).withUrl(questionnaireUrl).execute();
    return createCodeMap(questionnaire);
}
Also used : Questionnaire(org.hl7.fhir.dstu3.model.Questionnaire) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient)

Aggregations

XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)18 ArrayList (java.util.ArrayList)15 Questionnaire (org.hl7.fhir.r4.model.Questionnaire)14 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)13 QuestionnaireResponse (org.hl7.fhir.r4.model.QuestionnaireResponse)12 QuestionnaireItemComponent (org.hl7.fhir.r4b.model.Questionnaire.QuestionnaireItemComponent)11 QuestionnaireItemComponent (org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemComponent)11 File (java.io.File)10 TextFile (org.hl7.fhir.utilities.TextFile)10 Test (org.junit.jupiter.api.Test)10 FileOutputStream (java.io.FileOutputStream)9 Questionnaire (org.hl7.fhir.dstu3.model.Questionnaire)9 FHIRException (org.hl7.fhir.exceptions.FHIRException)8 QuestionnaireItemComponent (org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent)8 IOException (java.io.IOException)7 Bundle (org.hl7.fhir.r4.model.Bundle)7 CanonicalType (org.hl7.fhir.r4.model.CanonicalType)7 ValueSet (org.hl7.fhir.r5.model.ValueSet)7 FileNotFoundException (java.io.FileNotFoundException)6 Extension (org.hl7.fhir.r4.model.Extension)6