Search in sources :

Example 6 with Category

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);
    }
}
Also used : Category(io.gravitee.repository.management.model.Category) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 7 with Category

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;
}
Also used : Category(io.gravitee.repository.management.model.Category)

Example 8 with 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);
    }
}
Also used : Category(io.gravitee.repository.management.model.Category) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 9 with Category

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;
}
Also used : Upgrader(io.gravitee.rest.api.service.Upgrader) Ordered(org.springframework.core.Ordered) Logger(org.slf4j.Logger) Date(java.util.Date) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Autowired(org.springframework.beans.factory.annotation.Autowired) IdGenerator(io.gravitee.common.utils.IdGenerator) Category(io.gravitee.repository.management.model.Category) Component(org.springframework.stereotype.Component) ApiRepository(io.gravitee.repository.management.api.ApiRepository) CategoryRepository(io.gravitee.repository.management.api.CategoryRepository) Optional(java.util.Optional) Category(io.gravitee.repository.management.model.Category) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Date(java.util.Date)

Example 10 with Category

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");
}
Also used : Category(io.gravitee.repository.management.model.Category) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) DuplicateCategoryNameException(io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException) Test(org.junit.Test)

Aggregations

Category (io.gravitee.repository.management.model.Category)13 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)9 CategoryEntity (io.gravitee.rest.api.model.CategoryEntity)7 CategoryRepository (io.gravitee.repository.management.api.CategoryRepository)5 NewCategoryEntity (io.gravitee.rest.api.model.NewCategoryEntity)5 UpdateCategoryEntity (io.gravitee.rest.api.model.UpdateCategoryEntity)5 CategoryNotFoundException (io.gravitee.rest.api.service.exceptions.CategoryNotFoundException)5 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)5 Test (org.junit.Test)5 AuditService (io.gravitee.rest.api.service.AuditService)4 Date (java.util.Date)4 DuplicateCategoryNameException (io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException)3 CategoryServiceImpl (io.gravitee.rest.api.service.impl.CategoryServiceImpl)3 Optional (java.util.Optional)3 Assert (org.junit.Assert)3 RunWith (org.junit.runner.RunWith)3 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)3 InjectMocks (org.mockito.InjectMocks)3 Mock (org.mockito.Mock)3 Mockito (org.mockito.Mockito)3