Search in sources :

Example 6 with FlowPropertyDao

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

the class RefDataImport method syncUnit.

/**
 * For units that are not found in the mapping file, we try to find the
 * corresponding unit and flow property pair from the database. First, we
 * check if there is a unit with the given ID defined. If this is not the
 * case we search for a unit with the given name (or synonym). When we find
 * such a unit, we try to find also the default flow property and when there
 * is no such flow property some other flow property with that unit in the
 * corresponding unit group. If we cannot find something we create the flow
 * property, unit group, and unit if necessary.
 */
private FlowProperty syncUnit(String refID, String name) {
    // search for the unit
    Unit unit = null;
    UnitGroup group = null;
    boolean byID = false;
    for (UnitGroup ug : new UnitGroupDao(config.db).getAll()) {
        for (Unit u : ug.units) {
            if (Objects.equals(u.refId, refID)) {
                unit = u;
                group = ug;
                byID = true;
                break;
            }
            if (Objects.equals(u.name, name)) {
                if (unit != null) {
                    log.warn("There are multiple possible" + " definitions for unit " + name + " in the database");
                }
                unit = u;
                group = ug;
            }
        }
        if (byID) {
            break;
        }
    }
    // create the unit and unit group if necessary
    if (unit != null) {
        log.info("mapped unit '" + name + "' id='" + refID + "' by " + (byID ? "ID" : "name"));
    } else {
        log.info("create new unit: " + name);
        unit = new Unit();
        unit.name = name;
        unit.refId = refID;
        unit.conversionFactor = 1.0;
        group = new UnitGroup();
        group.name = "Unit group for " + name;
        group.refId = KeyGen.get(ModelType.UNIT_GROUP.name(), refID);
        group.referenceUnit = unit;
        group.units.add(unit);
        group.lastChange = new Date().getTime();
        group.version = Version.valueOf(1, 0, 0);
        group = new UnitGroupDao(config.db).insert(group);
        // JPA synced
        unit = group.referenceUnit;
        log.imported(group);
    }
    // try to find a matching flow property
    FlowProperty prop = group.defaultFlowProperty;
    FlowPropertyDao propDao = new FlowPropertyDao(config.db);
    if (prop == null) {
        for (FlowProperty fp : propDao.getAll()) {
            if (Objects.equals(fp.unitGroup, group)) {
                prop = fp;
                break;
            }
        }
    }
    // create a new flow property if this is necessary
    if (prop == null) {
        prop = new FlowProperty();
        prop.name = "Flow property for " + name;
        prop.refId = KeyGen.get(ModelType.FLOW_PROPERTY.name(), refID);
        prop.unitGroup = group;
        prop.flowPropertyType = FlowPropertyType.PHYSICAL;
        prop.lastChange = new Date().getTime();
        prop.version = Version.valueOf(1, 0, 0);
        prop = propDao.insert(prop);
        log.imported(prop);
        group.defaultFlowProperty = prop;
        group = new UnitGroupDao(config.db).update(group);
        // JPA synced
        unit = group.referenceUnit;
    }
    index.putFlowProperty(refID, prop);
    index.putUnit(refID, unit);
    return prop;
}
Also used : UnitGroup(org.openlca.core.model.UnitGroup) FlowPropertyDao(org.openlca.core.database.FlowPropertyDao) Unit(org.openlca.core.model.Unit) UnitGroupDao(org.openlca.core.database.UnitGroupDao) FlowProperty(org.openlca.core.model.FlowProperty) Date(java.util.Date)

Example 7 with FlowPropertyDao

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

the class RegionalizedCalculationTest method property.

private FlowProperty property(String unit) {
    FlowPropertyDao dao = new FlowPropertyDao(db);
    List<FlowProperty> props = dao.getForName(unit);
    if (!props.isEmpty())
        return props.get(0);
    FlowProperty prop = new FlowProperty();
    prop.name = unit;
    prop.unitGroup = unitGroup(unit);
    return dao.insert(prop);
}
Also used : FlowPropertyDao(org.openlca.core.database.FlowPropertyDao) FlowProperty(org.openlca.core.model.FlowProperty)

Example 8 with FlowPropertyDao

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

the class SystemsInSystemsTests method testCalc.

