use of io.gravitee.repository.management.model.Audit.AuditProperties.PLAN in project gravitee-management-rest-api by gravitee-io.
the class PlanServiceImpl method close.
@Override
public PlanEntity close(String planId, String userId) {
try {
logger.debug("Close 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);
}
// Update plan status
plan.setStatus(Plan.Status.CLOSED);
plan.setClosedAt(new Date());
plan.setUpdatedAt(plan.getClosedAt());
// Close active subscriptions and reject pending
if (plan.getSecurity() != Plan.PlanSecurityType.KEY_LESS) {
subscriptionService.findByPlan(planId).stream().filter(subscriptionEntity -> subscriptionEntity.getStatus() == SubscriptionStatus.ACCEPTED).forEach(subscription -> subscriptionService.close(subscription.getId()));
final String planName = plan.getName();
subscriptionService.findByPlan(planId).stream().filter(subscriptionEntity -> subscriptionEntity.getStatus() == SubscriptionStatus.PENDING).forEach(subscription -> {
ProcessSubscriptionEntity processSubscriptionEntity = new ProcessSubscriptionEntity();
processSubscriptionEntity.setId(subscription.getId());
processSubscriptionEntity.setAccepted(false);
processSubscriptionEntity.setReason("Plan " + planName + " has been closed.");
subscriptionService.process(processSubscriptionEntity, userId);
});
}
// Save plan
plan = planRepository.update(plan);
// Audit
auditService.createApiAuditLog(plan.getApis().iterator().next(), Collections.singletonMap(PLAN, plan.getId()), PLAN_CLOSED, plan.getUpdatedAt(), previousPlan, plan);
// reorder plan
reorderedAndSavePlansAfterRemove(optPlan.get());
return convert(plan);
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to delete plan: {}", planId, ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to delete plan: %s", planId), ex);
}
}
use of io.gravitee.repository.management.model.Audit.AuditProperties.PLAN 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);
}
}
Aggregations