Search in sources :

Example 56 with Narrative

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

the class NarrativeGenerator method generateDocumentNarrative.

public XhtmlNode generateDocumentNarrative(Bundle feed) {
    /*
     When the document is presented for human consumption, applications must present the collated narrative portions of the following resources in order:
     * The Composition resource
     * The Subject resource
     * Resources referenced in the section.content
     */
    XhtmlNode root = new XhtmlNode(NodeType.Element, "div");
    Composition comp = (Composition) feed.getEntry().get(0).getResource();
    root.getChildNodes().add(comp.getText().getDiv());
    Resource subject = ResourceUtilities.getById(feed, null, comp.getSubject().getReference());
    if (subject != null && subject instanceof DomainResource) {
        root.hr();
        root.getChildNodes().add(((DomainResource) subject).getText().getDiv());
    }
    List<SectionComponent> sections = comp.getSection();
    renderSections(feed, root, sections, 1);
    return root;
}
Also used : SectionComponent(org.hl7.fhir.r4.model.Composition.SectionComponent) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 57 with Narrative

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

the class NarrativeGenerator method generateByProfile.

private boolean generateByProfile(ResourceContext rc, StructureDefinition profile, boolean showCodeDetails) {
    XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
    x.para().b().tx("Generated Narrative" + (showCodeDetails ? " with Details" : ""));
    try {
        generateByProfile(rc.resourceResource, profile, rc.resourceResource, profile.getSnapshot().getElement(), profile.getSnapshot().getElement().get(0), getChildrenForPath(profile.getSnapshot().getElement(), rc.resourceResource.getResourceType().toString()), x, rc.resourceResource.getResourceType().toString(), showCodeDetails, rc);
    } catch (Exception e) {
        e.printStackTrace();
        x.para().b().setAttribute("style", "color: maroon").tx("Exception generating Narrative: " + e.getMessage());
    }
    inject(rc.resourceResource, x, NarrativeStatus.GENERATED);
    return true;
}
Also used : ParseException(java.text.ParseException) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 58 with Narrative

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

the class QuestionnaireBuilder method processDataType.

private void processDataType(StructureDefinition profile, QuestionnaireItemComponent group, ElementDefinition element, String path, TypeRefComponent t, List<QuestionnaireResponse.QuestionnaireResponseItemComponent> answerGroups, List<ElementDefinition> parents) throws FHIRException {
    String tc = t.getWorkingCode();
    if (tc.equals("code"))
        addCodeQuestions(group, element, path, answerGroups);
    else if (Utilities.existsInList(tc, "string", "id", "oid", "uuid", "markdown"))
        addStringQuestions(group, element, path, answerGroups);
    else if (Utilities.existsInList(tc, "uri", "url", "canonical"))
        addUriQuestions(group, element, path, answerGroups);
    else if (tc.equals("boolean"))
        addBooleanQuestions(group, element, path, answerGroups);
    else if (tc.equals("decimal"))
        addDecimalQuestions(group, element, path, answerGroups);
    else if (tc.equals("dateTime") || tc.equals("date"))
        addDateTimeQuestions(group, element, path, answerGroups);
    else if (tc.equals("instant"))
        addInstantQuestions(group, element, path, answerGroups);
    else if (tc.equals("time"))
        addTimeQuestions(group, element, path, answerGroups);
    else if (tc.equals("CodeableConcept"))
        addCodeableConceptQuestions(group, element, path, answerGroups);
    else if (tc.equals("Period"))
        addPeriodQuestions(group, element, path, answerGroups);
    else if (tc.equals("Ratio"))
        addRatioQuestions(group, element, path, answerGroups);
    else if (tc.equals("HumanName"))
        addHumanNameQuestions(group, element, path, answerGroups);
    else if (tc.equals("Address"))
        addAddressQuestions(group, element, path, answerGroups);
    else if (tc.equals("ContactPoint"))
        addContactPointQuestions(group, element, path, answerGroups);
    else if (tc.equals("Identifier"))
        addIdentifierQuestions(group, element, path, answerGroups);
    else if (tc.equals("integer") || tc.equals("positiveInt") || tc.equals("unsignedInt"))
        addIntegerQuestions(group, element, path, answerGroups);
    else if (tc.equals("Coding"))
        addCodingQuestions(group, element, path, answerGroups);
    else if (Utilities.existsInList(tc, "Quantity", "Count", "Age", "Duration", "Distance", "Money"))
        addQuantityQuestions(group, element, path, answerGroups);
    else if (tc.equals("Money"))
        addMoneyQuestions(group, element, path, answerGroups);
    else if (tc.equals("Reference"))
        addReferenceQuestions(group, element, path, t.getTargetProfile(), answerGroups);
    else if (tc.equals("Duration"))
        addDurationQuestions(group, element, path, answerGroups);
    else if (tc.equals("base64Binary"))
        addBinaryQuestions(group, element, path, answerGroups);
    else if (tc.equals("Attachment"))
        addAttachmentQuestions(group, element, path, answerGroups);
    else if (tc.equals("Age"))
        addAgeQuestions(group, element, path, answerGroups);
    else if (tc.equals("Range"))
        addRangeQuestions(group, element, path, answerGroups);
    else if (tc.equals("Timing"))
        addTimingQuestions(group, element, path, answerGroups);
    else if (tc.equals("Annotation"))
        addAnnotationQuestions(group, element, path, answerGroups);
    else if (tc.equals("SampledData"))
        addSampledDataQuestions(group, element, path, answerGroups);
    else if (tc.equals("Extension")) {
        if (t.hasProfile())
            addExtensionQuestions(profile, group, element, path, t.getProfile().get(0).getValue(), answerGroups, parents);
    } else if (tc.equals("SampledData"))
        addSampledDataQuestions(group, element, path, answerGroups);
    else if (!tc.equals("Narrative") && !tc.equals("Resource") && !tc.equals("Meta") && !tc.equals("Signature")) {
        StructureDefinition sd = context.fetchTypeDefinition(tc);
        if (sd == null)
            throw new NotImplementedException("Unhandled Data Type: " + tc + " on element " + element.getPath());
        buildGroup(group, sd, sd.getSnapshot().getElementFirstRep(), parents, answerGroups);
    }
}
Also used : StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) NotImplementedException(org.apache.commons.lang3.NotImplementedException)

