Search in sources :

Example 1 with MappingFileDao

use of org.openlca.core.database.MappingFileDao in project olca-modules by GreenDelta.

the class FlowMapService method delete.

@Override
public void delete(ProtoFlowMapName req, StreamObserver<Empty> resp) {
    var mapping = forceGet(req.getName(), resp);
    if (mapping == null)
        return;
    try {
        new MappingFileDao(db).delete(mapping);
        Response.ok(resp);
    } catch (Exception e) {
        Response.serverError(resp, "Failed to delete mapping with name='" + req.getName() + "' from database");
    }
}
Also used : MappingFileDao(org.openlca.core.database.MappingFileDao)

Example 2 with MappingFileDao

use of org.openlca.core.database.MappingFileDao in project olca-modules by GreenDelta.

the class FlowMapService method getAll.

@Override
public void getAll(Empty req, StreamObserver<ProtoFlowMapName> resp) {
    new MappingFileDao(db).getNames().stream().sorted(Strings::compare).map(name -> ProtoFlowMapName.newBuilder().setName(name).build()).forEach(resp::onNext);
    resp.onCompleted();
}
Also used : FlowMapServiceGrpc(org.openlca.proto.grpc.FlowMapServiceGrpc) Arrays(java.util.Arrays) In(org.openlca.proto.io.input.In) Refs(org.openlca.proto.io.output.Refs) MappingFileDao(org.openlca.core.database.MappingFileDao) Messages(org.openlca.proto.io.Messages) ProtoFlowMap(org.openlca.proto.ProtoFlowMap) ProtocolStringList(com.google.protobuf.ProtocolStringList) ProtoFlowMapRef(org.openlca.proto.ProtoFlowMapRef) FlowMapEntry(org.openlca.io.maps.FlowMapEntry) ProtoFlowMapEntry(org.openlca.proto.ProtoFlowMapEntry) FlowRef(org.openlca.io.maps.FlowRef) Empty(com.google.protobuf.Empty) ProtoFlowMapName(org.openlca.proto.grpc.ProtoFlowMapName) Strings(org.openlca.util.Strings) StreamObserver(io.grpc.stub.StreamObserver) MappingFile(org.openlca.core.model.MappingFile) IDatabase(org.openlca.core.database.IDatabase) FlowMap(org.openlca.io.maps.FlowMap) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) MappingFileDao(org.openlca.core.database.MappingFileDao) Strings(org.openlca.util.Strings)

Example 3 with MappingFileDao

use of org.openlca.core.database.MappingFileDao in project olca-app by GreenDelta.

the class OpenMappingAction method run.

/**
 * Open the mapping file with the given name from the currently
 * active database.
 */
public static void run(String mappingFile) {
    var db = Database.get();
    if (db == null || mappingFile == null)
        return;
    var mapping = new MappingFileDao(db).getForName(mappingFile);
    if (mapping == null) {
        ErrorReporter.on("Mapping file " + mappingFile + " does not exist");
        return;
    }
    try {
        MappingTool.open(mapping);
    } catch (Exception e) {
        ErrorReporter.on("Failed to open " + "mapping file: " + mappingFile, e);
    }
}
Also used : MappingFileDao(org.openlca.core.database.MappingFileDao)

Example 4 with MappingFileDao

use of org.openlca.core.database.MappingFileDao 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 MappingFileDao

use of org.openlca.core.database.MappingFileDao in project olca-app by GreenDelta.

the class GroupElement method queryChilds.

@Override
protected List<INavigationElement<?>> queryChilds() {
    var group = getContent();
    if (group == null)
        return Collections.emptyList();
    // add the model type elements of this group
    var elements = new ArrayList<INavigationElement<?>>();
    for (ModelType type : getContent().types) {
        elements.add(new ModelTypeElement(this, type));
    }
    // if available
    if (group.type != GroupType.BACKGROUND_DATA)
        return elements;
    var lib = getLibrary();
    if (lib.isPresent())
        return elements;
    var db = Database.get();
    if (db == null)
        return elements;
    var names = new MappingFileDao(db).getNames();
    if (!names.isEmpty()) {
        elements.add(new MappingDirElement(this, names));
    }
    return elements;
}
Also used : ArrayList(java.util.ArrayList) ModelType(org.openlca.core.model.ModelType) MappingFileDao(org.openlca.core.database.MappingFileDao)

Aggregations

MappingFileDao (org.openlca.core.database.MappingFileDao)8 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 FlowMap (org.openlca.io.maps.FlowMap)2 Strings (org.openlca.util.Strings)2 Empty (com.google.protobuf.Empty)1 ProtocolStringList (com.google.protobuf.ProtocolStringList)1 StreamObserver (io.grpc.stub.StreamObserver)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