Search in sources :

Example 11 with LocationDao

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

the class Processes method findForLabel.

/**
 * Searches the given database for a process with the given label. A label
 * can contain a suffix with a location code which is considered in this
 * function.
 */
public static ProcessDescriptor findForLabel(IDatabase db, String label) {
    if (db == null || label == null)
        return null;
    String name = null;
    Location location = null;
    if (label.contains(" - ")) {
        int splitIdx = label.lastIndexOf(" - ");
        name = label.substring(0, splitIdx).trim();
        String locationCode = label.substring(splitIdx + 3).trim();
        LocationDao dao = new LocationDao(db);
        for (Location loc : dao.getAll()) {
            if (Strings.nullOrEqual(loc.code, locationCode)) {
                location = loc;
                break;
            }
        }
    }
    ProcessDescriptor selected = null;
    ProcessDao pDao = new ProcessDao(db);
    for (ProcessDescriptor d : pDao.getDescriptors()) {
        if (!Strings.nullOrEqual(label, d.name) && !Strings.nullOrEqual(name, d.name))
            continue;
        if (selected == null) {
            selected = d;
            if (matchLocation(selected, location))
                break;
            else
                continue;
        }
        if (matchLocation(d, location) && !matchLocation(selected, location)) {
            selected = d;
            break;
        }
    }
    return selected;
}
Also used : ProcessDao(org.openlca.core.database.ProcessDao) LocationDao(org.openlca.core.database.LocationDao) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) Location(org.openlca.core.model.Location)

Example 12 with LocationDao

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

the class LocationDescriptorTest method test.

@Test
public void test() {
    Location loc = new Location();
    loc.name = "LOC";
    loc.refId = "LOC";
    loc.code = "LOC";
    LocationDao dao = new LocationDao(Tests.getDb());
    loc = dao.insert(loc);
    LocationDescriptor d = dao.descriptorMap().get(loc.id);
    dao.delete(loc);
    Assert.assertEquals(loc.name, d.name);
    Assert.assertEquals(loc.refId, d.refId);
    Assert.assertEquals(loc.code, d.code);
}
Also used : LocationDescriptor(org.openlca.core.model.descriptors.LocationDescriptor) LocationDao(org.openlca.core.database.LocationDao) Location(org.openlca.core.model.Location) Test(org.junit.Test)

Example 13 with LocationDao

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

the class LocationSheet method write.

private void write() {
    Excel.trackSize(sheet, 0, 5);
    writeHeader();
    var locations = new LocationDao(config.database).getAll();
    locations.sort(new EntitySorter());
    for (Location location : locations) {
        row++;
        write(location);
    }
    Excel.autoSize(sheet, 0, 5);
}
Also used : LocationDao(org.openlca.core.database.LocationDao) Location(org.openlca.core.model.Location)

Example 14 with LocationDao

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

the class MatrixExport method eachTechIndexRow.

protected void eachTechIndexRow(Consumer<String[]> fn) {
    if (data.techIndex == null || fn == null)
        return;
    String[] header = { "process ID", "process name", "process type", "process location", "process category", "flow ID", "flow name", "flow type", "flow location", "flow category", "flow unit" };
    fn.accept(header);
    var categories = Categories.pathsOf(db);
    var locations = new LocationDao(db).getCodes();
    var units = propUnits();
    for (int i = 0; i < data.techIndex.size(); i++) {
        var product = data.techIndex.at(i);
        var p = product.provider();
        var f = product.flow();
        var row = new String[header.length];
        row[0] = p.refId;
        row[1] = p.name;
        if (p instanceof ProcessDescriptor) {
            var pd = (ProcessDescriptor) p;
            row[2] = pd.processType != null ? pd.processType.toString() : ModelType.PROCESS.toString();
            row[3] = locations.get(pd.location);
        } else {
            row[2] = ModelType.PRODUCT_SYSTEM.toString();
            row[3] = "";
        }
        row[4] = categories.pathOf(p.category);
        row[5] = f.refId;
        row[6] = f.name;
        row[7] = f.flowType != null ? f.flowType.toString() : "";
        row[8] = locations.get(f.location);
        row[9] = categories.pathOf(f.category);
        row[10] = units.get(f.refFlowPropertyId);
        for (int j = 0; j < row.length; j++) {
            if (row[j] == null) {
                row[j] = "";
            }
        }
        fn.accept(row);
    }
}
Also used : LocationDao(org.openlca.core.database.LocationDao) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor)

Example 15 with LocationDao

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

the class LocationImport method of.

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

Aggregations

LocationDao (org.openlca.core.database.LocationDao)18 Location (org.openlca.core.model.Location)11 FlowDao (org.openlca.core.database.FlowDao)4 Test (org.junit.Test)3 ProcessDao (org.openlca.core.database.ProcessDao)3 Flow (org.openlca.core.model.Flow)3 ProcessDescriptor (org.openlca.core.model.descriptors.ProcessDescriptor)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)2 IDatabase (org.openlca.core.database.IDatabase)2 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)2 Unit (org.openlca.core.model.Unit)2 AbstractZipTest (org.openlca.jsonld.AbstractZipTest)2 Collections (java.util.Collections)1 Map (java.util.Map)1 Stream (java.util.stream.Stream)1 M (org.openlca.app.M)1