Search in sources :

Example 6 with UnitGroupDao

use of org.openlca.core.database.UnitGroupDao 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 UnitGroupDao

use of org.openlca.core.database.UnitGroupDao 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 8 with UnitGroupDao

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

the class UnitGroupImport method of.

@Override
public ImportStatus<UnitGroup> of(String id) {
    var group = imp.get(UnitGroup.class, id);
    // check if we are in update mode
    var inUpdateMode = false;
    if (group != null) {
        inUpdateMode = imp.shouldUpdate(group);
        if (!inUpdateMode) {
            return ImportStatus.skipped(group);
        }
    }
    // resolve the proto object
    var proto = imp.reader.getUnitGroup(id);
    if (proto == null)
        return group != null ? ImportStatus.skipped(group) : ImportStatus.error("Could not resolve UnitGroup " + id);
    var wrap = ProtoWrap.of(proto);
    if (inUpdateMode) {
        if (imp.skipUpdate(group, wrap))
            return ImportStatus.skipped(group);
    }
    // map the data
    if (group == null) {
        group = new UnitGroup();
        group.refId = id;
    }
    wrap.mapTo(group, imp);
    map(proto, group, inUpdateMode);
    // insert or update it
    var dao = new UnitGroupDao(imp.db);
    group = inUpdateMode ? dao.update(group) : dao.insert(group);
    imp.putHandled(group);
    // set a possible default flow property after
    // the unit group was saved to avoid endless
    // import cycles
    var propID = proto.getDefaultFlowProperty().getId();
    if (Strings.notEmpty(propID)) {
        group.defaultFlowProperty = new FlowPropertyImport(imp).of(propID).model();
        group = dao.update(group);
    }
    return inUpdateMode ? ImportStatus.updated(group) : ImportStatus.created(group);
}
Also used : ProtoUnitGroup(org.openlca.proto.ProtoUnitGroup) UnitGroup(org.openlca.core.model.UnitGroup) UnitGroupDao(org.openlca.core.database.UnitGroupDao)

Example 9 with UnitGroupDao

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

the class UnitGroupTest method testUnitGroup.

@Test
public void testUnitGroup() {
    UnitGroupDao dao = new UnitGroupDao(Tests.getDb());
    UnitGroup group = createModel(dao);
    doExport(group, dao);
    doImport(dao, group);
    dao.delete(group);
}
Also used : UnitGroup(org.openlca.core.model.UnitGroup) UnitGroupDao(org.openlca.core.database.UnitGroupDao) AbstractZipTest(org.openlca.jsonld.AbstractZipTest) Test(org.junit.Test)

Example 10 with UnitGroupDao

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

the class UnitMapping method createDefault.

/**
 * Creates a default mapping for the unit names in the database.
 */
public static UnitMapping createDefault(IDatabase database) {
    Logger log = LoggerFactory.getLogger(UnitMapping.class);
    log.trace("create default mappings");
    UnitMapping mapping = new UnitMapping();
    try {
        for (UnitGroup group : new UnitGroupDao(database).getAll()) {
            FlowProperty prop = group.defaultFlowProperty;
            if (prop == null)
                prop = findProperty(database, group);
            if (prop == null) {
                log.warn("no flow property found for unit group {}", group);
                continue;
            }
            registerUnits(group, prop, mapping);
        }
    } catch (Exception e) {
        log.error("failed to init. unit mapping", e);
    }
    return mapping;
}
Also used : UnitGroup(org.openlca.core.model.UnitGroup) Logger(org.slf4j.Logger) UnitGroupDao(org.openlca.core.database.UnitGroupDao) FlowProperty(org.openlca.core.model.FlowProperty)

Aggregations

UnitGroupDao (org.openlca.core.database.UnitGroupDao)19 UnitGroup (org.openlca.core.model.UnitGroup)14 Unit (org.openlca.core.model.Unit)9 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)7 FlowProperty (org.openlca.core.model.FlowProperty)4 Test (org.junit.Test)3 FlowDao (org.openlca.core.database.FlowDao)3 ProcessDao (org.openlca.core.database.ProcessDao)3 ProductSystemDao (org.openlca.core.database.ProductSystemDao)2 UnitDao (org.openlca.core.database.UnitDao)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 ActorDao (org.openlca.core.database.ActorDao)1 CategoryDao (org.openlca.core.database.CategoryDao)1 CurrencyDao (org.openlca.core.database.CurrencyDao)1 DQSystemDao (org.openlca.core.database.DQSystemDao)1 IDatabase (org.openlca.core.database.IDatabase)1 ImpactCategoryDao (org.openlca.core.database.ImpactCategoryDao)1 ImpactMethodDao (org.openlca.core.database.ImpactMethodDao)1 LocationDao (org.openlca.core.database.LocationDao)1