use of org.openlca.core.model.Category in project olca-modules by GreenDelta.
the class CategoryImport method updateParent.
private Category updateParent(Category category) {
// CategoryDao.update/insert will reassign a new ref id,
// it won't be possible to make the match with the updated child
// category, so we need to know which id will be generated by the db
String refId = Categories.createRefId(category);
Category parent = category.category;
// now check if category with id (generated) already exists
for (Category child : parent.childCategories) {
if (Objects.equals(child.refId, refId))
return child;
}
parent.childCategories.add(category);
parent = conf.db.updateChilds(parent);
for (Category child : parent.childCategories) {
if (Objects.equals(child.refId, refId))
return child;
}
return null;
}
use of org.openlca.core.model.Category in project olca-modules by GreenDelta.
the class CategoryImport method map.
@Override
Category map(JsonObject json, Category model) {
if (json == null)
return null;
boolean isNew = false;
String refId = Json.getString(json, "@id");
if (model == null) {
model = new Category();
isNew = true;
}
In.mapAtts(json, model, model.id, conf);
model.modelType = Json.getEnum(json, "modelType", ModelType.class);
if (!isNew || model.category == null)
model = conf.db.put(model);
else
model = updateParent(model);
if (!refId.equals(model.refId))
conf.db.categoryRefIdMapping.put(refId, model.refId);
return model;
}
use of org.openlca.core.model.Category 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);
}
use of org.openlca.core.model.Category in project olca-modules by GreenDelta.
the class ProcessConverter method createElementaryFlow.
private ElementaryExchangeRow createElementaryFlow(Exchange exchange) {
Flow olcaFlow = exchange.getFlow();
ElementaryExchangeRow flow = new ElementaryExchangeRow();
flow.setName(olcaFlow.getName());
flow.setUnit(map(exchange.getUnit()).getName());
flow.setAmount(String.valueOf(exchange.getAmountValue()));
CSVElementaryCategoryContent categoryContent = categoryMap.get(olcaFlow.getCategory().getRefId());
if (categoryContent != null) {
flow.setType(categoryContent.getType());
flow.setSubCompartment(categoryContent.getSubCompartment());
} else {
StringBuilder builder = new StringBuilder();
builder.append("Can not find category mapping for flow '");
builder.append(olcaFlow.getName());
builder.append("' in category '");
Category current = olcaFlow.getCategory();
String categoryTree = null;
while (current != null) {
if (categoryTree != null)
categoryTree = "/" + categoryTree;
categoryTree = current.getName() + categoryTree;
current = current.getCategory();
}
builder.append(categoryTree);
log.error(builder.toString());
}
return flow;
}
use of org.openlca.core.model.Category in project olca-modules by GreenDelta.
the class Categories method findOrAddChild.
public static Category findOrAddChild(IDatabase database, Category parent, String childName) {
for (Category child : parent.childCategories) {
if (StringUtils.equalsIgnoreCase(child.name, childName))
return child;
}
try {
Category child = new Category();
child.modelType = parent.modelType;
child.name = childName;
child.refId = UUID.randomUUID().toString();
child.category = parent;
parent.childCategories.add(child);
CategoryDao dao = new CategoryDao(database);
dao.insert(child);
new CategoryDao(database).update(parent);
return child;
} catch (Exception e) {
Logger log = LoggerFactory.getLogger(Categories.class);
log.error("failed to add child", e);
return null;
}
}
Aggregations