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();
}
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);
}
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);
}
}
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();
}
}
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);
}
}
Aggregations