Search in sources :

Example 16 with EntityCache

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);
}
Also used : EntityCache(org.openlca.core.database.EntityCache) LocationDescriptor(org.openlca.core.model.descriptors.LocationDescriptor) JsonObject(com.google.gson.JsonObject) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) ContributionResult(org.openlca.core.results.ContributionResult)

Example 17 with EntityCache

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);
}
Also used : EntityCache(org.openlca.core.database.EntityCache) LocationDescriptor(org.openlca.core.model.descriptors.LocationDescriptor) JsonObject(com.google.gson.JsonObject) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) ContributionResult(org.openlca.core.results.ContributionResult)

Example 18 with EntityCache

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;
}
Also used : IDatabase(org.openlca.core.database.IDatabase) EntityCache(org.openlca.core.database.EntityCache) LocationDescriptor(org.openlca.core.model.descriptors.LocationDescriptor) ArrayList(java.util.ArrayList) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) Unit(org.openlca.core.model.Unit) Logger(org.slf4j.Logger)

Aggregations

EntityCache (org.openlca.core.database.EntityCache)18 JsonObject (com.google.gson.JsonObject)13 ContributionResult (org.openlca.core.results.ContributionResult)7 ImpactDescriptor (org.openlca.core.model.descriptors.ImpactDescriptor)6 EnviFlow (org.openlca.core.matrix.index.EnviFlow)4 LocationDescriptor (org.openlca.core.model.descriptors.LocationDescriptor)4 ProcessDescriptor (org.openlca.core.model.descriptors.ProcessDescriptor)4 FullResult (org.openlca.core.results.FullResult)4 ArrayList (java.util.ArrayList)3 Location (org.openlca.core.model.Location)2 FlowDescriptor (org.openlca.core.model.descriptors.FlowDescriptor)2 SimpleResult (org.openlca.core.results.SimpleResult)2 CategoryPair (org.openlca.io.CategoryPair)2 JsonArray (com.google.gson.JsonArray)1 Collection (java.util.Collection)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 SWT (org.eclipse.swt.SWT)1 IDatabase (org.openlca.core.database.IDatabase)1