001package org.hl7.fhir.dstu2.utils;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032import java.io.File;
033import java.io.FileInputStream;
034import java.io.IOException;
035import java.net.URISyntaxException;
036import java.util.UUID;
037import java.util.zip.ZipEntry;
038import java.util.zip.ZipInputStream;
039
040import org.hl7.fhir.dstu2.formats.IParser;
041import org.hl7.fhir.dstu2.formats.JsonParser;
042import org.hl7.fhir.dstu2.model.Bundle;
043import org.hl7.fhir.dstu2.model.Bundle.BundleEntryComponent;
044import org.hl7.fhir.dstu2.model.Bundle.BundleType;
045import org.hl7.fhir.dstu2.model.Bundle.HTTPVerb;
046import org.hl7.fhir.dstu2.model.Resource;
047import org.hl7.fhir.dstu2.utils.client.FHIRToolingClient;
048import org.hl7.fhir.exceptions.FHIRException;
049import org.hl7.fhir.utilities.Utilities;
050import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
051
052@Deprecated
053public class BatchLoader {
054
055  public static void main(String[] args) throws IOException, Exception {
056    if (args.length < 4) {
057      System.out.println(
058          "Batch uploader takes 4 parameters in order: server base url, file/folder to upload, xml/json, and batch size");
059    } else {
060      String server = args[0];
061      String file = args[1];
062      IParser p = new JsonParser(); // args[2].equals("json") ? new JsonParser() : new XmlParser();
063      int size = Integer.parseInt(args[3]);
064      size = 500;
065      if (file.endsWith(".xml")) {
066        throw new FHIRException("Unimplemented file type " + file);
067      } else if (file.endsWith(".json")) {
068        throw new FHIRException("Unimplemented file type " + file);
069      } else if (file.endsWith(".zip")) {
070        LoadZipFile(server, file, p, size, 0, -1);
071      } else if (ManagedFileAccess.file(file).isDirectory()) {
072        LoadDirectory(server, file, p, size);
073      } else
074        throw new FHIRException("Unknown file type " + file);
075    }
076  }
077
078  private static void LoadDirectory(String server, String file, IParser p, int size) throws IOException, Exception {
079//    LoadZipFile(server, Utilities.path(file, "Patient.json.zip"), p, size, 1000, -1);
080//    LoadZipFile(server, Utilities.path(file, "Binary.json.zip"), p, size, 0, -1);
081//        LoadZipFile(server, Utilities.path(file, "DocumentReference.json.zip"), p, size, 0, -1);
082//        LoadZipFile(server, Utilities.path(file, "Encounter.json.zip"), p, size, 0, -1);
083//        LoadZipFile(server, Utilities.path(file, "Organization.json.zip"), p, size, 0, -1);
084//          LoadZipFile(server, Utilities.path(file, "Procedure.json.zip"), p, size, 0, -1);
085//        LoadZipFile(server, Utilities.path(file, "AllergyIntolerance.json.zip"), p, size, 1500, -1);
086//          LoadZipFile(server, Utilities.path(file, "Condition.json.zip"), p, size, 0, -1);
087    LoadZipFile(server, Utilities.path(file, "Immunization.json.zip"), p, size, 0, -1);
088//        LoadZipFile(server, Utilities.path(file, "MedicationStatement.json.zip"), p, size, 0, -1);
089//        LoadZipFile(server, Utilities.path(file, "Observation-res.json.zip"), p, size, 0, -1);
090//        LoadZipFile(server, Utilities.path(file, "Observation-sh.json.zip"), p, size, 0, -1);
091//        LoadZipFile(server, Utilities.path(file, "Observation-vs.json.zip"), p, size, 0, -1);
092//        LoadZipFile(server, Utilities.path(file, "Observation-gen.json.zip"), p, size, 0, -1);
093//        LoadZipFile(server, Utilities.path(file, "List.json.zip"), p, size, 6500, -1);
094//        LoadZipFile(server, Utilities.path(file, "List-res.json.zip"), p, size, 0, -1);
095//        LoadZipFile(server, Utilities.path(file, "List-vs.json.zip"), p, size, 0, -1);
096  }
097
098  private static void LoadZipFile(String server, String file, IParser p, int size, int start, int end)
099      throws IOException, Exception {
100    System.out.println("Load Zip file " + file);
101    Bundle b = new Bundle();
102    b.setType(BundleType.COLLECTION);
103    b.setId(UUID.randomUUID().toString().toLowerCase());
104    ZipInputStream zip = new ZipInputStream(ManagedFileAccess.inStream(file));
105    ZipEntry entry;
106    while ((entry = zip.getNextEntry()) != null) {
107      try {
108        Resource r = p.parse(zip);
109        b.addEntry().setResource(r);
110      } catch (Exception e) {
111        throw new Exception("Error parsing " + entry.getName() + ": " + e.getMessage(), e);
112      }
113    }
114    loadBundle(server, b, size, start, end);
115  }
116
117  private static int loadBundle(String server, Bundle b, int size, int start, int end) throws URISyntaxException {
118    System.out.println("Post to " + server + ". size = " + Integer.toString(size) + ", start = "
119        + Integer.toString(start) + ", total = " + Integer.toString(b.getEntry().size()));
120    FHIRToolingClient client = new FHIRToolingClient(server, "fhir/batch-loader");
121    int c = start;
122    if (end == -1)
123      end = b.getEntry().size();
124    while (c < end) {
125      Bundle bt = new Bundle();
126      bt.setType(BundleType.BATCH);
127      bt.setId(UUID.randomUUID().toString().toLowerCase());
128      for (int i = c; i < Math.min(b.getEntry().size(), c + size); i++) {
129        BundleEntryComponent be = bt.addEntry();
130        be.setResource(b.getEntry().get(i).getResource());
131        be.getRequest().setMethod(HTTPVerb.PUT);
132        be.getRequest().setUrl(be.getResource().getResourceType().toString() + "/" + be.getResource().getId());
133      }
134      System.out.print("  posting..");
135      long ms = System.currentTimeMillis();
136      Bundle resp = client.transaction(bt);
137
138      for (int i = 0; i < resp.getEntry().size(); i++) {
139        BundleEntryComponent t = resp.getEntry().get(i);
140        if (!t.getResponse().getStatus().startsWith("2")) {
141          System.out.println("failed status at " + Integer.toString(i) + ": " + t.getResponse().getStatus());
142          return c + i;
143        }
144      }
145      c = c + size;
146      System.out.println(
147          "  ..done: " + Integer.toString(c) + ". (" + Long.toString(System.currentTimeMillis() - ms) + " ms)");
148    }
149    System.out.println(" done");
150    return c;
151  }
152
153}