use of io.gravitee.repository.management.model.Category in project gravitee-management-rest-api by gravitee-io.
the class CategoryServiceImpl method delete.
@Override
public void delete(final String categoryId) {
try {
Optional<Category> categoryOptional = categoryRepository.findById(categoryId);
if (categoryOptional.isPresent()) {
categoryRepository.delete(categoryId);
auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, categoryId), CATEGORY_DELETED, new Date(), null, categoryOptional.get());
// delete all reference on APIs
apiService.deleteCategoryFromAPIs(categoryId);
}
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to delete category {}", categoryId, ex);
throw new TechnicalManagementException("An error occurs while trying to delete category " + categoryId, ex);
}
}
use of io.gravitee.repository.management.model.Category in project gravitee-management-rest-api by gravitee-io.
the class CategoryServiceImpl method convert.
private Category convert(final UpdateCategoryEntity categoryEntity, final String environment) {
final Category category = new Category();
category.setId(categoryEntity.getId());
category.setKey(IdGenerator.generate(categoryEntity.getName()));
category.setEnvironmentId(environment);
category.setName(categoryEntity.getName());
category.setDescription(categoryEntity.getDescription());
category.setOrder(categoryEntity.getOrder());
category.setHidden(categoryEntity.isHidden());
category.setHighlightApi(categoryEntity.getHighlightApi());
category.setPicture(categoryEntity.getPicture());
category.setBackground(categoryEntity.getBackground());
category.setPage(categoryEntity.getPage());
return category;
}
use of io.gravitee.repository.management.model.Category in project gravitee-management-rest-api by gravitee-io.
the class CategoryServiceImpl method findById.
@Override
public CategoryEntity findById(final String id) {
try {
LOGGER.debug("Find category by id : {}", id);
Optional<Category> category = categoryRepository.findById(id);
if (!category.isPresent()) {
category = categoryRepository.findByKey(id, GraviteeContext.getCurrentEnvironment());
}
if (category.isPresent()) {
return convert(category.get());
}
throw new CategoryNotFoundException(id);
} catch (TechnicalException ex) {
final String error = "An error occurs while trying to find a category using its id: " + id;
LOGGER.error(error, ex);
throw new TechnicalManagementException(error, ex);
}
}
use of io.gravitee.repository.management.model.Category in project gravitee-management-rest-api by gravitee-io.
the class DefaultCategoryUpgrader method upgrade.
@Override
public boolean upgrade() {
// Initialize default category
final Set<Category> categories;
try {
categories = categoryRepository.findAll();
Optional<Category> optionalKeyLessCategory = categories.stream().filter(c -> c.getKey() == null || c.getKey().isEmpty()).findFirst();
Optional<Category> optionalCategoryWithoutCreationDate = categories.stream().filter(c -> c.getCreatedAt() == null).findFirst();
final boolean keyLessCategoriesExist = optionalKeyLessCategory.isPresent();
final boolean categoriesWithoutCreationDateExist = optionalCategoryWithoutCreationDate.isPresent();
if (keyLessCategoriesExist || categoriesWithoutCreationDateExist) {
if (keyLessCategoriesExist) {
logger.info("Update categories to add field key");
}
if (categoriesWithoutCreationDateExist) {
logger.info("Update categories to add createdAt");
}
for (final Category category : categories) {
if (keyLessCategoriesExist) {
// add a key for keyless categories
category.setKey(IdGenerator.generate(category.getName()));
}
if (categoriesWithoutCreationDateExist) {
// add createdAt if not exist (https://github.com/gravitee-io/issues/issues/5275)
final Date createdAt = new Date();
category.setCreatedAt(createdAt);
category.setUpdatedAt(createdAt);
}
categoryRepository.update(category);
}
}
} catch (TechnicalException e) {
logger.error("Error while upgrading categories : {}", e);
}
return true;
}
use of io.gravitee.repository.management.model.Category in project gravitee-management-rest-api by gravitee-io.
the class CategoryService_CreateTest method shouldNotCreateExistingCategory.
@Test(expected = DuplicateCategoryNameException.class)
public void shouldNotCreateExistingCategory() throws TechnicalException {
Category v1 = new Category();
NewCategoryEntity nv1 = new NewCategoryEntity();
v1.setName("v1");
nv1.setName("v1");
when(mockCategoryRepository.findAllByEnvironment(any())).thenReturn(Collections.singleton(v1));
try {
categoryService.create(nv1);
} catch (DuplicateCategoryNameException e) {
verify(mockCategoryRepository, never()).create(any());
throw e;
}
Assert.fail("should throw DuplicateCategoryNameException");
}
Aggregations