Search in sources :

Example 1 with DuplicateTagNameException

use of io.gravitee.rest.api.service.exceptions.DuplicateTagNameException in project gravitee-management-rest-api by gravitee-io.

the class TagServiceImpl method create.

@Override
public List<TagEntity> create(final List<NewTagEntity> tagEntities, String referenceId, TagReferenceType referenceType) {
    // First we prevent the duplicate tag name
    final List<String> tagNames = tagEntities.stream().map(NewTagEntity::getName).collect(toList());
    final Optional<TagEntity> optionalTag = findByReference(referenceId, referenceType).stream().filter(tag -> tagNames.contains(tag.getName())).findAny();
    if (optionalTag.isPresent()) {
        throw new DuplicateTagNameException(optionalTag.get().getName());
    }
    final List<TagEntity> savedTags = new ArrayList<>(tagEntities.size());
    tagEntities.forEach(tagEntity -> {
        try {
            Tag tag = convert(tagEntity, referenceId, referenceType);
            savedTags.add(convert(tagRepository.create(tag)));
            auditService.createOrganizationAuditLog(Collections.singletonMap(TAG, tag.getId()), TAG_CREATED, new Date(), null, tag);
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to create tag {}", tagEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to create tag " + tagEntity.getName(), ex);
        }
    });
    return savedTags;
}
Also used : TagReferenceType(io.gravitee.rest.api.model.TagReferenceType) java.util(java.util) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) DuplicateTagNameException(io.gravitee.rest.api.service.exceptions.DuplicateTagNameException) Autowired(org.springframework.beans.factory.annotation.Autowired) NewTagEntity(io.gravitee.rest.api.model.NewTagEntity) ApiService(io.gravitee.rest.api.service.ApiService) Collections.singletonList(java.util.Collections.singletonList) TagRepository(io.gravitee.repository.management.api.TagRepository) IdGenerator(io.gravitee.common.utils.IdGenerator) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) TagNotFoundException(io.gravitee.rest.api.service.exceptions.TagNotFoundException) TAG(io.gravitee.repository.management.model.Audit.AuditProperties.TAG) Collectors.toSet(java.util.stream.Collectors.toSet) TagEntity(io.gravitee.rest.api.model.TagEntity) GroupService(io.gravitee.rest.api.service.GroupService) Logger(org.slf4j.Logger) Collections.emptyList(java.util.Collections.emptyList) AuditService(io.gravitee.rest.api.service.AuditService) AuditEvent(io.gravitee.repository.management.model.Tag.AuditEvent) Collectors.toList(java.util.stream.Collectors.toList) Tag(io.gravitee.repository.management.model.Tag) Component(org.springframework.stereotype.Component) GroupEntity(io.gravitee.rest.api.model.GroupEntity) UpdateTagEntity(io.gravitee.rest.api.model.UpdateTagEntity) TagService(io.gravitee.rest.api.service.TagService) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NewTagEntity(io.gravitee.rest.api.model.NewTagEntity) TagEntity(io.gravitee.rest.api.model.TagEntity) UpdateTagEntity(io.gravitee.rest.api.model.UpdateTagEntity) DuplicateTagNameException(io.gravitee.rest.api.service.exceptions.DuplicateTagNameException) Tag(io.gravitee.repository.management.model.Tag) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

IdGenerator (io.gravitee.common.utils.IdGenerator)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 TagRepository (io.gravitee.repository.management.api.TagRepository)1 TAG (io.gravitee.repository.management.model.Audit.AuditProperties.TAG)1 Tag (io.gravitee.repository.management.model.Tag)1 AuditEvent (io.gravitee.repository.management.model.Tag.AuditEvent)1 GroupEntity (io.gravitee.rest.api.model.GroupEntity)1 NewTagEntity (io.gravitee.rest.api.model.NewTagEntity)1 TagEntity (io.gravitee.rest.api.model.TagEntity)1 TagReferenceType (io.gravitee.rest.api.model.TagReferenceType)1 UpdateTagEntity (io.gravitee.rest.api.model.UpdateTagEntity)1 ApiService (io.gravitee.rest.api.service.ApiService)1 AuditService (io.gravitee.rest.api.service.AuditService)1 GroupService (io.gravitee.rest.api.service.GroupService)1 TagService (io.gravitee.rest.api.service.TagService)1 DuplicateTagNameException (io.gravitee.rest.api.service.exceptions.DuplicateTagNameException)1 TagNotFoundException (io.gravitee.rest.api.service.exceptions.TagNotFoundException)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 java.util (java.util)1 Collections.emptyList (java.util.Collections.emptyList)1