Search in sources :

Example 91 with QuestionnaireItemComponent

use of org.hl7.fhir.r4b.model.Questionnaire.QuestionnaireItemComponent in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireRenderer method renderLogic.

private boolean renderLogic(XhtmlNode x, Questionnaire q) throws FHIRException, IOException {
    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(context.getDestDir(), context.isInlineGraphics(), true);
    TableModel model = gen.new TableModel("qtree=" + q.getId(), true);
    model.setAlternating(true);
    model.setDocoImg(context.getSpecificationLink() + "help16.png");
    model.setDocoRef(context.getSpecificationLink() + "formats.html#table");
    model.getTitles().add(gen.new Title(null, model.getDocoRef(), translate("sd.head", "LinkId"), translate("sd.hint", "The linkId for the item"), null, 0));
    model.getTitles().add(gen.new Title(null, model.getDocoRef(), translate("sd.head", "Description & Constraints"), translate("sd.hint", "Additional information about the item"), null, 0));
    boolean hasExt = false;
    if (!q.hasItem()) {
        gen.emptyRow(model, 2);
    } else {
        for (QuestionnaireItemComponent i : q.getItem()) {
            hasExt = renderLogicItem(gen, model.getRows(), q, i) || hasExt;
        }
    }
    XhtmlNode xn = gen.generate(model, context.getLocalPrefix(), 1, null);
    x.getChildNodes().add(xn);
    return hasExt;
}
Also used : QuestionnaireItemComponent(org.hl7.fhir.r4b.model.Questionnaire.QuestionnaireItemComponent) HierarchicalTableGenerator(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator) TableModel(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.TableModel) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 92 with QuestionnaireItemComponent

use of org.hl7.fhir.r4b.model.Questionnaire.QuestionnaireItemComponent in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireRenderer method genDefinitionLink.

public void genDefinitionLink(HierarchicalTableGenerator gen, QuestionnaireItemComponent i, Cell defn) {
    // can we resolve the definition?
    String path = null;
    String d = i.getDefinition();
    if (d.contains("#")) {
        path = d.substring(d.indexOf("#") + 1);
        d = d.substring(0, d.indexOf("#"));
    }
    StructureDefinition sd = context.getWorker().fetchResource(StructureDefinition.class, d);
    if (sd != null) {
        String url = sd.getUserString("path");
        if (url != null) {
            defn.getPieces().add(gen.new Piece(url + "#" + path, path, null));
        } else {
            defn.getPieces().add(gen.new Piece(null, i.getDefinition(), null));
        }
    } else {
        defn.getPieces().add(gen.new Piece(null, i.getDefinition(), null));
    }
}
Also used : StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) Piece(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece)

Example 93 with QuestionnaireItemComponent

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

the class QuestionnaireController method getNextQuestionOperation.

/**
 * Returns the next question based on the request.
 * @param body
 * @param request
 * @return
 */
