Search in sources :

Example 1 with TenantReferenceType

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;
}
Also used : TenantService(io.gravitee.rest.api.service.TenantService) TENANT_UPDATED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_UPDATED) TagReferenceType(io.gravitee.rest.api.model.TagReferenceType) java.util(java.util) TENANT(io.gravitee.repository.management.model.Audit.AuditProperties.TENANT) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) TENANT_DELETED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_DELETED) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) IdGenerator(io.gravitee.common.utils.IdGenerator) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) TenantNotFoundException(io.gravitee.rest.api.service.exceptions.TenantNotFoundException) Logger(org.slf4j.Logger) TENANT_CREATED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_CREATED) TenantReferenceType(io.gravitee.rest.api.model.TenantReferenceType) NewTenantEntity(io.gravitee.rest.api.model.NewTenantEntity) AuditService(io.gravitee.rest.api.service.AuditService) TenantEntity(io.gravitee.rest.api.model.TenantEntity) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) TenantRepository(io.gravitee.repository.management.api.TenantRepository) Tenant(io.gravitee.repository.management.model.Tenant) UpdateTenantEntity(io.gravitee.rest.api.model.UpdateTenantEntity) DuplicateTenantNameException(io.gravitee.rest.api.service.exceptions.DuplicateTenantNameException) Tenant(io.gravitee.repository.management.model.Tenant) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NewTenantEntity(io.gravitee.rest.api.model.NewTenantEntity) TenantEntity(io.gravitee.rest.api.model.TenantEntity) UpdateTenantEntity(io.gravitee.rest.api.model.UpdateTenantEntity) DuplicateTenantNameException(io.gravitee.rest.api.service.exceptions.DuplicateTenantNameException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

IdGenerator (io.gravitee.common.utils.IdGenerator)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 TenantRepository (io.gravitee.repository.management.api.TenantRepository)1 TENANT (io.gravitee.repository.management.model.Audit.AuditProperties.TENANT)1 Tenant (io.gravitee.repository.management.model.Tenant)1 TENANT_CREATED (io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_CREATED)1 TENANT_DELETED (io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_DELETED)1 TENANT_UPDATED (io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_UPDATED)1 NewTenantEntity (io.gravitee.rest.api.model.NewTenantEntity)1 TagReferenceType (io.gravitee.rest.api.model.TagReferenceType)1 TenantEntity (io.gravitee.rest.api.model.TenantEntity)1 TenantReferenceType (io.gravitee.rest.api.model.TenantReferenceType)1 UpdateTenantEntity (io.gravitee.rest.api.model.UpdateTenantEntity)1 AuditService (io.gravitee.rest.api.service.AuditService)1 TenantService (io.gravitee.rest.api.service.TenantService)1 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)1 DuplicateTenantNameException (io.gravitee.rest.api.service.exceptions.DuplicateTenantNameException)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 TenantNotFoundException (io.gravitee.rest.api.service.exceptions.TenantNotFoundException)1 java.util (java.util)1