use of org.openlca.core.database.EntityCache in project olca-modules by GreenDelta.
the class Utils method contributionImpactLocationProcess.
RpcResponse contributionImpactLocationProcess(RpcRequest req, ContributionImpactLocationProcess handler) {
if (req == null || req.params == null || !req.params.isJsonObject())
return Responses.invalidParams("No parameter given", req);
JsonObject json = req.params.getAsJsonObject();
ContributionResult result = getResult(json);
ImpactDescriptor impact = get(result.impactIndex(), json);
if (impact == null)
return Responses.invalidParams("Missing or invalid impact category parameter", req);
LocationDescriptor location = get(ModelType.LOCATION, json);
if (location == null)
return Responses.invalidParams("Missing or invalid location parameter", req);
ProcessDescriptor process = get(ModelType.PROCESS, json, result.techIndex().getProcessIds());
if (process == null)
return Responses.invalidParams("Missing or invalid process parameter", req);
EntityCache cache = EntityCache.create(ctx.db);
return Responses.ok(handler.handle(result, impact, location, process, cache), req);
}
use of org.openlca.core.database.EntityCache in project olca-modules by GreenDelta.
the class Utils method contributionImpactLocation.
RpcResponse contributionImpactLocation(RpcRequest req, ContributionImpactLocation handler) {
if (req == null || req.params == null || !req.params.isJsonObject())
return Responses.invalidParams("No parameter given", req);
JsonObject json = req.params.getAsJsonObject();
ContributionResult result = getResult(json);
ImpactDescriptor impact = get(result.impactIndex(), json);
if (impact == null)
return Responses.invalidParams("Missing or invalid impact category parameter", req);
LocationDescriptor location = get(ModelType.LOCATION, json);
if (location == null)
return Responses.invalidParams("Missing or invalid location parameter", req);
EntityCache cache = EntityCache.create(ctx.db);
return Responses.ok(handler.handle(result, impact, location, cache), req);
}
use of org.openlca.core.database.EntityCache in project olca-app by GreenDelta.
the class ImpactPage method loadFactors.
private List<Factor> loadFactors() {
IDatabase db = Database.get();
String sql = "select f_impact_category, f_unit, f_location, " + "value from tbl_impact_factors where f_flow = " + getModel().id;
EntityCache ecache = Cache.getEntityCache();
List<Factor> factors = new ArrayList<>();
try {
NativeSql.on(db).query(sql, r -> {
Factor f = new Factor();
f.impact = ecache.get(ImpactDescriptor.class, r.getLong(1));
if (f.impact == null)
return true;
Unit unit = ecache.get(Unit.class, r.getLong(2));
if (unit != null) {
f.unit = unit.name;
}
long locID = r.getLong(3);
if (!r.wasNull()) {
f.location = ecache.get(LocationDescriptor.class, locID);
}
f.value = r.getDouble(4);
factors.add(f);
return true;
});
} catch (Exception e) {
Logger log = LoggerFactory.getLogger(getClass());
log.error("Failed to load LCIA factors", e);
}
// sort and set display flags
factors.sort((f1, f2) -> {
int c = Strings.compare(Labels.name(f1.impact), Labels.name(f2.impact));
if (c != 0)
return c;
if (f1.location == null)
return -1;
if (f2.location == null)
return 1;
return Strings.compare(f1.location.code, f2.location.code);
});
return factors;
}
Aggregations