Search in sources :

Example 1 with MemStore

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);
}
Also used : MemStore(org.openlca.jsonld.MemStore) JsonObject(com.google.gson.JsonObject)

Example 2 with MemStore

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();
}
Also used : JsonImport(org.openlca.jsonld.input.JsonImport) MemStore(org.openlca.jsonld.MemStore) JsonExport(org.openlca.jsonld.output.JsonExport) Test(org.junit.Test)

Example 3 with MemStore

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);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) MemStore(org.openlca.jsonld.MemStore) JsonExport(org.openlca.jsonld.output.JsonExport) Rpc(org.openlca.ipc.Rpc)

Example 4 with MemStore

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);
}
Also used : MemStore(org.openlca.jsonld.MemStore) JsonExport(org.openlca.jsonld.output.JsonExport) Test(org.junit.Test)

Example 5 with MemStore

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);
    }
}
Also used : JsonImport(org.openlca.jsonld.input.JsonImport) MemStore(org.openlca.jsonld.MemStore) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) Descriptor(org.openlca.core.model.descriptors.Descriptor) JsonObject(com.google.gson.JsonObject)

Aggregations

MemStore (org.openlca.jsonld.MemStore)7 JsonObject (com.google.gson.JsonObject)3 JsonImport (org.openlca.jsonld.input.JsonImport)3 JsonExport (org.openlca.jsonld.output.JsonExport)3 Test (org.junit.Test)2 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Descriptor (org.openlca.core.model.descriptors.Descriptor)1 ProcessDescriptor (org.openlca.core.model.descriptors.ProcessDescriptor)1 Rpc (org.openlca.ipc.Rpc)1