use of io.gravitee.repository.management.model.ApiHeader in project gravitee-management-rest-api by gravitee-io.
the class ApiHeaderServiceImpl method update.
@Override
public ApiHeaderEntity update(UpdateApiHeaderEntity updateEntity) {
try {
Optional<ApiHeader> optionalApiHeader = apiHeaderRepository.findById(updateEntity.getId());
if (!optionalApiHeader.isPresent()) {
throw new ApiHeaderNotFoundException(updateEntity.getId());
}
ApiHeader updatedHeader = new ApiHeader(optionalApiHeader.get());
Date updatedAt = new Date();
updatedHeader.setName(updateEntity.getName());
updatedHeader.setValue(updateEntity.getValue());
updatedHeader.setUpdatedAt(updatedAt);
if (updatedHeader.getOrder() != updateEntity.getOrder()) {
updatedHeader.setOrder(updateEntity.getOrder());
reorderAndSave(updatedHeader);
return convert(updatedHeader);
} else {
ApiHeader header = apiHeaderRepository.update(updatedHeader);
auditService.createEnvironmentAuditLog(singletonMap(API_HEADER, header.getId()), API_HEADER_UPDATED, header.getUpdatedAt(), optionalApiHeader.get(), header);
return convert(header);
}
} catch (TechnicalException e) {
LOGGER.error("An error occurs while trying to update header {}", updateEntity, e);
throw new TechnicalManagementException("An error occurs while trying to update header " + updateEntity, e);
}
}
use of io.gravitee.repository.management.model.ApiHeader in project gravitee-management-rest-api by gravitee-io.
the class ApiHeaderServiceImpl method create.
private ApiHeaderEntity create(NewApiHeaderEntity newEntity, String environmentId) {
try {
int order = apiHeaderRepository.findAllByEnvironment(environmentId).size() + 1;
ApiHeader apiHeader = new ApiHeader();
apiHeader.setId(UuidString.generateRandom());
apiHeader.setEnvironmentId(environmentId);
apiHeader.setName(newEntity.getName());
apiHeader.setValue(newEntity.getValue());
apiHeader.setOrder(order);
apiHeader.setCreatedAt(new Date());
apiHeader.setUpdatedAt(apiHeader.getCreatedAt());
auditService.createEnvironmentAuditLog(Collections.singletonMap(API_HEADER, apiHeader.getId()), API_HEADER_CREATED, apiHeader.getCreatedAt(), null, apiHeader);
return convert(apiHeaderRepository.create(apiHeader));
} catch (TechnicalException e) {
LOGGER.error("An error occurs while trying to create a header {}", newEntity, e);
throw new TechnicalManagementException("An error occurs while trying to create a header " + newEntity, e);
}
}
use of io.gravitee.repository.management.model.ApiHeader in project gravitee-management-rest-api by gravitee-io.
the class ApiHeaderServiceImpl method delete.
@Override
public void delete(String apiHeaderId) {
try {
Optional<ApiHeader> optionalApiHeader = apiHeaderRepository.findById(apiHeaderId);
if (!optionalApiHeader.isPresent()) {
throw new ApiHeaderNotFoundException(apiHeaderId);
}
apiHeaderRepository.delete(apiHeaderId);
auditService.createEnvironmentAuditLog(Collections.singletonMap(API_HEADER, apiHeaderId), API_HEADER_DELETED, new Date(), optionalApiHeader.get(), null);
// reorder headers
int currentOrder = 1;
for (ApiHeaderEntity apiHeaderEntity : this.findAll()) {
if (apiHeaderEntity.getOrder() != currentOrder) {
UpdateApiHeaderEntity updateEntity = convert(apiHeaderEntity);
updateEntity.setOrder(currentOrder);
this.update(updateEntity);
break;
}
currentOrder++;
}
} catch (TechnicalException e) {
LOGGER.error("An error occurs while trying to delete a header {}", apiHeaderId, e);
throw new TechnicalManagementException("An error occurs while trying to delete a header " + apiHeaderId, e);
}
}
Aggregations