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