Search in sources :

Example 21 with Unit

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

the class DisplayValues method referenceUnit.

/**
 * Returns the name of the reference unit of the given flow.
 */
public static String referenceUnit(FlowDescriptor flow, EntityCache cache) {
    FlowPropertyDescriptor descriptor = cache.get(FlowPropertyDescriptor.class, flow.refFlowPropertyId);
    if (descriptor == null) {
        log.warn("no reference flow property for flow {}", flow);
        return "";
    }
    try {
        FlowProperty property = cache.get(FlowProperty.class, descriptor.id);
        Unit refUnit = property.unitGroup.referenceUnit;
        return refUnit.name;
    } catch (Exception e) {
        log.error("failed to get reference unit for flow " + flow.id, e);
        return "";
    }
}
Also used : FlowPropertyDescriptor(org.openlca.core.model.descriptors.FlowPropertyDescriptor) Unit(org.openlca.core.model.Unit) FlowProperty(org.openlca.core.model.FlowProperty)

Example 22 with Unit

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

the class UnitMappingSync method updateUnitGroup.

/**
 * Add a new unit created from the given entry to the unit group.
 */
private UnitGroup updateUnitGroup(UnitMappingEntry entry, UnitGroup unitGroup) {
    log.trace("add new unit {} to group {}", entry.unitName, unitGroup);
    Unit unit = new Unit();
    unit.name = entry.unitName;
    unit.refId = UUID.randomUUID().toString();
    double factor = entry.factor == null ? 1d : entry.factor;
    unit.conversionFactor = factor;
    unitGroup.units.add(unit);
    unitGroup.lastChange = Calendar.getInstance().getTimeInMillis();
    Version.incUpdate(unitGroup);
    unitGroup = new UnitGroupDao(database).update(unitGroup);
    entry.factor = factor;
    entry.unitGroup = unitGroup;
    entry.unit = unitGroup.getUnit(entry.unitName);
    return unitGroup;
}
Also used : Unit(org.openlca.core.model.Unit) UnitGroupDao(org.openlca.core.database.UnitGroupDao)

Example 23 with Unit

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

the class UnitMappingSync method syncEntries.

/**
 * Replace the unit group and units in the entries with the last updated
 * versions. This ensures that all units and unit groups are synchronous
 * with the persistence layer.
 */
private void syncEntries(UnitGroup updatedGroup, List<UnitMappingEntry> entries) {
    for (UnitMappingEntry entry : entries) {
        if (!Objects.equals(updatedGroup, entry.unitGroup))
            continue;
        Unit unit = updatedGroup.getUnit(entry.unitName);
        entry.unit = unit;
        entry.unitGroup = updatedGroup;
    }
}
Also used : Unit(org.openlca.core.model.Unit)

Example 24 with Unit

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

the class EcoSpold2ImportTest method createUnit.

private void createUnit(String unitRefId, String propertyRefId, String name) {
    Unit unit = new Unit();
    unit.name = name;
    unit.refId = unitRefId;
    UnitGroup group = new UnitGroup();
    group.refId = UUID.randomUUID().toString();
    group.referenceUnit = unit;
    group.units.add(unit);
    group = new UnitGroupDao(Tests.getDb()).insert(group);
    FlowProperty prop = new FlowProperty();
    prop.unitGroup = group;
    prop.name = "property for " + name;
    prop.refId = propertyRefId;
    prop = new FlowPropertyDao(Tests.getDb()).insert(prop);
    group.defaultFlowProperty = prop;
    group = new UnitGroupDao(Tests.getDb()).update(group);
}
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)

Example 25 with Unit

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

the class Utils method getUnit.

String getUnit(EnviFlow flow, EntityCache cache) {
    if (flow == null || flow.flow() == null)
        return null;
    FlowProperty prop = cache.get(FlowProperty.class, flow.flow().refFlowPropertyId);
    if (prop == null || prop.unitGroup == null)
        return null;
    Unit unit = prop.unitGroup.referenceUnit;
    if (unit == null)
        return null;
    return unit.name;
}
Also used : Unit(org.openlca.core.model.Unit) FlowProperty(org.openlca.core.model.FlowProperty)

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