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 "";
}
}
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;
}
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;
}
}
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);
}
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;
}
Aggregations