Search in sources :

Example 1 with CategoryNotFoundException

use of io.gravitee.rest.api.service.exceptions.CategoryNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class CategoryServiceImpl method update.

@Override
public CategoryEntity update(String categoryId, UpdateCategoryEntity categoryEntity) {
    try {
        LOGGER.debug("Update Category {}", categoryId);
        Optional<Category> optCategoryToUpdate = categoryRepository.findById(categoryId);
        if (!optCategoryToUpdate.isPresent()) {
            throw new CategoryNotFoundException(categoryId);
        }
        final Category categoryToUpdate = optCategoryToUpdate.get();
        Category category = convert(categoryEntity, categoryToUpdate.getEnvironmentId());
        // If no new picture and the current picture url is not the default one, keep the current picture
        if (categoryEntity.getPicture() == null && categoryEntity.getPictureUrl() != null && categoryEntity.getPictureUrl().indexOf("?hash") > 0) {
            category.setPicture(categoryToUpdate.getPicture());
        }
        final Date updatedAt = new Date();
        category.setCreatedAt(categoryToUpdate.getCreatedAt());
        category.setUpdatedAt(updatedAt);
        CategoryEntity updatedCategory = convert(categoryRepository.update(category));
        auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, category.getId()), CATEGORY_UPDATED, updatedAt, categoryToUpdate, category);
        return updatedCategory;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to update category {}", categoryEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to update category " + categoryEntity.getName(), ex);
    }
}
Also used : Category(io.gravitee.repository.management.model.Category) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

Example 2 with CategoryNotFoundException

use of io.gravitee.rest.api.service.exceptions.CategoryNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class CategoryResourceTest method shouldNotGetCategory.

@Test
public void shouldNotGetCategory() {
    doThrow(new CategoryNotFoundException(UNKNOWN_CATEGORY)).when(categoryService).findNotHiddenById(UNKNOWN_CATEGORY);
    final Response response = target(UNKNOWN_CATEGORY).request().get();
    assertEquals(NOT_FOUND_404, response.getStatus());
    ErrorResponse errorResponse = response.readEntity(ErrorResponse.class);
    List<Error> errors = errorResponse.getErrors();
    assertNotNull(errors);
    assertEquals(1, errors.size());
    Error error = errors.get(0);
    assertNotNull(error);
    assertEquals("errors.category.notFound", error.getCode());
    assertEquals("404", error.getStatus());
    assertEquals("Category [" + UNKNOWN_CATEGORY + "] can not be found.", error.getMessage());
}
Also used : ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) Error(io.gravitee.rest.api.portal.rest.model.Error) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Test(org.junit.Test)

Example 3 with CategoryNotFoundException

use of io.gravitee.rest.api.service.exceptions.CategoryNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiMapper method convert.

public Api convert(ApiEntity api) {
    final Api apiItem = new Api();
    apiItem.setDescription(api.getDescription());
    List<ApiEntrypointEntity> apiEntrypoints = api.getEntrypoints();
    if (apiEntrypoints != null) {
        List<String> entrypoints = apiEntrypoints.stream().map(ApiEntrypointEntity::getTarget).collect(Collectors.toList());
        apiItem.setEntrypoints(entrypoints);
    }
    apiItem.setDraft(api.getLifecycleState() == ApiLifecycleState.UNPUBLISHED || api.getLifecycleState() == ApiLifecycleState.CREATED);
    apiItem.setRunning(api.getState() == Lifecycle.State.STARTED);
    apiItem.setPublic(api.getVisibility() == Visibility.PUBLIC);
    apiItem.setId(api.getId());
    List<String> apiLabels = api.getLabels();
    if (apiLabels != null) {
        apiItem.setLabels(new ArrayList<>(apiLabels));
    } else {
        apiItem.setLabels(new ArrayList<>());
    }
    apiItem.setName(api.getName());
    PrimaryOwnerEntity primaryOwner = api.getPrimaryOwner();
    if (primaryOwner != null) {
        User owner = new User();
        owner.setId(primaryOwner.getId());
        owner.setDisplayName(primaryOwner.getDisplayName());
        owner.setEmail(primaryOwner.getEmail());
        apiItem.setOwner(owner);
    }
    apiItem.setPages(null);
    apiItem.setPlans(null);
    if (ratingService.isEnabled()) {
        final RatingSummaryEntity ratingSummaryEntity = ratingService.findSummaryByApi(api.getId());
        RatingSummary ratingSummary = new RatingSummary().average(ratingSummaryEntity.getAverageRate()).count(BigDecimal.valueOf(ratingSummaryEntity.getNumberOfRatings()));
        apiItem.setRatingSummary(ratingSummary);
    }
    if (api.getUpdatedAt() != null) {
        apiItem.setUpdatedAt(api.getUpdatedAt().toInstant().atOffset(ZoneOffset.UTC));
    }
    apiItem.setVersion(api.getVersion());
    boolean isCategoryModeEnabled = this.parameterService.findAsBoolean(Key.PORTAL_APIS_CATEGORY_ENABLED, ParameterReferenceType.ENVIRONMENT);
    if (isCategoryModeEnabled && api.getCategories() != null) {
        apiItem.setCategories(api.getCategories().stream().filter(categoryId -> {
            try {
                categoryService.findNotHiddenById(categoryId);
                return true;
            } catch (CategoryNotFoundException v) {
                return false;
            }
        }).collect(Collectors.toList()));
    } else {
        apiItem.setCategories(new ArrayList<>());
    }
    return apiItem;
}
Also used : RatingSummary(io.gravitee.rest.api.portal.rest.model.RatingSummary) ApiEntrypointEntity(io.gravitee.rest.api.model.api.ApiEntrypointEntity) User(io.gravitee.rest.api.portal.rest.model.User) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) Api(io.gravitee.rest.api.portal.rest.model.Api)

Example 4 with CategoryNotFoundException

use of io.gravitee.rest.api.service.exceptions.CategoryNotFoundException 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)

Aggregations

CategoryNotFoundException (io.gravitee.rest.api.service.exceptions.CategoryNotFoundException)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Category (io.gravitee.repository.management.model.Category)2 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)2 CategoryEntity (io.gravitee.rest.api.model.CategoryEntity)1 NewCategoryEntity (io.gravitee.rest.api.model.NewCategoryEntity)1 PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)1 RatingSummaryEntity (io.gravitee.rest.api.model.RatingSummaryEntity)1 UpdateCategoryEntity (io.gravitee.rest.api.model.UpdateCategoryEntity)1 ApiEntrypointEntity (io.gravitee.rest.api.model.api.ApiEntrypointEntity)1 Api (io.gravitee.rest.api.portal.rest.model.Api)1 Error (io.gravitee.rest.api.portal.rest.model.Error)1 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)1 RatingSummary (io.gravitee.rest.api.portal.rest.model.RatingSummary)1 User (io.gravitee.rest.api.portal.rest.model.User)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1