Some bugfixes for category ordering

Former-commit-id: 9de74d77291d945df502383710a1b5cffbbc86e6
pull/10/head
Jens Pelzetter 2021-02-19 17:36:08 +01:00
parent f8d7da1fde
commit 086a8622de
2 changed files with 42 additions and 8 deletions

View File

@ -830,11 +830,19 @@ public class CategoriesController {
break;
}
final String parentCategoryPath = categoryManager
.getCategoryPath(parentCategory);
final String pathFragment;
if ("/".equals(parentCategoryPath)) {
pathFragment = "";
} else {
pathFragment = parentCategoryPath;
}
return String.format(
"redirect:/%s/categorysystems/%s/categories/%s",
"redirect:/%s/categorysystems/%s/categories/%s#subcategories-section",
sectionIdentifier,
context,
categoryManager.getCategoryPath(parentCategory)
pathFragment
);
} else {
return result.getResponseTemplate();

View File

@ -789,6 +789,8 @@ public class CategoryManager implements Serializable {
categoryRepo.save(subCategory);
categoryRepo.save(nextCategory);
});
fixSubCategoryOrder(parentCategory);
}
/**
@ -864,6 +866,30 @@ public class CategoryManager implements Serializable {
categoryRepo.save(subCategory);
categoryRepo.save(prevCategory);
});
fixSubCategoryOrder(parentCategory);
}
@AuthorizationRequired
@Transactional(Transactional.TxType.REQUIRED)
public void fixSubCategoryOrder(final Category category) {
Objects.requireNonNull(category);
long order = 1;
final List<Category> subCategories = new ArrayList<>(
category.getSubCategories()
);
subCategories.sort(
(c1, c2) -> {
return Long.compare(c1.getCategoryOrder(), c2.getCategoryOrder());
});
for (final Category subCategory : subCategories) {
subCategory.setCategoryOrder(order);
shiro.getSystemUser().execute(() -> {
categoryRepo.save(subCategory);
});
order++;
}
}
/**
@ -888,7 +914,7 @@ public class CategoryManager implements Serializable {
"No category with ID %d in the database. Where did that ID come from?",
ofCategory.getObjectId())));
while(current.getParentCategory() != null) {
while (current.getParentCategory() != null) {
categories.add(current);
current = current.getParentCategory();
}