private ResponseEntity<String> getNextQuestionOperation(String body, HttpServletRequest request) {
    logger.info("POST /Questionnaire/$next-question fhir+");
    FhirContext ctx = new FhirComponents().getFhirContext();
    IParser parser = ctx.newJsonParser();
    // Parses the body.
    IDomainResource domainResource = (IDomainResource) parser.parseResource(QuestionnaireResponse.class, body);
    if (!domainResource.fhirType().equalsIgnoreCase("QuestionnaireResponse")) {
        logger.warning("unsupported resource type: ");
        HttpStatus status = HttpStatus.BAD_REQUEST;
        MediaType contentType = MediaType.TEXT_PLAIN;
        return ResponseEntity.status(status).contentType(contentType).body("Bad Request");
    } else {
        logger.info(" ---- Resource received " + domainResource.toString());
        QuestionnaireResponse inputQuestionnaireResponse = (QuestionnaireResponse) domainResource;
        String fragmentId = inputQuestionnaireResponse.getQuestionnaire();
        List<Resource> containedResource = inputQuestionnaireResponse.getContained();
        Questionnaire inputQuestionnaireFromRequest = null;
        for (int i = 0; i < containedResource.size(); i++) {
            Resource item = containedResource.get(i);
            if (item.getResourceType().equals(ResourceType.Questionnaire)) {
                Questionnaire checkInputQuestionnaire = (Questionnaire) item;
                if (checkInputQuestionnaire.getId().equals(fragmentId)) {
                    inputQuestionnaireFromRequest = checkInputQuestionnaire;
                    break;
                }
            }
        }
        logger.info("--- Received questionnaire response: " + ctx.newJsonParser().encodeResourceToString(inputQuestionnaireResponse));
        // Check that there are no duplicates in the recieved set of questions.
        if ((new HashSet(((Questionnaire) inputQuestionnaireResponse.getContained().get(0)).getItem().stream().map(item -> item.getLinkId()).collect(Collectors.toList()))).size() != ((Questionnaire) inputQuestionnaireResponse.getContained().get(0)).getItem().size()) {
            throw new RuntimeException("Received a set of questions with duplicates.");
        }
        String questionnaireId = ((Reference) inputQuestionnaireResponse.getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/contained-id").getValue()).getReference();
        System.out.println("Input Questionnaire: " + questionnaireId);
        if (inputQuestionnaireFromRequest != null) {
            if (!questionnaireTrees.containsKey(questionnaireId)) {
                // If there is not already a tree that matches the requested questionnaire id, build it.
                // Import the requested CDS-Library Questionnaire.
                Questionnaire cdsQuestionnaire = importCdsAdaptiveQuestionnaire(request, parser, fileStore, questionnaireId);
                // Build the tree.
                AdaptiveQuestionnaireTree newTree = new AdaptiveQuestionnaireTree(cdsQuestionnaire);
                questionnaireTrees.put(questionnaireId, newTree);
                logger.info("--- Built Questionnaire Tree for " + questionnaireId);
            }
            // Pull the tree for the requested questionnaire id.
            AdaptiveQuestionnaireTree currentTree = questionnaireTrees.get(questionnaireId);
            // Get the request's set of answer responses.
            List<QuestionnaireResponseItemComponent> allResponses = inputQuestionnaireResponse.getItem();
            // Pull the resulting next question that the recieved responses and answers point to from the tree without including its children.
            List<QuestionnaireItemComponent> nextQuestionSetResults = currentTree.getNextQuestionsForAnswers(allResponses, inputQuestionnaireResponse);
            // Add the next set of questions to the response.
            QuestionnaireController.addQuestionSetToQuestionnaireResponse(inputQuestionnaireResponse, nextQuestionSetResults);
            // Check that there no duplicates in the set of questions.
            if ((new HashSet(((Questionnaire) inputQuestionnaireResponse.getContained().get(0)).getItem().stream().map(item -> item.getLinkId()).collect(Collectors.toList()))).size() != ((Questionnaire) inputQuestionnaireResponse.getContained().get(0)).getItem().size()) {
                throw new RuntimeException("Attempted to send a set of questions with duplicates. Question IDs are: " + (((Questionnaire) inputQuestionnaireResponse.getContained().get(0)).getItem().stream().map(item -> item.getLinkId()).collect(Collectors.toList())));
            }
            logger.info("--- Added next question set for questionnaire \'" + questionnaireId + "\' for responses \'" + allResponses + "\'.");
            // Build and send the response.
            String formattedResourceString = ctx.newJsonParser().encodeResourceToString(inputQuestionnaireResponse);
            logger.info("--- Sending questionnaire response: " + formattedResourceString);
            return ResponseEntity.status(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON).header(HttpHeaders.CONTENT_TYPE, "application/fhir+json" + "; charset=utf-8").body(formattedResourceString);
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).header(HttpHeaders.CONTENT_TYPE, "application/fhir+json" + "; charset=utf-8").body("Input questionnaire from the request does not exist.");
        }
    }
}
Also used : Application(org.hl7.davinci.endpoint.Application) QuestionnaireItemComponent(org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent) DataFormatException(ca.uhn.fhir.parser.DataFormatException) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) FhirComponents(org.hl7.davinci.r4.FhirComponents) Reference(org.hl7.fhir.r4.model.Reference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FhirContext(ca.uhn.fhir.context.FhirContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) Map(java.util.Map) QuestionnaireEmbeddedCQLProcessor(org.hl7.davinci.endpoint.files.QuestionnaireEmbeddedCQLProcessor) QuestionnaireResponseStatus(org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseStatus) IParser(ca.uhn.fhir.parser.IParser) HttpHeaders(org.springframework.http.HttpHeaders) QuestionnaireResponseItemAnswerComponent(org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent) MediaType(org.springframework.http.MediaType) FileResource(org.hl7.davinci.endpoint.files.FileResource) Resource(org.hl7.fhir.r4.model.Resource) FileStore(org.hl7.davinci.endpoint.files.FileStore) IOException(java.io.IOException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) HttpStatus(org.springframework.http.HttpStatus) ResourceType(org.hl7.fhir.r4.model.ResourceType) QuestionnaireResponseItemComponent(org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComponent) HttpEntity(org.springframework.http.HttpEntity) List(java.util.List) QuestionnaireResponse(org.hl7.fhir.r4.model.QuestionnaireResponse) IDomainResource(org.hl7.fhir.instance.model.api.IDomainResource) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) ResponseEntity(org.springframework.http.ResponseEntity) QuestionnaireItemAnswerOptionComponent(org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemAnswerOptionComponent) Questionnaire(org.hl7.fhir.r4.model.Questionnaire) FhirContext(ca.uhn.fhir.context.FhirContext) QuestionnaireItemComponent(org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent) Questionnaire(org.hl7.fhir.r4.model.Questionnaire) HttpStatus(org.springframework.http.HttpStatus) Reference(org.hl7.fhir.r4.model.Reference) FileResource(org.hl7.davinci.endpoint.files.FileResource) Resource(org.hl7.fhir.r4.model.Resource) IDomainResource(org.hl7.fhir.instance.model.api.IDomainResource) QuestionnaireResponse(org.hl7.fhir.r4.model.QuestionnaireResponse) IDomainResource(org.hl7.fhir.instance.model.api.IDomainResource) QuestionnaireResponseItemComponent(org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComponent) MediaType(org.springframework.http.MediaType) FhirComponents(org.hl7.davinci.r4.FhirComponents) IParser(ca.uhn.fhir.parser.IParser) HashSet(java.util.HashSet)

