Search in sources :

Example 96 with TechnicalManagementException

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

the class RatingServiceImpl method createAnswer.

@Override
public RatingEntity createAnswer(final NewRatingAnswerEntity answerEntity) {
    if (!isEnabled()) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Rating rating = findModelById(answerEntity.getRatingId());
        final RatingAnswer ratingAnswer = new RatingAnswer();
        ratingAnswer.setId(UuidString.generateRandom());
        ratingAnswer.setRating(answerEntity.getRatingId());
        ratingAnswer.setUser(getAuthenticatedUsername());
        ratingAnswer.setComment(answerEntity.getComment());
        ratingAnswer.setCreatedAt(new Date());
        ratingAnswerRepository.create(ratingAnswer);
        auditService.createApiAuditLog(rating.getReferenceId(), null, RatingAnswer.RatingAnswerEvent.RATING_ANSWER_CREATED, ratingAnswer.getCreatedAt(), null, ratingAnswer);
        notifierService.trigger(ApiHook.NEW_RATING_ANSWER, rating.getReferenceId(), new NotificationParamsBuilder().api(apiService.findById(rating.getReferenceId())).build());
        return convert(rating);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to create a rating answer on rating {}", answerEntity.getRatingId(), ex);
        throw new TechnicalManagementException("An error occurred while trying to create a rating answer on rating" + answerEntity.getRatingId(), ex);
    }
}
Also used : RatingAnswer(io.gravitee.repository.management.model.RatingAnswer) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NotificationParamsBuilder(io.gravitee.rest.api.service.notification.NotificationParamsBuilder)

Example 97 with TechnicalManagementException

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

the class RatingServiceImpl method convert.

private RatingEntity convert(final Rating rating) {
    final RatingEntity ratingEntity = new RatingEntity();
    final UserEntity user = userService.findById(rating.getUser());
    ratingEntity.setUser(user.getId());
    ratingEntity.setUserDisplayName(user.getDisplayName());
    ratingEntity.setId(rating.getId());
    ratingEntity.setApi(rating.getReferenceId());
    ratingEntity.setTitle(rating.getTitle());
    ratingEntity.setComment(rating.getComment());
    ratingEntity.setRate(rating.getRate());
    ratingEntity.setCreatedAt(rating.getCreatedAt());
    ratingEntity.setUpdatedAt(rating.getUpdatedAt());
    try {
        final List<RatingAnswer> ratingAnswers = ratingAnswerRepository.findByRating(rating.getId());
        if (ratingAnswers != null) {
            ratingEntity.setAnswers(ratingAnswers.stream().map(ratingAnswer -> {
                final RatingAnswerEntity ratingAnswerEntity = new RatingAnswerEntity();
                ratingAnswerEntity.setId(ratingAnswer.getId());
                final UserEntity userAnswer = userService.findById(ratingAnswer.getUser());
                ratingAnswerEntity.setUser(userAnswer.getId());
                if (userAnswer.getFirstname() != null && userAnswer.getLastname() != null) {
                    ratingAnswerEntity.setUserDisplayName(userAnswer.getFirstname() + ' ' + userAnswer.getLastname());
                } else {
                    ratingAnswerEntity.setUserDisplayName(userAnswer.getEmail());
                }
                ratingAnswerEntity.setComment(ratingAnswer.getComment());
                ratingAnswerEntity.setCreatedAt(ratingAnswer.getCreatedAt());
                return ratingAnswerEntity;
            }).sorted(comparing(RatingAnswerEntity::getCreatedAt, reverseOrder())).collect(toList()));
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find rating answers by rating id {}", rating.getId(), ex);
        throw new TechnicalManagementException("An error occurred while trying to find rating answers by rating id " + rating.getId(), ex);
    }
    return ratingEntity;
}
Also used : Rating(io.gravitee.repository.management.model.Rating) RatingAnswer(io.gravitee.repository.management.model.RatingAnswer) io.gravitee.rest.api.service(io.gravitee.rest.api.service) RatingReferenceType(io.gravitee.repository.management.model.RatingReferenceType) ApiHook(io.gravitee.rest.api.service.notification.ApiHook) Date(java.util.Date) Page(io.gravitee.common.data.domain.Page) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) OptionalDouble(java.util.OptionalDouble) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) RatingAlreadyExistsException(io.gravitee.rest.api.service.exceptions.RatingAlreadyExistsException) io.gravitee.rest.api.model(io.gravitee.rest.api.model) RatingAnswerRepository(io.gravitee.repository.management.api.RatingAnswerRepository) Comparator.comparing(java.util.Comparator.comparing) UuidString(io.gravitee.rest.api.service.common.UuidString) Logger(org.slf4j.Logger) ParameterReferenceType(io.gravitee.rest.api.model.parameters.ParameterReferenceType) RatingRepository(io.gravitee.repository.management.api.RatingRepository) Pageable(io.gravitee.repository.management.api.search.Pageable) Collectors(java.util.stream.Collectors) Key(io.gravitee.rest.api.model.parameters.Key) List(java.util.List) Component(org.springframework.stereotype.Component) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) NotificationParamsBuilder(io.gravitee.rest.api.service.notification.NotificationParamsBuilder) Comparator.reverseOrder(java.util.Comparator.reverseOrder) Optional(java.util.Optional) RatingAnswer(io.gravitee.repository.management.model.RatingAnswer) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 98 with TechnicalManagementException

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

