Search in sources :

Example 1 with DuplicateMetadataNameException

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

the class ApiMetadataServiceImpl method update.

@Override
public ApiMetadataEntity update(final UpdateApiMetadataEntity metadataEntity) {
    final ApiEntity apiEntity = apiService.findById(metadataEntity.getApiId());
    // First we prevent the duplicate metadata name
    final Optional<ApiMetadataEntity> optionalMetadata = findAllByApi(apiEntity.getId()).stream().filter(metadata -> !metadataEntity.getKey().equals(metadata.getKey()) && metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
    if (optionalMetadata.isPresent()) {
        throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
    }
    metadataService.checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getDefaultValue());
    try {
        final List<MetadataEntity> defaultMedatata = metadataService.findAllDefault();
        final Optional<MetadataEntity> optDefaultMetadata = defaultMedatata.stream().filter(metadata -> metadata.getKey().equals(metadataEntity.getKey())).findAny();
        final Optional<Metadata> apiMetadata = metadataRepository.findById(metadataEntity.getKey(), metadataEntity.getApiId(), MetadataReferenceType.API);
        final Metadata savedMetadata;
        final Metadata metadata = convertForAPI(metadataEntity);
        final Date now = new Date();
        if (apiMetadata.isPresent()) {
            metadata.setUpdatedAt(now);
            savedMetadata = metadataRepository.update(metadata);
            // Audit
            auditService.createApiAuditLog(apiEntity.getId(), Collections.singletonMap(METADATA, metadata.getKey()), METADATA_UPDATED, metadata.getUpdatedAt(), apiMetadata.get(), metadata);
        } else {
            metadata.setCreatedAt(now);
            metadata.setUpdatedAt(now);
            savedMetadata = metadataRepository.create(metadata);
            // Audit
            auditService.createApiAuditLog(apiEntity.getId(), Collections.singletonMap(METADATA, metadata.getKey()), METADATA_CREATED, metadata.getCreatedAt(), null, metadata);
        }
        final ApiMetadataEntity apiMetadataEntity = convert(savedMetadata, null);
        optDefaultMetadata.ifPresent(defaultMetadata -> apiMetadataEntity.setDefaultValue(defaultMetadata.getValue()));
        return apiMetadataEntity;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to update metadata {} on API {}", metadataEntity.getName(), metadataEntity.getApiId(), ex);
        throw new TechnicalManagementException("An error occurred while trying to update metadata " + metadataEntity.getName() + " on API " + metadataEntity.getApiId(), ex);
    }
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) ApiService(io.gravitee.management.service.ApiService) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) ApiMetadataService(io.gravitee.management.service.ApiMetadataService) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.management.service.AuditService) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) ApiMetadataNotFoundException(io.gravitee.management.service.exceptions.ApiMetadataNotFoundException) METADATA_UPDATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED) MetadataService(io.gravitee.management.service.MetadataService) IdGenerator(io.gravitee.common.utils.IdGenerator) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) METADATA_DELETED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED) Metadata(io.gravitee.repository.management.model.Metadata) io.gravitee.management.model(io.gravitee.management.model) METADATA_CREATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 2 with DuplicateMetadataNameException

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

the class MetadataServiceImpl method update.

@Override
public MetadataEntity update(final UpdateMetadataEntity metadataEntity) {
    try {
        // First we prevent the duplicate metadata name
        final Optional<Metadata> optionalMetadata = metadataRepository.findByReferenceType(MetadataReferenceType.DEFAULT).stream().filter(metadata -> !metadataEntity.getKey().equals(metadata.getKey()) && metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
        if (optionalMetadata.isPresent()) {
            throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
        }
        checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
        final Metadata metadata = convert(metadataEntity);
        final Date now = new Date();
        metadata.setUpdatedAt(now);
        metadataRepository.update(metadata);
        // Audit
        auditService.createPortalAuditLog(Collections.singletonMap(METADATA, metadata.getKey()), METADATA_UPDATED, metadata.getCreatedAt(), null, metadata);
        return convert(metadata);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to update metadata {}", metadataEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurred while trying to update metadata " + metadataEntity.getName(), ex);
    }
}
Also used : URL(java.net.URL) Date(java.util.Date) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleDateFormat(java.text.SimpleDateFormat) MetadataEntity(io.gravitee.management.model.MetadataEntity) AuditService(io.gravitee.management.service.AuditService) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) InternetAddress(javax.mail.internet.InternetAddress) IdGenerator(io.gravitee.common.utils.IdGenerator) MetadataFormat(io.gravitee.management.model.MetadataFormat) METADATA_CREATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) Logger(org.slf4j.Logger) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) UpdateMetadataEntity(io.gravitee.management.model.UpdateMetadataEntity) Collectors(java.util.stream.Collectors) METADATA_UPDATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED) MetadataService(io.gravitee.management.service.MetadataService) NewMetadataEntity(io.gravitee.management.model.NewMetadataEntity) Component(org.springframework.stereotype.Component) List(java.util.List) METADATA_DELETED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED) Metadata(io.gravitee.repository.management.model.Metadata) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Optional(java.util.Optional) Collections(java.util.Collections) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 3 with DuplicateMetadataNameException

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

the class MetadataServiceImpl method create.