Example 94 with QuestionnaireItemComponent

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

the class QuestionnaireEmbeddedCQLProcessor method hasEmbeddedCql.

private boolean hasEmbeddedCql(QuestionnaireItemComponent item) {
    List<Extension> extensionList = item.getExtension();
    // sdc-questionnaire-initialExpression for now, could add more if needed
    if (extensionList.isEmpty())
        return false;
    for (int i = 0; i < extensionList.size(); i++) {
        Extension extension = extensionList.get(i);
        if (extension.getUrl().equals("http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-initialExpression")) {
            Expression expression = (Expression) extension.getValue();
            if (expression.getLanguage().equals("text/cql")) {
                String expressionString = expression.getExpression();
                // regex for \"library\".statement
                final String libraryRefRegex = "^\\\\\\\"[a-zA-Z0-9]+\\\\\\\".[a-zA-Z0-9]+$";
                final Pattern pattern = Pattern.compile(libraryRefRegex, Pattern.MULTILINE);
                // cql-execution library to throw error if it is invalid
                if (!pattern.matcher(expressionString).find()) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Extension(org.hl7.fhir.r4.model.Extension) Pattern(java.util.regex.Pattern) Expression(org.hl7.fhir.r4.model.Expression)

Aggregations

ArrayList (java.util.ArrayList)32 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)20 QuestionnaireItemComponent (org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemComponent)16 QuestionnaireItemComponent (org.hl7.fhir.r4b.model.Questionnaire.QuestionnaireItemComponent)15 QuestionnaireItemComponent (org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent)11 Element (org.hl7.fhir.r5.elementmodel.Element)9 NodeStack (org.hl7.fhir.validation.instance.utils.NodeStack)9 QuestionnaireItemComponent (org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemComponent)7 QuestionnaireResponse (org.hl7.fhir.r4.model.QuestionnaireResponse)7 QuestionnaireResponse (org.hl7.fhir.r4b.model.QuestionnaireResponse)7 QuestionnaireItemAnswerOptionComponent (org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemAnswerOptionComponent)7 QuestionnaireResponse (org.hl7.fhir.r5.model.QuestionnaireResponse)7 QuestionnaireItemComponent (org.hl7.fhir.dstu2016may.model.Questionnaire.QuestionnaireItemComponent)6 ValueSet (org.hl7.fhir.r4b.model.ValueSet)6 ValueSet (org.hl7.fhir.r5.model.ValueSet)6 Piece (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece)6 Row (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Row)6 QuestionnaireResponse (org.hl7.fhir.dstu2016may.model.QuestionnaireResponse)5 QuestionnaireResponse (org.hl7.fhir.dstu3.model.QuestionnaireResponse)5 FHIRException (org.hl7.fhir.exceptions.FHIRException)5