Search in sources :

Example 6 with IDatabase

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

the class IsicTreeTest method testSyncProcessCategories.

@Test
public void testSyncProcessCategories() {
    IDatabase database = Tests.getDb();
    CategoryDao dao = new CategoryDao(database);
    Category cat = new Category();
    String catName = "01:Crop and animal production, hunting and related service activities";
    cat.name = catName;
    cat.modelType = ModelType.PROCESS;
    cat = dao.insert(cat);
    new IsicCategoryTreeSync(database, ModelType.PROCESS).run();
    cat = dao.getForId(cat.id);
    Assert.assertNotNull(cat.category);
    Assert.assertEquals("A:Agriculture, forestry and fishing", cat.category.name);
}
Also used : IDatabase(org.openlca.core.database.IDatabase) Category(org.openlca.core.model.Category) CategoryDao(org.openlca.core.database.CategoryDao) Test(org.junit.Test)

Example 7 with IDatabase

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

the class JsonExportWizard method performFinish.

@Override
public boolean performFinish() {
    IDatabase db = Database.get();
    if (db == null)
        return true;
    var models = page.getSelectedModels();
    if (models == null || models.isEmpty())
        return true;
    File target = page.getExportDestination();
    if (target == null)
        return false;
    try {
        Export export = new Export(target, models, db);
        getContainer().run(true, true, export);
        return true;
    } catch (Exception e) {
        ErrorReporter.on("Failed to export data sets", e);
        return false;
    }
}
Also used : IDatabase(org.openlca.core.database.IDatabase) JsonExport(org.openlca.jsonld.output.JsonExport) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 8 with IDatabase

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

the class XNexusEcoinventIndexExportAction method run.

@Override
public void run() {
    DbSelectDialog dialog = new DbSelectDialog();
    if (dialog.open() != IDialogConstants.OK_ID)
        return;
    File file = FileChooser.forSavingFile(M.Export, "ecoinvent_nexus_index.json");
    if (file == null)
        return;
    App.runWithProgress("Creating Ecoinvent Nexus Index", () -> {
        try {
            Map<String, IndexEntry> index = new HashMap<>();
            for (Entry e : dialog.entries) {
                IDatabase db;
                if (Database.get() != null && Database.get().getName().equals(e.database.name())) {
                    db = Database.get();
                } else {
                    db = e.database.connect(DataDir.databases());
                }
                ProcessDao dao = new ProcessDao(db);
                for (ProcessDescriptor descriptor : dao.getDescriptors()) {
                    Process process = dao.getForId(descriptor.id);
                    String id = getId(process);
                    IndexEntry entry = index.get(id);
                    if (entry == null) {
                        index.put(id, entry = new IndexEntry(process));
                    }
                    entry.name = getName(process);
                    entry.systemModel.add(e.systemModel);
                }
                db.close();
            }
            IndexEntry.writeEntries(index.values(), file);
        } catch (Exception e) {
            Log.error("Error creating ecoinvent nexus index", e);
        }
    });
}
Also used : IndexEntry(org.openlca.app.navigation.actions.XNexusIndexExportAction.IndexEntry) IDatabase(org.openlca.core.database.IDatabase) HashMap(java.util.HashMap) ProcessDao(org.openlca.core.database.ProcessDao) IndexEntry(org.openlca.app.navigation.actions.XNexusIndexExportAction.IndexEntry) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) Process(org.openlca.core.model.Process) File(java.io.File)

Example 9 with IDatabase

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

the class Exchanges method canRemove.

/**
 * Checks if the given exchanges can be removed from the process. The exchanges
 * cannot be removed and a corresponding error message is displayed when:
 *
 * <li>one of the given exchanges is the reference flow of the process
 * <li>at least one of the exchanges is used in a product system
 * <li>at least one of the exchanges is needed as default provider link
 */
