use of org.openlca.io.maps.FlowRef in project olca-app by GreenDelta.
the class FlowRefDialog method open.
static void open(IProvider provider, Consumer<Optional<FlowRef>> fn) {
if (provider == null || fn == null)
return;
AtomicReference<Tree> treeRef = new AtomicReference<>();
App.runWithProgress("Collect flows and build tree ...", () -> {
Tree tree = Tree.build(provider.getFlowRefs());
treeRef.set(tree);
}, () -> {
Tree tree = treeRef.get();
FlowRefDialog dialog = new FlowRefDialog(tree);
if (dialog.open() == OK) {
FlowRef selected = dialog.selected;
if (selected != null) {
selected = selected.copy();
}
fn.accept(Optional.ofNullable(selected));
} else {
fn.accept(Optional.empty());
}
});
}
use of org.openlca.io.maps.FlowRef in project olca-app by GreenDelta.
the class MappingPage method bindActions.
private void bindActions(Section section, TableViewer table) {
Action add = Actions.onAdd(() -> {
var e = new FlowMapEntry(new FlowRef(), new FlowRef(), 1);
if (Dialog.OK != MappingDialog.open(tool, e))
return;
tool.mapping.entries.add(e);
table.refresh();
tool.setDirty();
});
Action edit = Actions.onEdit(() -> {
FlowMapEntry e = Viewers.getFirstSelected(table);
if (e == null)
return;
if (Dialog.OK == MappingDialog.open(tool, e)) {
table.refresh();
tool.setDirty();
}
});
Tables.onDoubleClick(table, _e -> edit.run());
Action delete = Actions.onRemove(() -> {
List<FlowMapEntry> entries = Viewers.getAllSelected(table);
if (entries.isEmpty())
return;
tool.mapping.entries.removeAll(entries);
table.refresh();
tool.setDirty();
});
Tables.onDeletePressed(table, $ -> delete.run());
Action copy = TableClipboard.onCopySelected(table);
Actions.bind(section, add, edit, delete);
Actions.bind(table, add, edit, copy, delete);
}
use of org.openlca.io.maps.FlowRef 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.io.maps.FlowRef in project olca-app by GreenDelta.
the class JsonProvider method persist.
@Override
public void persist(List<FlowRef> refs, IDatabase db) {
if (refs == null || db == null)
return;
try (ZipStore store = ZipStore.open(file)) {
FlowDao dao = new FlowDao(db);
JsonImport imp = new JsonImport(store, db);
for (FlowRef ref : refs) {
Flow flow = dao.getForRefId(ref.flow.refId);
if (flow != null)
continue;
imp.run(ModelType.FLOW, ref.flow.refId);
}
} catch (Exception e) {
Logger log = LoggerFactory.getLogger(getClass());
log.error("failed persist flows", e);
}
}
use of org.openlca.io.maps.FlowRef in project olca-app by GreenDelta.
the class JsonRefCollector method buildFlows.
private List<FlowRef> buildFlows(ZipFile zip) {
List<FlowRef> flowRefs = new ArrayList<>();
for (ZipEntry e : flowEntries) {
JsonObject obj = unpack(e, zip);
if (obj == null)
continue;
String id = Json.getString(obj, "@id");
if (id == null)
continue;
FlowRef ref = new FlowRef();
ref.flow = new FlowDescriptor();
ref.flow.refId = id;
ref.flow.name = Json.getString(obj, "name");
ref.flow.flowType = Json.getEnum(obj, "flowType", FlowType.class);
// the category
String catID = Json.getRefId(obj, "category");
ref.flowCategory = categoryPaths.get(catID);
// find the reference flow property
String propID = null;
JsonArray props = Json.getArray(obj, "flowProperties");
if (props != null) {
for (JsonElement elem : props) {
if (!elem.isJsonObject())
continue;
JsonObject prop = elem.getAsJsonObject();
boolean isRef = Json.getBool(prop, "referenceFlowProperty", false);
if (!isRef)
continue;
propID = Json.getRefId(prop, "flowProperty");
break;
}
}
if (propID != null) {
ref.property = new FlowPropertyDescriptor();
ref.property.refId = propID;
ref.property.name = propertyNames.get(propID);
String unitID = propertyUnits.get(propID);
if (unitID != null) {
ref.unit = new UnitDescriptor();
ref.unit.refId = unitID;
ref.unit.name = unitNames.get(unitID);
}
}
flowRefs.add(ref);
}
return flowRefs;
}
Aggregations