Search in sources :

Example 1 with ZipStore

use of org.openlca.jsonld.ZipStore in project olca-modules by GreenDelta.

the class ReOpenTest method testReOpen.

@Test
public void testReOpen() throws Exception {
    Path p = Files.createTempFile("_olca_", ".zip");
    Files.delete(p);
    File f = p.toFile();
    ZipStore store = ZipStore.open(f);
    store.put("test/file.txt", "Test".getBytes());
    store.close();
    store = ZipStore.open(f);
    byte[] data = store.getBytes("test/file.txt");
    assertEquals("Test", new String(data));
    store.put("test/file.txt", "Next".getBytes());
    store.close();
    store = ZipStore.open(f);
    data = store.getBytes("test/file.txt");
    assertEquals("Next", new String(data));
    f.delete();
}
Also used : Path(java.nio.file.Path) File(java.io.File) ZipStore(org.openlca.jsonld.ZipStore) Test(org.junit.Test)

Example 2 with ZipStore

use of org.openlca.jsonld.ZipStore in project olca-modules by GreenDelta.

the class JsonExportTest method testWriteActor.

@Test
public void testWriteActor() throws Exception {
    Actor actor = new Actor();
    actor.refId = UUID.randomUUID().toString();
    actor.name = "actor";
    Category cat = new Category();
    cat.refId = UUID.randomUUID().toString();
    cat.name = "category";
    actor.category = cat;
    Path tempdir = Files.createTempDirectory("_olca_tests_");
    Path zip = tempdir.resolve("test.zip");
    ZipStore store = ZipStore.open(zip.toFile());
    JsonExport export = new JsonExport(Tests.getDb(), store);
    AtomicInteger count = new AtomicInteger(0);
    export.write(actor, (message, entity) -> {
        count.incrementAndGet();
    });
    Assert.assertEquals(2, count.get());
    Assert.assertNotNull(store.get(ModelType.ACTOR, actor.refId));
    Assert.assertNotNull(store.get(ModelType.CATEGORY, cat.refId));
    store.close();
    Dirs.delete(tempdir);
}
Also used : Path(java.nio.file.Path) Category(org.openlca.core.model.Category) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Actor(org.openlca.core.model.Actor) ZipStore(org.openlca.jsonld.ZipStore) Test(org.junit.Test)

Example 3 with ZipStore

use of org.openlca.jsonld.ZipStore in project olca-modules by GreenDelta.

the class SyncTestUtils method doImport.

static void doImport(File file, IDatabase db) {
    try {
        ZipStore store = ZipStore.open(file);
        JsonImport json = new JsonImport(store, db);
        json.setUpdateMode(UpdateMode.ALWAYS);
        json.run();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ZipStore(org.openlca.jsonld.ZipStore)

Example 4 with ZipStore

use of org.openlca.jsonld.ZipStore in project olca-app by GreenDelta.

the class JsonProvider method getFlowMaps.

public List<FlowMap> getFlowMaps() {
    try (ZipStore store = ZipStore.open(file)) {
        List<String> files = store.getFiles("flow_mappings");
        List<FlowMap> maps = new ArrayList<>();
        for (String f : files) {
            byte[] data = store.getBytes(f);
            String json = new String(data, StandardCharsets.UTF_8);
            JsonObject obj = new Gson().fromJson(json, JsonObject.class);
            FlowMap map = FlowMap.fromJson(obj);
            maps.add(map);
        }
        return maps;
    } catch (Exception e) {
        Logger log = LoggerFactory.getLogger(getClass());
        log.error("failed to read mapping files", e);
        return Collections.emptyList();
    }
}
Also used : ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Logger(org.slf4j.Logger) FlowMap(org.openlca.io.maps.FlowMap) ZipStore(org.openlca.jsonld.ZipStore)

Example 5 with ZipStore

use of org.openlca.jsonld.ZipStore in project olca-app by GreenDelta.

the class JsonProvider method persist.

@Override
public void persist(List<FlowRef> refs, IDatabase db) {
    if (refs == null || db == null)
        return;
    try (ZipStore store = ZipStore.open(file)) {
        FlowDao dao = new FlowDao(db);
        JsonImport imp = new JsonImport(store, db);
        for (FlowRef ref : refs) {
            Flow flow = dao.getForRefId(ref.flow.refId);
            if (flow != null)
                continue;
            imp.run(ModelType.FLOW, ref.flow.refId);
        }
    } catch (Exception e) {
        Logger log = LoggerFactory.getLogger(getClass());
        log.error("failed persist flows", e);
    }
}
Also used : JsonImport(org.openlca.jsonld.input.JsonImport) FlowDao(org.openlca.core.database.FlowDao) FlowRef(org.openlca.io.maps.FlowRef) Logger(org.slf4j.Logger) ZipStore(org.openlca.jsonld.ZipStore) Flow(org.openlca.core.model.Flow)

Aggregations

ZipStore (org.openlca.jsonld.ZipStore)6 Path (java.nio.file.Path)2 Test (org.junit.Test)2 JsonImport (org.openlca.jsonld.input.JsonImport)2 Logger (org.slf4j.Logger)2 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 FlowDao (org.openlca.core.database.FlowDao)1 Actor (org.openlca.core.model.Actor)1 Category (org.openlca.core.model.Category)1 Flow (org.openlca.core.model.Flow)1 FlowMap (org.openlca.io.maps.FlowMap)1 FlowRef (org.openlca.io.maps.FlowRef)1 UpdateMode (org.openlca.jsonld.input.UpdateMode)1