static boolean canRemove(Process p, List<Exchange> exchanges) {
    if (p == null || exchanges == null)
        return false;
    // check reference flow
    if (p.quantitativeReference != null && exchanges.contains(p.quantitativeReference)) {
        MsgBox.error(M.CannotDeleteRefFlow, M.CannotDeleteRefFlowMessage);
        return false;
    }
    // collect product and waste flows
    List<Exchange> techFlows = exchanges.stream().filter(e -> e.flow != null && e.flow.flowType != FlowType.ELEMENTARY_FLOW).collect(Collectors.toList());
    if (techFlows.isEmpty())
        return true;
    // check usage in product systems
    var usages = new ExchangeUseSearch(Database.get(), p).findUses(techFlows);
    if (!usages.isEmpty()) {
        MsgBox.error(M.CannotRemoveExchanges, M.ExchangesAreUsed);
        return false;
    }
    // check provider links
    List<Exchange> providers = techFlows.stream().filter(e -> (e.isInput && e.flow.flowType == FlowType.WASTE_FLOW) || (!e.isInput && e.flow.flowType == FlowType.PRODUCT_FLOW)).collect(Collectors.toList());
    if (providers.isEmpty())
        return true;
    for (Exchange provider : providers) {
        String query = "select f_owner from tbl_exchanges where " + "f_default_provider = " + p.id + " and " + "f_flow = " + provider.flow.id + "";
        IDatabase db = Database.get();
        AtomicReference<ProcessDescriptor> ref = new AtomicReference<>();
        try {
            NativeSql.on(db).query(query, r -> {
                long owner = r.getLong(1);
                ProcessDescriptor d = new ProcessDao(db).getDescriptor(owner);
                if (d != null) {
                    ref.set(d);
                    return false;
                }
                return true;
            });
        } catch (Exception e) {
            Logger log = LoggerFactory.getLogger(Exchanges.class);
            log.error("Failed to query default providers " + query, e);
            return false;
        }
        if (ref.get() == null)
            continue;
        // we found an usage as default provider, now we need to make sure
        // that there is no other exchange with the same flow and direction
        // that can fulfill this role (and that is not in the list of
        // exchanges to be deleted).
        boolean ok = p.exchanges.stream().filter(e -> e.id != provider.id && e.isInput == provider.isInput && e.flow != null && e.flow.id == provider.flow.id && !exchanges.contains(e)).findAny().isPresent();
        if (ok)
            continue;
        MsgBox.error("Flow used as default provider", "This process is linked as default provider with flow `" + Strings.cut(Labels.name(provider.flow), 75) + "` in process `" + Strings.cut(Labels.name(ref.get()), 75) + "`.");
        return false;
    }
    return true;
}
Also used : FlowType(org.openlca.core.model.FlowType) Descriptor(org.openlca.core.model.descriptors.Descriptor) M(org.openlca.app.M) Labels(org.openlca.app.util.Labels) ExchangeUseSearch(org.openlca.core.database.usage.ExchangeUseSearch) Logger(org.slf4j.Logger) Process(org.openlca.core.model.Process) ProcessDao(org.openlca.core.database.ProcessDao) LoggerFactory(org.slf4j.LoggerFactory) MsgBox(org.openlca.app.util.MsgBox) NativeSql(org.openlca.core.database.NativeSql) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) ModelType(org.openlca.core.model.ModelType) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Database(org.openlca.app.db.Database) Strings(org.openlca.util.Strings) IDatabase(org.openlca.core.database.IDatabase) Exchange(org.openlca.core.model.Exchange) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) IDatabase(org.openlca.core.database.IDatabase) ExchangeUseSearch(org.openlca.core.database.usage.ExchangeUseSearch) AtomicReference(java.util.concurrent.atomic.AtomicReference) Logger(org.slf4j.Logger) Exchange(org.openlca.core.model.Exchange) ProcessDao(org.openlca.core.database.ProcessDao) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor)

Example 10 with IDatabase

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

the class IsicTreeTest method testSyncFlowCategories.

@Test
public void testSyncFlowCategories() {
    IDatabase database = Tests.getDb();
    CategoryDao dao = new CategoryDao(database);
    Category cat = new Category();
    cat.name = "0121:Growing of grapes";
    cat.modelType = ModelType.FLOW;
    cat = dao.insert(cat);
    new IsicCategoryTreeSync(database, ModelType.FLOW).run();
    cat = dao.getForId(cat.id);
    Assert.assertNotNull(cat.category);
    Assert.assertEquals("012:Growing of perennial crops", cat.category.name);
}
Also used : IDatabase(org.openlca.core.database.IDatabase) Category(org.openlca.core.model.Category) CategoryDao(org.openlca.core.database.CategoryDao) Test(org.junit.Test)

Aggregations

IDatabase (org.openlca.core.database.IDatabase)25 ProcessDao (org.openlca.core.database.ProcessDao)12 File (java.io.File)9 Process (org.openlca.core.model.Process)9 Test (org.junit.Test)8 Category (org.openlca.core.model.Category)4 Logger (org.slf4j.Logger)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 FlowDao (org.openlca.core.database.FlowDao)3 ProcessDescriptor (org.openlca.core.model.descriptors.ProcessDescriptor)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CategoryDao (org.openlca.core.database.CategoryDao)2 Derby (org.openlca.core.database.Derby)2 NativeSql (org.openlca.core.database.NativeSql)2 ParameterDao (org.openlca.core.database.ParameterDao)2 ProductSystemDao (org.openlca.core.database.ProductSystemDao)2 ProductSystemBuilder (org.openlca.core.matrix.ProductSystemBuilder)2