Search in sources :

Example 41 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project org.hl7.fhir.core by hapifhir.

the class BatchLoader method main.

public static void main(String[] args) throws IOException, Exception {
    if (args.length < 4) {
        System.out.println("Batch uploader takes 4 parameters in order: server base url, file/folder to upload, xml/json, and batch size");
    } else {
        String server = args[0];
        String file = args[1];
        // args[2].equals("json") ? new JsonParser() : new XmlParser();
        IParser p = new JsonParser();
        int size = Integer.parseInt(args[3]);
        size = 500;
        if (file.endsWith(".xml")) {
            throw new FHIRException("Unimplemented file type " + file);
        } else if (file.endsWith(".json")) {
            throw new FHIRException("Unimplemented file type " + file);
        } else if (file.endsWith(".zip")) {
            LoadZipFile(server, file, p, size, 0, -1);
        } else if (new File(file).isDirectory()) {
            LoadDirectory(server, file, p, size);
        } else
            throw new FHIRException("Unknown file type " + file);
    }
}
Also used : FHIRException(org.hl7.fhir.exceptions.FHIRException) File(java.io.File) IParser(org.hl7.fhir.r4b.formats.IParser) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Example 42 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project org.hl7.fhir.core by hapifhir.

the class ByteUtils method resourceToByteArray.

public static <T extends Resource> byte[] resourceToByteArray(T resource, boolean pretty, boolean isJson) {
    ByteArrayOutputStream baos = null;
    byte[] byteArray = null;
    try {
        baos = new ByteArrayOutputStream();
        IParser parser = null;
        if (isJson) {
            parser = new JsonParser();
        } else {
            parser = new XmlParser();
        }
        parser.setOutputStyle(pretty ? IParser.OutputStyle.PRETTY : IParser.OutputStyle.NORMAL);
        parser.compose(baos, resource);
        baos.close();
        byteArray = baos.toByteArray();
        baos.close();
    } catch (Exception e) {
        try {
            baos.close();
        } catch (Exception ex) {
            throw new EFhirClientException("Error closing output stream", ex);
        }
        throw new EFhirClientException("Error converting output stream to byte array", e);
    }
    return byteArray;
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) IOException(java.io.IOException) EFhirClientException(org.hl7.fhir.r5.utils.client.EFhirClientException) IParser(org.hl7.fhir.r5.formats.IParser) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 43 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project fhir-bridge by ehrbase.

the class FhirValidationConfiguration method validationSupport.

public IValidationSupport validationSupport() {
    ValidationSupportChain validationSupportChain = new ValidationSupportChain();
    // Validates core structure definitions
    DefaultProfileValidationSupport defaultProfileValidationSupport = new DefaultProfileValidationSupport(fhirContext);
    defaultProfileValidationSupport.fetchAllStructureDefinitions();
    defaultProfileValidationSupport.fetchCodeSystem("");
    validationSupportChain.addValidationSupport(defaultProfileValidationSupport);
    // Validates custom profiles (loaded from classpath)
    PrePopulatedValidationSupport prePopulatedValidationSupport = new PrePopulatedValidationSupport(fhirContext);
    IParser parser = fhirContext.newXmlParser();
    try {
        for (Resource resource : applicationContext.getResources("classpath:/profiles/*")) {
            StructureDefinition profile = parser.parseResource(StructureDefinition.class, resource.getInputStream());
            if (properties.isOptionalIdentifier()) {
                modifyProfile(profile);
            }
            prePopulatedValidationSupport.addStructureDefinition(profile);
        }
    } catch (IOException e) {
        throw new FhirBridgeException("An I/O exception occurred while loading custom profiles");
    }
    validationSupportChain.fetchAllStructureDefinitions();
    defaultProfileValidationSupport.fetchCodeSystem("");
    validationSupportChain.addValidationSupport(prePopulatedValidationSupport);
    // Validates terminology: CodeSystems and ValueSets (using the internal and/or remote terminology service)
    if (isTerminologyValidationEnabled()) {
        TerminologyValidationMode mode = properties.getTerminology().getMode();
        if (mode == TerminologyValidationMode.REMOTE || mode == TerminologyValidationMode.MIXED) {
            RemoteTerminologyServiceValidationSupport remoteTerminologyServerValidationSupport = new RemoteTerminologyServiceValidationSupport(fhirContext);
            remoteTerminologyServerValidationSupport.setBaseUrl(properties.getTerminology().getServerBaseUrl());
            validationSupportChain.addValidationSupport(remoteTerminologyServerValidationSupport);
        }
        if (mode == TerminologyValidationMode.INTERNAL || mode == TerminologyValidationMode.MIXED) {
            validationSupportChain.addValidationSupport(new InMemoryTerminologyServerValidationSupport(fhirContext));
            validationSupportChain.addValidationSupport(new CommonCodeSystemsTerminologyService(fhirContext));
        }
    }
    return new CachingValidationSupport(validationSupportChain);
}
Also used : PrePopulatedValidationSupport(org.hl7.fhir.common.hapi.validation.support.PrePopulatedValidationSupport) RemoteTerminologyServiceValidationSupport(org.hl7.fhir.common.hapi.validation.support.RemoteTerminologyServiceValidationSupport) Resource(org.springframework.core.io.Resource) FhirBridgeException(org.ehrbase.fhirbridge.FhirBridgeException) IOException(java.io.IOException) DefaultProfileValidationSupport(ca.uhn.fhir.context.support.DefaultProfileValidationSupport) CommonCodeSystemsTerminologyService(org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService) StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) InMemoryTerminologyServerValidationSupport(org.hl7.fhir.common.hapi.validation.support.InMemoryTerminologyServerValidationSupport) ValidationSupportChain(org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain) TerminologyValidationMode(org.ehrbase.fhirbridge.fhir.common.validation.TerminologyValidationMode) CachingValidationSupport(org.hl7.fhir.common.hapi.validation.support.CachingValidationSupport) IParser(ca.uhn.fhir.parser.IParser)

