Search in sources :

Example 1 with InstanceDomainEventPublisher

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())));
        }
    });
}
Also used : Future.succeededFuture(io.vertx.core.Future.succeededFuture) Instance(org.folio.rest.jaxrs.model.Instance) Function(java.util.function.Function) Context(io.vertx.core.Context) ArrayList(java.util.ArrayList) PostInstanceStorageBatchInstancesResponse.respond500WithApplicationJson(org.folio.rest.jaxrs.resource.InstanceStorageBatchInstances.PostInstanceStorageBatchInstancesResponse.respond500WithApplicationJson) MetadataUtil(org.folio.rest.tools.utils.MetadataUtil) CompositeFuture(io.vertx.core.CompositeFuture) Lists(com.google.common.collect.Lists) StatusUpdatedDateGenerator.generateStatusUpdatedDate(org.folio.rest.support.StatusUpdatedDateGenerator.generateStatusUpdatedDate) Instances(org.folio.rest.jaxrs.model.Instances) Map(java.util.Map) InstanceDomainEventPublisher(org.folio.services.domainevent.InstanceDomainEventPublisher) PostInstanceStorageBatchInstancesResponse.respond500WithTextPlain(org.folio.rest.jaxrs.resource.InstanceStorageBatchInstances.PostInstanceStorageBatchInstancesResponse.respond500WithTextPlain) AsyncResult(io.vertx.core.AsyncResult) PostInstanceStorageBatchInstancesResponse.respond201WithApplicationJson(org.folio.rest.jaxrs.resource.InstanceStorageBatchInstances.PostInstanceStorageBatchInstancesResponse.respond201WithApplicationJson) MODULE_SPECIFIC_ARGS(org.folio.rest.RestVerticle.MODULE_SPECIFIC_ARGS) Vertx(io.vertx.core.Vertx) Collectors(java.util.stream.Collectors) Validate(org.folio.rest.annotations.Validate) Future(io.vertx.core.Future) PostgresClient(org.folio.rest.persist.PostgresClient) HridManager(org.folio.rest.support.HridManager) InstanceStorageBatchInstances(org.folio.rest.jaxrs.resource.InstanceStorageBatchInstances) List(java.util.List) PgUtil(org.folio.rest.persist.PgUtil) Logger(org.apache.logging.log4j.Logger) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Response(javax.ws.rs.core.Response) InstancesBatchResponse(org.folio.rest.jaxrs.model.InstancesBatchResponse) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) Instance(org.folio.rest.jaxrs.model.Instance) PostgresClient(org.folio.rest.persist.PostgresClient) InstancesBatchResponse(org.folio.rest.jaxrs.model.InstancesBatchResponse) InstanceDomainEventPublisher(org.folio.services.domainevent.InstanceDomainEventPublisher) Validate(org.folio.rest.annotations.Validate)

Aggregations

Lists (com.google.common.collect.Lists)1 AsyncResult (io.vertx.core.AsyncResult)1 CompositeFuture (io.vertx.core.CompositeFuture)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Future.succeededFuture (io.vertx.core.Future.succeededFuture)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Response (javax.ws.rs.core.Response)1 StringUtils.isBlank (org.apache.commons.lang3.StringUtils.isBlank)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 MODULE_SPECIFIC_ARGS (org.folio.rest.RestVerticle.MODULE_SPECIFIC_ARGS)1 Validate (org.folio.rest.annotations.Validate)1 Instance (org.folio.rest.jaxrs.model.Instance)1