Search in sources :

Example 11 with CategoryDao

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

the class CopyPaste method move.

private static void move(CategoryElement element, INavigationElement<?> categoryElement) {
    Category newParent = getCategory(categoryElement);
    Category oldParent = getCategory(element.getParent());
    Category category = element.getContent();
    if (Objects.equals(category, newParent))
        return;
    if (isChild(newParent, category))
        // do not create category cycles
        return;
    if (oldParent != null)
        oldParent.childCategories.remove(category);
    if (newParent != null)
        newParent.childCategories.add(category);
    category.category = newParent;
    CategoryDao dao = new CategoryDao(Database.get());
    if (oldParent != null) {
        dao.update(oldParent);
    }
    if (newParent != null) {
        dao.update(newParent);
    }
    dao.update(category);
}
Also used : Category(org.openlca.core.model.Category) CategoryDao(org.openlca.core.database.CategoryDao)

Example 12 with CategoryDao

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

the class DeleteModelAction method delete.

private boolean delete(CategoryElement element) {
    boolean canBeDeleted = true;
    for (INavigationElement<?> child : element.getChildren()) {
        if (child instanceof CategoryElement) {
            boolean deleted = delete((CategoryElement) child);
            if (!deleted) {
                canBeDeleted = false;
            }
        } else if (child instanceof ModelElement) {
            var descriptor = ((ModelElement) child).getContent();
            if (isUsed(descriptor)) {
                canBeDeleted = false;
                continue;
            }
            App.close(descriptor);
            delete(descriptor);
        }
    }
    if (!canBeDeleted) {
        Navigator.refresh(element);
        return false;
    }
    Category category = element.getContent();
    try {
        CategoryDao dao = new CategoryDao(Database.get());
        Category parent = category.category;
        if (parent != null) {
            parent.childCategories.remove(category);
            category.category = null;
            dao.update(parent);
        }
        dao.delete(category);
        Cache.evict(Descriptor.of(category));
        return true;
    } catch (Exception e) {
        ErrorReporter.on("failed to delete category " + category, e);
        return false;
    }
}
Also used : ModelElement(org.openlca.app.navigation.elements.ModelElement) Category(org.openlca.core.model.Category) CategoryElement(org.openlca.app.navigation.elements.CategoryElement) CategoryDao(org.openlca.core.database.CategoryDao)

Example 13 with CategoryDao

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

the class CategoryElement method update.

@Override
public void update() {
    super.update();
    Category category = getContent();
    // refId might have changed
    setContent(new CategoryDao(Database.get()).getForId(category.id));
}
Also used : Category(org.openlca.core.model.Category) CategoryDao(org.openlca.core.database.CategoryDao)

Example 14 with CategoryDao

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

the class Categories method createRoot.

/**
 * Creates a new root category with the given name and of the given type and
 * inserts it into the given database.
 */
public static Category createRoot(IDatabase database, ModelType type, String name) {
    try {
        Category category = new Category();
        category.refId = UUID.randomUUID().toString();
        category.modelType = type;
        category.name = name;
        new CategoryDao(database).insert(category);
        return category;
    } catch (Exception e) {
        Logger log = LoggerFactory.getLogger(Categories.class);
        log.error("failed to insert root category " + name + " / " + type, e);
        return null;
    }
}
Also used : Category(org.openlca.core.model.Category) CategoryDao(org.openlca.core.database.CategoryDao) Logger(org.slf4j.Logger)

Example 15 with CategoryDao

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

the class DB method getPutCategory.

public Category getPutCategory(ModelType type, String parentName, String name) {
    String key = StringUtils.join(new Object[] { type.name(), parentName, name }, "/");
    Category category = categories.get(key);
    if (category != null)
        return category;
    try {
        CategoryDao dao = new CategoryDao(database);
        dao.getRootCategories(type);
        Category parent = null;
        if (parentName != null) {
            parent = Categories.findRoot(database, type, parentName);
            if (parent == null)
                parent = Categories.createRoot(database, type, parentName);
        }
        category = parent;
        if (name != null) {
            if (parent != null)
                category = Categories.findOrAddChild(database, parent, name);
            else
                category = Categories.createRoot(database, type, name);
        }
        categories.put(key, category);
        return category;
    } catch (Exception e) {
        log.error("Failed to get category " + key, e);
        return null;
    }
}
Also used : Category(org.openlca.core.model.Category) CategoryDao(org.openlca.core.database.CategoryDao)

Aggregations

CategoryDao (org.openlca.core.database.CategoryDao)20 Category (org.openlca.core.model.Category)11 Test (org.junit.Test)4 CategoryElement (org.openlca.app.navigation.elements.CategoryElement)2 ModelElement (org.openlca.app.navigation.elements.ModelElement)2 FlowDao (org.openlca.core.database.FlowDao)2 IDatabase (org.openlca.core.database.IDatabase)2 ImpactMethodDao (org.openlca.core.database.ImpactMethodDao)2 ProductSystemDao (org.openlca.core.database.ProductSystemDao)2 FlowProperty (org.openlca.core.model.FlowProperty)2 Logger (org.slf4j.Logger)2 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 ActorDao (org.openlca.core.database.ActorDao)1 CurrencyDao (org.openlca.core.database.CurrencyDao)1 DQSystemDao (org.openlca.core.database.DQSystemDao)1 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)1 ImpactCategoryDao (org.openlca.core.database.ImpactCategoryDao)1 LocationDao (org.openlca.core.database.LocationDao)1 NwSetDao (org.openlca.core.database.NwSetDao)1