the class RatingServiceImpl method findByApi.

@Override
public List<RatingEntity> findByApi(String api) {
    if (!isEnabled()) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final List<Rating> ratings = ratingRepository.findByReferenceIdAndReferenceType(api, RatingReferenceType.API);
        final List<RatingEntity> ratingEntities = ratings.stream().map(this::convert).collect(toList());
        return ratingEntities;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find ratings for api {}", api, ex);
        throw new TechnicalManagementException("An error occurred while trying to find ratings for api " + api, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 99 with TechnicalManagementException

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

the class RatingServiceImpl method deleteAnswer.

@Override
public void deleteAnswer(final String ratingId, final String answerId) {
    if (!isEnabled()) {
        throw new ApiRatingUnavailableException();
    }
    try {
        Rating rating = findModelById(ratingId);
        ratingAnswerRepository.delete(answerId);
        auditService.createApiAuditLog(rating.getReferenceId(), null, RatingAnswer.RatingAnswerEvent.RATING_ANSWER_DELETED, new Date(), rating, null);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete rating answer {}", answerId, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete rating answer " + answerId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 100 with TechnicalManagementException

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

the class GenericNotificationConfigServiceImpl method update.

@Override
public GenericNotificationConfigEntity update(GenericNotificationConfigEntity entity) {
    try {
        if (entity.getNotifier() == null || entity.getNotifier().isEmpty() || entity.getName() == null || entity.getName().isEmpty()) {
            throw new BadNotificationConfigException();
        }
        if (entity.getId() == null || entity.getId().isEmpty()) {
            throw new NotificationConfigNotFoundException();
        }
        Optional<GenericNotificationConfig> optionalConfig = genericNotificationConfigRepository.findById(entity.getId());
        if (!optionalConfig.isPresent()) {
            throw new NotificationConfigNotFoundException();
        }
        GenericNotificationConfig notificationConfig = convert(entity);
        notificationConfig.setCreatedAt(optionalConfig.get().getCreatedAt());
        notificationConfig.setUpdatedAt(new Date());
        return convert(genericNotificationConfigRepository.update(notificationConfig));
    } catch (TechnicalException te) {
        LOGGER.error("An error occurs while trying to save the generic notification settings {}", entity, te);
        throw new TechnicalManagementException("An error occurs while trying to save the generic notification settings " + entity, te);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) GenericNotificationConfig(io.gravitee.repository.management.model.GenericNotificationConfig) BadNotificationConfigException(io.gravitee.rest.api.service.exceptions.BadNotificationConfigException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NotificationConfigNotFoundException(io.gravitee.rest.api.service.exceptions.NotificationConfigNotFoundException)

Aggregations

TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)149 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)120 UuidString (io.gravitee.rest.api.service.common.UuidString)26 Date (java.util.Date)23 Component (org.springframework.stereotype.Component)18 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)16 Collectors (java.util.stream.Collectors)13 Autowired (org.springframework.beans.factory.annotation.Autowired)13 IOException (java.io.IOException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 Rating (io.gravitee.repository.management.model.Rating)9 ApiRatingUnavailableException (io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Dictionary (io.gravitee.repository.management.model.Dictionary)8 AuditService (io.gravitee.rest.api.service.AuditService)8 java.util (java.util)8 Theme (io.gravitee.repository.management.model.Theme)6 List (java.util.List)6