@Test
public void testCalc() {
    IDatabase db = Tests.getDb();
    UnitGroupDao ugDao = new UnitGroupDao(db);
    FlowPropertyDao fpDao = new FlowPropertyDao(db);
    FlowDao flowDao = new FlowDao(db);
    ProcessDao processDao = new ProcessDao(db);
    ProductSystemDao systemDao = new ProductSystemDao(db);
    UnitGroup ug6939 = new UnitGroup();
    ug6939.refId = UUID.randomUUID().toString();
    ug6939.name = "Units of mass";
    Unit kg = new Unit();
    kg.name = "kg";
    kg.conversionFactor = 1.0;
    kg.refId = UUID.randomUUID().toString();
}
Also used : IDatabase(org.openlca.core.database.IDatabase) UnitGroup(org.openlca.core.model.UnitGroup) FlowDao(org.openlca.core.database.FlowDao) FlowPropertyDao(org.openlca.core.database.FlowPropertyDao) ProcessDao(org.openlca.core.database.ProcessDao) Unit(org.openlca.core.model.Unit) UnitGroupDao(org.openlca.core.database.UnitGroupDao) ProductSystemDao(org.openlca.core.database.ProductSystemDao) Test(org.junit.Test)

Example 9 with FlowPropertyDao

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

the class FlowPropertyImport method of.

@Override
public ImportStatus<FlowProperty> of(String id) {
    var flowProperty = imp.get(FlowProperty.class, id);
    // check if we are in update mode
    var update = false;
    if (flowProperty != null) {
        update = imp.shouldUpdate(flowProperty);
        if (!update) {
            return ImportStatus.skipped(flowProperty);
        }
    }
    // resolve the proto object
    var proto = imp.reader.getFlowProperty(id);
    if (proto == null)
        return flowProperty != null ? ImportStatus.skipped(flowProperty) : ImportStatus.error("Could not resolve FlowProperty " + id);
    var wrap = ProtoWrap.of(proto);
    if (update) {
        if (imp.skipUpdate(flowProperty, wrap))
            return ImportStatus.skipped(flowProperty);
    }
    // map the data
    if (flowProperty == null) {
        flowProperty = new FlowProperty();
        flowProperty.refId = id;
    }
    wrap.mapTo(flowProperty, imp);
    map(proto, flowProperty);
    // insert or update it
    var dao = new FlowPropertyDao(imp.db);
    flowProperty = update ? dao.update(flowProperty) : dao.insert(flowProperty);
    imp.putHandled(flowProperty);
    return update ? ImportStatus.updated(flowProperty) : ImportStatus.created(flowProperty);
}
Also used : FlowPropertyDao(org.openlca.core.database.FlowPropertyDao) ProtoFlowProperty(org.openlca.proto.ProtoFlowProperty) FlowProperty(org.openlca.core.model.FlowProperty)

Example 10 with FlowPropertyDao

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

the class ForegroundSystemGenerator method selectQuantity.

private Optional<FlowProperty> selectQuantity() {
    var props = new FlowPropertyDao(db).getAll();
    if (props.isEmpty())
        return Optional.empty();
    var mass = props.stream().filter(prop -> {
        if (prop.unitGroup == null)
            return false;
        var unit = prop.unitGroup.referenceUnit;
        return unit != null && "kg".equals(unit.name);
    }).findAny();
    return mass.isPresent() ? mass : Optional.of(props.get(0));
}
Also used : FlowType(org.openlca.core.model.FlowType) FlowPropertyDao(org.openlca.core.database.FlowPropertyDao) Process(org.openlca.core.model.Process) ProcessDao(org.openlca.core.database.ProcessDao) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) ModelType(org.openlca.core.model.ModelType) ArrayList(java.util.ArrayList) Flow(org.openlca.core.model.Flow) CategoryDao(org.openlca.core.database.CategoryDao) ProcessTable(org.openlca.core.matrix.cache.ProcessTable) IDatabase(org.openlca.core.database.IDatabase) Optional(java.util.Optional) FlowDao(org.openlca.core.database.FlowDao) FlowProperty(org.openlca.core.model.FlowProperty) FlowPropertyDao(org.openlca.core.database.FlowPropertyDao)

Aggregations

FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)20 FlowProperty (org.openlca.core.model.FlowProperty)14 FlowDao (org.openlca.core.database.FlowDao)7 UnitGroupDao (org.openlca.core.database.UnitGroupDao)7 ProcessDao (org.openlca.core.database.ProcessDao)5 Flow (org.openlca.core.model.Flow)5 Unit (org.openlca.core.model.Unit)5 UnitGroup (org.openlca.core.model.UnitGroup)5 IDatabase (org.openlca.core.database.IDatabase)3 UnitDao (org.openlca.core.database.UnitDao)3 Process (org.openlca.core.model.Process)3 ArrayList (java.util.ArrayList)2 After (org.junit.After)2 Before (org.junit.Before)2 Test (org.junit.Test)2 CategoryDao (org.openlca.core.database.CategoryDao)2 LocationDao (org.openlca.core.database.LocationDao)2 ProductSystemDao (org.openlca.core.database.ProductSystemDao)2 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)2 FlowType (org.openlca.core.model.FlowType)2