use of org.folio.services.domainevent.InstanceDomainEventPublisher in project mod-inventory-storage by folio-org.
the class InstanceStorageBatchAPI method postInstanceStorageBatchInstances.
@Validate
@Override
public void postInstanceStorageBatchInstances(Instances entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
final String statusUpdatedDate = generateStatusUpdatedDate();
for (Instance instance : entity.getInstances()) {
instance.setStatusUpdatedDate(statusUpdatedDate);
}
vertxContext.runOnContext(v -> {
try {
PostgresClient postgresClient = PgUtil.postgresClient(vertxContext, okapiHeaders);
final InstanceDomainEventPublisher instanceDomainEventPublisher = new InstanceDomainEventPublisher(vertxContext, okapiHeaders);
MetadataUtil.populateMetadata(entity.getInstances(), okapiHeaders);
executeInBatch(entity.getInstances(), (instances) -> saveInstances(instances, postgresClient)).onComplete(ar -> {
InstancesBatchResponse response = constructResponse(ar.result());
if (!response.getInstances().isEmpty()) {
instanceDomainEventPublisher.publishInstancesCreated(response.getInstances()).onSuccess(notUsed -> asyncResultHandler.handle(succeededFuture(respond201WithApplicationJson(response)))).onFailure(error -> {
log.error("Failed to send events for instances", error);
asyncResultHandler.handle(succeededFuture(respond500WithTextPlain(error.getMessage())));
});
} else {
// return 500 response with the list of errors - not one Instance was created
log.error("Failed to create some of the Instances: " + response.getErrorMessages());
asyncResultHandler.handle(succeededFuture(respond500WithApplicationJson(response)));
}
});
} catch (Exception e) {
log.error("Failed to create Instances", e);
asyncResultHandler.handle(succeededFuture(respond500WithTextPlain(e.getMessage())));
}
});
}
Aggregations