Search in sources :

Example 26 with Category

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;
}
Also used : Category(org.openlca.core.model.Category)

Example 27 with Category

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;
}
Also used : Category(org.openlca.core.model.Category) ModelType(org.openlca.core.model.ModelType)

Example 28 with Category

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);
}
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 29 with Category

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;
}
Also used : Category(org.openlca.core.model.Category) ProcessCategory(org.openlca.simapro.csv.model.enums.ProcessCategory) ElementaryExchangeRow(org.openlca.simapro.csv.model.process.ElementaryExchangeRow) CSVElementaryCategoryContent(org.openlca.io.maps.content.CSVElementaryCategoryContent) Flow(org.openlca.core.model.Flow)

Example 30 with Category

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;
    }
}
Also used : Category(org.openlca.core.model.Category) CategoryDao(org.openlca.core.database.CategoryDao) Logger(org.slf4j.Logger)

Aggregations

Category (org.openlca.core.model.Category)77 Test (org.junit.Test)11 CategoryDao (org.openlca.core.database.CategoryDao)11 Flow (org.openlca.core.model.Flow)6 Parameter (org.openlca.core.model.Parameter)6 CategoryElement (org.openlca.app.navigation.elements.CategoryElement)4 ModelElement (org.openlca.app.navigation.elements.ModelElement)4 IDatabase (org.openlca.core.database.IDatabase)4 FlowProperty (org.openlca.core.model.FlowProperty)4 Location (org.openlca.core.model.Location)4 Process (org.openlca.core.model.Process)4 UnitGroup (org.openlca.core.model.UnitGroup)4 ArrayList (java.util.ArrayList)3 ImpactCategory (org.openlca.core.model.ImpactCategory)3 ImpactMethod (org.openlca.core.model.ImpactMethod)3 ModelType (org.openlca.core.model.ModelType)3 IsicNode (org.openlca.io.ecospold2.input.IsicTree.IsicNode)3 Actor (org.openlca.core.model.Actor)2 Currency (org.openlca.core.model.Currency)2 DQSystem (org.openlca.core.model.DQSystem)2