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");
}
}
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();
}
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);
}
}
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);
});
}
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;
}
Aggregations