Search in sources :

Example 1 with FlowMap

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;
}
Also used : FlowMapEntry(org.openlca.io.maps.FlowMapEntry) ProtoFlowMapEntry(org.openlca.proto.ProtoFlowMapEntry) ProtoFlowMap(org.openlca.proto.ProtoFlowMap) FlowMap(org.openlca.io.maps.FlowMap)

Example 2 with 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;
    }
}
Also used : Logger(org.slf4j.Logger) JsonProvider(org.openlca.app.tools.mapping.model.JsonProvider) FlowMap(org.openlca.io.maps.FlowMap)

Example 3 with FlowMap

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);
}
Also used : FlowMap(org.openlca.io.maps.FlowMap)

Example 4 with FlowMap

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);
    });
}
Also used : M(org.openlca.app.M) Arrays(java.util.Arrays) PaintListener(org.eclipse.swt.events.PaintListener) TableViewer(org.eclipse.jface.viewers.TableViewer) Colors(org.openlca.app.util.Colors) Controls(org.openlca.app.util.Controls) Images(org.openlca.app.rcp.images.Images) IntConsumer(java.util.function.IntConsumer) Image(org.eclipse.swt.graphics.Image) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PaintEvent(org.eclipse.swt.events.PaintEvent) Database(org.openlca.app.db.Database) Composite(org.eclipse.swt.widgets.Composite) ErrorReporter(org.openlca.app.util.ErrorReporter) WizardPage(org.eclipse.jface.wizard.WizardPage) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) Text(org.eclipse.swt.widgets.Text) Combo(org.eclipse.swt.widgets.Combo) Viewer(org.eclipse.jface.viewers.Viewer) Button(org.eclipse.swt.widgets.Button) MappingFileDao(org.openlca.core.database.MappingFileDao) Set(java.util.Set) Selections(org.openlca.app.viewers.Selections) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) Preferences(org.openlca.app.preferences.Preferences) Collectors(java.util.stream.Collectors) File(java.io.File) ISharedImages(org.eclipse.ui.ISharedImages) List(java.util.List) FileType(org.openlca.app.util.FileType) Strings(org.openlca.util.Strings) UI(org.openlca.app.util.UI) FileChooser(org.openlca.app.components.FileChooser) SWT(org.eclipse.swt.SWT) DirectoryDialog(org.eclipse.swt.widgets.DirectoryDialog) FlowMap(org.openlca.io.maps.FlowMap) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Collections(java.util.Collections) Label(org.eclipse.swt.widgets.Label) FilenameUtils(org.apache.commons.io.FilenameUtils) LabelProvider(org.eclipse.jface.viewers.LabelProvider) Composite(org.eclipse.swt.widgets.Composite) Button(org.eclipse.swt.widgets.Button) Label(org.eclipse.swt.widgets.Label) Combo(org.eclipse.swt.widgets.Combo) MappingFileDao(org.openlca.core.database.MappingFileDao) File(java.io.File) IntConsumer(java.util.function.IntConsumer)

Example 5 with FlowMap

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);
        }
    });
}
Also used : Logger(org.slf4j.Logger) FlowMap(org.openlca.io.maps.FlowMap)

Aggregations

FlowMap (org.openlca.io.maps.FlowMap)6 ArrayList (java.util.ArrayList)2 Logger (org.slf4j.Logger)2 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 File (java.io.File)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 IntConsumer (java.util.function.IntConsumer)1 Collectors (java.util.stream.Collectors)1 FilenameUtils (org.apache.commons.io.FilenameUtils)1 ArrayContentProvider (org.eclipse.jface.viewers.ArrayContentProvider)1 ITreeContentProvider (org.eclipse.jface.viewers.ITreeContentProvider)1 LabelProvider (org.eclipse.jface.viewers.LabelProvider)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 TableViewer (org.eclipse.jface.viewers.TableViewer)1 TreeViewer (org.eclipse.jface.viewers.TreeViewer)1