Example 44 with IParser

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

the class FhirBulkDataImporter method synchronize.

public void synchronize() {
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setConnectionManager(connectionManager);
    CloseableHttpClient myClient = builder.build();
    FhirContext ctx = FhirContext.forR4();
    String statusUrl = initiateBulkExport(myClient, ctx);
    if (statusUrl != null) {
        final List<String> outputs = getExportResults(myClient, statusUrl);
        IGenericClient client = ctx.newRestfulGenericClient(this.system.getUrl());
        for (String binaryUrl : outputs) {
            Binary binary = client.fetchResourceFromUrl(Binary.class, binaryUrl);
            String base64 = binary.getContentAsBase64();
            byte[] result = Base64.getDecoder().decode(base64);
            IParser parser = ctx.newJsonParser();
            String message = new String(result);
            try (BufferedReader reader = new BufferedReader(new StringReader(message))) {
                String line = null;
                while ((line = reader.readLine()) != null) {
                    IBaseResource resource = parser.parseResource(line);
                    IIdType id = resource.getIdElement();
                    String resourceType = id.getResourceType();
                    if (resourceType.equals(ResourceTypes.LOCATION.toCode())) {
                        Location location = (Location) resource;
                        this.processor.process(location);
                    } else if (resourceType.equals(ResourceTypes.ORGANIZATION.toCode())) {
                        Organization organization = (Organization) resource;
                        this.processor.process(organization);
                    }
                }
            } catch (IOException e) {
                throw new ProgrammingErrorException(e);
            }
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) FhirContext(ca.uhn.fhir.context.FhirContext) Organization(org.hl7.fhir.r4.model.Organization) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) IOException(java.io.IOException) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Binary(org.hl7.fhir.r4.model.Binary) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) IParser(ca.uhn.fhir.parser.IParser) IIdType(org.hl7.fhir.instance.model.api.IIdType) Location(org.hl7.fhir.r4.model.Location)

Example 45 with IParser

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

the class FhirExportSynchronizationManager method writeToFile.

public File writeToFile() throws IOException {
    final FhirExternalSystem system = (FhirExternalSystem) this.config.getSystem();
    try (FhirConnection connection = FhirConnectionFactory.get(system)) {
        String name = SessionPredicate.generateId();
        File root = new File(new File(VaultProperties.getPath("vault.default"), "files"), name);
        root.mkdirs();
        Bundle bundle = this.generateBundle(connection);
        FhirContext ctx = FhirContext.forR4();
        IParser parser = ctx.newJsonParser();
        try {
            parser.encodeResourceToWriter(bundle, new FileWriter(new File(root, "bundle.json")));
        } catch (DataFormatException | IOException e) {
            throw new ProgrammingErrorException(e);
        }
        return root;
    } catch (Exception e) {
        throw new HttpError(e);
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) Bundle(org.hl7.fhir.r4.model.Bundle) FileWriter(java.io.FileWriter) IOException(java.io.IOException) FhirExternalSystem(net.geoprism.registry.graph.FhirExternalSystem) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) IOException(java.io.IOException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) HttpError(net.geoprism.registry.etl.export.HttpError) File(java.io.File) 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