use of org.openlca.jsonld.output.JsonExport in project olca-modules by GreenDelta.
the class EpdTest method testWithProduct.
@Test
public void testWithProduct() {
var units = UnitGroup.of("Units of mass", "kg");
var mass = FlowProperty.of("Mass", units);
var product = Flow.product("Product", mass);
var epd = Epd.of("EPD", product);
db.insert(units, mass, product, epd);
var store = new MemStore();
new JsonExport(db, store).write(epd);
db.clear();
new JsonImport(store, db).run();
var clone = db.get(Epd.class, epd.refId);
assertEquals("EPD", clone.name);
assertEquals("Product", clone.product.flow.name);
assertEquals("Mass", clone.product.property.name);
assertEquals("kg", clone.product.unit.name);
db.clear();
}
use of org.openlca.jsonld.output.JsonExport in project olca-modules by GreenDelta.
the class ModelHandler method getAll.
@Rpc("get/models")
public RpcResponse getAll(RpcRequest req) {
if (req.params == null || !req.params.isJsonObject())
return Responses.invalidParams("params must be an object with" + " valid @type attribute", req);
var type = getType(req.params.getAsJsonObject());
if (type == null)
return Responses.invalidParams("params must be an object with" + " valid @type attribute", req);
try {
var store = new MemStore();
var exp = new JsonExport(db, store).withReferences(false);
Daos.refDao(db, type).getAll().forEach(exp::write);
var array = new JsonArray();
store.getAll(type).forEach(array::add);
return Responses.ok(array, req);
} catch (Exception e) {
return Responses.serverError(e, req);
}
}
use of org.openlca.jsonld.output.JsonExport in project olca-modules by GreenDelta.
the class ExportHandler method jsonLd.
@Rpc("export/json-ld")
public RpcResponse jsonLd(RpcRequest req) {
if (req == null || req.params == null || !req.params.isJsonObject())
return Responses.badRequest("No @id given", req);
JsonObject obj = req.params.getAsJsonObject();
String path = Json.getString(obj, "path");
if (path == null)
return Responses.badRequest("No `path` given", req);
Map<ModelType, Set<String>> toExport = getModels(obj);
if (toExport == null)
return Responses.badRequest("No `models` given", req);
try {
var store = ZipStore.open(new File(path));
var export = new JsonExport(context.db, store);
for (ModelType type : toExport.keySet()) {
for (String refId : toExport.get(type)) {
export.write(Daos.root(context.db, type).getForRefId(refId));
}
}
store.close();
return Responses.ok("Exported to " + path, req);
} catch (IOException e) {
return Responses.serverError(e, req);
}
}
use of org.openlca.jsonld.output.JsonExport in project olca-modules by GreenDelta.
the class FormatTest method testDetectJSONLD.
@Test
public void testDetectJSONLD() throws Exception {
var db = Tests.getDb();
var units = db.insert(UnitGroup.of("Mass units", Unit.of("kg")));
var mass = db.insert(FlowProperty.of("Mass", units));
var steel = db.insert(Flow.product("Steel", mass));
var process = db.insert(Process.of("Steel production", steel));
var file = Files.createTempFile("_olca_test", ".zip").toFile();
Files.delete(file.toPath());
try (var zip = ZipStore.open(file)) {
new JsonExport(db, zip).write(process);
}
check(file, Format.JSON_LD_ZIP);
db.clear();
}
use of org.openlca.jsonld.output.JsonExport in project olca-modules by GreenDelta.
the class MountCheckTest method testOk.
@Test
public void testOk() throws Exception {
var group = UnitGroup.of("Units of mass", "kg");
try (var zip = lib.openJsonZip()) {
new JsonExport(zip).write(group);
}
var state = MountCheck.check(db, lib);
assertTrue(state.isOk());
assertFalse(state.isUsed());
assertFalse(state.isError());
assertNull(state.error());
}
Aggregations