use of io.gravitee.management.service.exceptions.ApplicationNotFoundException in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method update.
@Override
public ApplicationEntity update(String applicationId, UpdateApplicationEntity updateApplicationEntity) {
try {
LOGGER.debug("Update application {}", applicationId);
if (updateApplicationEntity.getGroups() != null && !updateApplicationEntity.getGroups().isEmpty()) {
// throw a NotFoundException if the group doesn't exist
groupService.findByIds(updateApplicationEntity.getGroups());
}
Optional<Application> optApplicationToUpdate = applicationRepository.findById(applicationId);
if (!optApplicationToUpdate.isPresent()) {
throw new ApplicationNotFoundException(applicationId);
}
// If clientId is set, check for uniqueness
String clientId = updateApplicationEntity.getClientId();
if (clientId != null && !clientId.trim().isEmpty()) {
LOGGER.debug("Check that client_id is unique among all applications");
final Set<Application> applications = applicationRepository.findAll(ApplicationStatus.ACTIVE);
final Optional<Application> byClientId = applications.stream().filter(application -> clientId.equals(application.getClientId())).findAny();
if (byClientId.isPresent() && !byClientId.get().getId().equals(optApplicationToUpdate.get().getId())) {
LOGGER.error("An application already exists with the same client_id");
throw new ClientIdAlreadyExistsException(clientId);
}
}
Application application = convert(updateApplicationEntity);
application.setId(applicationId);
application.setStatus(ApplicationStatus.ACTIVE);
application.setCreatedAt(optApplicationToUpdate.get().getCreatedAt());
application.setUpdatedAt(new Date());
Application updatedApplication = applicationRepository.update(application);
// Audit
auditService.createApplicationAuditLog(updatedApplication.getId(), Collections.emptyMap(), APPLICATION_CREATED, updatedApplication.getUpdatedAt(), optApplicationToUpdate.get(), updatedApplication);
// Set correct client_id for all subscriptions
SubscriptionQuery subQuery = new SubscriptionQuery();
subQuery.setApplication(applicationId);
subQuery.setStatuses(Collections.singleton(SubscriptionStatus.ACCEPTED));
subscriptionService.search(subQuery).forEach(new Consumer<SubscriptionEntity>() {
@Override
public void accept(SubscriptionEntity subscriptionEntity) {
UpdateSubscriptionEntity updateSubscriptionEntity = new UpdateSubscriptionEntity();
updateSubscriptionEntity.setId(subscriptionEntity.getId());
updateSubscriptionEntity.setStartingAt(subscriptionEntity.getStartingAt());
updateSubscriptionEntity.setEndingAt(subscriptionEntity.getEndingAt());
subscriptionService.update(updateSubscriptionEntity, application.getClientId());
}
});
return convert(Collections.singleton(updatedApplication)).iterator().next();
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to update application {}", applicationId, ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to update application %s", applicationId), ex);
}
}
use of io.gravitee.management.service.exceptions.ApplicationNotFoundException in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method findById.
@Override
public ApplicationEntity findById(String applicationId) {
try {
LOGGER.debug("Find application by ID: {}", applicationId);
Optional<Application> application = applicationRepository.findById(applicationId);
if (application.isPresent()) {
Optional<Membership> primaryOwnerMembership = membershipRepository.findByReferenceAndRole(MembershipReferenceType.APPLICATION, applicationId, RoleScope.APPLICATION, SystemRole.PRIMARY_OWNER.name()).stream().findFirst();
if (!primaryOwnerMembership.isPresent()) {
LOGGER.error("The Application {} doesn't have any primary owner.", applicationId);
throw new TechnicalException("The Application " + applicationId + " doesn't have any primary owner.");
}
return convert(application.get(), userService.findById(primaryOwnerMembership.get().getUserId()));
}
throw new ApplicationNotFoundException(applicationId);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to find an application using its ID {}", applicationId, ex);
throw new TechnicalManagementException("An error occurs while trying to find an application using its ID " + applicationId, ex);
}
}
use of io.gravitee.management.service.exceptions.ApplicationNotFoundException 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