Search in sources :

Example 11 with IParser

use of org.hl7.fhir.r4.formats.IParser in project kindling by HL7.

the class CodeSystemConvertor method convert.

public void convert(IParser p, ValueSet vs, String name, PackageVersion packageInfo) throws Exception {
    String nname = name.replace("valueset-", "codesystem-");
    if (nname.equals(name))
        nname = Utilities.changeFileExt(name, "-cs.xml");
    if (new File(nname).exists()) {
        FileInputStream input = new FileInputStream(nname);
        CodeSystem cs = CodeSystemUtilities.makeShareable((CodeSystem) p.parse(input));
        if (!cs.hasTitle())
            cs.setTitle(Utilities.capitalize(Utilities.unCamelCase(cs.getName())));
        populate(cs, vs);
        // if (codeSystems.containsKey(cs.getUrl()))
        // throw new Exception("Duplicate Code System: "+cs.getUrl());
        codeSystems.see(cs, packageInfo);
    }
}
Also used : File(java.io.File) CodeSystem(org.hl7.fhir.r5.model.CodeSystem) FileInputStream(java.io.FileInputStream)

Example 12 with IParser

use of org.hl7.fhir.r4.formats.IParser in project kindling by HL7.

the class Publisher method buildLoincExample.

private String buildLoincExample(String filename) throws FileNotFoundException, Exception {
    LoincToDEConvertor conv = new LoincToDEConvertor();
    conv.setDefinitions(Utilities.path(page.getFolders().srcDir, "loinc", "loincS.xml"));
    conv.process();
    IParser xml = new XmlParser().setOutputStyle(OutputStyle.PRETTY);
    FileOutputStream s = new FileOutputStream(Utilities.path(page.getFolders().dstDir, filename + ".xml"));
    xml.compose(s, conv.getBundle());
    s.close();
    IParser json = new JsonParser().setOutputStyle(OutputStyle.PRETTY);
    s = new FileOutputStream(Utilities.path(page.getFolders().dstDir, filename + ".json"));
    json.compose(s, conv.getBundle());
    s.close();
    return "Loinc Narrative";
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) LoincToDEConvertor(org.hl7.fhir.r5.terminologies.LoincToDEConvertor) IParser(org.hl7.fhir.r5.formats.IParser) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 13 with IParser

use of org.hl7.fhir.r4.formats.IParser in project health-patterns by LinuxForHealth.

the class DeIdentifier method postBundle.

/**
 * Posts the given Bundle to the FHIR server
 *
 * @param fhirBundle the bundle to post
 * @return the FHIR response of executing the transaction Bundle
 * @throws DeIdentifierException if there is a problem posting the bundle or parsing the response
 */
