use of org.openlca.io.maps.FlowMap in project olca-modules by GreenDelta.
the class FlowMapService method toModel.
private FlowMap toModel(ProtoFlowMap proto) {
if (proto == null)
return FlowMap.empty();
var flowMap = new FlowMap();
flowMap.name = proto.getName();
flowMap.description = proto.getDescription();
flowMap.refId = proto.getId();
for (var protoEntry : proto.getMappingsList()) {
var source = toModelRef(protoEntry.getFrom());
var target = toModelRef(protoEntry.getTo());
var factor = protoEntry.getConversionFactor();
flowMap.entries.add(new FlowMapEntry(source, target, factor));
}
return flowMap;
}
use of org.openlca.io.maps.FlowMap in project olca-app by GreenDelta.
the class JsonImportDialog method open.
/**
* Returns the selected flow map or null when no mapping was selected.
*/
static FlowMap open(File file) {
if (file == null)
return null;
try {
JsonProvider provider = JsonProvider.of(file);
List<FlowMap> maps = App.exec("Search flow mappings ...", () -> provider.getFlowMaps());
JsonImportDialog d = new JsonImportDialog(provider, maps);
if (d.open() != Dialog.OK) {
return null;
}
if (d.selectedMap == null) {
MsgBox.info("Not yet implemented.");
return null;
}
return d.selectedMap;
} catch (Exception e) {
MsgBox.error("Failed to open file as JSON-LD package");
Logger log = LoggerFactory.getLogger(JsonImportDialog.class);
log.error("failed to open JSON-LD package " + file, e);
return null;
}
}
use of org.openlca.io.maps.FlowMap in project olca-app by GreenDelta.
the class MappingTool method createNew.
public static void createNew() {
FlowMap mapping = new FlowMap();
mapping.name = "New flow mapping";
mapping.refId = UUID.randomUUID().toString();
open(mapping);
}
use of org.openlca.io.maps.FlowMap in project olca-app by GreenDelta.
the class FileImportPage method mappingRow.
private void mappingRow(Composite body) {
if (!withMappingFile)
return;
var db = Database.get();
if (db == null)
return;
var comp = new Composite(body, SWT.NONE);
UI.gridLayout(comp, 3, 5, 0);
UI.gridData(comp, true, false);
new Label(comp, SWT.NONE).setText("Flow mapping");
// initialize the combo box
var combo = new Combo(comp, SWT.READ_ONLY);
UI.gridData(combo, true, false);
var dbFiles = new MappingFileDao(db).getNames().stream().sorted().collect(Collectors.toList());
var items = new String[dbFiles.size() + 1];
items[0] = "";
for (int i = 0; i < dbFiles.size(); i++) {
items[i + 1] = dbFiles.get(i);
}
combo.setItems(items);
combo.select(0);
// handle a combo selection event
IntConsumer onSelect = idx -> {
try {
// no mapping
var mapping = combo.getItem(idx);
if (Strings.nullOrEmpty(mapping)) {
flowMap = null;
return;
}
// db mapping
var dbMap = new MappingFileDao(db).getForName(mapping);
if (dbMap != null) {
flowMap = FlowMap.of(dbMap);
return;
}
// file mapping
var file = new File(mapping);
if (file.exists()) {
flowMap = FlowMap.fromCsv(file);
}
} catch (Exception e) {
ErrorReporter.on("Failed to open mapping", e);
}
};
Controls.onSelect(combo, _e -> {
var i = combo.getSelectionIndex();
onSelect.accept(i);
});
// add the file button
var fileBtn = new Button(comp, SWT.NONE);
fileBtn.setText("From file");
Controls.onSelect(fileBtn, e -> {
var file = FileChooser.open("*.csv");
if (file == null)
return;
var oldItems = combo.getItems();
var nextItems = Arrays.copyOf(oldItems, oldItems.length + 1);
var idx = nextItems.length - 1;
nextItems[idx] = file.getAbsolutePath();
combo.setItems(nextItems);
// does not fire an event
combo.select(idx);
onSelect.accept(idx);
});
}
use of org.openlca.io.maps.FlowMap in project olca-app by GreenDelta.
the class MappingMenu method onSave.
private void onSave() {
MappingTool tool = Editors.getActive();
if (tool == null || tool.mapping == null)
return;
FlowMap map = tool.mapping;
String name = map.name;
if (name == null) {
name = "flow_map.csv";
} else {
if (!name.endsWith(".csv")) {
name += ".csv";
}
}
var file = FileChooser.forSavingFile(M.Export, name);
if (file == null)
return;
App.runWithProgress("Save flow mapping ...", () -> {
try {
FlowMap.toCsv(map, file);
} catch (Exception e) {
Logger log = LoggerFactory.getLogger(MappingMenu.class);
log.error("failed to save flow mapping", e);
}
});
}
Aggregations