use of org.openlca.core.database.LocationDao in project olca-modules by GreenDelta.
the class DocImportMapper method mapGeography.
private void mapGeography(Geography geography) {
if (geography == null)
return;
process.documentation.geography = RichText.join(geography.comment);
try {
String refId = KeyGen.get(geography.shortName);
LocationDao dao = new LocationDao(database);
Location location = dao.getForRefId(refId);
process.location = location;
} catch (Exception e) {
log.error("failed to load geography from DB", e);
}
}
use of org.openlca.core.database.LocationDao in project olca-app by GreenDelta.
the class DBProvider method getFlowRefs.
@Override
public List<FlowRef> getFlowRefs() {
// collect categories, properties, locations
var categories = Categories.pathsOf(db);
Map<Long, FlowProperty> props = new FlowPropertyDao(db).getAll().stream().collect(Collectors.toMap(fp -> fp.id, fp -> fp));
Map<Long, String> locations = new LocationDao(db).getCodes();
List<FlowRef> refs = new ArrayList<>();
new FlowDao(db).getDescriptors().forEach(flow -> {
FlowRef ref = new FlowRef();
ref.flow = flow;
ref.flowCategory = categories.pathOf(flow.category);
ref.flowLocation = locations.get(flow.location);
Fn.with(props.get(flow.refFlowPropertyId), prop -> {
if (prop == null)
return;
ref.property = Descriptor.of(prop);
if (prop.unitGroup != null && prop.unitGroup.referenceUnit != null) {
ref.unit = Descriptor.of(prop.unitGroup.referenceUnit);
}
});
refs.add(ref);
});
return refs;
}
use of org.openlca.core.database.LocationDao in project olca-app by GreenDelta.
the class FactorClipboard method factor.
private ImpactFactor factor(String[] row) {
if (row.length < 4)
return null;
String name = row[0];
String category = row[1];
String amount = row[2];
String unit = row[3];
// filter the flows by matching names and categories
List<Flow> candidates = flows.stream().filter(d -> Strings.nullOrEqual(d.name, name)).map(d -> new FlowDao(db).getForId(d.id)).filter(flow -> {
if (flow.category == null)
return Strings.nullOrEmpty(category);
String path = CategoryPath.getFull(flow.category);
return Strings.nullOrEqual(path, category);
}).collect(Collectors.toList());
if (candidates.isEmpty())
return null;
// find a matching flow for the unit
// the unit in the table has the format:
// <LCIA ref. unit> / <flow unit>
// the following only works if the LCIA
// ref. unit does not contain a slash, but
// this should be very unlikely
int i = unit.indexOf('/');
if (i >= 0) {
unit = unit.substring(i + 1).trim();
}
ImpactFactor factor = new ImpactFactor();
for (Flow flow : candidates) {
for (FlowPropertyFactor p : flow.flowPropertyFactors) {
if (p.flowProperty == null || p.flowProperty.unitGroup == null)
continue;
Unit u = p.flowProperty.unitGroup.getUnit(unit);
if (u == null)
continue;
factor.flow = flow;
factor.flowPropertyFactor = p;
factor.unit = u;
if (Objects.equals(p.flowProperty, flow.referenceFlowProperty))
break;
}
if (factor.flow != null)
break;
}
if (factor.flow == null)
return null;
// set the amount value / formula
try {
factor.value = Double.parseDouble(amount);
} catch (Exception e) {
factor.formula = amount;
}
// uncertainty value
if (row.length > 4) {
factor.uncertainty = Uncertainty.fromString(row[4]);
}
// location
if (row.length > 5) {
String code = row[5];
if (!Strings.nullOrEmpty(code)) {
LocationDao dao = new LocationDao(db);
factor.location = dao.getDescriptors().stream().filter(d -> Strings.nullOrEqual(code, d.code)).map(d -> dao.getForId(d.id)).findFirst().orElse(null);
}
}
return factor;
}
Aggregations