private JsonNode postBundle(JsonNode fhirBundle) throws DeIdentifierException {
    System.out.println("Initializing FHIR transacton with Bundle...");
    IParser parser = fhirClient.getFhirContext().newJsonParser();
    Bundle bundle = parser.parseResource(Bundle.class, fhirBundle.toString());
    Bundle resp;
    try {
        resp = fhirClient.transaction().withBundle(bundle).execute();
    } catch (BaseServerResponseException e) {
        throw new DeIdentifierException("The FHIR transaction could not be executed: " + e.getMessage(), e);
    }
    String jsonResponseString = parser.setPrettyPrint(true).encodeResourceToString(resp);
    JsonNode jsonResponse;
    try {
        jsonResponse = jsonDeserializer.readTree(jsonResponseString);
    } catch (JsonProcessingException e) {
        throw new DeIdentifierException("The FHIR response JSON could not be parsed: " + e.getMessage(), e);
    }
    System.out.println("FHIR transacton with Bundle done!");
    return jsonResponse;
}
Also used : Bundle(org.hl7.fhir.r4.model.Bundle) JsonNode(com.fasterxml.jackson.databind.JsonNode) BaseServerResponseException(ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IParser(ca.uhn.fhir.parser.IParser)

Example 14 with IParser

use of org.hl7.fhir.r4.formats.IParser in project BridgeServer2 by Sage-Bionetworks.

the class CRCController method postProcedureRequest.

@PutMapping("/v1/cuimc/procedurerequests")
public ResponseEntity<StatusMessage> postProcedureRequest() {
    App app = httpBasicAuthentication();
    IParser parser = FHIR_CONTEXT.newJsonParser();
    JsonNode data = parseJson(JsonNode.class);
    ProcedureRequest procedure = parser.parseResource(ProcedureRequest.class, data.toString());
    String userId = findUserId(procedure.getSubject());
    int status = writeReportAndUpdateState(app, userId, data, PROCEDURE_REPORT, AccountStates.TESTS_COLLECTED, true);
    if (status == 200) {
        return ResponseEntity.ok(new StatusMessage("ProcedureRequest updated."));
    }
    return ResponseEntity.created(URI.create("/v1/cuimc/procedurerequests/" + userId)).body(new StatusMessage("ProcedureRequest created."));
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) ProcedureRequest(org.hl7.fhir.dstu3.model.ProcedureRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) ContactPoint(org.hl7.fhir.dstu3.model.ContactPoint) IParser(ca.uhn.fhir.parser.IParser) StatusMessage(org.sagebionetworks.bridge.models.StatusMessage) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 15 with IParser

use of org.hl7.fhir.r4.formats.IParser in project cqf-ruler by DBCG.

the class R4CarePlanToCdsCard method convert.

private static List<CdsCard> convert(RequestGroup requestGroup) {
    List<CdsCard> cards = new ArrayList<>();
    // links
    List<CdsCard.Links> links = new ArrayList<>();
    if (requestGroup.hasExtension()) {
        for (Extension extension : requestGroup.getExtension()) {
            CdsCard.Links link = new CdsCard.Links();
            if (extension.getValue() instanceof Attachment) {
                Attachment attachment = (Attachment) extension.getValue();
                if (attachment.hasUrl()) {
                    link.setUrl(attachment.getUrl());
                }
                if (attachment.hasTitle()) {
                    link.setLabel(attachment.getTitle());
                }
                if (attachment.hasExtension()) {
                    link.setType(attachment.getExtensionFirstRep().getValue().primitiveValue());
                }
            } else {
                throw new RuntimeException("Invalid link extension type: " + extension.getValue().fhirType());
            }
            links.add(link);
        }
    }
    if (requestGroup.hasAction()) {
        for (RequestGroup.RequestGroupActionComponent action : requestGroup.getAction()) {
            IParser jsonParser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser().setPrettyPrint(true);
            CdsCard card = new CdsCard(jsonParser);
            // basic
            if (action.hasTitle()) {
                card.setSummary(action.getTitle());
            }
            if (action.hasDescription()) {
                card.setDetail(action.getDescription());
            }
            if (action.hasExtension()) {
                card.setIndicator(action.getExtensionFirstRep().getValue().toString());
            }
            // source
            if (action.hasDocumentation()) {
                // Assuming first related artifact has everything
                RelatedArtifact documentation = action.getDocumentationFirstRep();
                CdsCard.Source source = new CdsCard.Source();
                if (documentation.hasDisplay()) {
                    source.setLabel(documentation.getDisplay());
                }
                if (documentation.hasUrl()) {
                    source.setUrl(documentation.getUrl());
                }
                if (documentation.hasDocument() && documentation.getDocument().hasUrl()) {
                    source.setIcon(documentation.getDocument().getUrl());
                }
                card.setSource(source);
            }
            if (action.hasSelectionBehavior()) {
                card.setSelectionBehavior(action.getSelectionBehavior().toCode());
            }
            // suggestions
            // TODO - uuid
            boolean hasSuggestions = false;
            CdsCard.Suggestions suggestions = new CdsCard.Suggestions();
            CdsCard.Suggestions.Action actions = new CdsCard.Suggestions.Action();
            if (action.hasPrefix()) {
                suggestions.setLabel(action.getPrefix());
                hasSuggestions = true;
                if (action.hasDescription()) {
                    actions.setDescription(action.getDescription());
                }
                if (action.hasType() && !action.getType().getCodingFirstRep().getCode().equals("fire-event")) {
                    String code = action.getType().getCodingFirstRep().getCode();
                    actions.setType(CdsCard.Suggestions.Action.ActionType.valueOf(code.equals("remove") ? "delete" : code));
                }
                if (action.hasResource()) {
                    if (actions.getType().name().equalsIgnoreCase("create")) {
                        action.getResourceTarget().setId((String) null);
                    }
                    actions.setResource(action.getResourceTarget());
                }
            }
            if (hasSuggestions) {
                suggestions.addAction(actions);
                card.addSuggestion(suggestions);
            }
            if (!links.isEmpty()) {
                card.setLinks(links);
            }
            cards.add(card);
        }
    }
    return cards;
}
Also used : ArrayList(java.util.ArrayList) Attachment(org.hl7.fhir.r4.model.Attachment) RelatedArtifact(org.hl7.fhir.r4.model.RelatedArtifact) Extension(org.hl7.fhir.r4.model.Extension) RequestGroup(org.hl7.fhir.r4.model.RequestGroup) IParser(ca.uhn.fhir.parser.IParser)

Aggregations

IParser (ca.uhn.fhir.parser.IParser)89 FhirContext (ca.uhn.fhir.context.FhirContext)43 IOException (java.io.IOException)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 Test (org.junit.Test)24 InputStream (java.io.InputStream)22 IParser (org.hl7.fhir.r5.formats.IParser)19 JsonParser (org.hl7.fhir.r5.formats.JsonParser)18 Test (org.junit.jupiter.api.Test)18 FHIRException (org.hl7.fhir.exceptions.FHIRException)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 File (java.io.File)16 FileInputStream (java.io.FileInputStream)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Bundle (org.hl7.fhir.r4.model.Bundle)14 FileOutputStream (java.io.FileOutputStream)12 Bundle (org.hl7.fhir.dstu3.model.Bundle)12 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)12 XmlParser (org.hl7.fhir.r5.formats.XmlParser)12 ArrayList (java.util.ArrayList)11