use of io.gravitee.repository.management.model.Audit.AuditProperties.METADATA 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);
}
}
use of io.gravitee.repository.management.model.Audit.AuditProperties.METADATA in project gravitee-management-rest-api by gravitee-io.
the class ApiMetadataServiceImpl method findByIdAndApi.
@Override
public ApiMetadataEntity findByIdAndApi(final String metadataId, final String apiId) {
LOGGER.debug("Find metadata by id {} and api {}", metadataId, apiId);
try {
final List<MetadataEntity> defaultMedatata = metadataService.findAllDefault();
final Optional<Metadata> optMetadata = metadataRepository.findById(metadataId, apiId, MetadataReferenceType.API);
if (optMetadata.isPresent()) {
final Metadata metadata = optMetadata.get();
final Optional<MetadataEntity> optDefaultMetadata = defaultMedatata.stream().filter(metadataEntity -> metadata.getKey().equals(metadataEntity.getKey())).findAny();
final ApiMetadataEntity apiMetadataEntity = convert(metadata, apiId);
optDefaultMetadata.ifPresent(defMetadata -> apiMetadataEntity.setDefaultValue(defMetadata.getValue()));
return apiMetadataEntity;
}
throw new ApiMetadataNotFoundException(apiId, metadataId);
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to find metadata by id and API", ex);
throw new TechnicalManagementException("An error occurred while trying to find metadata by id and API", ex);
}
}
use of io.gravitee.repository.management.model.Audit.AuditProperties.METADATA 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);
}
}
use of io.gravitee.repository.management.model.Audit.AuditProperties.METADATA 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);
}
}
use of io.gravitee.repository.management.model.Audit.AuditProperties.METADATA in project gravitee-management-rest-api by gravitee-io.
the class ApiMetadataServiceImpl method findAllByApi.
@Override
public List<ApiMetadataEntity> findAllByApi(final String apiId) {
try {
LOGGER.debug("Find all metadata by api ID {}", apiId);
final List<MetadataEntity> defaultMetadata = metadataService.findAllDefault();
final List<String> defaultMetadataKeys = defaultMetadata.stream().map(MetadataEntity::getKey).collect(toList());
final List<Metadata> apiMetadata = metadataRepository.findByReferenceTypeAndReferenceId(MetadataReferenceType.API, apiId);
final List<ApiMetadataEntity> allMetadata = new ArrayList<>();
allMetadata.addAll(defaultMetadata.stream().map(metadata -> {
final Optional<Metadata> optApiMetadata = apiMetadata.stream().filter(metadataEntity -> metadata.getKey().equals(metadataEntity.getKey())).findAny();
return convert(optApiMetadata, metadata, null);
}).collect(toList()));
allMetadata.addAll(apiMetadata.stream().filter(metadata -> !defaultMetadataKeys.contains(metadata.getKey())).map(metadata -> convert(metadata, apiId)).collect(toList()));
return allMetadata;
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to find all metadata by API", ex);
throw new TechnicalManagementException("An error occurred while trying to find all metadata by API", ex);
}
}
Aggregations