use of org.openlca.jsonld.MemStore in project olca-modules by GreenDelta.
the class JsonExport method toJson.
public static <T extends RefEntity> JsonObject toJson(T entity) {
if (entity == null)
return new JsonObject();
var exp = new JsonExport(null, new MemStore()).withReferences(false);
Writer<T> writer = exp.getWriter(entity);
return writer.write(entity);
}
use of org.openlca.jsonld.MemStore 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.MemStore 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.MemStore in project olca-modules by GreenDelta.
the class UnitGroupImportTest method testUpdateUnit.
@Test
public void testUpdateUnit() {
// create and export the unit group
var group = db.insert(UnitGroup.of("Mass units", Unit.of("kg")));
var store = new MemStore();
new JsonExport(db, store).write(group);
// update the JSON object
var json = store.get(ModelType.UNIT_GROUP, group.refId);
json.addProperty("description", "unit group description");
json.addProperty("version", "42.0.0");
var units = Json.getArray(json, "units");
var unit = units.get(0).getAsJsonObject();
unit.addProperty("description", "unit description");
// update it
new JsonImport(store, db).setUpdateMode(UpdateMode.IF_NEWER).run();
var updated = db.get(UnitGroup.class, group.id);
db.delete(updated);
// check updated fields
assertEquals("unit group description", updated.description);
assertEquals("unit description", updated.referenceUnit.description);
}
use of org.openlca.jsonld.MemStore in project olca-modules by GreenDelta.
the class ModelHandler method saveModel.
private RpcResponse saveModel(RpcRequest req, UpdateMode mode) {
Descriptor d = descriptorOf(req);
if (d == null)
return Responses.invalidParams("params must be an object with" + " valid @id and @type", req);
JsonObject obj = req.params.getAsJsonObject();
try {
MemStore store = new MemStore();
store.put(d.type, obj);
JsonImport imp = new JsonImport(store, db);
imp.setUpdateMode(mode);
imp.run(d.type, d.refId);
return Responses.ok(req);
} catch (Exception e) {
return Responses.serverError(e, req);
}
}
Aggregations