Search in sources :

Example 1 with TENANT

use of io.gravitee.repository.management.model.Audit.AuditProperties.TENANT in project gravitee-management-rest-api by gravitee-io.

the class TenantServiceImpl method create.

@Override
public List<TenantEntity> create(final List<NewTenantEntity> tenantEntities) {
    // First we prevent the duplicate tenant name
    final List<String> tenantNames = tenantEntities.stream().map(NewTenantEntity::getName).collect(Collectors.toList());
    final Optional<TenantEntity> optionalTenant = findAll().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);
            savedTenants.add(convert(tenantRepository.create(tenant)));
            auditService.createPortalAuditLog(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 : TenantEntity(io.gravitee.management.model.TenantEntity) TENANT_UPDATED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_UPDATED) java.util(java.util) TenantNotFoundException(io.gravitee.management.service.exceptions.TenantNotFoundException) Logger(org.slf4j.Logger) NewTenantEntity(io.gravitee.management.model.NewTenantEntity) TENANT_CREATED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_CREATED) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) TENANT(io.gravitee.repository.management.model.Audit.AuditProperties.TENANT) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.management.service.AuditService) TENANT_DELETED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_DELETED) UpdateTenantEntity(io.gravitee.management.model.UpdateTenantEntity) Collectors(java.util.stream.Collectors) IdGenerator(io.gravitee.common.utils.IdGenerator) Component(org.springframework.stereotype.Component) TenantRepository(io.gravitee.repository.management.api.TenantRepository) Tenant(io.gravitee.repository.management.model.Tenant) TenantService(io.gravitee.management.service.TenantService) DuplicateTenantNameException(io.gravitee.management.service.exceptions.DuplicateTenantNameException) Tenant(io.gravitee.repository.management.model.Tenant) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TenantEntity(io.gravitee.management.model.TenantEntity) NewTenantEntity(io.gravitee.management.model.NewTenantEntity) UpdateTenantEntity(io.gravitee.management.model.UpdateTenantEntity) DuplicateTenantNameException(io.gravitee.management.service.exceptions.DuplicateTenantNameException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

IdGenerator (io.gravitee.common.utils.IdGenerator)1 NewTenantEntity (io.gravitee.management.model.NewTenantEntity)1 TenantEntity (io.gravitee.management.model.TenantEntity)1 UpdateTenantEntity (io.gravitee.management.model.UpdateTenantEntity)1 AuditService (io.gravitee.management.service.AuditService)1 TenantService (io.gravitee.management.service.TenantService)1 DuplicateTenantNameException (io.gravitee.management.service.exceptions.DuplicateTenantNameException)1 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)1 TenantNotFoundException (io.gravitee.management.service.exceptions.TenantNotFoundException)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 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1