Search in sources :

Example 6 with FHIRToolingClient

use of org.hl7.fhir.r5.utils.client.FHIRToolingClient in project org.hl7.fhir.core by hapifhir.

the class BatchLoader method sendFile.

private static void sendFile(FHIRToolingClient client, File f, int size, IniFile ini) throws FHIRFormatError, FileNotFoundException, IOException {
    long ms = System.currentTimeMillis();
    System.out.print("Loading " + f.getName() + ".. ");
    IParser parser = f.getName().endsWith(".json") ? new JsonParser() : new XmlParser();
    Resource res = parser.parse(new FileInputStream(f));
    System.out.println("  done: (" + Long.toString(System.currentTimeMillis() - ms) + " ms)");
    if (res instanceof Bundle) {
        Bundle bnd = (Bundle) res;
        int cursor = ini.hasProperty("progress", f.getName()) ? ini.getIntegerProperty("progress", f.getName()) : 0;
        while (cursor < bnd.getEntry().size()) {
            Bundle bt = new Bundle();
            bt.setType(BundleType.BATCH);
            bt.setId(UUID.randomUUID().toString().toLowerCase());
            for (int i = cursor; i < Math.min(bnd.getEntry().size(), cursor + size); i++) {
                BundleEntryComponent be = bt.addEntry();
                be.setResource(bnd.getEntry().get(i).getResource());
                be.getRequest().setMethod(HTTPVerb.PUT);
                be.getRequest().setUrl(be.getResource().getResourceType().toString() + "/" + be.getResource().getId());
            }
            System.out.print(f.getName() + " (" + cursor + "/" + bnd.getEntry().size() + "): ");
            ms = System.currentTimeMillis();
            Bundle resp = client.transaction(bt);
            int ncursor = cursor + size;
            for (int i = 0; i < resp.getEntry().size(); i++) {
                BundleEntryComponent t = resp.getEntry().get(i);
                if (!t.getResponse().getStatus().startsWith("2")) {
                    System.out.println("failed status at " + Integer.toString(i) + ": " + t.getResponse().getStatus());
                    ncursor = cursor + i - 1;
                    break;
                }
            }
            cursor = ncursor;
            System.out.println("  .. done: (" + Long.toString(System.currentTimeMillis() - ms) + " ms) " + SimpleDateFormat.getInstance().format(new Date()));
            ini.setIntegerProperty("progress", f.getName(), cursor, null);
            ini.save();
        }
        ini.setBooleanProperty("finished", f.getName(), true, null);
        ini.save();
    } else {
        client.update(res);
        ini.setBooleanProperty("finished", f.getName(), true, null);
        ini.save();
    }
}
Also used : XmlParser(org.hl7.fhir.r4.formats.XmlParser) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.r4.model.Bundle) Resource(org.hl7.fhir.r4.model.Resource) FileInputStream(java.io.FileInputStream) Date(java.util.Date) IParser(org.hl7.fhir.r4.formats.IParser) JsonParser(org.hl7.fhir.r4.formats.JsonParser)

Example 7 with FHIRToolingClient

use of org.hl7.fhir.r5.utils.client.FHIRToolingClient in project org.hl7.fhir.core by hapifhir.

the class BatchLoader method loadBundle.

