use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.
the class CostHandler method getTotalRequirements.
@Rpc("get/costs/total_requirements")
public RpcResponse getTotalRequirements(RpcRequest req) {
return utils.contribution(req, (result, cache) -> {
JsonArray items = new JsonArray();
var techIdx = result.techIndex();
for (int i = 0; i < techIdx.size(); i++) {
var tr = result.totalRequirements()[i];
if (tr == 0)
continue;
var product = techIdx.at(i);
var obj = new JsonObject();
obj.add("process", JsonRef.of(product.provider(), cache));
obj.add("product", JsonRef.of(product.flow(), cache));
obj.addProperty("amount", tr);
obj.addProperty("costs", result.getDirectCostResult(product));
items.add(obj);
}
return items;
});
}
use of org.openlca.ipc.Rpc 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.ipc.Rpc in project olca-modules by GreenDelta.
the class ImpactHandler method getProcessResultsImpacts.
@Rpc("get/impacts/process_results")
public RpcResponse getProcessResultsImpacts(RpcRequest req) {
return utils.fullProcess(req, (result, process, cache) -> {
JsonArray contributions = new JsonArray();
result.getImpacts().forEach(impact -> {
double total = result.getTotalImpactResult(impact);
if (total == 0)
return;
Contribution<ImpactDescriptor> c = new Contribution<>();
c.item = impact;
c.amount = result.getDirectImpactResult(process, impact);
c.share = c.amount / total;
if (c.amount == 0)
return;
contributions.add(JsonRpc.encode(c, cache, json -> {
json.addProperty("unit", impact.referenceUnit);
json.addProperty("upstream", result.getUpstreamImpactResult(process, impact));
}));
});
return contributions;
});
}
use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.
the class InventoryHandler method getLocationContributions.
@Rpc("get/inventory/contributions/locations")
public RpcResponse getLocationContributions(RpcRequest req) {
return utils.contributionFlow(req, (result, flow, cache) -> {
LocationResult r = new LocationResult(result, cache.db);
List<Contribution<LocationDescriptor>> cons = utils.toDescriptors(r.getContributions(flow.flow()));
cons = utils.filter(cons, c -> c.amount != 0);
String unit = utils.getUnit(flow, cache);
return JsonRpc.encode(cons, cache, json -> json.addProperty("unit", unit));
});
}
use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.
the class ModelHandler method createProductSystem.
@Rpc("create/product_system")
public RpcResponse createProductSystem(RpcRequest req) {
if (req.params == null || !req.params.isJsonObject())
return Responses.invalidParams("params must be an object with valid processId", req);
var obj = req.params.getAsJsonObject();
if (!obj.has("processId") || !obj.get("processId").isJsonPrimitive())
return Responses.invalidParams("params must be an object with valid processId", req);
var processId = obj.get("processId").getAsString();
if (Strings.nullOrEmpty(processId))
return Responses.invalidParams("params must be an object with valid processId", req);
var refProcess = new ProcessDao(db).getForRefId(processId);
if (refProcess == null)
return Responses.invalidParams("No process found for ref id " + processId, req);
var system = ProductSystem.of(refProcess);
system = new ProductSystemDao(db).insert(system);
var config = new LinkingConfig().preferredType(ProcessType.UNIT_PROCESS);
if (obj.has("preferredType") && obj.get("preferredType").getAsString().equalsIgnoreCase("lci_result")) {
config.preferredType(ProcessType.LCI_RESULT);
}
config.providerLinking(ProviderLinking.PREFER_DEFAULTS);
if (obj.has("providerLinking")) {
if (obj.get("providerLinking").getAsString().equalsIgnoreCase("ignore")) {
config.providerLinking(ProviderLinking.IGNORE_DEFAULTS);
} else if (obj.get("providerLinking").getAsString().equalsIgnoreCase("only")) {
config.providerLinking(ProviderLinking.ONLY_DEFAULTS);
}
}
var builder = new ProductSystemBuilder(MatrixCache.createLazy(db), config);
builder.autoComplete(system);
system = ProductSystemBuilder.update(db, system);
var res = new JsonObject();
res.addProperty("@id", system.refId);
return Responses.ok(res, req);
}
Aggregations