Search in sources :

Example 66 with QuestionnaireResponse

use of org.hl7.fhir.dstu2.model.QuestionnaireResponse 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 67 with QuestionnaireResponse

use of org.hl7.fhir.dstu2.model.QuestionnaireResponse in project CRD by HL7-DaVinci.

the class Metadata method buildCapabilityStatement.

/**
 * Builds the CapabilityStatement describing the Coverage Requirements Discovery Reference
 * Implementation.
 *
 * @return CapabilityStatement - the CapabilityStatement.
 */
private CapabilityStatement buildCapabilityStatement(String baseUrl) {
    CapabilityStatement metadata = new CapabilityStatement();
    metadata.setTitle("Da Vinci Coverage Requirements Discovery (CRD) Reference Implementation");
    metadata.setStatus(PublicationStatus.DRAFT);
    metadata.setExperimental(true);
    Calendar calendar = Calendar.getInstance();
    calendar.set(2019, 4, 28, 0, 0, 0);
    metadata.setDate(calendar.getTime());
    metadata.setPublisher("Da Vinci");
    metadata.setKind(CapabilityStatementKind.INSTANCE);
    CapabilityStatementSoftwareComponent software = new CapabilityStatementSoftwareComponent();
    software.setName("https://github.com/HL7-DaVinci/CRD");
    metadata.setSoftware(software);
    CapabilityStatementImplementationComponent implementation = new CapabilityStatementImplementationComponent();
    implementation.setDescription(metadata.getTitle());
    implementation.setUrl(baseUrl + "metadata");
    metadata.setImplementation(implementation);
    metadata.setFhirVersion(FHIRVersion._4_0_1);
    metadata.addFormat("json");
    metadata.addExtension("http://hl7.org/fhir/StructureDefinition/capabilitystatement-websocket", new StringType("/fhir/r4"));
    metadata.addImplementationGuide("https://build.fhir.org/ig/HL7/davinci-crd/index.html");
    CapabilityStatementRestComponent rest = new CapabilityStatementRestComponent();
    rest.setMode(RestfulCapabilityMode.SERVER);
    CapabilityStatementRestSecurityComponent security = new CapabilityStatementRestSecurityComponent();
    security.setCors(true);
    rest.setSecurity(security);
    // Library Resource
    CapabilityStatementRestResourceComponent library = new CapabilityStatementRestResourceComponent();
    library.setType("Library");
    library.addInteraction().setCode(TypeRestfulInteraction.READ);
    library.addInteraction().setCode(TypeRestfulInteraction.SEARCHTYPE);
    library.addInteraction().setCode(TypeRestfulInteraction.CREATE);
    rest.addResource(library);
    // Questionnaire Resource
    CapabilityStatementRestResourceComponent questionnaire = new CapabilityStatementRestResourceComponent();
    questionnaire.setType("Questionnaire");
    questionnaire.addInteraction().setCode(TypeRestfulInteraction.READ);
    questionnaire.addInteraction().setCode(TypeRestfulInteraction.SEARCHTYPE);
    questionnaire.addInteraction().setCode(TypeRestfulInteraction.CREATE);
    CapabilityStatementRestResourceOperationComponent questionnairePackageOperation = new CapabilityStatementRestResourceOperationComponent();
    questionnairePackageOperation.setName("questionnaire-package");
    questionnairePackageOperation.setDefinition("http://hl7.org/fhir/us/davinci-dtr/OperationDefinition/Questionnaire-package");
    questionnairePackageOperation.setDocumentation("Retrieve the Questionnaire(s), Libraries, and Valuesets for a given order and coverage. This operation is to support HL7 DaVinci DTR.");
    questionnaire.addOperation(questionnairePackageOperation);
    rest.addResource(questionnaire);
    // QuestionnaireResponse Resource
    CapabilityStatementRestResourceComponent questionnaireResponse = new CapabilityStatementRestResourceComponent();
    questionnaireResponse.setType("QuestionnaireResponse");
    questionnaireResponse.addInteraction().setCode(TypeRestfulInteraction.READ);
    questionnaireResponse.addInteraction().setCode(TypeRestfulInteraction.SEARCHTYPE);
    questionnaireResponse.addInteraction().setCode(TypeRestfulInteraction.CREATE);
    rest.addResource(questionnaireResponse);
    // ValueSet Resource
    CapabilityStatementRestResourceComponent valueset = new CapabilityStatementRestResourceComponent();
    valueset.setType("ValueSet");
    valueset.addInteraction().setCode(TypeRestfulInteraction.READ);
    valueset.addInteraction().setCode(TypeRestfulInteraction.SEARCHTYPE);
    valueset.addInteraction().setCode(TypeRestfulInteraction.CREATE);
    // ValueSet $expand Operator
    CapabilityStatementRestResourceOperationComponent expandOperator = new CapabilityStatementRestResourceOperationComponent();
    expandOperator.setName("expand");
    expandOperator.setDefinition("http://hl7.org/fhir/OperationDefinition/ValueSet-expand");
    expandOperator.setDocumentation("Only works at the ValueSet type level with a 'url' query parameter. Will only return expansions that are pre-cached on this server.");
    valueset.addOperation(expandOperator);
    rest.addResource(valueset);
    metadata.addRest(rest);
    return metadata;
}
Also used : StringType(org.hl7.fhir.r4.model.StringType) CapabilityStatementImplementationComponent(org.hl7.fhir.r4.model.CapabilityStatement.CapabilityStatementImplementationComponent) CapabilityStatementRestSecurityComponent(org.hl7.fhir.r4.model.CapabilityStatement.CapabilityStatementRestSecurityComponent) Calendar(java.util.Calendar) CapabilityStatement(org.hl7.fhir.r4.model.CapabilityStatement) CapabilityStatementSoftwareComponent(org.hl7.fhir.r4.model.CapabilityStatement.CapabilityStatementSoftwareComponent) CapabilityStatementRestComponent(org.hl7.fhir.r4.model.CapabilityStatement.CapabilityStatementRestComponent) CapabilityStatementRestResourceComponent(org.hl7.fhir.r4.model.CapabilityStatement.CapabilityStatementRestResourceComponent) CapabilityStatementRestResourceOperationComponent(org.hl7.fhir.r4.model.CapabilityStatement.CapabilityStatementRestResourceOperationComponent)

Aggregations

QuestionnaireResponse (org.hl7.fhir.r4.model.QuestionnaireResponse)15 ArrayList (java.util.ArrayList)14 Bundle (org.hl7.fhir.r4.model.Bundle)11 QuestionnaireResponse (org.hl7.fhir.dstu3.model.QuestionnaireResponse)8 Questionnaire (org.hl7.fhir.r4.model.Questionnaire)8 Row (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Row)8 Test (org.junit.jupiter.api.Test)8 Reference (org.hl7.fhir.dstu3.model.Reference)7 Reference (org.hl7.fhir.r4.model.Reference)7 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 Cell (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell)6 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)6 Map (java.util.Map)5 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)5 FhirUtil (org.hl7.gravity.refimpl.sdohexchange.util.FhirUtil)5 Coding (org.hl7.fhir.r4.model.Coding)4 Date (java.util.Date)3 Getter (lombok.Getter)3 Bundle (org.hl7.fhir.dstu3.model.Bundle)3