private static int loadBundle(String server, Bundle b, int size, int start, int end) throws URISyntaxException {
    System.out.println("Post to " + server + ". size = " + Integer.toString(size) + ", start = " + Integer.toString(start) + ", total = " + Integer.toString(b.getEntry().size()));
    FHIRToolingClient client = new FHIRToolingClient(server);
    int c = start;
    if (end == -1)
        end = b.getEntry().size();
    while (c < end) {
        Bundle bt = new Bundle();
        bt.setType(BundleType.BATCH);
        bt.setId(UUID.randomUUID().toString().toLowerCase());
        for (int i = c; i < Math.min(b.getEntry().size(), c + size); i++) {
            BundleEntryComponent be = bt.addEntry();
            be.setResource(b.getEntry().get(i).getResource());
            be.getRequest().setMethod(HTTPVerb.PUT);
            be.getRequest().setUrl(be.getResource().getResourceType().toString() + "/" + be.getResource().getId());
        }
        System.out.print("  posting..");
        long ms = System.currentTimeMillis();
        Bundle resp = client.transaction(bt);
        for (int i = 0; i < resp.getEntry().size(); i++) {
            BundleEntryComponent t = resp.getEntry().get(i);
            if (!t.getResponse().getStatus().startsWith("2")) {
                System.out.println("failed status at " + Integer.toString(i) + ": " + t.getResponse().getStatus());
                return c + i;
            }
        }
        c = c + size;
        System.out.println("  ..done: " + Integer.toString(c) + ". (" + Long.toString(System.currentTimeMillis() - ms) + " ms)");
    }
    System.out.println(" done");
    return c;
}
Also used : FHIRToolingClient(org.hl7.fhir.dstu2016may.utils.client.FHIRToolingClient) BundleEntryComponent(org.hl7.fhir.dstu2016may.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.dstu2016may.model.Bundle)

Example 8 with FHIRToolingClient

use of org.hl7.fhir.r5.utils.client.FHIRToolingClient in project org.hl7.fhir.core by hapifhir.

the class BatchLoader method loadBundle.

private static int loadBundle(String server, Bundle b, int size, int start, int end) throws URISyntaxException {
    System.out.println("Post to " + server + ". size = " + Integer.toString(size) + ", start = " + Integer.toString(start) + ", total = " + Integer.toString(b.getEntry().size()));
    FHIRToolingClient client = new FHIRToolingClient(server, "fhir/batch-loader");
    int c = start;
    if (end == -1)
        end = b.getEntry().size();
    while (c < end) {
        Bundle bt = new Bundle();
        bt.setType(BundleType.BATCH);
        bt.setId(UUID.randomUUID().toString().toLowerCase());
        for (int i = c; i < Math.min(b.getEntry().size(), c + size); i++) {
            BundleEntryComponent be = bt.addEntry();
            be.setResource(b.getEntry().get(i).getResource());
            be.getRequest().setMethod(HTTPVerb.PUT);
            be.getRequest().setUrl(be.getResource().getResourceType().toString() + "/" + be.getResource().getId());
        }
        System.out.print("  posting..");
        long ms = System.currentTimeMillis();
        Bundle resp = client.transaction(bt);
        for (int i = 0; i < resp.getEntry().size(); i++) {
            BundleEntryComponent t = resp.getEntry().get(i);
            if (!t.getResponse().getStatus().startsWith("2")) {
                System.out.println("failed status at " + Integer.toString(i) + ": " + t.getResponse().getStatus());
                return c + i;
            }
        }
        c = c + size;
        System.out.println("  ..done: " + Integer.toString(c) + ". (" + Long.toString(System.currentTimeMillis() - ms) + " ms)");
    }
    System.out.println(" done");
    return c;
}
Also used : FHIRToolingClient(org.hl7.fhir.r4b.utils.client.FHIRToolingClient) BundleEntryComponent(org.hl7.fhir.r4b.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.r4b.model.Bundle)

Example 9 with FHIRToolingClient

use of org.hl7.fhir.r5.utils.client.FHIRToolingClient in project org.hl7.fhir.core by hapifhir.

the class BatchLoader method sendFile.

