Search in sources :

Example 11 with FlowDao

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

the class FlowImport method of.

@Override
public ImportStatus<Flow> of(String id) {
    var flow = imp.get(Flow.class, id);
    // check if we are in update mode
    var inUpdateMode = false;
    if (flow != null) {
        inUpdateMode = imp.shouldUpdate(flow);
        if (!inUpdateMode) {
            return ImportStatus.skipped(flow);
        }
    }
    // resolve the proto object
    var proto = imp.reader.getFlow(id);
    if (proto == null)
        return flow != null ? ImportStatus.skipped(flow) : ImportStatus.error("Could not resolve Flow " + id);
    var wrap = ProtoWrap.of(proto);
    if (inUpdateMode) {
        if (imp.skipUpdate(flow, wrap))
            return ImportStatus.skipped(flow);
    }
    // map the data
    if (flow == null) {
        flow = new Flow();
        flow.refId = id;
    }
    wrap.mapTo(flow, imp);
    map(proto, flow, inUpdateMode);
    // insert or update it
    var dao = new FlowDao(imp.db);
    flow = inUpdateMode ? dao.update(flow) : dao.insert(flow);
    imp.putHandled(flow);
    return inUpdateMode ? ImportStatus.updated(flow) : ImportStatus.created(flow);
}
Also used : FlowDao(org.openlca.core.database.FlowDao) ProtoFlow(org.openlca.proto.ProtoFlow) Flow(org.openlca.core.model.Flow)

Example 12 with FlowDao

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

the class Library method syncProducts.

/**
 * Returns the products of the library in matrix order. If this library has
 * no product index or if this index is not in sync with the database, an
 * empty option is returned.
 */
public Optional<TechIndex> syncProducts(IDatabase db) {
    var processes = descriptors(new ProcessDao(db));
    var products = descriptors(new FlowDao(db));
    TechIndex index = null;
    var proto = getProductIndex();
    int size = proto.getProductCount();
    for (int i = 0; i < size; i++) {
        var entry = proto.getProduct(i);
        var process = processes.get(entry.getProcess().getId());
        var product = products.get(entry.getProduct().getId());
        if (process == null || product == null)
            return Optional.empty();
        if (index == null) {
            index = new TechIndex(TechFlow.of(process, product));
        } else {
            index.add(TechFlow.of(process, product));
        }
    }
    return Optional.ofNullable(index);
}
Also used : FlowDao(org.openlca.core.database.FlowDao) ProcessDao(org.openlca.core.database.ProcessDao) TechIndex(org.openlca.core.matrix.index.TechIndex)

Example 13 with FlowDao

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

the class Library method syncElementaryFlows.

/**
 * Returns the elementary flows of the library in matrix order. If this
 * information is not present or something went wrong while synchronizing
 * the flow index with the database, an empty option is returned.
 */
public Optional<EnviIndex> syncElementaryFlows(IDatabase db) {
    var proto = getElemFlowIndex();
    int size = proto.getFlowCount();
    if (size == 0)
        return Optional.empty();
    var info = getInfo();
    var index = info.isRegionalized() ? EnviIndex.createRegionalized() : EnviIndex.create();
    var flows = descriptors(new FlowDao(db));
    var locations = descriptors(new LocationDao(db));
    for (int i = 0; i < size; i++) {
        var entry = proto.getFlow(i);
        var flow = flows.get(entry.getFlow().getId());
        var location = locations.get(entry.getLocation().getId());
        if (flow == null)
            return Optional.empty();
        if (entry.getIsInput()) {
            index.add(EnviFlow.inputOf(flow, location));
        } else {
            index.add(EnviFlow.outputOf(flow, location));
        }
    }
    return Optional.of(index);
}
Also used : FlowDao(org.openlca.core.database.FlowDao) LocationDao(org.openlca.core.database.LocationDao)

Example 14 with FlowDao

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

the class RefIdMapTest method setUp.

@Before
public void setUp() {
    flow = new Flow();
    flow.refId = UUID.randomUUID().toString();
    flow = new FlowDao(db).insert(flow);
}
Also used : FlowDao(org.openlca.core.database.FlowDao) Flow(org.openlca.core.model.Flow) Before(org.junit.Before)

Example 15 with FlowDao

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

the class ReplaceFlowsDialog method getReplacementCandidates.

private List<FlowDescriptor> getReplacementCandidates(FlowDescriptor flow) {
    if (flow == null || flow.id == 0L)
        return Collections.emptyList();
    FlowDao dao = new FlowDao(Database.get());
    Set<Long> ids = dao.getReplacementCandidates(flow.id, flow.flowType);
    List<FlowDescriptor> result = new ArrayList<>();
    result.addAll(dao.getDescriptors(ids));
    result.remove(flow);
    return result;
}
Also used : FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) FlowDao(org.openlca.core.database.FlowDao) ArrayList(java.util.ArrayList)

Aggregations

FlowDao (org.openlca.core.database.FlowDao)38 Flow (org.openlca.core.model.Flow)21 ProcessDao (org.openlca.core.database.ProcessDao)13 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)10 ArrayList (java.util.ArrayList)7 Collectors (java.util.stream.Collectors)6 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)6 IDatabase (org.openlca.core.database.IDatabase)6 FlowProperty (org.openlca.core.model.FlowProperty)6 FlowDescriptor (org.openlca.core.model.descriptors.FlowDescriptor)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 Test (org.junit.Test)4 LocationDao (org.openlca.core.database.LocationDao)4 Process (org.openlca.core.model.Process)4 Unit (org.openlca.core.model.Unit)4 TLongHashSet (gnu.trove.set.hash.TLongHashSet)3 HashSet (java.util.HashSet)3 Objects (java.util.Objects)3