use of io.gravitee.rest.api.model.TenantReferenceType in project gravitee-management-rest-api by gravitee-io.
the class TenantServiceImpl method create.
@Override
public List<TenantEntity> create(final List<NewTenantEntity> tenantEntities, String referenceId, TenantReferenceType referenceType) {
// First we prevent the duplicate tenant name
final List<String> tenantNames = tenantEntities.stream().map(NewTenantEntity::getName).collect(Collectors.toList());
final Optional<TenantEntity> optionalTenant = findByReference(referenceId, referenceType).stream().filter(tenant -> tenantNames.contains(tenant.getName())).findAny();
if (optionalTenant.isPresent()) {
throw new DuplicateTenantNameException(optionalTenant.get().getName());
}
final List<TenantEntity> savedTenants = new ArrayList<>(tenantEntities.size());
tenantEntities.forEach(tenantEntity -> {
try {
Tenant tenant = convert(tenantEntity, referenceId, referenceType);
savedTenants.add(convert(tenantRepository.create(tenant)));
auditService.createEnvironmentAuditLog(Collections.singletonMap(TENANT, tenant.getId()), TENANT_CREATED, new Date(), null, tenant);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to create tenant {}", tenantEntity.getName(), ex);
throw new TechnicalManagementException("An error occurs while trying to create tenant " + tenantEntity.getName(), ex);
}
});
return savedTenants;
}
Aggregations