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