Search in sources :

Example 46 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project geoprism-registry by terraframe.

the class FhirImportTest method testBasicImport.

@Request
@Test
public void testBasicImport() throws InterruptedException {
    try {
        FhirExternalSystem system = createExternalSystem();
        SynchronizationConfig config = createSyncConfig(system);
        FhirSyncImportConfig iConfig = (FhirSyncImportConfig) config.buildConfiguration();
        FhirResourceProcessor processor = FhirFactory.getProcessor(iConfig.getImplementation());
        IParser parser = FhirContext.forR4().newJsonParser();
        Bundle bundle = new Bundle();
        bundle.addEntry(new BundleEntryComponent().setResource((Resource) parser.parseResource(this.getClass().getResourceAsStream("/fhir/organization.json"))));
        bundle.addEntry(new BundleEntryComponent().setResource((Resource) parser.parseResource(this.getClass().getResourceAsStream("/fhir/location.json"))));
        FhirResourceImporter importer = new FhirResourceImporter(new BasicFhirConnection(system), processor, null, null);
        importer.synchronize(bundle);
        ServerGeoObjectIF geoobject = new ServerGeoObjectService().getGeoObjectByCode("USATestDataHsTwo", "USATestDataHealthStop");
        geoobject.setDate(new Date());
        LocalizedValue displayLabel = geoobject.getDisplayLabel();
        Assert.assertEquals("USATestDataHsTwo ZZZZZZZ", displayLabel.getValue());
    } finally {
        TestDataSet.deleteExternalSystems("FHIRImportTest");
    }
}
Also used : FhirResourceImporter(net.geoprism.registry.etl.fhir.FhirResourceImporter) ServerGeoObjectService(net.geoprism.registry.geoobject.ServerGeoObjectService) ServerGeoObjectIF(net.geoprism.registry.model.ServerGeoObjectIF) Bundle(org.hl7.fhir.r4.model.Bundle) Resource(org.hl7.fhir.r4.model.Resource) BasicFhirResourceProcessor(net.geoprism.registry.etl.fhir.BasicFhirResourceProcessor) FhirResourceProcessor(net.geoprism.registry.etl.fhir.FhirResourceProcessor) BasicFhirConnection(net.geoprism.registry.etl.fhir.BasicFhirConnection) FhirExternalSystem(net.geoprism.registry.graph.FhirExternalSystem) Date(java.util.Date) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) LocalizedValue(org.commongeoregistry.adapter.dataaccess.LocalizedValue) SynchronizationConfig(net.geoprism.registry.SynchronizationConfig) IParser(ca.uhn.fhir.parser.IParser) Test(org.junit.Test) Request(com.runwaysdk.session.Request)

Example 47 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project redmatch by aehrc.

the class RedmatchApi method save.

private void save(Map<String, List<DomainResource>> grouped, Path tgtDir, ProgressReporter progressReporter, CancelChecker cancelToken) throws IOException {
    try {
        log.info("Saving to output folder " + tgtDir);
        if (progressReporter != null) {
            progressReporter.reportProgress(Progress.reportStart("Saving files"));
        }
        IParser jsonParser = ctx.newJsonParser();
        double div = grouped.size() / 100.0;
        int i = 0;
        for (String key : grouped.keySet()) {
            File f = new File(tgtDir.toFile(), key + ".ndjson");
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(f))) {
                for (DomainResource dr : grouped.get(key)) {
                    jsonParser.encodeResourceToWriter(dr, bw);
                    bw.newLine();
                }
            }
            i++;
            if (progressReporter != null) {
                progressReporter.reportProgress(Progress.reportProgress((int) Math.floor(i / div)));
            }
            if (cancelToken != null && cancelToken.isCanceled()) {
                return;
            }
        }
    } finally {
        if (progressReporter != null) {
            progressReporter.reportProgress(Progress.reportEnd());
        }
    }
}
Also used : DomainResource(org.hl7.fhir.r4.model.DomainResource) IParser(ca.uhn.fhir.parser.IParser)

Example 48 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project nia-patient-switching-standard-adaptor by NHSDigital.

the class SDSServiceTest method parseResponses.

