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