Search in sources :

Example 26 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project ab2d by CMSgov.

the class PatientClaimsProcessorImpl method writeOutResource.

@Trace(metricName = "EOBWriteToFile", dispatcher = true)
private String writeOutResource(FhirVersion version, ProgressTrackerUpdate update, List<IBaseResource> eobs, ClaimsStream stream) {
    IParser parser = version.getJsonParser().setPrettyPrint(false);
    if (eobs == null) {
        log.debug("ignoring empty results because pulling eobs failed");
        return null;
    }
    if (eobs.isEmpty()) {
        return null;
    }
    int eobsWritten = 0;
    int eobsError = 0;
    update.incPatientsWithEobsCount();
    update.addEobFetchedCount(eobs.size());
    StringBuilder errorPayload = new StringBuilder();
    for (IBaseResource resource : eobs) {
        try {
            stream.write(parser.encodeResourceToString(resource) + System.lineSeparator());
            eobsWritten++;
        } catch (Exception ex) {
            log.warn("Encountered exception while processing job resources: {}", ex.getClass());
            String errMsg = ExceptionUtils.getRootCauseMessage(ex);
            IBaseResource operationOutcome = version.getErrorOutcome(errMsg);
            errorPayload.append(parser.encodeResourceToString(operationOutcome)).append(System.lineSeparator());
            eobsError++;
        }
    }
    update.addEobProcessedCount(eobsWritten);
    if (eobsError != 0) {
        return errorPayload.toString();
    }
    return errorPayload.toString();
}
Also used : IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) ParseException(java.text.ParseException) IOException(java.io.IOException) IParser(ca.uhn.fhir.parser.IParser) Trace(com.newrelic.api.agent.Trace)

Example 27 with IParser

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

the class BatchLoader method LoadZipFile.

private static void LoadZipFile(String server, String file, IParser p, int size, int start, int end) throws IOException, Exception {
    System.out.println("Load Zip file " + file);
    Bundle b = new Bundle();
    b.setType(BundleType.COLLECTION);
    b.setId(UUID.randomUUID().toString().toLowerCase());
    ZipInputStream zip = new ZipInputStream(new FileInputStream(file));
    ZipEntry entry;
    while ((entry = zip.getNextEntry()) != null) {
        try {
            Resource r = p.parse(zip);
            b.addEntry().setResource(r);
        } catch (Exception e) {
            throw new Exception("Error parsing " + entry.getName() + ": " + e.getMessage(), e);
        }
    }
    loadBundle(server, b, size, start, end);
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) Bundle(org.hl7.fhir.dstu2.model.Bundle) ZipEntry(java.util.zip.ZipEntry) Resource(org.hl7.fhir.dstu2.model.Resource) FileInputStream(java.io.FileInputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 28 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.dstu2.formats.IParser) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser)

Example 29 with IParser

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

the class BatchLoader method LoadZipFile.

private static void LoadZipFile(String server, String file, IParser p, int size, int start, int end) throws IOException, Exception {
    System.out.println("Load Zip file " + file);
    Bundle b = new Bundle();
    b.setType(BundleType.COLLECTION);
    b.setId(UUID.randomUUID().toString().toLowerCase());
    ZipInputStream zip = new ZipInputStream(new FileInputStream(file));
    ZipEntry entry;
    while ((entry = zip.getNextEntry()) != null) {
        try {
            Resource r = p.parse(zip);
            b.addEntry().setResource(r);
        } catch (Exception e) {
            throw new Exception("Error parsing " + entry.getName() + ": " + e.getMessage(), e);
        }
    }
    loadBundle(server, b, size, start, end);
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) Bundle(org.hl7.fhir.dstu2016may.model.Bundle) ZipEntry(java.util.zip.ZipEntry) Resource(org.hl7.fhir.dstu2016may.model.Resource) FileInputStream(java.io.FileInputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 30 with IParser

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

the class MessageTest method test.

@Test
public void test() throws FHIRException, IOException {
    // Create new Atom Feed
    Bundle feed = new Bundle();
    // Serialize Atom Feed
    IParser comp = new JsonParser();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    comp.compose(os, feed);
    os.close();
    String json = os.toString();
    // Deserialize Atom Feed
    JsonParser parser = new JsonParser();
    InputStream is = new ByteArrayInputStream(json.getBytes("UTF-8"));
    Resource result = parser.parse(is);
    if (result == null)
        throw new FHIRException("Bundle was null");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Bundle(org.hl7.fhir.dstu2.model.Bundle) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(org.hl7.fhir.dstu2.model.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FHIRException(org.hl7.fhir.exceptions.FHIRException) IParser(org.hl7.fhir.dstu2.formats.IParser) JsonParser(org.hl7.fhir.dstu2.formats.JsonParser) Test(org.junit.jupiter.api.Test)

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