Search in sources :

Example 11 with JsonExport

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

Example 12 with JsonExport

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

Example 13 with JsonExport

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);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) JsonObject(com.google.gson.JsonObject) ModelType(org.openlca.core.model.ModelType) JsonExport(org.openlca.jsonld.output.JsonExport) IOException(java.io.IOException) File(java.io.File) Rpc(org.openlca.ipc.Rpc)

Example 14 with JsonExport

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

Example 15 with JsonExport

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

Aggregations

JsonExport (org.openlca.jsonld.output.JsonExport)25 Test (org.junit.Test)13 JsonImport (org.openlca.jsonld.input.JsonImport)7 AbstractZipTest (org.openlca.jsonld.AbstractZipTest)6 File (java.io.File)4 MemStore (org.openlca.jsonld.MemStore)3 Parameter (org.openlca.core.model.Parameter)2 Process (org.openlca.core.model.Process)2 ProductSystem (org.openlca.core.model.ProductSystem)2 Rpc (org.openlca.ipc.Rpc)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ImpactCategoryDao (org.openlca.core.database.ImpactCategoryDao)1 ParameterDao (org.openlca.core.database.ParameterDao)1 ProcessDao (org.openlca.core.database.ProcessDao)1 ProductSystemDao (org.openlca.core.database.ProductSystemDao)1 Currency (org.openlca.core.model.Currency)1