use of io.gravitee.repository.management.model.Plan in project gravitee-management-rest-api by gravitee-io.
the class PlanServiceImpl method update.
@Override
public PlanEntity update(UpdatePlanEntity updatePlan) {
try {
logger.debug("Update plan {}", updatePlan.getName());
Optional<Plan> optPlan = planRepository.findById(updatePlan.getId());
if (!optPlan.isPresent()) {
throw new PlanNotFoundException(updatePlan.getId());
}
Plan oldPlan = optPlan.get();
Plan newPlan = new Plan();
// copy immutable values
newPlan.setId(oldPlan.getId());
newPlan.setSecurity(oldPlan.getSecurity());
newPlan.setType(oldPlan.getType());
newPlan.setStatus(oldPlan.getStatus());
newPlan.setOrder(oldPlan.getOrder());
newPlan.setApis(oldPlan.getApis());
newPlan.setCreatedAt(oldPlan.getCreatedAt());
newPlan.setPublishedAt(oldPlan.getPublishedAt());
newPlan.setClosedAt(oldPlan.getClosedAt());
// update datas
newPlan.setName(updatePlan.getName());
newPlan.setDescription(updatePlan.getDescription());
newPlan.setUpdatedAt(new Date());
newPlan.setSecurityDefinition(updatePlan.getSecurityDefinition());
String planPolicies = objectMapper.writeValueAsString(updatePlan.getPaths());
newPlan.setDefinition(planPolicies);
newPlan.setExcludedGroups(updatePlan.getExcludedGroups());
if (newPlan.getSecurity() == Plan.PlanSecurityType.KEY_LESS) {
// There is no need for a validation when authentication is KEY_LESS, force to AUTO
newPlan.setValidation(Plan.PlanValidationType.AUTO);
} else {
newPlan.setValidation(Plan.PlanValidationType.valueOf(updatePlan.getValidation().name()));
}
newPlan.setCharacteristics(updatePlan.getCharacteristics());
// if order change, reorder all pages
if (newPlan.getOrder() != updatePlan.getOrder()) {
newPlan.setOrder(updatePlan.getOrder());
reorderAndSavePlans(newPlan);
return null;
} else {
newPlan = planRepository.update(newPlan);
auditService.createApiAuditLog(newPlan.getApis().iterator().next(), Collections.singletonMap(PLAN, newPlan.getId()), PLAN_UPDATED, newPlan.getUpdatedAt(), oldPlan, newPlan);
return convert(newPlan);
}
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to update plan {}", updatePlan.getName(), ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to update plan %s", updatePlan.getName()), ex);
} catch (JsonProcessingException jse) {
logger.error("Unexpected error while generating plan definition", jse);
throw new TechnicalManagementException(String.format("An error occurs while trying to update a plan %s", updatePlan.getName()), jse);
}
}
use of io.gravitee.repository.management.model.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