Search in sources :

Example 1 with UpdateMetadataEntity

use of io.gravitee.rest.api.model.UpdateMetadataEntity 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());
        }
        checkMetadataValue(metadataEntity.getValue());
        checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
        final Metadata metadata = convert(metadataEntity);
        final Date now = new Date();
        metadata.setUpdatedAt(now);
        metadataRepository.update(metadata);
        // Audit
        auditService.createEnvironmentAuditLog(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) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) SimpleDateFormat(java.text.SimpleDateFormat) Autowired(org.springframework.beans.factory.annotation.Autowired) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) InternetAddress(javax.mail.internet.InternetAddress) IdGenerator(io.gravitee.common.utils.IdGenerator) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NotificationTemplateService(io.gravitee.rest.api.service.notification.NotificationTemplateService) AuditEvent(io.gravitee.repository.management.model.Metadata.AuditEvent) UpdateMetadataEntity(io.gravitee.rest.api.model.UpdateMetadataEntity) MetadataService(io.gravitee.rest.api.service.MetadataService) Collections.singletonMap(java.util.Collections.singletonMap) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) Logger(org.slf4j.Logger) NewMetadataEntity(io.gravitee.rest.api.model.NewMetadataEntity) AuditService(io.gravitee.rest.api.service.AuditService) MetadataFormat(io.gravitee.rest.api.model.MetadataFormat) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Metadata(io.gravitee.repository.management.model.Metadata) StringReader(java.io.StringReader) DuplicateMetadataNameException(io.gravitee.rest.api.service.exceptions.DuplicateMetadataNameException) Optional(java.util.Optional) MetadataEntity(io.gravitee.rest.api.model.MetadataEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.rest.api.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

IdGenerator (io.gravitee.common.utils.IdGenerator)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 MetadataRepository (io.gravitee.repository.management.api.MetadataRepository)1 METADATA (io.gravitee.repository.management.model.Audit.AuditProperties.METADATA)1 Metadata (io.gravitee.repository.management.model.Metadata)1 AuditEvent (io.gravitee.repository.management.model.Metadata.AuditEvent)1 MetadataReferenceType (io.gravitee.repository.management.model.MetadataReferenceType)1 MetadataEntity (io.gravitee.rest.api.model.MetadataEntity)1 MetadataFormat (io.gravitee.rest.api.model.MetadataFormat)1 NewMetadataEntity (io.gravitee.rest.api.model.NewMetadataEntity)1 UpdateMetadataEntity (io.gravitee.rest.api.model.UpdateMetadataEntity)1 AuditService (io.gravitee.rest.api.service.AuditService)1 MetadataService (io.gravitee.rest.api.service.MetadataService)1 DuplicateMetadataNameException (io.gravitee.rest.api.service.exceptions.DuplicateMetadataNameException)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 NotificationTemplateService (io.gravitee.rest.api.service.notification.NotificationTemplateService)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Collections.singletonMap (java.util.Collections.singletonMap)1