Search in sources :

Example 11 with Unit

use of org.openlca.core.model.Unit in project olca-modules by GreenDelta.

the class UnitExport method doIt.

@Override
protected void doIt(CSVPrinter printer, IDatabase db) throws IOException {
    log.trace("write units");
    UnitGroupDao dao = new UnitGroupDao(db);
    int count = 0;
    for (UnitGroup unitGroup : dao.getAll()) {
        for (Unit unit : unitGroup.units) {
            Object[] line = createLine(unitGroup, unit);
            printer.printRecord(line);
        }
        count++;
    }
    log.trace("{} units written", count);
}
Also used : UnitGroup(org.openlca.core.model.UnitGroup) Unit(org.openlca.core.model.Unit) UnitGroupDao(org.openlca.core.database.UnitGroupDao)

Example 12 with Unit

use of org.openlca.core.model.Unit 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 13 with Unit

use of org.openlca.core.model.Unit in project olca-modules by GreenDelta.

the class ProductSystemImport method sameExchange.

private boolean sameExchange(Exchange srcExchange, Exchange destExchange) {
    if (srcExchange.isInput != destExchange.isInput)
        return false;
    Unit srcUnit = srcExchange.unit;
    Unit destUnit = destExchange.unit;
    Flow srcFlow = srcExchange.flow;
    Flow destFlow = destExchange.flow;
    return srcUnit != null && destUnit != null && srcFlow != null && destFlow != null && Strings.nullOrEqual(srcUnit.refId, destUnit.refId) && Strings.nullOrEqual(srcFlow.refId, destFlow.refId);
}
Also used : Unit(org.openlca.core.model.Unit) Flow(org.openlca.core.model.Flow)

Example 14 with Unit

use of org.openlca.core.model.Unit in project olca-modules by GreenDelta.

the class UnitGroupImport method createUnits.

private void createUnits() {
    Integer refUnitId = ilcdUnitGroup.getReferenceUnitId();
    for (var iUnit : ilcdUnitGroup.getUnits()) {
        if (iUnit == null)
            continue;
        Unit oUnit = new Unit();
        unitGroup.units.add(oUnit);
        mapUnitAttributes(iUnit, oUnit);
        if (refUnitId != null && refUnitId == iUnit.id) {
            unitGroup.referenceUnit = oUnit;
        }
    }
}
Also used : Unit(org.openlca.core.model.Unit)

Example 15 with Unit

use of org.openlca.core.model.Unit in project olca-modules by GreenDelta.

the class UnitGroupImport method map.

private void map(ProtoUnitGroup proto, UnitGroup group, boolean inUpdateMode) {
    // sync units (keep the IDs) if we are in update mode
    // this is important because these units may are used
    // in exchanges etc. and we do not want to break these
    // pointers when updating an unit
    Map<String, Unit> oldUnits = null;
    if (inUpdateMode) {
        oldUnits = new HashMap<>();
        for (var unit : group.units) {
            oldUnits.put(unit.name, unit);
        }
        group.units.clear();
        group.referenceUnit = null;
    }
    for (var protoUnit : proto.getUnitsList()) {
        Unit unit = null;
        if (oldUnits != null) {
            unit = oldUnits.get(protoUnit.getName());
        }
        if (unit == null) {
            unit = new Unit();
        }
        mapUnit(protoUnit, unit);
        if (protoUnit.getReferenceUnit()) {
            group.referenceUnit = unit;
        }
        group.units.add(unit);
    }
}
Also used : ProtoUnit(org.openlca.proto.ProtoUnit) Unit(org.openlca.core.model.Unit)

Aggregations

Unit (org.openlca.core.model.Unit)50 UnitGroup (org.openlca.core.model.UnitGroup)18 Flow (org.openlca.core.model.Flow)13 FlowProperty (org.openlca.core.model.FlowProperty)13 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)11 UnitGroupDao (org.openlca.core.database.UnitGroupDao)9 ArrayList (java.util.ArrayList)6 FlowDao (org.openlca.core.database.FlowDao)4 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)4 List (java.util.List)3 IDatabase (org.openlca.core.database.IDatabase)3 Collections (java.util.Collections)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Objects (java.util.Objects)2 Test (org.junit.Test)2 M (org.openlca.app.M)2 ProcessDao (org.openlca.core.database.ProcessDao)2 UnitDao (org.openlca.core.database.UnitDao)2 ImpactFactor (org.openlca.core.model.ImpactFactor)2