Search in sources :

Example 6 with METADATA

use of io.gravitee.repository.management.model.Audit.AuditProperties.METADATA 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)6 AuditService (io.gravitee.management.service.AuditService)6 MetadataService (io.gravitee.management.service.MetadataService)6 DuplicateMetadataNameException (io.gravitee.management.service.exceptions.DuplicateMetadataNameException)6 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)6 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)6 MetadataRepository (io.gravitee.repository.management.api.MetadataRepository)6 METADATA (io.gravitee.repository.management.model.Audit.AuditProperties.METADATA)6 Metadata (io.gravitee.repository.management.model.Metadata)6 METADATA_CREATED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED)6 METADATA_DELETED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED)6 METADATA_UPDATED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED)6 MetadataReferenceType (io.gravitee.repository.management.model.MetadataReferenceType)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 Component (org.springframework.stereotype.Component)6 io.gravitee.management.model (io.gravitee.management.model)4 ApiMetadataService (io.gravitee.management.service.ApiMetadataService)4 ApiService (io.gravitee.management.service.ApiService)4