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