Search in sources :

Example 26 with IniFile

use of org.hl7.fhir.utilities.IniFile in project org.hl7.fhir.core by hapifhir.

the class TerminologyCacheManager method initialize.

public void initialize() throws IOException {
    File f = new File(cacheFolder);
    if (!f.exists()) {
        Utilities.createDirectory(cacheFolder);
    }
    if (!version.equals(getCacheVersion())) {
        clearCache();
        fillCache("http://tx.fhir.org/tx-cache/" + ghOrg + "/" + ghRepo + "/" + ghBranch + ".zip");
    }
    if (!version.equals(getCacheVersion())) {
        clearCache();
        fillCache("http://tx.fhir.org/tx-cache/" + ghOrg + "/" + ghRepo + "/default.zip");
    }
    if (!version.equals(getCacheVersion())) {
        clearCache();
    }
    IniFile ini = new IniFile(Utilities.path(cacheFolder, "cache.ini"));
    ini.setStringProperty("cache", "version", version, null);
    ini.setDateProperty("cache", "last-use", new Date(), null);
    ini.save();
}
Also used : IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) IniFile(org.hl7.fhir.utilities.IniFile) TextFile(org.hl7.fhir.utilities.TextFile) Date(java.util.Date)

Example 27 with IniFile

use of org.hl7.fhir.utilities.IniFile in project org.hl7.fhir.core by hapifhir.

the class FilesystemPackageCacheManager method createIniFile.

private void createIniFile() throws IOException {
    IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
    boolean save = false;
    String v = ini.getStringProperty("cache", "version");
    if (!CACHE_VERSION.equals(v)) {
        clearCache();
        ini.setStringProperty("cache", "version", CACHE_VERSION, null);
        ini.save();
    }
}
Also used : IniFile(org.hl7.fhir.utilities.IniFile)

Example 28 with IniFile

use of org.hl7.fhir.utilities.IniFile in project org.hl7.fhir.core by hapifhir.

the class FilesystemPackageCacheManager method removePackage.

// ========================= Utilities ============================================================================
/**
 * Remove a particular package from the cache
 *
 * @param id
 * @param ver
 * @throws IOException
 */
public void removePackage(String id, String ver) throws IOException {
    new CacheLock(id + "#" + ver).doWithLock(() -> {
        String f = Utilities.path(cacheFolder, id + "#" + ver);
        File ff = new File(f);
        if (ff.exists()) {
            Utilities.clearDirectory(f);
            IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
            ini.removeProperty("packages", id + "#" + ver);
            ini.save();
            ff.delete();
        }
        return null;
    });
}
Also used : IniFile(org.hl7.fhir.utilities.IniFile) RandomAccessFile(java.io.RandomAccessFile) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 29 with IniFile

use of org.hl7.fhir.utilities.IniFile in project org.hl7.fhir.core by hapifhir.

the class FilesystemPackageCacheManager method clearCache.

private void clearCache() throws IOException {
    for (File f : new File(cacheFolder).listFiles()) {
        if (f.isDirectory()) {
            new CacheLock(f.getName()).doWithLock(() -> {
                Utilities.clearDirectory(f.getAbsolutePath());
                try {
                    FileUtils.deleteDirectory(f);
                } catch (Exception e1) {
                    try {
                        FileUtils.deleteDirectory(f);
                    } catch (Exception e2) {
                    // just give up
                    }
                }
                // must return something
                return null;
            });
        } else if (!f.getName().equals("packages.ini"))
            FileUtils.forceDelete(f);
    }
    IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
    ini.removeSection("packages");
    ini.save();
}
Also used : IniFile(org.hl7.fhir.utilities.IniFile) RandomAccessFile(java.io.RandomAccessFile) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 30 with IniFile

use of org.hl7.fhir.utilities.IniFile 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)

Aggregations

IniFile (org.hl7.fhir.utilities.IniFile)27 File (java.io.File)16 TextFile (org.hl7.fhir.utilities.TextFile)12 ArrayList (java.util.ArrayList)7 CSFile (org.hl7.fhir.utilities.CSFile)6 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 Date (java.util.Date)5 ResourceDefn (org.hl7.fhir.definitions.model.ResourceDefn)5 FileInputStream (java.io.FileInputStream)4 FHIRException (org.hl7.fhir.exceptions.FHIRException)4 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)4 RandomAccessFile (java.io.RandomAccessFile)3 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)3 JsonObject (com.google.gson.JsonObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ParseException (java.text.ParseException)2