Search in sources :

Example 31 with TechnicalManagementException

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

the class RatingServiceImpl method create.

@Override
public RatingEntity create(final NewRatingEntity ratingEntity) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Optional<Rating> ratingOptional = ratingRepository.findByApiAndUser(ratingEntity.getApi(), getAuthenticatedUsername());
        if (ratingOptional.isPresent()) {
            throw new RatingAlreadyExistsException(ratingEntity.getApi(), getAuthenticatedUsername());
        }
        Rating rating = ratingRepository.create(convert(ratingEntity));
        auditService.createApiAuditLog(rating.getApi(), null, Rating.RatingEvent.RATING_CREATED, rating.getCreatedAt(), null, rating);
        return convert(rating);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to create rating on api {}", ratingEntity.getApi(), ex);
        throw new TechnicalManagementException("An error occurred while trying to create rating on api " + ratingEntity.getApi(), ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) RatingAlreadyExistsException(io.gravitee.management.service.exceptions.RatingAlreadyExistsException) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 32 with TechnicalManagementException

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

the class RatingServiceImpl method update.

@Override
public RatingEntity update(final UpdateRatingEntity ratingEntity) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Rating rating = findById(ratingEntity.getId());
        final Rating oldRating = new Rating(rating);
        if (!rating.getApi().equals(ratingEntity.getApi())) {
            throw new RatingNotFoundException(ratingEntity.getId(), ratingEntity.getApi());
        }
        final Date now = new Date();
        rating.setUpdatedAt(now);
        rating.setRate(ratingEntity.getRate());
        // we can save a title/comment only once
        if (isBlank(rating.getTitle())) {
            rating.setTitle(ratingEntity.getTitle());
        }
        if (isBlank(rating.getComment())) {
            rating.setComment(ratingEntity.getComment());
        }
        Rating updatedRating = ratingRepository.update(rating);
        auditService.createApiAuditLog(rating.getApi(), null, Rating.RatingEvent.RATING_UPDATED, updatedRating.getUpdatedAt(), oldRating, updatedRating);
        return convert(updatedRating);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to update rating {}", ratingEntity.getId(), ex);
        throw new TechnicalManagementException("An error occurred while trying to update rating " + ratingEntity.getId(), ex);
    }
}
Also used : RatingNotFoundException(io.gravitee.management.service.exceptions.RatingNotFoundException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 33 with TechnicalManagementException

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

the class RoleServiceImpl method delete.

@Override
public void delete(final RoleScope scope, final String name) {
    if (isReserved(name)) {
        throw new RoleReservedNameException(SystemRole.ADMIN.name());
    }
    try {
        Optional<Role> optRole = roleRepository.findById(scope, name);
        if (!optRole.isPresent()) {
            throw new RoleNotFoundException(scope, name);
        }
        Role role = optRole.get();
        roleRepository.delete(scope, name);
        auditService.createPortalAuditLog(Collections.singletonMap(ROLE, role.getScope() + ":" + role.getName()), ROLE_DELETED, role.getUpdatedAt(), role, null);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete role {}/{}", scope, name, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete role " + scope + "/" + name, ex);
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RoleReservedNameException(io.gravitee.management.service.exceptions.RoleReservedNameException) RoleNotFoundException(io.gravitee.management.service.exceptions.RoleNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 34 with TechnicalManagementException

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

the class RoleServiceImpl method create.

@Override
public RoleEntity create(final NewRoleEntity roleEntity) {
    try {
        Role role = convert(roleEntity);
        if (roleRepository.findById(role.getScope(), role.getName()).isPresent()) {
            throw new RoleAlreadyExistsException(role.getScope(), role.getName());
        }
        role.setCreatedAt(new Date());
        role.setUpdatedAt(role.getCreatedAt());
        RoleEntity entity = convert(roleRepository.create(role));
        auditService.createPortalAuditLog(Collections.singletonMap(ROLE, role.getScope() + ":" + role.getName()), ROLE_CREATED, role.getCreatedAt(), null, role);
        if (entity.isDefaultRole()) {
            toggleDefaultRole(convert(roleEntity.getScope()), entity.getName());
        }
        return entity;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create role {}", roleEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to create role " + roleEntity.getName(), ex);
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) UpdateRoleEntity(io.gravitee.management.model.UpdateRoleEntity) NewRoleEntity(io.gravitee.management.model.NewRoleEntity) RoleEntity(io.gravitee.management.model.RoleEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RoleAlreadyExistsException(io.gravitee.management.service.exceptions.RoleAlreadyExistsException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 35 with TechnicalManagementException

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

the class RoleServiceImpl method update.

@Override
public RoleEntity update(final UpdateRoleEntity roleEntity) {
    if (isReserved(roleEntity.getName())) {
        throw new RoleReservedNameException(SystemRole.ADMIN.name());
    }
    RoleScope scope = convert(roleEntity.getScope());
    try {
        Optional<Role> optRole = roleRepository.findById(scope, roleEntity.getName());
        if (!optRole.isPresent()) {
            throw new RoleNotFoundException(scope, roleEntity.getName());
        }
        Role role = optRole.get();
        Role updatedRole = convert(roleEntity);
        updatedRole.setCreatedAt(role.getCreatedAt());
        RoleEntity entity = convert(roleRepository.update(updatedRole));
        auditService.createPortalAuditLog(Collections.singletonMap(ROLE, role.getScope() + ":" + role.getName()), ROLE_UPDATED, updatedRole.getUpdatedAt(), role, updatedRole);
        if (entity.isDefaultRole()) {
            toggleDefaultRole(scope, entity.getName());
        }
        return entity;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to update role {}", roleEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to update role " + roleEntity.getName(), ex);
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) UpdateRoleEntity(io.gravitee.management.model.UpdateRoleEntity) NewRoleEntity(io.gravitee.management.model.NewRoleEntity) RoleEntity(io.gravitee.management.model.RoleEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RoleScope(io.gravitee.repository.management.model.RoleScope) RoleReservedNameException(io.gravitee.management.service.exceptions.RoleReservedNameException) RoleNotFoundException(io.gravitee.management.service.exceptions.RoleNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)90 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)83 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 Autowired (org.springframework.beans.factory.annotation.Autowired)17 Component (org.springframework.stereotype.Component)17 Collectors (java.util.stream.Collectors)13 java.util (java.util)11 Date (java.util.Date)11 AuditService (io.gravitee.management.service.AuditService)10 IdGenerator (io.gravitee.common.utils.IdGenerator)9 io.gravitee.management.model (io.gravitee.management.model)9 User (io.gravitee.repository.management.model.User)9 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)8 UserNotFoundException (io.gravitee.management.service.exceptions.UserNotFoundException)8 Metadata (io.gravitee.repository.management.model.Metadata)8 Rating (io.gravitee.repository.management.model.Rating)8 DuplicateMetadataNameException (io.gravitee.management.service.exceptions.DuplicateMetadataNameException)7 ApiService (io.gravitee.management.service.ApiService)6 MetadataService (io.gravitee.management.service.MetadataService)6