private static void sendFile(FHIRToolingClient client, File f, int size, IniFile ini) throws FHIRFormatError, FileNotFoundException, IOException {
    long ms = System.currentTimeMillis();
    System.out.print("Loading " + f.getName() + ".. ");
    IParser parser = f.getName().endsWith(".json") ? new JsonParser() : new XmlParser();
    Resource res = parser.parse(new FileInputStream(f));
    System.out.println("  done: (" + Long.toString(System.currentTimeMillis() - ms) + " ms)");
    if (res instanceof Bundle) {
        Bundle bnd = (Bundle) res;
        int cursor = ini.hasProperty("progress", f.getName()) ? ini.getIntegerProperty("progress", f.getName()) : 0;
        while (cursor < bnd.getEntry().size()) {
            Bundle bt = new Bundle();
            bt.setType(BundleType.BATCH);
            bt.setId(UUID.randomUUID().toString().toLowerCase());
            for (int i = cursor; i < Math.min(bnd.getEntry().size(), cursor + size); i++) {
                if (i >= 0 && i < bnd.getEntry().size()) {
                    BundleEntryComponent be = bt.addEntry();
                    be.setResource(bnd.getEntry().get(i).getResource());
                    be.getRequest().setMethod(HTTPVerb.PUT);
                    be.getRequest().setUrl(be.getResource().getResourceType().toString() + "/" + be.getResource().getId());
                }
            }
            System.out.print(f.getName() + " (" + cursor + "/" + bnd.getEntry().size() + "): ");
            ms = System.currentTimeMillis();
            Bundle resp = client.transaction(bt);
            int ncursor = cursor + size;
            for (int i = 0; i < resp.getEntry().size(); i++) {
                BundleEntryComponent t = resp.getEntry().get(i);
                if (!t.getResponse().getStatus().startsWith("2")) {
                    System.out.println("failed status at " + Integer.toString(i) + ": " + t.getResponse().getStatus());
                    ncursor = cursor + i - 1;
                    break;
                }
            }
            cursor = ncursor;
            System.out.println("  .. done: (" + Long.toString(System.currentTimeMillis() - ms) + " ms) " + SimpleDateFormat.getInstance().format(new Date()));
            ini.setIntegerProperty("progress", f.getName(), cursor, null);
            ini.save();
        }
        ini.setBooleanProperty("finished", f.getName(), true, null);
        ini.save();
    } else {
        client.update(res);
        ini.setBooleanProperty("finished", f.getName(), true, null);
        ini.save();
    }
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.dstu3.model.Bundle) Resource(org.hl7.fhir.dstu3.model.Resource) FileInputStream(java.io.FileInputStream) Date(java.util.Date) IParser(org.hl7.fhir.dstu3.formats.IParser) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Example 10 with FHIRToolingClient

use of org.hl7.fhir.r5.utils.client.FHIRToolingClient in project org.hl7.fhir.core by hapifhir.

the class BatchLoader method LoadDirectory.

private static void LoadDirectory(String server, String folder, int size) throws IOException, Exception {
    System.out.print("Connecting to " + server + ".. ");
    FHIRToolingClient client = new FHIRToolingClient(server, "fhir/batch-loader");
    System.out.println("Done");
    IniFile ini = new IniFile(Utilities.path(folder, "batch-load-progress.ini"));
    for (File f : new File(folder).listFiles()) {
        if (f.getName().endsWith(".json") || f.getName().endsWith(".xml")) {
            if (!ini.getBooleanProperty("finished", f.getName())) {
                sendFile(client, f, size, ini);
            }
        }
    }
}
Also used : FHIRToolingClient(org.hl7.fhir.dstu3.utils.client.FHIRToolingClient) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) IniFile(org.hl7.fhir.utilities.IniFile)

Aggregations

FileInputStream (java.io.FileInputStream)3 File (java.io.File)2 Date (java.util.Date)2 FHIRToolingClient (org.hl7.fhir.dstu3.utils.client.FHIRToolingClient)2 JsonParser (org.hl7.fhir.r4.formats.JsonParser)2 FHIRToolingClient (org.hl7.fhir.r4.utils.client.FHIRToolingClient)2 IniFile (org.hl7.fhir.utilities.IniFile)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ParseException (java.text.ParseException)1 Bundle (org.hl7.fhir.dstu2.model.Bundle)1 BundleEntryComponent (org.hl7.fhir.dstu2.model.Bundle.BundleEntryComponent)1 FHIRToolingClient (org.hl7.fhir.dstu2.utils.client.FHIRToolingClient)1 Bundle (org.hl7.fhir.dstu2016may.model.Bundle)1 BundleEntryComponent (org.hl7.fhir.dstu2016may.model.Bundle.BundleEntryComponent)1 FHIRToolingClient (org.hl7.fhir.dstu2016may.utils.client.FHIRToolingClient)1 IParser (org.hl7.fhir.dstu3.formats.IParser)1 JsonParser (org.hl7.fhir.dstu3.formats.JsonParser)1 XmlParser (org.hl7.fhir.dstu3.formats.XmlParser)1