Search in sources :

Example 26 with TechnicalManagementException

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

the class PortalNotificationServiceImpl method create.

@Override
public void create(Hook hook, List<String> users, Object params) {
    try {
        // get notification template
        String tpl = RELATIVE_TPL_PATH + hook.getScope().name() + "." + hook.name() + ".yml";
        final Template template = freemarkerConfiguration.getTemplate(tpl);
        final String yamlContent = processTemplateIntoString(template, params);
        Yaml yaml = new Yaml();
        Map<String, String> load = yaml.loadAs(yamlContent, HashMap.class);
        List<NewPortalNotificationEntity> notifications = new ArrayList<>(users.size());
        users.forEach(user -> {
            NewPortalNotificationEntity notification = new NewPortalNotificationEntity();
            notification.setUser(user);
            notification.setTitle(load.get("title"));
            notification.setMessage(load.get("message"));
            notifications.add(notification);
        });
        create(notifications);
    } catch (final Exception ex) {
        LOGGER.error("Error while sending notification", ex);
        throw new TechnicalManagementException("Error while sending notification", ex);
    }
}
Also used : FreeMarkerTemplateUtils.processTemplateIntoString(org.springframework.ui.freemarker.FreeMarkerTemplateUtils.processTemplateIntoString) NewPortalNotificationEntity(io.gravitee.management.model.notification.NewPortalNotificationEntity) Yaml(org.yaml.snakeyaml.Yaml) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) Template(freemarker.template.Template)

Example 27 with TechnicalManagementException

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

the class PortalNotificationServiceImpl method create.

private void create(List<NewPortalNotificationEntity> notificationEntities) {
    final Date now = new Date();
    List<PortalNotification> notifications = notificationEntities.stream().map(this::convert).collect(Collectors.toList());
    notifications.forEach(n -> {
        n.setId(UUID.toString(UUID.random()));
        n.setCreatedAt(now);
    });
    try {
        portalNotificationRepository.create(notifications);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create {}", notifications, ex);
        throw new TechnicalManagementException("An error occurs while trying create " + notifications, ex);
    }
}
Also used : PortalNotification(io.gravitee.repository.management.model.PortalNotification) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 28 with TechnicalManagementException

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

the class RatingServiceImpl method findSummaryByApi.

@Override
public RatingSummaryEntity findSummaryByApi(final String api) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final List<Rating> ratings = ratingRepository.findByApi(api);
        final RatingSummaryEntity ratingSummary = new RatingSummaryEntity();
        ratingSummary.setApi(api);
        ratingSummary.setNumberOfRatings(ratings.size());
        final OptionalDouble optionalAvg = ratings.stream().mapToInt(Rating::getRate).average();
        if (optionalAvg.isPresent()) {
            ratingSummary.setAverageRate(optionalAvg.getAsDouble());
        }
        ratingSummary.setNumberOfRatingsByRate(ratings.stream().collect(groupingBy(Rating::getRate, counting())));
        return ratingSummary;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find summary rating for api {}", api, ex);
        throw new TechnicalManagementException("An error occurred while trying to find summary rating for api " + api, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) OptionalDouble(java.util.OptionalDouble) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 29 with TechnicalManagementException

use of io.gravitee.management.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 (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Rating rating = findById(answerEntity.getRatingId());
        final RatingAnswer ratingAnswer = new RatingAnswer();
        ratingAnswer.setId(UUID.toString(UUID.random()));
        ratingAnswer.setRating(answerEntity.getRatingId());
        ratingAnswer.setUser(getAuthenticatedUsername());
        ratingAnswer.setComment(answerEntity.getComment());
        ratingAnswer.setCreatedAt(new Date());
        ratingAnswerRepository.create(ratingAnswer);
        auditService.createApiAuditLog(rating.getApi(), null, RatingAnswer.RatingAnswerEvent.RATING_ANSWER_CREATED, ratingAnswer.getCreatedAt(), null, ratingAnswer);
        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.management.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 30 with TechnicalManagementException

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

the class RatingServiceImpl method delete.

@Override
public void delete(final String id) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        Rating rating = findById(id);
        ratingRepository.delete(id);
        auditService.createApiAuditLog(rating.getApi(), null, Rating.RatingEvent.RATING_DELETED, new Date(), rating, null);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete rating {}", id, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete rating " + id, ex);
    }
}
Also used : 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)

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