Search in sources :

Example 36 with FlowDao

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

the class ReplaceFlowsDialog method okPressed.

@Override
protected void okPressed() {
    FlowDescriptor oldFlow = selectionViewer.getSelected();
    FlowDescriptor newFlow = replacementViewer.getSelected();
    FlowDao dao = new FlowDao(Database.get());
    boolean replaceFlows = replaceBothButton.getSelection() || replaceFlowsButton.getSelection();
    boolean replaceImpacts = replaceBothButton.getSelection() || replaceImpactsButton.getSelection();
    if (replaceFlows) {
        if (excludeWithProviders.getSelection()) {
            dao.replaceExchangeFlowsWithoutProviders(oldFlow.id, newFlow.id);
        } else {
            dao.replaceExchangeFlows(oldFlow.id, newFlow.id);
        }
    }
    if (replaceImpacts) {
        dao.replaceImpactFlows(oldFlow.id, newFlow.id);
    }
    Database.get().getEntityFactory().getCache().evictAll();
    super.okPressed();
}
Also used : FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) FlowDao(org.openlca.core.database.FlowDao)

Example 37 with FlowDao

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

the class FactorClipboard method factor.

private ImpactFactor factor(String[] row) {
    if (row.length < 4)
        return null;
    String name = row[0];
    String category = row[1];
    String amount = row[2];
    String unit = row[3];
    // filter the flows by matching names and categories
    List<Flow> candidates = flows.stream().filter(d -> Strings.nullOrEqual(d.name, name)).map(d -> new FlowDao(db).getForId(d.id)).filter(flow -> {
        if (flow.category == null)
            return Strings.nullOrEmpty(category);
        String path = CategoryPath.getFull(flow.category);
        return Strings.nullOrEqual(path, category);
    }).collect(Collectors.toList());
    if (candidates.isEmpty())
        return null;
    // find a matching flow for the unit
    // the unit in the table has the format:
    // <LCIA ref. unit> / <flow unit>
    // the following only works if the LCIA
    // ref. unit does not contain a slash, but
    // this should be very unlikely
    int i = unit.indexOf('/');
    if (i >= 0) {
        unit = unit.substring(i + 1).trim();
    }
    ImpactFactor factor = new ImpactFactor();
    for (Flow flow : candidates) {
        for (FlowPropertyFactor p : flow.flowPropertyFactors) {
            if (p.flowProperty == null || p.flowProperty.unitGroup == null)
                continue;
            Unit u = p.flowProperty.unitGroup.getUnit(unit);
            if (u == null)
                continue;
            factor.flow = flow;
            factor.flowPropertyFactor = p;
            factor.unit = u;
            if (Objects.equals(p.flowProperty, flow.referenceFlowProperty))
                break;
        }
        if (factor.flow != null)
            break;
    }
    if (factor.flow == null)
        return null;
    // set the amount value / formula
    try {
        factor.value = Double.parseDouble(amount);
    } catch (Exception e) {
        factor.formula = amount;
    }
    // uncertainty value
    if (row.length > 4) {
        factor.uncertainty = Uncertainty.fromString(row[4]);
    }
    // location
    if (row.length > 5) {
        String code = row[5];
        if (!Strings.nullOrEmpty(code)) {
            LocationDao dao = new LocationDao(db);
            factor.location = dao.getDescriptors().stream().filter(d -> Strings.nullOrEqual(code, d.code)).map(d -> dao.getForId(d.id)).findFirst().orElse(null);
        }
    }
    return factor;
}
Also used : M(org.openlca.app.M) Unit(org.openlca.core.model.Unit) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Flow(org.openlca.core.model.Flow) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) ImpactFactor(org.openlca.core.model.ImpactFactor) Objects(java.util.Objects) LocationDao(org.openlca.core.database.LocationDao) List(java.util.List) Database(org.openlca.app.db.Database) Strings(org.openlca.util.Strings) IDatabase(org.openlca.core.database.IDatabase) Uncertainty(org.openlca.core.model.Uncertainty) FlowDao(org.openlca.core.database.FlowDao) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) Collections(java.util.Collections) CategoryPath(org.openlca.io.CategoryPath) ImpactFactor(org.openlca.core.model.ImpactFactor) FlowDao(org.openlca.core.database.FlowDao) LocationDao(org.openlca.core.database.LocationDao) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Unit(org.openlca.core.model.Unit) Flow(org.openlca.core.model.Flow)

Example 38 with FlowDao

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

the class ReplaceProvidersDialog method getProviders.

private List<ProcessDescriptor> getProviders(FlowDescriptor product) {
    List<ProcessDescriptor> result = new ArrayList<>();
    // TODO: search for processes and waste flows
    FlowDao flowDao = new FlowDao(Database.get());
    Set<Long> ids = flowDao.getWhereOutput(product.id);
    ProcessDao processDao = new ProcessDao(Database.get());
    result.addAll(processDao.getDescriptors(ids));
    result.remove(processViewer.getSelected());
    return result;
}
Also used : FlowDao(org.openlca.core.database.FlowDao) ArrayList(java.util.ArrayList) ProcessDao(org.openlca.core.database.ProcessDao) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor)

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