Example 59 with Narrative

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

the class NarrativeGenerationTests method test.

@ParameterizedTest(name = "{index}: file {0}")
@MethodSource("data")
public void test(String id, TestDetails test) throws Exception {
    RenderingContext rc = new RenderingContext(context, null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
    rc.setDestDir("");
    rc.setHeader(test.isHeader());
    rc.setDefinitionsTarget("test.html");
    rc.setTerminologyServiceOptions(TerminologyServiceOptions.defaults());
    rc.setParser(new TestTypeParser());
    // getting timezones correct (well, at least consistent, so tests pass on any computer)
    rc.setLocale(new java.util.Locale("en", "AU"));
    rc.setTimeZoneId(ZoneId.of("Australia/Sydney"));
    rc.setDateTimeFormatString("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
    rc.setDateFormatString("yyyy-MM-dd");
    rc.setMode(test.technical ? ResourceRendererMode.TECHNICAL : ResourceRendererMode.END_USER);
    Resource source;
    if (TestingUtilities.findTestResource("r5", "narrative", test.getId() + ".json")) {
        source = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".json"));
    } else {
        source = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".xml"));
    }
    XhtmlNode x = RendererFactory.factory(source, rc).build(source);
    String expected = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".html"));
    String actual = HEADER + new XhtmlComposer(true, true).compose(x) + FOOTER;
    String expectedFileName = CompareUtilities.tempFile("narrative", test.getId() + ".expected.html");
    String actualFileName = CompareUtilities.tempFile("narrative", test.getId() + ".actual.html");
    TextFile.stringToFile(expected, expectedFileName);
    TextFile.stringToFile(actual, actualFileName);
    String msg = CompareUtilities.checkXMLIsSame(expectedFileName, actualFileName);
    Assertions.assertTrue(msg == null, "Output does not match expected: " + msg);
    if (test.isMeta()) {
        org.hl7.fhir.r5.elementmodel.Element e = Manager.parseSingle(context, TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".xml"), FhirFormat.XML);
        x = RendererFactory.factory(source, rc).render(new ElementWrappers.ResourceWrapperMetaElement(rc, e));
        expected = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + "-meta.html"));
        actual = HEADER + new XhtmlComposer(true, true).compose(x) + FOOTER;
        actualFileName = CompareUtilities.tempFile("narrative", test.getId() + "-meta.actual.html");
        TextFile.stringToFile(actual, actualFileName);
        msg = CompareUtilities.checkXMLIsSame(expectedFileName, actualFileName);
        Assertions.assertTrue(msg == null, "Meta output does not match expected: " + msg);
    }
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r5.formats.XmlParser) Resource(org.hl7.fhir.r5.model.Resource) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) JsonParser(org.hl7.fhir.r5.formats.JsonParser) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 60 with Narrative

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

the class Params method loadCliContext.

/**
 * TODO Don't do this all in one for loop. Use the above methods.
 */
