Search in sources :

Example 16 with LocationDao

use of org.openlca.core.database.LocationDao in project olca-modules by GreenDelta.

the class DocImportMapper method mapGeography.

private void mapGeography(Geography geography) {
    if (geography == null)
        return;
    process.documentation.geography = RichText.join(geography.comment);
    try {
        String refId = KeyGen.get(geography.shortName);
        LocationDao dao = new LocationDao(database);
        Location location = dao.getForRefId(refId);
        process.location = location;
    } catch (Exception e) {
        log.error("failed to load geography from DB", e);
    }
}
Also used : LocationDao(org.openlca.core.database.LocationDao) Location(org.openlca.core.model.Location)

Example 17 with LocationDao

use of org.openlca.core.database.LocationDao in project olca-app by GreenDelta.

the class DBProvider method getFlowRefs.

@Override
public List<FlowRef> getFlowRefs() {
    // collect categories, properties, locations
    var categories = Categories.pathsOf(db);
    Map<Long, FlowProperty> props = new FlowPropertyDao(db).getAll().stream().collect(Collectors.toMap(fp -> fp.id, fp -> fp));
    Map<Long, String> locations = new LocationDao(db).getCodes();
    List<FlowRef> refs = new ArrayList<>();
    new FlowDao(db).getDescriptors().forEach(flow -> {
        FlowRef ref = new FlowRef();
        ref.flow = flow;
        ref.flowCategory = categories.pathOf(flow.category);
        ref.flowLocation = locations.get(flow.location);
        Fn.with(props.get(flow.refFlowPropertyId), prop -> {
            if (prop == null)
                return;
            ref.property = Descriptor.of(prop);
            if (prop.unitGroup != null && prop.unitGroup.referenceUnit != null) {
                ref.unit = Descriptor.of(prop.unitGroup.referenceUnit);
            }
        });
        refs.add(ref);
    });
    return refs;
}
Also used : FlowType(org.openlca.core.model.FlowType) Descriptor(org.openlca.core.model.descriptors.Descriptor) Categories(org.openlca.util.Categories) Fn(org.openlca.app.util.Fn) FlowPropertyDao(org.openlca.core.database.FlowPropertyDao) Process(org.openlca.core.model.Process) ProcessDao(org.openlca.core.database.ProcessDao) Unit(org.openlca.core.model.Unit) Collectors(java.util.stream.Collectors) FlowRef(org.openlca.io.maps.FlowRef) ArrayList(java.util.ArrayList) Flow(org.openlca.core.model.Flow) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Objects(java.util.Objects) LocationDao(org.openlca.core.database.LocationDao) List(java.util.List) Stream(java.util.stream.Stream) IDatabase(org.openlca.core.database.IDatabase) Map(java.util.Map) FlowDao(org.openlca.core.database.FlowDao) FlowProperty(org.openlca.core.model.FlowProperty) MappingStatus(org.openlca.io.maps.MappingStatus) FlowPropertyDao(org.openlca.core.database.FlowPropertyDao) ArrayList(java.util.ArrayList) LocationDao(org.openlca.core.database.LocationDao) FlowRef(org.openlca.io.maps.FlowRef) FlowDao(org.openlca.core.database.FlowDao) FlowProperty(org.openlca.core.model.FlowProperty)

Example 18 with LocationDao

use of org.openlca.core.database.LocationDao in project olca-app by GreenDelta.

the class FactorClipboard method factor.

private ImpactFactor factor(String[] row) {
    if (row.length < 4)
        return null;
    String name = row[0];
    String category = row[1];
    String amount = row[2];
    String unit = row[3];
    // filter the flows by matching names and categories
    List<Flow> candidates = flows.stream().filter(d -> Strings.nullOrEqual(d.name, name)).map(d -> new FlowDao(db).getForId(d.id)).filter(flow -> {
        if (flow.category == null)
            return Strings.nullOrEmpty(category);
        String path = CategoryPath.getFull(flow.category);
        return Strings.nullOrEqual(path, category);
    }).collect(Collectors.toList());
    if (candidates.isEmpty())
        return null;
    // find a matching flow for the unit
    // the unit in the table has the format:
    // <LCIA ref. unit> / <flow unit>
    // the following only works if the LCIA
    // ref. unit does not contain a slash, but
    // this should be very unlikely
    int i = unit.indexOf('/');
    if (i >= 0) {
        unit = unit.substring(i + 1).trim();
    }
    ImpactFactor factor = new ImpactFactor();
    for (Flow flow : candidates) {
        for (FlowPropertyFactor p : flow.flowPropertyFactors) {
            if (p.flowProperty == null || p.flowProperty.unitGroup == null)
                continue;
            Unit u = p.flowProperty.unitGroup.getUnit(unit);
            if (u == null)
                continue;
            factor.flow = flow;
            factor.flowPropertyFactor = p;
            factor.unit = u;
            if (Objects.equals(p.flowProperty, flow.referenceFlowProperty))
                break;
        }
        if (factor.flow != null)
            break;
    }
    if (factor.flow == null)
        return null;
    // set the amount value / formula
    try {
        factor.value = Double.parseDouble(amount);
    } catch (Exception e) {
        factor.formula = amount;
    }
    // uncertainty value
    if (row.length > 4) {
        factor.uncertainty = Uncertainty.fromString(row[4]);
    }
    // location
    if (row.length > 5) {
        String code = row[5];
        if (!Strings.nullOrEmpty(code)) {
            LocationDao dao = new LocationDao(db);
            factor.location = dao.getDescriptors().stream().filter(d -> Strings.nullOrEqual(code, d.code)).map(d -> dao.getForId(d.id)).findFirst().orElse(null);
        }
    }
    return factor;
}
Also used : M(org.openlca.app.M) Unit(org.openlca.core.model.Unit) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Flow(org.openlca.core.model.Flow) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) ImpactFactor(org.openlca.core.model.ImpactFactor) Objects(java.util.Objects) LocationDao(org.openlca.core.database.LocationDao) List(java.util.List) Database(org.openlca.app.db.Database) Strings(org.openlca.util.Strings) IDatabase(org.openlca.core.database.IDatabase) Uncertainty(org.openlca.core.model.Uncertainty) FlowDao(org.openlca.core.database.FlowDao) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) Collections(java.util.Collections) CategoryPath(org.openlca.io.CategoryPath) ImpactFactor(org.openlca.core.model.ImpactFactor) FlowDao(org.openlca.core.database.FlowDao) LocationDao(org.openlca.core.database.LocationDao) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Unit(org.openlca.core.model.Unit) Flow(org.openlca.core.model.Flow)

Aggregations

LocationDao (org.openlca.core.database.LocationDao)18 Location (org.openlca.core.model.Location)11 FlowDao (org.openlca.core.database.FlowDao)4 Test (org.junit.Test)3 ProcessDao (org.openlca.core.database.ProcessDao)3 Flow (org.openlca.core.model.Flow)3 ProcessDescriptor (org.openlca.core.model.descriptors.ProcessDescriptor)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)2 IDatabase (org.openlca.core.database.IDatabase)2 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)2 Unit (org.openlca.core.model.Unit)2 AbstractZipTest (org.openlca.jsonld.AbstractZipTest)2 Collections (java.util.Collections)1 Map (java.util.Map)1 Stream (java.util.stream.Stream)1 M (org.openlca.app.M)1