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;
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations