Search in sources :

Example 1 with NewMetadataEntity

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

Aggregations

IdGenerator (io.gravitee.common.utils.IdGenerator)1 MetadataEntity (io.gravitee.management.model.MetadataEntity)1 MetadataFormat (io.gravitee.management.model.MetadataFormat)1 NewMetadataEntity (io.gravitee.management.model.NewMetadataEntity)1 UpdateMetadataEntity (io.gravitee.management.model.UpdateMetadataEntity)1 AuditService (io.gravitee.management.service.AuditService)1 MetadataService (io.gravitee.management.service.MetadataService)1 DuplicateMetadataNameException (io.gravitee.management.service.exceptions.DuplicateMetadataNameException)1 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)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 METADATA_CREATED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED)1 METADATA_DELETED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED)1 METADATA_UPDATED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED)1 MetadataReferenceType (io.gravitee.repository.management.model.MetadataReferenceType)1 URL (java.net.URL)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Collections (java.util.Collections)1