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