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