Search in sources :

Example 6 with ModelType

use of org.openlca.core.model.ModelType in project olca-modules by GreenDelta.

the class ZipStoreTest method writeModels.

private Map<ModelType, String> writeModels() {
    Map<ModelType, String> entries = new HashMap<>();
    with(zip -> {
        for (var type : ModelType.values()) {
            if (!type.isRoot())
                continue;
            String refId = UUID.randomUUID().toString();
            entries.put(type, refId);
            JsonObject obj = new JsonObject();
            obj.addProperty("@id", refId);
            obj.addProperty("name", type.name());
            zip.put(type, obj);
        }
    });
    return entries;
}
Also used : HashMap(java.util.HashMap) ModelType(org.openlca.core.model.ModelType) JsonObject(com.google.gson.JsonObject)

Example 7 with ModelType

use of org.openlca.core.model.ModelType in project olca-modules by GreenDelta.

the class SyncTestUtils method validate.

static boolean validate(ModelType[] modelTypes, Predicate<Reference> isValid) {
    var db = Tests.getDb();
    for (ModelType type : modelTypes) {
        var ids = db.getAll(type.getModelClass()).stream().map(e -> e.id).collect(Collectors.toSet());
        var refs = IReferenceSearch.FACTORY.createFor(type, db, true).findReferences(ids);
        for (var ref : refs) {
            if (!isValid.test(ref))
                return false;
        }
    }
    return true;
}
Also used : Tests(org.openlca.core.Tests) Files(java.nio.file.Files) IDatabase(org.openlca.core.database.IDatabase) Predicate(java.util.function.Predicate) Reference(org.openlca.core.database.references.Reference) ZipStore(org.openlca.jsonld.ZipStore) IReferenceSearch(org.openlca.core.database.references.IReferenceSearch) Path(java.nio.file.Path) Collectors(java.util.stream.Collectors) ModelType(org.openlca.core.model.ModelType) File(java.io.File) StandardCopyOption(java.nio.file.StandardCopyOption) ModelType(org.openlca.core.model.ModelType)

Example 8 with ModelType

use of org.openlca.core.model.ModelType in project olca-modules by GreenDelta.

the class CategoryImport method map.

@Override
Category map(JsonObject json, Category model) {
    if (json == null)
        return null;
    boolean isNew = false;
    String refId = Json.getString(json, "@id");
    if (model == null) {
        model = new Category();
        isNew = true;
    }
    In.mapAtts(json, model, model.id, conf);
    model.modelType = Json.getEnum(json, "modelType", ModelType.class);
    if (!isNew || model.category == null)
        model = conf.db.put(model);
    else
        model = updateParent(model);
    if (!refId.equals(model.refId))
        conf.db.categoryRefIdMapping.put(refId, model.refId);
    return model;
}
Also used : Category(org.openlca.core.model.Category) ModelType(org.openlca.core.model.ModelType)

Example 9 with ModelType

use of org.openlca.core.model.ModelType 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 10 with ModelType

use of org.openlca.core.model.ModelType in project olca-modules by GreenDelta.

the class ExportHandler method getModels.

private Map<ModelType, Set<String>> getModels(JsonObject obj) {
    JsonArray models = Json.getArray(obj, "models");
    if (models == null)
        return null;
    Map<ModelType, Set<String>> map = new HashMap<>();
    for (JsonElement e : models) {
        if (!e.isJsonObject())
            continue;
        JsonObject model = e.getAsJsonObject();
        String id = Json.getString(model, "@id");
        String type = Json.getString(model, "@type");
        if (id == null || type == null)
            continue;
        for (ModelType t : ModelType.values()) {
            if (t.getModelClass() != null && t.getModelClass().getSimpleName().equals(type)) {
                Set<String> ids = map.get(t);
                if (ids == null) {
                    ids = new HashSet<>();
                    map.put(t, ids);
                }
                ids.add(id);
            }
        }
    }
    return map;
}
Also used : JsonArray(com.google.gson.JsonArray) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) ModelType(org.openlca.core.model.ModelType) JsonObject(com.google.gson.JsonObject)

Aggregations

ModelType (org.openlca.core.model.ModelType)21 JsonObject (com.google.gson.JsonObject)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 Category (org.openlca.core.model.Category)3 JsonArray (com.google.gson.JsonArray)2 File (java.io.File)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 JsonElement (com.google.gson.JsonElement)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 ArrayList (java.util.ArrayList)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 TextDropComponent (org.openlca.app.components.TextDropComponent)1 CommentControl (org.openlca.app.editors.comments.CommentControl)1 Tests (org.openlca.core.Tests)1