Search in sources :

Example 6 with FlowRef

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());
        }
    });
}
Also used : FlowRef(org.openlca.io.maps.FlowRef) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 7 with FlowRef

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);
}
Also used : Action(org.eclipse.jface.action.Action) FlowRef(org.openlca.io.maps.FlowRef) FlowMapEntry(org.openlca.io.maps.FlowMapEntry)

Example 8 with FlowRef

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;
}
Also used : FlowType(org.openlca.core.model.FlowType) Descriptor(org.openlca.core.model.descriptors.Descriptor) Categories(org.openlca.util.Categories) Fn(org.openlca.app.util.Fn) FlowPropertyDao(org.openlca.core.database.FlowPropertyDao) Process(org.openlca.core.model.Process) ProcessDao(org.openlca.core.database.ProcessDao) Unit(org.openlca.core.model.Unit) Collectors(java.util.stream.Collectors) FlowRef(org.openlca.io.maps.FlowRef) ArrayList(java.util.ArrayList) Flow(org.openlca.core.model.Flow) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Objects(java.util.Objects) LocationDao(org.openlca.core.database.LocationDao) List(java.util.List) Stream(java.util.stream.Stream) IDatabase(org.openlca.core.database.IDatabase) Map(java.util.Map) FlowDao(org.openlca.core.database.FlowDao) FlowProperty(org.openlca.core.model.FlowProperty) MappingStatus(org.openlca.io.maps.MappingStatus) FlowPropertyDao(org.openlca.core.database.FlowPropertyDao) ArrayList(java.util.ArrayList) LocationDao(org.openlca.core.database.LocationDao) FlowRef(org.openlca.io.maps.FlowRef) FlowDao(org.openlca.core.database.FlowDao) FlowProperty(org.openlca.core.model.FlowProperty)

Example 9 with FlowRef

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);
    }
}
Also used : JsonImport(org.openlca.jsonld.input.JsonImport) FlowDao(org.openlca.core.database.FlowDao) FlowRef(org.openlca.io.maps.FlowRef) Logger(org.slf4j.Logger) ZipStore(org.openlca.jsonld.ZipStore) Flow(org.openlca.core.model.Flow)

Example 10 with FlowRef

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;
}
Also used : FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) FlowPropertyDescriptor(org.openlca.core.model.descriptors.FlowPropertyDescriptor) JsonArray(com.google.gson.JsonArray) FlowRef(org.openlca.io.maps.FlowRef) JsonElement(com.google.gson.JsonElement) UnitDescriptor(org.openlca.core.model.descriptors.UnitDescriptor) FlowType(org.openlca.core.model.FlowType)

Aggregations

FlowRef (org.openlca.io.maps.FlowRef)11 FlowMapEntry (org.openlca.io.maps.FlowMapEntry)5 Flow (org.openlca.core.model.Flow)4 FlowType (org.openlca.core.model.FlowType)4 ArrayList (java.util.ArrayList)3 FlowDao (org.openlca.core.database.FlowDao)3 Map (java.util.Map)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 DBProvider (org.openlca.app.tools.mapping.model.DBProvider)2 Fn (org.openlca.app.util.Fn)2 IDatabase (org.openlca.core.database.IDatabase)2 LocationDao (org.openlca.core.database.LocationDao)2 ProcessDao (org.openlca.core.database.ProcessDao)2 Descriptor (org.openlca.core.model.descriptors.Descriptor)2 MappingStatus (org.openlca.io.maps.MappingStatus)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1