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