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