use of io.gravitee.management.service.exceptions.SubscriptionNotClosableException in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method archive.
@Override
public void archive(String applicationId) {
try {
LOGGER.debug("Delete application {}", applicationId);
Optional<Application> optApplication = applicationRepository.findById(applicationId);
if (!optApplication.isPresent()) {
throw new ApplicationNotFoundException(applicationId);
}
Application application = optApplication.get();
Application previousApplication = new Application(application);
Collection<SubscriptionEntity> subscriptions = subscriptionService.findByApplicationAndPlan(applicationId, null);
subscriptions.forEach(subscription -> {
Set<ApiKeyEntity> apiKeys = apiKeyService.findBySubscription(subscription.getId());
apiKeys.forEach(apiKey -> {
try {
apiKeyService.delete(apiKey.getKey());
} catch (TechnicalManagementException tme) {
LOGGER.error("An error occurs while deleting API Key {}", apiKey.getKey(), tme);
}
});
try {
subscriptionService.close(subscription.getId());
} catch (SubscriptionNotClosableException snce) {
// Subscription can not be closed because it is already closed or not yet accepted
LOGGER.debug("The subscription can not be closed: {}", snce.getMessage());
}
});
// Archive the application
application.setUpdatedAt(new Date());
application.setStatus(ApplicationStatus.ARCHIVED);
applicationRepository.update(application);
// Audit
auditService.createApplicationAuditLog(application.getId(), Collections.emptyMap(), APPLICATION_ARCHIVED, application.getUpdatedAt(), previousApplication, application);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to delete application {}", applicationId, ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to delete application %s", applicationId), ex);
}
}
Aggregations