public static CliContext loadCliContext(String[] args) throws Exception {
    CliContext cliContext = new CliContext();
    // load the parameters - so order doesn't matter
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals(VERSION)) {
            cliContext.setSv(VersionUtilities.getCurrentPackageVersion(args[++i]));
        } else if (args[i].equals(OUTPUT)) {
            if (i + 1 == args.length)
                throw new Error("Specified -output without indicating output file");
            else
                cliContext.setOutput(args[++i]);
        } else if (args[i].equals(HTML_OUTPUT)) {
            if (i + 1 == args.length)
                throw new Error("Specified -html-output without indicating output file");
            else
                cliContext.setHtmlOutput(args[++i]);
        } else if (args[i].equals(PROXY)) {
            // ignore next parameter
            i++;
        } else if (args[i].equals(PROXY_AUTH)) {
            i++;
        } else if (args[i].equals(PROFILE)) {
            String p = null;
            if (i + 1 == args.length) {
                throw new Error("Specified -profile without indicating profile source");
            } else {
                p = args[++i];
                cliContext.addProfile(p);
            }
        } else if (args[i].equals(BUNDLE)) {
            String p = null;
            String r = null;
            if (i + 1 == args.length) {
                throw new Error("Specified -profile without indicating bundle rule ");
            } else {
                r = args[++i];
            }
            if (i + 1 == args.length) {
                throw new Error("Specified -profile without indicating profile source");
            } else {
                p = args[++i];
            }
            cliContext.getBundleValidationRules().add(new BundleValidationRule(r, p));
        } else if (args[i].equals(QUESTIONNAIRE)) {
            if (i + 1 == args.length)
                throw new Error("Specified -questionnaire without indicating questionnaire mode");
            else {
                String q = args[++i];
                cliContext.setQuestionnaireMode(QuestionnaireMode.fromCode(q));
            }
        } else if (args[i].equals(LEVEL)) {
            if (i + 1 == args.length)
                throw new Error("Specified -level without indicating level mode");
            else {
                String q = args[++i];
                cliContext.setLevel(ValidationLevel.fromCode(q));
            }
        } else if (args[i].equals(NATIVE)) {
            cliContext.setDoNative(true);
        } else if (args[i].equals(ASSUME_VALID_REST_REF)) {
            cliContext.setAssumeValidRestReferences(true);
        } else if (args[i].equals(DEBUG)) {
            cliContext.setDoDebug(true);
        } else if (args[i].equals(SCT)) {
            cliContext.setSnomedCT(args[++i]);
        } else if (args[i].equals(RECURSE)) {
            cliContext.setRecursive(true);
        } else if (args[i].equals(SHOW_MESSAGES_FROM_REFERENCES)) {
            cliContext.setShowMessagesFromReferences(true);
        } else if (args[i].equals(LOCALE)) {
            if (i + 1 == args.length) {
                throw new Error("Specified -locale without indicating locale");
            } else {
                cliContext.setLocale(new Locale(args[++i]));
            }
        } else if (args[i].equals(EXTENSIONS)) {
            cliContext.getExtensions().add(args[++i]);
        } else if (args[i].equals(NO_INTERNAL_CACHING)) {
            cliContext.setNoInternalCaching(true);
        } else if (args[i].equals(NO_EXTENSIBLE_BINDING_WARNINGS)) {
            cliContext.setNoExtensibleBindingMessages(true);
        } else if (args[i].equals(NO_UNICODE_BIDI_CONTROL_CHARS)) {
            cliContext.setNoUnicodeBiDiControlChars(true);
        } else if (args[i].equals(NO_INVARIANTS)) {
            cliContext.setNoInvariants(true);
        } else if (args[i].equals(WANT_INVARIANTS_IN_MESSAGES)) {
            cliContext.setWantInvariantsInMessages(true);
        } else if (args[i].equals(HINT_ABOUT_NON_MUST_SUPPORT)) {
            cliContext.setHintAboutNonMustSupport(true);
        } else if (args[i].equals(TO_VERSION)) {
            cliContext.setTargetVer(args[++i]);
            cliContext.setMode(EngineMode.VERSION);
        } else if (args[i].equals(DO_NATIVE)) {
            cliContext.setCanDoNative(true);
        } else if (args[i].equals(NO_NATIVE)) {
            cliContext.setCanDoNative(false);
        } else if (args[i].equals(TRANSFORM)) {
            cliContext.setMap(args[++i]);
            cliContext.setMode(EngineMode.TRANSFORM);
        } else if (args[i].equals(COMPILE)) {
            cliContext.setMap(args[++i]);
            cliContext.setMode(EngineMode.COMPILE);
        } else if (args[i].equals(NARRATIVE)) {
            cliContext.setMode(EngineMode.NARRATIVE);
        } else if (args[i].equals(SPREADSHEET)) {
            cliContext.setMode(EngineMode.SPREADSHEET);
        } else if (args[i].equals(SNAPSHOT)) {
            cliContext.setMode(EngineMode.SNAPSHOT);
        } else if (args[i].equals(SECURITY_CHECKS)) {
            cliContext.setSecurityChecks(true);
        } else if (args[i].equals(CRUMB_TRAIL)) {
            cliContext.setCrumbTrails(true);
        } else if (args[i].equals(VERBOSE)) {
            cliContext.setCrumbTrails(true);
        } else if (args[i].equals(ALLOW_EXAMPLE_URLS)) {
            String bl = args[++i];
            if ("true".equals(bl)) {
                cliContext.setAllowExampleUrls(true);
            } else if ("false".equals(bl)) {
                cliContext.setAllowExampleUrls(false);
            } else {
                throw new Error("Value for " + ALLOW_EXAMPLE_URLS + " not understood: " + bl);
            }
        } else if (args[i].equals(SHOW_TIMES)) {
            cliContext.setShowTimes(true);
        } else if (args[i].equals(OUTPUT_STYLE)) {
            cliContext.setOutputStyle(args[++i]);
        } else if (args[i].equals(SCAN)) {
            cliContext.setMode(EngineMode.SCAN);
        } else if (args[i].equals(TERMINOLOGY)) {
            if (i + 1 == args.length)
                throw new Error("Specified -tx without indicating terminology server");
            else
                cliContext.setTxServer("n/a".equals(args[++i]) ? null : args[i]);
        } else if (args[i].equals(TERMINOLOGY_LOG)) {
            if (i + 1 == args.length)
                throw new Error("Specified -txLog without indicating file");
            else
                cliContext.setTxLog(args[++i]);
        } else if (args[i].equals(TERMINOLOGY_CACHE)) {
            if (i + 1 == args.length)
                throw new Error("Specified -txCache without indicating file");
            else
                cliContext.setTxCache(args[++i]);
        } else if (args[i].equals(LOG)) {
            if (i + 1 == args.length)
                throw new Error("Specified -log without indicating file");
            else
                cliContext.setMapLog(args[++i]);
        } else if (args[i].equals(LANGUAGE)) {
            if (i + 1 == args.length)
                throw new Error("Specified -language without indicating language");
            else
                cliContext.setLang(args[++i]);
        } else if (args[i].equals(IMPLEMENTATION_GUIDE) || args[i].equals(DEFINITION)) {
            if (i + 1 == args.length)
                throw new Error("Specified " + args[i] + " without indicating ig file");
            else {
                String s = args[++i];
                String version = Common.getVersionFromIGName(null, s);
                if (version == null) {
                    cliContext.addIg(s);
                } else {
                    cliContext.setSv(version);
                }
            }
        } else if (args[i].equals(MAP)) {
            if (cliContext.getMap() == null) {
                if (i + 1 == args.length)
                    throw new Error("Specified -map without indicating map file");
                else
                    cliContext.setMap(args[++i]);
            } else {
                throw new Exception("Can only nominate a single -map parameter");
            }
        } else if (args[i].startsWith(X)) {
            i++;
        } else if (args[i].equals(CONVERT)) {
            cliContext.setMode(EngineMode.CONVERT);
        } else if (args[i].equals(FHIRPATH)) {
            cliContext.setMode(EngineMode.FHIRPATH);
            if (cliContext.getFhirpath() == null)
                if (i + 1 == args.length)
                    throw new Error("Specified -fhirpath without indicating a FHIRPath expression");
                else
                    cliContext.setFhirpath(args[++i]);
            else
                throw new Exception("Can only nominate a single -fhirpath parameter");
        } else {
            cliContext.addSource(args[i]);
        }
    }
    return cliContext;
}
Also used : Locale(java.util.Locale) BundleValidationRule(org.hl7.fhir.r5.utils.validation.BundleValidationRule) CliContext(org.hl7.fhir.validation.cli.model.CliContext)

Aggregations

XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)80 FHIRException (org.hl7.fhir.exceptions.FHIRException)40 IOException (java.io.IOException)34 NotImplementedException (org.apache.commons.lang3.NotImplementedException)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)27 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)23 Test (org.junit.Test)22 MethodOutcome (ca.uhn.fhir.rest.api.MethodOutcome)19 ArrayList (java.util.ArrayList)19 XhtmlParser (org.hl7.fhir.utilities.xhtml.XhtmlParser)17 Narrative (org.hl7.fhir.r4.model.Narrative)15 Narrative (org.hl7.fhir.r5.model.Narrative)15 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)14 XhtmlComposer (org.hl7.fhir.utilities.xhtml.XhtmlComposer)14 GET (javax.ws.rs.GET)12 Path (javax.ws.rs.Path)12 Produces (javax.ws.rs.Produces)12 IBaseOperationOutcome (org.hl7.fhir.instance.model.api.IBaseOperationOutcome)12 FileNotFoundException (java.io.FileNotFoundException)10 LinkedHashMap (java.util.LinkedHashMap)10