use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class PlanServiceImpl method publish.
@Override
public PlanEntity publish(String planId) {
try {
logger.debug("Publish plan {}", planId);
Optional<Plan> optPlan = planRepository.findById(planId);
if (!optPlan.isPresent()) {
throw new PlanNotFoundException(planId);
}
Plan plan = optPlan.get();
Plan previousPlan = new Plan(plan);
if (plan.getStatus() == Plan.Status.CLOSED) {
throw new PlanAlreadyClosedException(planId);
} else if (plan.getStatus() == Plan.Status.PUBLISHED) {
throw new PlanAlreadyPublishedException(planId);
}
Set<Plan> plans = planRepository.findByApi(plan.getApis().iterator().next());
if (plan.getSecurity() == Plan.PlanSecurityType.KEY_LESS) {
// Look to other plans if there is already a keyless-published plan
long count = plans.stream().filter(plan1 -> plan1.getStatus() == Plan.Status.PUBLISHED).filter(plan1 -> plan1.getSecurity() == Plan.PlanSecurityType.KEY_LESS).count();
if (count > 0) {
throw new KeylessPlanAlreadyPublishedException(planId);
}
} else if (plan.getSecurity() == Plan.PlanSecurityType.OAUTH2 || plan.getSecurity() == Plan.PlanSecurityType.JWT) {
// Look to other plans if there is already an OAuth2 or JWT plan
long count = plans.stream().filter(plan1 -> plan1.getStatus() == Plan.Status.PUBLISHED).filter(plan1 -> plan1.getSecurity() == Plan.PlanSecurityType.OAUTH2 || plan1.getSecurity() == Plan.PlanSecurityType.JWT).count();
if (count > 0) {
throw new ClientIdBasedPlanAlreadyPublishedException(planId);
}
}
// Update plan status
plan.setStatus(Plan.Status.PUBLISHED);
// Update plan order
List<Plan> orderedPublishedPlans = plans.stream().filter(plan1 -> Plan.Status.PUBLISHED.equals(plan1.getStatus())).sorted(Comparator.comparingInt(Plan::getOrder)).collect(Collectors.toList());
plan.setOrder(orderedPublishedPlans.isEmpty() ? 1 : (orderedPublishedPlans.get(orderedPublishedPlans.size() - 1).getOrder() + 1));
plan.setPublishedAt(new Date());
plan.setUpdatedAt(plan.getPublishedAt());
// Save plan
plan = planRepository.update(plan);
// Audit
auditService.createApiAuditLog(plan.getApis().iterator().next(), Collections.singletonMap(PLAN, plan.getId()), PLAN_PUBLISHED, plan.getUpdatedAt(), previousPlan, plan);
return convert(plan);
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to publish plan: {}", planId, ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to publish plan: %s", planId), ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class PortalNotificationConfigServiceImpl method save.
@Override
public PortalNotificationConfigEntity save(PortalNotificationConfigEntity notificationEntity) {
try {
if (notificationEntity.getHooks() == null || notificationEntity.getHooks().isEmpty()) {
portalNotificationConfigRepository.delete(convert(notificationEntity));
return getDefaultEmpty(notificationEntity.getUser(), NotificationReferenceType.valueOf(notificationEntity.getReferenceType()), notificationEntity.getReferenceId());
} else {
Optional<PortalNotificationConfig> optionalConfig = portalNotificationConfigRepository.findById(notificationEntity.getUser(), NotificationReferenceType.valueOf(notificationEntity.getReferenceType()), notificationEntity.getReferenceId());
PortalNotificationConfig notificationConfig = convert(notificationEntity);
if (optionalConfig.isPresent()) {
notificationConfig.setCreatedAt(optionalConfig.get().getCreatedAt());
notificationConfig.setUpdatedAt(new Date());
return convert(portalNotificationConfigRepository.update(notificationConfig));
} else {
notificationConfig.setCreatedAt(new Date());
notificationConfig.setUpdatedAt(notificationConfig.getCreatedAt());
return convert(portalNotificationConfigRepository.create(notificationConfig));
}
}
} catch (TechnicalException te) {
LOGGER.error("An error occurs while trying to save the notification settings {}", notificationEntity, te);
throw new TechnicalManagementException("An error occurs while trying to save the notification settings " + notificationEntity, te);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class RatingServiceImpl method findByApi.
@Override
public Page<RatingEntity> findByApi(final String api, final Pageable pageable) {
if (!enabled) {
throw new ApiRatingUnavailableException();
}
try {
final Page<Rating> pageRating = ratingRepository.findByApiPageable(api, new PageableBuilder().pageNumber(pageable.pageNumber() - 1).pageSize(pageable.pageSize()).build());
final List<RatingEntity> ratingEntities = pageRating.getContent().stream().map(this::convert).collect(toList());
return new Page<>(ratingEntities, pageRating.getPageNumber(), (int) pageRating.getPageElements(), pageRating.getTotalElements());
} 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.repository.exceptions.TechnicalException 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 (!enabled) {
throw new ApiRatingUnavailableException();
}
try {
Rating rating = findById(ratingId);
ratingAnswerRepository.delete(answerId);
auditService.createApiAuditLog(rating.getApi(), 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.repository.exceptions.TechnicalException 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());
if (user.getFirstname() != null && user.getLastname() != null) {
ratingEntity.setUserDisplayName(user.getFirstname() + ' ' + user.getLastname());
} else {
ratingEntity.setUserDisplayName(user.getUsername());
}
ratingEntity.setId(rating.getId());
ratingEntity.setApi(rating.getApi());
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.getUsername());
}
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;
}
Aggregations