Search in sources :

Example 1 with Tenant

use of io.gravitee.repository.management.model.Tenant in project gravitee-management-rest-api by gravitee-io.

the class TenantServiceImpl method convert.

private Tenant convert(final UpdateTenantEntity tenantEntity) {
    final Tenant tenant = new Tenant();
    tenant.setId(tenantEntity.getId());
    tenant.setName(tenantEntity.getName());
    tenant.setDescription(tenantEntity.getDescription());
    return tenant;
}
Also used : Tenant(io.gravitee.repository.management.model.Tenant)

Example 2 with Tenant

use of io.gravitee.repository.management.model.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)

Example 3 with Tenant

use of io.gravitee.repository.management.model.Tenant in project gravitee-management-rest-api by gravitee-io.

the class TenantServiceImpl method convert.

private Tenant convert(final NewTenantEntity tenantEntity) {
    final Tenant tenant = new Tenant();
    tenant.setId(IdGenerator.generate(tenantEntity.getName()));
    tenant.setName(tenantEntity.getName());
    tenant.setDescription(tenantEntity.getDescription());
    return tenant;
}
Also used : Tenant(io.gravitee.repository.management.model.Tenant)

Example 4 with Tenant

use of io.gravitee.repository.management.model.Tenant in project gravitee-management-rest-api by gravitee-io.

the class TenantServiceImpl method update.

@Override
public List<TenantEntity> update(final List<UpdateTenantEntity> tenantEntities) {
    final List<TenantEntity> savedTenants = new ArrayList<>(tenantEntities.size());
    tenantEntities.forEach(tenantEntity -> {
        try {
            Tenant tenant = convert(tenantEntity);
            Optional<Tenant> tenantOptional = tenantRepository.findById(tenant.getId());
            if (tenantOptional.isPresent()) {
                savedTenants.add(convert(tenantRepository.update(tenant)));
                auditService.createPortalAuditLog(Collections.singletonMap(TENANT, tenant.getId()), TENANT_UPDATED, new Date(), tenantOptional.get(), tenant);
            }
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to update tenant {}", tenantEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to update tenant " + tenantEntity.getName(), ex);
        }
    });
    return savedTenants;
}
Also used : 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) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 5 with Tenant

use of io.gravitee.repository.management.model.Tenant in project gravitee-management-rest-api by gravitee-io.

the class TenantServiceImpl method findById.

@Override
public TenantEntity findById(String tenantId) {
    try {
        LOGGER.debug("Find tenant by ID: {}", tenantId);
        Optional<Tenant> optTenant = tenantRepository.findById(tenantId);
        if (!optTenant.isPresent()) {
            throw new TenantNotFoundException(tenantId);
        }
        return convert(optTenant.get());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find tenant by ID", ex);
        throw new TechnicalManagementException("An error occurs while trying to find tenant by ID", ex);
    }
}
Also used : Tenant(io.gravitee.repository.management.model.Tenant) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TenantNotFoundException(io.gravitee.management.service.exceptions.TenantNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

Tenant (io.gravitee.repository.management.model.Tenant)6 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 NewTenantEntity (io.gravitee.management.model.NewTenantEntity)2 TenantEntity (io.gravitee.management.model.TenantEntity)2 UpdateTenantEntity (io.gravitee.management.model.UpdateTenantEntity)2 TenantNotFoundException (io.gravitee.management.service.exceptions.TenantNotFoundException)2 IdGenerator (io.gravitee.common.utils.IdGenerator)1 AuditService (io.gravitee.management.service.AuditService)1 TenantService (io.gravitee.management.service.TenantService)1 DuplicateTenantNameException (io.gravitee.management.service.exceptions.DuplicateTenantNameException)1 TenantRepository (io.gravitee.repository.management.api.TenantRepository)1 TENANT (io.gravitee.repository.management.model.Audit.AuditProperties.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