use of io.gravitee.repository.management.model.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);
}
}
use of io.gravitee.repository.management.model.Metadata in project gravitee-management-rest-api by gravitee-io.
the class ApiMetadataServiceImpl method convert.
private Metadata convert(final MetadataEntity 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());
}
return metadata;
}
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 UpdateMetadataEntity 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(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 MetadataServiceImpl method findDefaultByKey.
@Override
public MetadataEntity findDefaultByKey(final String key) {
try {
LOGGER.debug("Find default metadata by key");
final Optional<Metadata> optMetadata = metadataRepository.findById(key, DEFAUT_REFERENCE_ID, MetadataReferenceType.DEFAULT);
if (optMetadata.isPresent()) {
return convert(optMetadata.get());
} else {
return null;
}
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to find default metadata by key", ex);
throw new TechnicalManagementException("An error occurred while trying to find default metadata by key", ex);
}
}
Aggregations