use of io.gravitee.repository.management.model.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.Metadata in project gravitee-management-rest-api by gravitee-io.
the class MetadataServiceImpl method delete.
@Override
public void delete(final String key) {
try {
final Optional<Metadata> optMetadata = metadataRepository.findById(key, DEFAUT_REFERENCE_ID, MetadataReferenceType.DEFAULT);
if (optMetadata.isPresent()) {
metadataRepository.delete(key, DEFAUT_REFERENCE_ID, MetadataReferenceType.DEFAULT);
// Audit
auditService.createPortalAuditLog(Collections.singletonMap(METADATA, key), METADATA_DELETED, new Date(), optMetadata.get(), null);
// delete all overridden API metadata
final List<Metadata> apiMetadata = metadataRepository.findByKeyAndReferenceType(key, MetadataReferenceType.API);
for (final Metadata metadata : apiMetadata) {
metadataRepository.delete(key, metadata.getReferenceId(), metadata.getReferenceType());
// Audit
auditService.createApiAuditLog(metadata.getReferenceId(), Collections.singletonMap(METADATA, key), METADATA_DELETED, new Date(), metadata, null);
}
}
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to delete metadata {}", key, ex);
throw new TechnicalManagementException("An error occurs while trying to delete metadata " + key, ex);
}
}
use of io.gravitee.repository.management.model.Metadata in project gravitee-management-rest-api by gravitee-io.
the class MetadataServiceImpl method convert.
private Metadata convert(final NewMetadataEntity metadataEntity) {
final Metadata metadata = new Metadata();
metadata.setKey(IdGenerator.generate(metadataEntity.getName()));
metadata.setName(metadataEntity.getName());
metadata.setFormat(io.gravitee.repository.management.model.MetadataFormat.valueOf(metadataEntity.getFormat().name()));
if (MetadataFormat.DATE.equals(metadataEntity.getFormat())) {
metadata.setValue(metadataEntity.getValue().substring(0, 10));
} else {
metadata.setValue(metadataEntity.getValue());
}
metadata.setReferenceId(DEFAUT_REFERENCE_ID);
metadata.setReferenceType(MetadataReferenceType.DEFAULT);
return metadata;
}
use of io.gravitee.repository.management.model.Metadata in project gravitee-management-rest-api by gravitee-io.
the class ApiMetadataServiceImpl method convertForAPI.
private Metadata convertForAPI(final UpdateApiMetadataEntity metadataEntity) {
final Metadata metadata = new Metadata();
metadata.setKey(metadataEntity.getKey());
metadata.setName(metadataEntity.getName());
metadata.setFormat(io.gravitee.repository.management.model.MetadataFormat.valueOf(metadataEntity.getFormat().name()));
if (MetadataFormat.DATE.equals(metadataEntity.getFormat())) {
metadata.setValue(metadataEntity.getValue().substring(0, 10));
} else {
metadata.setValue(metadataEntity.getValue());
}
metadata.setReferenceId(metadataEntity.getApiId());
metadata.setReferenceType(MetadataReferenceType.API);
return metadata;
}
use of io.gravitee.repository.management.model.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