@Override
public MetadataEntity create(final NewMetadataEntity metadataEntity) {
    // if no format defined, we just set String format
    if (metadataEntity.getFormat() == null) {
        metadataEntity.setFormat(MetadataFormat.STRING);
    }
    try {
        // First we prevent the duplicate metadata name
        final Optional<MetadataEntity> optionalMetadata = findAllDefault().stream().filter(metadata -> metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
        if (optionalMetadata.isPresent()) {
            throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
        }
        checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
        final Metadata metadata = convert(metadataEntity);
        final Date now = new Date();
        metadata.setCreatedAt(now);
        metadata.setUpdatedAt(now);
        metadataRepository.create(metadata);
        // Audit
        auditService.createPortalAuditLog(Collections.singletonMap(METADATA, metadata.getKey()), METADATA_CREATED, metadata.getCreatedAt(), null, metadata);
        return convert(metadata);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to create metadata {}", metadataEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurred while trying to create metadata " + metadataEntity.getName(), ex);
    }
}
Also used : URL(java.net.URL) Date(java.util.Date) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleDateFormat(java.text.SimpleDateFormat) MetadataEntity(io.gravitee.management.model.MetadataEntity) AuditService(io.gravitee.management.service.AuditService) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) InternetAddress(javax.mail.internet.InternetAddress) IdGenerator(io.gravitee.common.utils.IdGenerator) MetadataFormat(io.gravitee.management.model.MetadataFormat) METADATA_CREATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) Logger(org.slf4j.Logger) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) UpdateMetadataEntity(io.gravitee.management.model.UpdateMetadataEntity) Collectors(java.util.stream.Collectors) METADATA_UPDATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED) MetadataService(io.gravitee.management.service.MetadataService) NewMetadataEntity(io.gravitee.management.model.NewMetadataEntity) Component(org.springframework.stereotype.Component) List(java.util.List) METADATA_DELETED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED) Metadata(io.gravitee.repository.management.model.Metadata) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Optional(java.util.Optional) Collections(java.util.Collections) MetadataEntity(io.gravitee.management.model.MetadataEntity) UpdateMetadataEntity(io.gravitee.management.model.UpdateMetadataEntity) NewMetadataEntity(io.gravitee.management.model.NewMetadataEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 4 with DuplicateMetadataNameException

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

the class ApiMetadataServiceImpl method create.

@Override
public ApiMetadataEntity create(final NewApiMetadataEntity metadataEntity) {
    final ApiEntity apiEntity = apiService.findById(metadataEntity.getApiId());
    // if no format defined, we just set String format
    if (metadataEntity.getFormat() == null) {
        metadataEntity.setFormat(MetadataFormat.STRING);
    }
    // First we prevent the duplicate metadata name
    final Optional<ApiMetadataEntity> optionalMetadata = findAllByApi(apiEntity.getId()).stream().filter(metadata -> metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
    if (optionalMetadata.isPresent()) {
        throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
    }
    metadataService.checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
    try {
        final Metadata metadata = convertForAPI(metadataEntity);
        final Date now = new Date();
        metadata.setCreatedAt(now);
        metadata.setUpdatedAt(now);
        metadataRepository.create(metadata);
        // Audit
        auditService.createApiAuditLog(apiEntity.getId(), Collections.singletonMap(METADATA, metadata.getKey()), METADATA_CREATED, metadata.getCreatedAt(), null, metadata);
        return convert(metadata, metadataEntity.getApiId());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to create metadata {} on API {}", metadataEntity.getName(), metadataEntity.getApiId(), ex);
        throw new TechnicalManagementException("An error occurred while trying to create metadata " + metadataEntity.getName() + " on API " + metadataEntity.getApiId(), ex);
    }
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) ApiService(io.gravitee.management.service.ApiService) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) ApiMetadataService(io.gravitee.management.service.ApiMetadataService) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.management.service.AuditService) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) ApiMetadataNotFoundException(io.gravitee.management.service.exceptions.ApiMetadataNotFoundException) METADATA_UPDATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED) MetadataService(io.gravitee.management.service.MetadataService) IdGenerator(io.gravitee.common.utils.IdGenerator) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) METADATA_DELETED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED) Metadata(io.gravitee.repository.management.model.Metadata) io.gravitee.management.model(io.gravitee.management.model) METADATA_CREATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

IdGenerator (io.gravitee.common.utils.IdGenerator)4 AuditService (io.gravitee.management.service.AuditService)4 MetadataService (io.gravitee.management.service.MetadataService)4 DuplicateMetadataNameException (io.gravitee.management.service.exceptions.DuplicateMetadataNameException)4 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 MetadataRepository (io.gravitee.repository.management.api.MetadataRepository)4 METADATA (io.gravitee.repository.management.model.Audit.AuditProperties.METADATA)4 Metadata (io.gravitee.repository.management.model.Metadata)4 METADATA_CREATED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED)4 METADATA_DELETED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED)4 METADATA_UPDATED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED)4 MetadataReferenceType (io.gravitee.repository.management.model.MetadataReferenceType)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Component (org.springframework.stereotype.Component)4 io.gravitee.management.model (io.gravitee.management.model)2 MetadataEntity (io.gravitee.management.model.MetadataEntity)2 MetadataFormat (io.gravitee.management.model.MetadataFormat)2