@BeforeAll
public static void parseResponses() throws IOException {
    FhirContext context = FhirContext.forDstu3();
    context.newJsonParser();
    context.setParserErrorHandler(new StrictErrorHandler());
    IParser jsonParser = context.newJsonParser();
    readResponsesFromFile();
    ehrResponseBundle = jsonParser.parseResource(Bundle.class, sdsResponseEHRExtract);
    copcResponseBundle = jsonParser.parseResource(Bundle.class, sdsResponseCopcMessage);
    noResultsBundle = jsonParser.parseResource(Bundle.class, sdsResponseNoResults);
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) StrictErrorHandler(ca.uhn.fhir.parser.StrictErrorHandler) Bundle(org.hl7.fhir.dstu3.model.Bundle) IParser(ca.uhn.fhir.parser.IParser) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 49 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project CRD by HL7-DaVinci.

the class CdsConnectFileStore method processFhirFiles.

private void processFhirFiles(List<CdsConnectFile> files, String topic) {
    // process the fhir resource files
    // setup the proper FHIR Context for the version of FHIR we are dealing with
    FhirContext r4ctx = new org.hl7.davinci.r4.FhirComponents().getFhirContext();
    IParser r4parser = r4ctx.newJsonParser();
    // suppress the unknown element warnings
    r4parser.setParserErrorHandler(new SuppressParserErrorHandler());
    // process all of the files found within the topic/artifact
    for (CdsConnectFile file : files) {
        String path = file.getPath();
        String filename = file.getFilename();
        if (filename.endsWith(".json")) {
            logger.info("        process: FHIR Resource: " + filename);
            String[] parts = filename.split("-");
            if (parts.length > 2) {
                // String resourceType = parts[0];
                String fhirVersion = parts[1];
                String name = parts[2];
                IBaseResource baseResource = null;
                byte[] fileContents = file.getCqlBundle();
                if (fhirVersion.equalsIgnoreCase("R4")) {
                    baseResource = r4parser.parseResource(new ByteArrayInputStream(fileContents));
                }
                processFhirResource(baseResource, path, filename, fhirVersion, topic);
            }
        }
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) SuppressParserErrorHandler(org.hl7.davinci.SuppressParserErrorHandler) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) IParser(ca.uhn.fhir.parser.IParser)

Example 50 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project CRD by HL7-DaVinci.

the class GitHubFileStore method processFhirFolder.

private void processFhirFolder(String topic, String fhirVersion, String fhirPath) {
    fhirVersion = fhirVersion.toUpperCase();
    logger.info("      GitHubFileStore::processFhirFolder(): " + fhirVersion + ": " + fhirPath);
    // setup the proper FHIR Context for the version of FHIR we are dealing with
    FhirContext ctx = null;
    if (fhirVersion.equalsIgnoreCase("R4")) {
        ctx = new org.hl7.davinci.r4.FhirComponents().getFhirContext();
    } else {
        logger.warn("unsupported FHIR version: " + fhirVersion + ", skipping folder");
        return;
    }
    IParser parser = ctx.newJsonParser();
    // suppress the unknown element warnings
    parser.setParserErrorHandler(new SuppressParserErrorHandler());
    for (String folder : connection.getDirectory(fhirPath)) {
        if (folder.equalsIgnoreCase("resources")) {
            String fullFolderPath = fhirPath + "/" + folder;
            for (String resource : connection.getDirectory(fullFolderPath)) {
                String filename = resource;
                String fullFilePath = fullFolderPath + "/" + filename;
                logger.info("        process: FHIR Resource: " + filename);
                String[] parts = filename.split("-");
                if (parts.length > 2) {
                    // = parts[0];
                    String resourceType;
                    if (!parts[1].equalsIgnoreCase(fhirVersion)) {
                        logger.warn("GitHubFileStore::processFhirFolder() warning: FhirVersion doesn't match!");
                        continue;
                    }
                    InputStream inputStream = connection.getFile(fullFilePath);
                    if (inputStream != null) {
                        IBaseResource baseResource = parser.parseResource(inputStream);
                        processFhirResource(baseResource, filename, filename, fhirVersion, topic);
                    } else {
                        logger.warn("could not find file: " + fullFilePath);
                        continue;
                    }
                }
            }
        }
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) SuppressParserErrorHandler(org.hl7.davinci.SuppressParserErrorHandler) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) 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